Skip to content

Commit 331b1ff

Browse files
🤖 Merge PR DefinitelyTyped#70130 [@types/govuk-frontend] Add new definition for govuk-frontend by @colinrotherham
1 parent ef9e475 commit 331b1ff

52 files changed

Lines changed: 1733 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export { version } from "./common/govuk-frontend-version.mjs";
2+
export { Accordion } from "./components/accordion/accordion.mjs";
3+
export { Button } from "./components/button/button.mjs";
4+
export { CharacterCount } from "./components/character-count/character-count.mjs";
5+
export { Checkboxes } from "./components/checkboxes/checkboxes.mjs";
6+
export { ErrorSummary } from "./components/error-summary/error-summary.mjs";
7+
export { ExitThisPage } from "./components/exit-this-page/exit-this-page.mjs";
8+
export { Header } from "./components/header/header.mjs";
9+
export { NotificationBanner } from "./components/notification-banner/notification-banner.mjs";
10+
export { PasswordInput } from "./components/password-input/password-input.mjs";
11+
export { Radios } from "./components/radios/radios.mjs";
12+
export { SkipLink } from "./components/skip-link/skip-link.mjs";
13+
export { Tabs } from "./components/tabs/tabs.mjs";
14+
export { type Config, type ConfigKey, createAll, initAll } from "./init.mjs";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { version } from "./common/govuk-frontend-version.js";
2+
import { Accordion } from "./components/accordion/accordion.js";
3+
import { Button } from "./components/button/button.js";
4+
import { CharacterCount } from "./components/character-count/character-count.js";
5+
import { Checkboxes } from "./components/checkboxes/checkboxes.js";
6+
import { ErrorSummary } from "./components/error-summary/error-summary.js";
7+
import { ExitThisPage } from "./components/exit-this-page/exit-this-page.js";
8+
import { Header } from "./components/header/header.js";
9+
import { NotificationBanner } from "./components/notification-banner/notification-banner.js";
10+
import { PasswordInput } from "./components/password-input/password-input.js";
11+
import { Radios } from "./components/radios/radios.js";
12+
import { SkipLink } from "./components/skip-link/skip-link.js";
13+
import { Tabs } from "./components/tabs/tabs.js";
14+
import { type Config as ConfigImport, type ConfigKey as ConfigKeyImport, createAll, initAll } from "./init.js";
15+
16+
declare namespace GOVUKFrontend {
17+
type Config = ConfigImport;
18+
type ConfigKey = ConfigKeyImport;
19+
}
20+
21+
declare const GOVUKFrontend: {
22+
version: typeof version;
23+
Accordion: typeof Accordion;
24+
Button: typeof Button;
25+
CharacterCount: typeof CharacterCount;
26+
Checkboxes: typeof Checkboxes;
27+
ErrorSummary: typeof ErrorSummary;
28+
ExitThisPage: typeof ExitThisPage;
29+
Header: typeof Header;
30+
NotificationBanner: typeof NotificationBanner;
31+
PasswordInput: typeof PasswordInput;
32+
Radios: typeof Radios;
33+
SkipLink: typeof SkipLink;
34+
Tabs: typeof Tabs;
35+
createAll: typeof createAll;
36+
initAll: typeof initAll;
37+
};
38+
39+
export = GOVUKFrontend;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./all.bundle.mjs";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./closest-attribute-value.js";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Returns the value of the given attribute closest to the given element (including itself)
3+
*
4+
* @internal
5+
* @param {Element} $element - The element to start walking the DOM tree up
6+
* @param {string} attributeName - The name of the attribute
7+
* @returns {string | null} Attribute value
8+
*/
9+
export function closestAttributeValue($element: Element, attributeName: string): string | null;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./govuk-frontend-version.js";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* GOV.UK Frontend release version
3+
*
4+
* {@link https://github.com/alphagov/govuk-frontend/releases}
5+
*/
6+
export const version: "development";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./index.js";
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/**
2+
* Common helpers which do not require polyfill.
3+
*
4+
* IMPORTANT: If a helper require a polyfill, please isolate it in its own module
5+
* so that the polyfill can be properly tree-shaken and does not burden
6+
* the components that do not need that helper
7+
*/
8+
/**
9+
* Config merging function
10+
*
11+
* Takes any number of objects and combines them together, with
12+
* greatest priority on the LAST item passed in.
13+
*
14+
* @internal
15+
* @param {...{ [key: string]: unknown }} configObjects - Config objects to merge
16+
* @returns {{ [key: string]: unknown }} A merged config object
17+
*/
18+
export function mergeConfigs(
19+
...configObjects: Array<{
20+
[key: string]: unknown;
21+
}>
22+
): {
23+
[key: string]: unknown;
24+
};
25+
26+
/**
27+
* Extracts keys starting with a particular namespace from dataset ('data-*')
28+
* object, removing the namespace in the process, normalising all values
29+
*
30+
* @internal
31+
* @param {{ schema: Schema }} Component - Component class
32+
* @param {DOMStringMap} dataset - The object to extract key-value pairs from
33+
* @param {string} namespace - The namespace to filter keys with
34+
* @returns {ObjectNested | undefined} Nested object with dot-separated key namespace removed
35+
*/
36+
export function extractConfigByNamespace(
37+
Component: {
38+
schema: Schema;
39+
},
40+
dataset: DOMStringMap,
41+
namespace: string,
42+
): ObjectNested | undefined;
43+
44+
/**
45+
* Get hash fragment from URL
46+
*
47+
* Extract the hash fragment (everything after the hash) from a URL,
48+
* but not including the hash symbol
49+
*
50+
* @param {string} url - URL
51+
* @returns {string | undefined} Fragment from URL, without the hash
52+
*/
53+
export function getFragmentFromUrl(url: string): string | undefined;
54+
55+
/**
56+
* Get GOV.UK Frontend breakpoint value from CSS custom property
57+
*
58+
* @param {string} name - Breakpoint name
59+
* @returns {{ property: string, value?: string }} Breakpoint object
60+
*/
61+
export function getBreakpoint(name: string): {
62+
property: string;
63+
value?: string;
64+
};
65+
66+
/**
67+
* Move focus to element
68+
*
69+
* Sets tabindex to -1 to make the element programmatically focusable,
70+
* but removes it on blur as the element doesn't need to be focused again.
71+
*
72+
* @template {HTMLElement} FocusElement
73+
* @param {FocusElement} $element - HTML element
74+
* @param {object} [options] - Handler options
75+
* @param {function(this: FocusElement): void} [options.onBeforeFocus] - Callback before focus
76+
* @param {function(this: FocusElement): void} [options.onBlur] - Callback on blur
77+
*/
78+
export function setFocus<FocusElement extends HTMLElement>(
79+
$element: FocusElement,
80+
options?: {
81+
onBeforeFocus?: ((this: FocusElement) => void) | undefined;
82+
onBlur?: ((this: FocusElement) => void) | undefined;
83+
},
84+
): void;
85+
86+
/**
87+
* Checks if GOV.UK Frontend is supported on this page
88+
*
89+
* Some browsers will load and run our JavaScript but GOV.UK Frontend
90+
* won't be supported.
91+
*
92+
* @internal
93+
* @param {HTMLElement | null} [$scope] - HTML element `<body>` checked for browser support
94+
* @returns {boolean} Whether GOV.UK Frontend is supported on this page
95+
*/
96+
export function isSupported($scope?: HTMLElement | null): boolean;
97+
98+
/**
99+
* Validate component config by schema
100+
*
101+
* Follows limited examples in JSON schema for wider support in future
102+
*
103+
* {@link https://ajv.js.org/json-schema.html#compound-keywords}
104+
* {@link https://ajv.js.org/packages/ajv-errors.html#single-message}
105+
*
106+
* @internal
107+
* @param {Schema} schema - Config schema
108+
* @param {{ [key: string]: unknown }} config - Component config
109+
* @returns {string[]} List of validation errors
110+
*/
111+
export function validateConfig(schema: Schema, config: {
112+
[key: string]: unknown;
113+
}): string[];
114+
115+
/**
116+
* Schema for component config
117+
*/
118+
export interface Schema {
119+
/**
120+
* - Schema properties
121+
*/
122+
properties: {
123+
[field: string]: SchemaProperty | undefined;
124+
};
125+
126+
/**
127+
* - List of schema conditions
128+
*/
129+
anyOf?: SchemaCondition[] | undefined;
130+
}
131+
132+
/**
133+
* Schema property for component config
134+
*/
135+
export interface SchemaProperty {
136+
/**
137+
* - Property type
138+
*/
139+
type: "string" | "boolean" | "number" | "object";
140+
}
141+
142+
/**
143+
* Schema condition for component config
144+
*/
145+
export interface SchemaCondition {
146+
/**
147+
* - List of required config fields
148+
*/
149+
required: string[];
150+
151+
/**
152+
* - Error message when required config fields not provided
153+
*/
154+
errorMessage: string;
155+
}
156+
export type NestedKey = keyof ObjectNested;
157+
export interface ObjectNested {
158+
[key: string]: string | boolean | number | ObjectNested | undefined;
159+
}

0 commit comments

Comments
 (0)