@types/govuk-frontend 5.6.0 → 5.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. govuk-frontend/README.md +1 -1
  2. govuk-frontend/dist/govuk/all.bundle.d.mts +3 -0
  3. govuk-frontend/dist/govuk/all.bundle.d.ts +6 -0
  4. govuk-frontend/dist/govuk/common/configuration.d.mts +1 -0
  5. govuk-frontend/dist/govuk/common/configuration.d.ts +208 -0
  6. govuk-frontend/dist/govuk/common/index.d.ts +32 -88
  7. govuk-frontend/dist/govuk/components/accordion/accordion.d.ts +4 -4
  8. govuk-frontend/dist/govuk/components/button/button.d.ts +4 -4
  9. govuk-frontend/dist/govuk/components/character-count/character-count.d.ts +17 -4
  10. govuk-frontend/dist/govuk/components/checkboxes/checkboxes.d.ts +3 -3
  11. govuk-frontend/dist/govuk/components/error-summary/error-summary.d.ts +4 -4
  12. govuk-frontend/dist/govuk/components/exit-this-page/exit-this-page.d.ts +4 -4
  13. govuk-frontend/dist/govuk/components/header/header.d.ts +3 -3
  14. govuk-frontend/dist/govuk/components/notification-banner/notification-banner.d.ts +4 -4
  15. govuk-frontend/dist/govuk/components/password-input/password-input.d.ts +4 -4
  16. govuk-frontend/dist/govuk/components/radios/radios.d.ts +3 -3
  17. govuk-frontend/dist/govuk/components/service-navigation/service-navigation.d.ts +3 -3
  18. govuk-frontend/dist/govuk/components/skip-link/skip-link.d.ts +10 -5
  19. govuk-frontend/dist/govuk/components/tabs/tabs.d.ts +3 -3
  20. govuk-frontend/dist/govuk/errors/index.d.ts +18 -5
  21. govuk-frontend/dist/govuk/govuk-frontend-component.d.ts +24 -3
  22. govuk-frontend/dist/govuk/init.d.ts +48 -12
  23. govuk-frontend/package.json +4 -3
  24. govuk-frontend/dist/govuk/common/normalise-dataset.d.mts +0 -1
  25. govuk-frontend/dist/govuk/common/normalise-dataset.d.ts +0 -16
  26. govuk-frontend/dist/govuk/common/normalise-string.d.mts +0 -1
  27. govuk-frontend/dist/govuk/common/normalise-string.d.ts +0 -22
govuk-frontend/README.md CHANGED
@@ -8,7 +8,7 @@ This package contains type definitions for govuk-frontend (https://github.com/al
8
8
  Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/govuk-frontend.
9
9
 
10
10
  ### Additional Details
11
- * Last updated: Thu, 10 Oct 2024 14:07:44 GMT
11
+ * Last updated: Mon, 03 Feb 2025 16:02:40 GMT
12
12
  * Dependencies: none
13
13
 
14
14
  # Credits
@@ -1,4 +1,6 @@
1
+ export { ConfigurableComponent } from "./common/configuration.mjs";
1
2
  export { version } from "./common/govuk-frontend-version.mjs";
3
+ export { isSupported } from "./common/index.mjs";
2
4
  export { Accordion } from "./components/accordion/accordion.mjs";
3
5
  export { Button } from "./components/button/button.mjs";
4
6
  export { CharacterCount } from "./components/character-count/character-count.mjs";
@@ -12,4 +14,5 @@ export { Radios } from "./components/radios/radios.mjs";
12
14
  export { ServiceNavigation } from "./components/service-navigation/service-navigation.mjs";
13
15
  export { SkipLink } from "./components/skip-link/skip-link.mjs";
14
16
  export { Tabs } from "./components/tabs/tabs.mjs";
17
+ export { GOVUKFrontendComponent as Component } from "./govuk-frontend-component.mjs";
15
18
  export { type Config, type ConfigKey, createAll, initAll } from "./init.mjs";
@@ -1,4 +1,6 @@
1
+ import { ConfigurableComponent } from "./common/configuration.js";
1
2
  import { version } from "./common/govuk-frontend-version.js";
3
+ import { isSupported } from "./common/index.js";
2
4
  import { Accordion } from "./components/accordion/accordion.js";
3
5
  import { Button } from "./components/button/button.js";
4
6
  import { CharacterCount } from "./components/character-count/character-count.js";
@@ -12,6 +14,7 @@ import { Radios } from "./components/radios/radios.js";
12
14
  import { ServiceNavigation } from "./components/service-navigation/service-navigation.js";
13
15
  import { SkipLink } from "./components/skip-link/skip-link.js";
14
16
  import { Tabs } from "./components/tabs/tabs.js";
17
+ import { GOVUKFrontendComponent as Component } from "./govuk-frontend-component.js";
15
18
  import { type Config as ConfigImport, type ConfigKey as ConfigKeyImport, createAll, initAll } from "./init.js";
16
19
 
17
20
  declare namespace GOVUKFrontend {
@@ -21,6 +24,7 @@ declare namespace GOVUKFrontend {
21
24
 
22
25
  declare const GOVUKFrontend: {
23
26
  version: typeof version;
27
+ isSupported: typeof isSupported;
24
28
  Accordion: typeof Accordion;
25
29
  Button: typeof Button;
26
30
  CharacterCount: typeof CharacterCount;
@@ -34,6 +38,8 @@ declare const GOVUKFrontend: {
34
38
  ServiceNavigation: typeof ServiceNavigation;
35
39
  SkipLink: typeof SkipLink;
36
40
  Tabs: typeof Tabs;
41
+ Component: typeof Component;
42
+ ConfigurableComponent: typeof ConfigurableComponent;
37
43
  createAll: typeof createAll;
38
44
  initAll: typeof initAll;
39
45
  };
@@ -0,0 +1 @@
1
+ export * from "./configuration.js";
@@ -0,0 +1,208 @@
1
+ import { GOVUKFrontendComponent } from "../govuk-frontend-component.js";
2
+
3
+ /**
4
+ * Normalise string
5
+ *
6
+ * 'If it looks like a duck, and it quacks like a duck…' 🦆
7
+ *
8
+ * If the passed value looks like a boolean or a number, convert it to a boolean
9
+ * or number.
10
+ *
11
+ * Designed to be used to convert config passed via data attributes (which are
12
+ * always strings) into something sensible.
13
+ *
14
+ * @internal
15
+ * @param {DOMStringMap[string]} value - The value to normalise
16
+ * @param {SchemaProperty} [property] - Component schema property
17
+ * @returns {string | boolean | number | undefined} Normalised data
18
+ */
19
+ export function normaliseString(
20
+ value: DOMStringMap[string],
21
+ property?: SchemaProperty,
22
+ ): string | boolean | number | undefined;
23
+
24
+ /**
25
+ * Normalise dataset
26
+ *
27
+ * Loop over an object and normalise each value using {@link normaliseString},
28
+ * optionally expanding nested `i18n.field`
29
+ *
30
+ * @internal
31
+ * @param {{ schema?: Schema<ObjectNested>, moduleName: string }} Component - Component class
32
+ * @param {DOMStringMap} dataset - HTML element dataset
33
+ * @returns {ObjectNested} Normalised dataset
34
+ */
35
+ export function normaliseDataset(
36
+ Component: {
37
+ schema?: Schema<ObjectNested>;
38
+ moduleName: string;
39
+ },
40
+ dataset: DOMStringMap,
41
+ ): ObjectNested;
42
+
43
+ /**
44
+ * Config merging function
45
+ *
46
+ * Takes any number of objects and combines them together, with
47
+ * greatest priority on the LAST item passed in.
48
+ *
49
+ * @internal
50
+ * @param {...{ [key: string]: unknown }} configObjects - Config objects to merge
51
+ * @returns {{ [key: string]: unknown }} A merged config object
52
+ */
53
+ export function mergeConfigs(
54
+ ...configObjects: Array<{
55
+ [key: string]: unknown;
56
+ }>
57
+ ): {
58
+ [key: string]: unknown;
59
+ };
60
+
61
+ /**
62
+ * Validate component config by schema
63
+ *
64
+ * Follows limited examples in JSON schema for wider support in future
65
+ *
66
+ * {@link https://ajv.js.org/json-schema.html#compound-keywords}
67
+ * {@link https://ajv.js.org/packages/ajv-errors.html#single-message}
68
+ *
69
+ * @internal
70
+ * @template {Partial<Record<keyof ConfigurationType, unknown>>} ConfigurationType
71
+ * @param {Schema<ConfigurationType>} schema - The schema of a component
72
+ * @param {ConfigurationType} config - Component config
73
+ * @returns {string[]} List of validation errors
74
+ */
75
+ export function validateConfig<
76
+ ConfigurationType extends Partial<
77
+ Record<keyof ConfigurationType, unknown>
78
+ >,
79
+ >(
80
+ schema: Schema<ConfigurationType>,
81
+ config: ConfigurationType,
82
+ ): string[];
83
+
84
+ /**
85
+ * Extracts keys starting with a particular namespace from dataset ('data-*')
86
+ * object, removing the namespace in the process, normalising all values
87
+ *
88
+ * @internal
89
+ * @template {Partial<Record<keyof ConfigurationType, unknown>>} ConfigurationType
90
+ * @param {Schema<ConfigurationType>} schema - The schema of a component
91
+ * @param {DOMStringMap} dataset - The object to extract key-value pairs from
92
+ * @param {keyof ConfigurationType} namespace - The namespace to filter keys with
93
+ * @returns {ObjectNested | undefined} Nested object with dot-separated key namespace removed
94
+ */
95
+ export function extractConfigByNamespace<
96
+ ConfigurationType extends Partial<
97
+ Record<keyof ConfigurationType, unknown>
98
+ >,
99
+ >(
100
+ schema: Schema<ConfigurationType>,
101
+ dataset: DOMStringMap,
102
+ namespace: keyof ConfigurationType,
103
+ ): ObjectNested | undefined;
104
+
105
+ export const configOverride: unique symbol;
106
+
107
+ /**
108
+ * Base Component class
109
+ *
110
+ * Centralises the behaviours shared by our components
111
+ *
112
+ * @template {Partial<Record<keyof ConfigurationType, unknown>>} [ConfigurationType=ObjectNested]
113
+ * @template {Element & { dataset: DOMStringMap }} [RootElementType=HTMLElement]
114
+ */
115
+ export abstract class ConfigurableComponent<
116
+ ConfigurationType extends Partial<
117
+ Record<keyof ConfigurationType, unknown>
118
+ > = ObjectNested,
119
+ RootElementType extends Element & {
120
+ dataset: DOMStringMap;
121
+ } = HTMLElement,
122
+ > extends GOVUKFrontendComponent<RootElementType> {
123
+ /**
124
+ * Constructs a new component, validating that GOV.UK Frontend is supported
125
+ *
126
+ * @internal
127
+ * @param {Element | null} [$root] - HTML element to use for component
128
+ * @param {ConfigurationType} [config] - HTML element to use for component
129
+ */
130
+ constructor($root?: Element | null, config?: ConfigurationType);
131
+
132
+ /**
133
+ * Returns the root element of the component
134
+ *
135
+ * @returns {ConfigurationType} - the root element of component
136
+ */
137
+ protected get config(): ConfigurationType;
138
+
139
+ /** */
140
+ _config: ConfigurationType;
141
+
142
+ /**
143
+ * configOverride
144
+ *
145
+ * Function which defines configuration overrides to prioritize
146
+ * properties from the root element's dataset.
147
+ *
148
+ * It should take a subset of configuration as input and return
149
+ * a new configuration object with properties that should be
150
+ * overridden based on the root element's dataset. A Symbol
151
+ * is used for indexing to prevent conflicts.
152
+ *
153
+ * @internal
154
+ * @param {Partial<ConfigurationType>} [param] - Configuration object
155
+ * @returns {Partial<ConfigurationType>} return - Configuration object
156
+ */
157
+ [configOverride](param?: Partial<ConfigurationType>): Partial<ConfigurationType>;
158
+ }
159
+
160
+ export type NestedKey = keyof ObjectNested;
161
+
162
+ export interface ObjectNested {
163
+ [key: string]: string | boolean | number | ObjectNested | undefined;
164
+ }
165
+
166
+ /**
167
+ * Schema for component config
168
+ */
169
+ export interface Schema<
170
+ ConfigurationType extends Partial<
171
+ Record<keyof ConfigurationType, unknown>
172
+ >,
173
+ > {
174
+ /**
175
+ * - Schema properties
176
+ */
177
+ properties: Record<keyof ConfigurationType, SchemaProperty | undefined>;
178
+
179
+ /**
180
+ * - List of schema conditions
181
+ */
182
+ anyOf?: SchemaCondition[] | undefined;
183
+ }
184
+
185
+ /**
186
+ * Schema property for component config
187
+ */
188
+ export interface SchemaProperty {
189
+ /**
190
+ * - Property type
191
+ */
192
+ type: "string" | "boolean" | "number" | "object";
193
+ }
194
+
195
+ /**
196
+ * Schema condition for component config
197
+ */
198
+ export interface SchemaCondition {
199
+ /**
200
+ * - List of required config fields
201
+ */
202
+ required: string[];
203
+
204
+ /**
205
+ * - Error message when required config fields not provided
206
+ */
207
+ errorMessage: string;
208
+ }
@@ -1,3 +1,5 @@
1
+ import { type ObjectNested } from "./configuration.js";
2
+
1
3
  /**
2
4
  * Common helpers which do not require polyfill.
3
5
  *
@@ -5,41 +7,6 @@
5
7
  * so that the polyfill can be properly tree-shaken and does not burden
6
8
  * the components that do not need that helper
7
9
  */
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
10
 
44
11
  /**
45
12
  * Get hash fragment from URL
@@ -83,77 +50,54 @@ export function setFocus<FocusElement extends HTMLElement>(
83
50
  },
84
51
  ): void;
85
52
 
53
+ /**
54
+ * Checks if component is already initialised
55
+ *
56
+ * @internal
57
+ * @param {Element} $root - HTML element to be checked
58
+ * @param {string} moduleName - name of component module
59
+ * @returns {boolean} Whether component is already initialised
60
+ */
61
+ export function isInitialised($root: Element, moduleName: string): boolean;
62
+
86
63
  /**
87
64
  * Checks if GOV.UK Frontend is supported on this page
88
65
  *
89
66
  * Some browsers will load and run our JavaScript but GOV.UK Frontend
90
67
  * won't be supported.
91
68
  *
92
- * @internal
93
- * @param {HTMLElement | null} [$scope] - HTML element `<body>` checked for browser support
69
+ * @param {HTMLElement | null} [$scope] - (internal) `<body>` HTML element checked for browser support
94
70
  * @returns {boolean} Whether GOV.UK Frontend is supported on this page
95
71
  */
96
72
  export function isSupported($scope?: HTMLElement | null): boolean;
97
73
 
98
74
  /**
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}
75
+ * Check for an object
105
76
  *
106
77
  * @internal
107
- * @param {Schema} schema - Config schema
108
- * @param {{ [key: string]: unknown }} config - Component config
109
- * @returns {string[]} List of validation errors
78
+ * @template {Partial<Record<keyof ObjectType, unknown>>} [ObjectType=ObjectNested]
79
+ * @param {unknown | ObjectType} option - Option to check
80
+ * @returns {option is ObjectType} Whether the option is an object
110
81
  */
111
- export function validateConfig(schema: Schema, config: {
112
- [key: string]: unknown;
113
- }): string[];
82
+ export function isObject<
83
+ ObjectType extends Partial<Record<keyof ObjectType, unknown>> = ObjectNested,
84
+ >(
85
+ option: unknown,
86
+ ): option is ObjectType;
114
87
 
115
88
  /**
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
89
+ * Format error message
90
+ *
91
+ * @internal
92
+ * @param {ComponentWithModuleName} Component - Component that threw the error
93
+ * @param {string} message - Error message
94
+ * @returns {string} - Formatted error message
144
95
  */
145
- export interface SchemaCondition {
146
- /**
147
- * - List of required config fields
148
- */
149
- required: string[];
96
+ export function formatErrorMessage(Component: ComponentWithModuleName, message: string): string;
150
97
 
98
+ export interface ComponentWithModuleName {
151
99
  /**
152
- * - Error message when required config fields not provided
100
+ * - Name of the component
153
101
  */
154
- errorMessage: string;
155
- }
156
- export type NestedKey = keyof ObjectNested;
157
- export interface ObjectNested {
158
- [key: string]: string | boolean | number | ObjectNested | undefined;
102
+ moduleName: string;
159
103
  }
@@ -1,4 +1,4 @@
1
- import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
1
+ import { ConfigurableComponent } from "../../common/configuration.js";
2
2
 
3
3
  /**
4
4
  * Accordion component
@@ -12,7 +12,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
12
12
  * The state of each section is saved to the DOM via the `aria-expanded`
13
13
  * attribute, which also provides accessibility.
14
14
  */
15
- export class Accordion extends GOVUKFrontendComponent {
15
+ export class Accordion extends ConfigurableComponent<AccordionConfig, HTMLElement> {
16
16
  /**
17
17
  * Name for the component used when initialising using data-module attributes.
18
18
  */
@@ -42,10 +42,10 @@ export class Accordion extends GOVUKFrontendComponent {
42
42
  }>;
43
43
 
44
44
  /**
45
- * @param {Element | null} $module - HTML element to use for accordion
45
+ * @param {Element | null} $root - HTML element to use for accordion
46
46
  * @param {AccordionConfig} [config] - Accordion config
47
47
  */
48
- constructor($module: Element | null, config?: AccordionConfig);
48
+ constructor($root: Element | null, config?: AccordionConfig);
49
49
 
50
50
  /**
51
51
  * Get the identifier for a section
@@ -1,9 +1,9 @@
1
- import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
1
+ import { ConfigurableComponent } from "../../common/configuration.js";
2
2
 
3
3
  /**
4
4
  * JavaScript enhancements for the Button component
5
5
  */
6
- export class Button extends GOVUKFrontendComponent {
6
+ export class Button extends ConfigurableComponent<ButtonConfig, HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
@@ -30,10 +30,10 @@ export class Button extends GOVUKFrontendComponent {
30
30
  }>;
31
31
 
32
32
  /**
33
- * @param {Element | null} $module - HTML element to use for button
33
+ * @param {Element | null} $root - HTML element to use for button
34
34
  * @param {ButtonConfig} [config] - Button config
35
35
  */
36
- constructor($module: Element | null, config?: ButtonConfig);
36
+ constructor($root: Element | null, config?: ButtonConfig);
37
37
  }
38
38
 
39
39
  /**
@@ -1,4 +1,4 @@
1
- import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
1
+ import { configOverride, ConfigurableComponent } from "../../common/configuration.js";
2
2
  import { type TranslationPluralForms } from "../../i18n.js";
3
3
 
4
4
  /**
@@ -11,7 +11,7 @@ import { type TranslationPluralForms } from "../../i18n.js";
11
11
  * You can configure the message to only appear after a certain percentage
12
12
  * of the available characters/words has been entered.
13
13
  */
14
- export class CharacterCount extends GOVUKFrontendComponent {
14
+ export class CharacterCount extends ConfigurableComponent<CharacterCountConfig, HTMLElement> {
15
15
  /**
16
16
  * Name for the component used when initialising using data-module attributes.
17
17
  */
@@ -51,10 +51,23 @@ export class CharacterCount extends GOVUKFrontendComponent {
51
51
  }>;
52
52
 
53
53
  /**
54
- * @param {Element | null} $module - HTML element to use for character count
54
+ * @param {Element | null} $root - HTML element to use for character count
55
55
  * @param {CharacterCountConfig} [config] - Character count config
56
56
  */
57
- constructor($module: Element | null, config?: CharacterCountConfig);
57
+ constructor($root: Element | null, config?: CharacterCountConfig);
58
+
59
+ /**
60
+ * Character count config override
61
+ *
62
+ * To ensure data-attributes take complete precedence, even if they change
63
+ * the type of count, we need to reset the `maxlength` and `maxwords` from
64
+ * the JavaScript config.
65
+ *
66
+ * @internal
67
+ * @param {CharacterCountConfig} datasetConfig - configuration specified by dataset
68
+ * @returns {CharacterCountConfig} - configuration to override by dataset
69
+ */
70
+ [configOverride](datasetConfig: CharacterCountConfig): CharacterCountConfig;
58
71
  }
59
72
 
60
73
  /**
@@ -3,7 +3,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Checkboxes component
5
5
  */
6
- export class Checkboxes extends GOVUKFrontendComponent {
6
+ export class Checkboxes extends GOVUKFrontendComponent<HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
@@ -21,7 +21,7 @@ export class Checkboxes extends GOVUKFrontendComponent {
21
21
  * (for example if the user has navigated back), and set up event handlers to
22
22
  * keep the reveal in sync with the checkbox state.
23
23
  *
24
- * @param {Element | null} $module - HTML element to use for checkboxes
24
+ * @param {Element | null} $root - HTML element to use for checkboxes
25
25
  */
26
- constructor($module: Element | null);
26
+ constructor($root: Element | null);
27
27
  }
@@ -1,4 +1,4 @@
1
- import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
1
+ import { ConfigurableComponent } from "../../common/configuration.js";
2
2
 
3
3
  /**
4
4
  * Error summary component
@@ -6,7 +6,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
6
6
  * Takes focus on initialisation for accessible announcement, unless disabled in
7
7
  * configuration.
8
8
  */
9
- export class ErrorSummary extends GOVUKFrontendComponent {
9
+ export class ErrorSummary extends ConfigurableComponent<ErrorSummaryConfig, HTMLElement> {
10
10
  /**
11
11
  * Name for the component used when initialising using data-module attributes.
12
12
  */
@@ -33,10 +33,10 @@ export class ErrorSummary extends GOVUKFrontendComponent {
33
33
  }>;
34
34
 
35
35
  /**
36
- * @param {Element | null} $module - HTML element to use for error summary
36
+ * @param {Element | null} $root - HTML element to use for error summary
37
37
  * @param {ErrorSummaryConfig} [config] - Error summary config
38
38
  */
39
- constructor($module: Element | null, config?: ErrorSummaryConfig);
39
+ constructor($root: Element | null, config?: ErrorSummaryConfig);
40
40
  }
41
41
 
42
42
  /**
@@ -1,9 +1,9 @@
1
- import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
1
+ import { ConfigurableComponent } from "../../common/configuration.js";
2
2
 
3
3
  /**
4
4
  * Exit this page component
5
5
  */
6
- export class ExitThisPage extends GOVUKFrontendComponent {
6
+ export class ExitThisPage extends ConfigurableComponent<ExitThisPageConfig, HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
@@ -30,10 +30,10 @@ export class ExitThisPage extends GOVUKFrontendComponent {
30
30
  }>;
31
31
 
32
32
  /**
33
- * @param {Element | null} $module - HTML element that wraps the Exit This Page button
33
+ * @param {Element | null} $root - HTML element that wraps the Exit This Page button
34
34
  * @param {ExitThisPageConfig} [config] - Exit This Page config
35
35
  */
36
- constructor($module: Element | null, config?: ExitThisPageConfig);
36
+ constructor($root: Element | null, config?: ExitThisPageConfig);
37
37
  }
38
38
 
39
39
  /**
@@ -3,7 +3,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Header component
5
5
  */
6
- export class Header extends GOVUKFrontendComponent {
6
+ export class Header extends GOVUKFrontendComponent<HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
@@ -13,7 +13,7 @@ export class Header extends GOVUKFrontendComponent {
13
13
  * Apply a matchMedia for desktop which will trigger a state sync if the
14
14
  * browser viewport moves between states.
15
15
  *
16
- * @param {Element | null} $module - HTML element to use for header
16
+ * @param {Element | null} $root - HTML element to use for header
17
17
  */
18
- constructor($module: Element | null);
18
+ constructor($root: Element | null);
19
19
  }
@@ -1,9 +1,9 @@
1
- import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
1
+ import { ConfigurableComponent } from "../../common/configuration.js";
2
2
 
3
3
  /**
4
4
  * Notification Banner component
5
5
  */
6
- export class NotificationBanner extends GOVUKFrontendComponent {
6
+ export class NotificationBanner extends ConfigurableComponent<NotificationBannerConfig, HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
@@ -30,10 +30,10 @@ export class NotificationBanner extends GOVUKFrontendComponent {
30
30
  }>;
31
31
 
32
32
  /**
33
- * @param {Element | null} $module - HTML element to use for notification banner
33
+ * @param {Element | null} $root - HTML element to use for notification banner
34
34
  * @param {NotificationBannerConfig} [config] - Notification banner config
35
35
  */
36
- constructor($module: Element | null, config?: NotificationBannerConfig);
36
+ constructor($root: Element | null, config?: NotificationBannerConfig);
37
37
  }
38
38
 
39
39
  /**
@@ -1,9 +1,9 @@
1
- import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
1
+ import { ConfigurableComponent } from "../../common/configuration.js";
2
2
 
3
3
  /**
4
4
  * Password input component
5
5
  */
6
- export class PasswordInput extends GOVUKFrontendComponent {
6
+ export class PasswordInput extends ConfigurableComponent<PasswordInputConfig, HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
@@ -30,10 +30,10 @@ export class PasswordInput extends GOVUKFrontendComponent {
30
30
  }>;
31
31
 
32
32
  /**
33
- * @param {Element | null} $module - HTML element to use for password input
33
+ * @param {Element | null} $root - HTML element to use for password input
34
34
  * @param {PasswordInputConfig} [config] - Password input config
35
35
  */
36
- constructor($module: Element | null, config?: PasswordInputConfig);
36
+ constructor($root: Element | null, config?: PasswordInputConfig);
37
37
  }
38
38
 
39
39
  /**
@@ -3,7 +3,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Radios component
5
5
  */
6
- export class Radios extends GOVUKFrontendComponent {
6
+ export class Radios extends GOVUKFrontendComponent<HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
@@ -21,7 +21,7 @@ export class Radios extends GOVUKFrontendComponent {
21
21
  * (for example if the user has navigated back), and set up event handlers to
22
22
  * keep the reveal in sync with the radio state.
23
23
  *
24
- * @param {Element | null} $module - HTML element to use for radios
24
+ * @param {Element | null} $root - HTML element to use for radios
25
25
  */
26
- constructor($module: Element | null);
26
+ constructor($root: Element | null);
27
27
  }
@@ -3,14 +3,14 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Service Navigation component
5
5
  */
6
- export class ServiceNavigation extends GOVUKFrontendComponent {
6
+ export class ServiceNavigation extends GOVUKFrontendComponent<HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
10
10
  static moduleName: string;
11
11
 
12
12
  /**
13
- * @param {Element | null} $module - HTML element to use for header
13
+ * @param {Element | null} $root - HTML element to use for header
14
14
  */
15
- constructor($module: Element | null);
15
+ constructor($root: Element | null);
16
16
  }
@@ -3,17 +3,22 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Skip link component
5
5
  */
6
- export class SkipLink extends GOVUKFrontendComponent {
6
+ export class SkipLink extends GOVUKFrontendComponent<HTMLAnchorElement> {
7
+ static elementType: {
8
+ new(): HTMLAnchorElement;
9
+ prototype: HTMLAnchorElement;
10
+ };
11
+
7
12
  /**
8
13
  * Name for the component used when initialising using data-module attributes.
9
14
  */
10
15
  static moduleName: string;
11
16
 
12
17
  /**
13
- * @param {Element | null} $module - HTML element to use for skip link
14
- * @throws {ElementError} when $module is not set or the wrong type
15
- * @throws {ElementError} when $module.hash does not contain a hash
18
+ * @param {Element | null} $root - HTML element to use for skip link
19
+ * @throws {ElementError} when $root is not set or the wrong type
20
+ * @throws {ElementError} when $root.hash does not contain a hash
16
21
  * @throws {ElementError} when the linked element is missing or the wrong type
17
22
  */
18
- constructor($module: Element | null);
23
+ constructor($root: Element | null);
19
24
  }
@@ -3,14 +3,14 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Tabs component
5
5
  */
6
- export class Tabs extends GOVUKFrontendComponent {
6
+ export class Tabs extends GOVUKFrontendComponent<HTMLElement> {
7
7
  /**
8
8
  * Name for the component used when initialising using data-module attributes.
9
9
  */
10
10
  static moduleName: string;
11
11
 
12
12
  /**
13
- * @param {Element | null} $module - HTML element to use for tabs
13
+ * @param {Element | null} $root - HTML element to use for tabs
14
14
  */
15
- constructor($module: Element | null);
15
+ constructor($root: Element | null);
16
16
  }
@@ -1,3 +1,5 @@
1
+ import { type ComponentWithModuleName } from "../common/index.js";
2
+
1
3
  /**
2
4
  * GOV.UK Frontend error
3
5
  *
@@ -17,7 +19,7 @@
17
19
  * }
18
20
  * ```
19
21
  */
20
- export class GOVUKFrontendError extends Error {
22
+ export abstract class GOVUKFrontendError extends Error {
21
23
  }
22
24
 
23
25
  /**
@@ -58,14 +60,20 @@ export class ElementError extends GOVUKFrontendError {
58
60
  }
59
61
 
60
62
  /**
61
- * Element error options
63
+ * Indicates that a component is already initialised
62
64
  */
63
- export interface ElementErrorOptions {
65
+ export class InitError extends GOVUKFrontendError {
64
66
  /**
65
- * - The name of the component throwing the error
67
+ * @internal
68
+ * @param {ComponentWithModuleName | string} componentOrMessage - name of the component module
66
69
  */
67
- componentName: string;
70
+ constructor(componentOrMessage: ComponentWithModuleName | string);
71
+ }
68
72
 
73
+ /**
74
+ * Element error options
75
+ */
76
+ export interface ElementErrorOptions {
69
77
  /**
70
78
  * - An identifier that'll let the user understand which element has an error. This is whatever makes the most sense
71
79
  */
@@ -80,4 +88,9 @@ export interface ElementErrorOptions {
80
88
  * - The type that was expected for the identifier
81
89
  */
82
90
  expectedType?: string | undefined;
91
+
92
+ /**
93
+ * - Component throwing the error
94
+ */
95
+ component: ComponentWithModuleName;
83
96
  }
@@ -2,8 +2,29 @@
2
2
  * Base Component class
3
3
  *
4
4
  * Centralises the behaviours shared by our components
5
- *
6
- * @internal
7
5
  */
8
- export class GOVUKFrontendComponent {
6
+ export abstract class GOVUKFrontendComponent<RootElementType extends Element = HTMLElement> {
7
+ static elementType: typeof Element;
8
+
9
+ /**
10
+ * Validates whether components are supported
11
+ *
12
+ * @throws {SupportError} when the components are not supported
13
+ */
14
+ static checkSupport(): void;
15
+
16
+ /**
17
+ * Constructs a new component, validating that GOV.UK Frontend is supported
18
+ *
19
+ * @internal
20
+ * @param {Element | null} [$root] - HTML element to use for component
21
+ */
22
+ constructor($root?: Element | null);
23
+
24
+ /**
25
+ * Returns the root element of the component
26
+ *
27
+ * @returns {RootElementType} - the root element of component
28
+ */
29
+ protected get $root(): RootElementType;
9
30
  }
@@ -8,7 +8,6 @@ import { type PasswordInputConfig } from "./components/password-input/password-i
8
8
 
9
9
  export interface CompatibleClass {
10
10
  new(...args: any[]): any;
11
- defaults?: object;
12
11
  moduleName: string;
13
12
  }
14
13
 
@@ -57,17 +56,54 @@ export interface Config {
57
56
  */
58
57
  export type ConfigKey = keyof Config;
59
58
 
59
+ export type ComponentConfig<ComponentClass extends CompatibleClass> = ConstructorParameters<ComponentClass>[1];
60
+
61
+ export interface ErrorContext<ComponentClass extends CompatibleClass> {
62
+ /**
63
+ * - Element used for component module initialisation
64
+ */
65
+ element?: Element | undefined;
66
+
67
+ /**
68
+ * - Class of component
69
+ */
70
+ component?: ComponentClass | undefined;
71
+
72
+ /**
73
+ * - Config supplied to component
74
+ */
75
+ config: ComponentConfig<ComponentClass>;
76
+ }
77
+
78
+ export type OnErrorCallback<ComponentClass extends CompatibleClass> = (
79
+ error: unknown,
80
+ context: ErrorContext<ComponentClass>,
81
+ ) => any;
82
+
83
+ export interface CreateAllOptions<ComponentClass extends CompatibleClass> {
84
+ /**
85
+ * - scope of the document to search within
86
+ */
87
+ scope?: Document | Element | undefined;
88
+
89
+ /**
90
+ * - callback function if error throw by component on init
91
+ */
92
+ onError?: OnErrorCallback<ComponentClass> | undefined;
93
+ }
94
+
60
95
  /**
61
96
  * Initialise all components
62
97
  *
63
98
  * Use the `data-module` attributes to find, instantiate and init all of the
64
99
  * components provided as part of GOV.UK Frontend.
65
100
  *
66
- * @param {Config & { scope?: Element }} [config] - Config for all components (with optional scope)
101
+ * @param {Config & { scope?: Element, onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)
67
102
  */
68
103
  export function initAll(
69
104
  config?: Config & {
70
105
  scope?: Element;
106
+ onError?: OnErrorCallback<CompatibleClass>;
71
107
  },
72
108
  ): void;
73
109
 
@@ -80,14 +116,14 @@ export function initAll(
80
116
  *
81
117
  * Any component errors will be caught and logged to the console.
82
118
  *
83
- * @template {CompatibleClass} T
84
- * @param {T} Component - class of the component to create
85
- * @param {T["defaults"]} [config] - config for the component
86
- * @param {Element|Document} [$scope] - scope of the document to search within
87
- * @returns {Array<InstanceType<T>>} - array of instantiated components
119
+ * @template {CompatibleClass} ComponentClass
120
+ * @param {ComponentClass} Component - class of the component to create
121
+ * @param {ComponentConfig<ComponentClass>} [config] - Config supplied to component
122
+ * @param {OnErrorCallback<ComponentClass> | Element | Document | CreateAllOptions<ComponentClass> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
123
+ * @returns {Array<InstanceType<ComponentClass>>} - array of instantiated components
88
124
  */
89
- export function createAll<T extends CompatibleClass>(
90
- Component: T,
91
- config?: T["defaults"],
92
- $scope?: Document | Element,
93
- ): Array<InstanceType<T>>;
125
+ export function createAll<ComponentClass extends CompatibleClass>(
126
+ Component: ComponentClass,
127
+ config?: ComponentConfig<ComponentClass>,
128
+ createAllOptions?: OnErrorCallback<ComponentClass> | Element | Document | CreateAllOptions<ComponentClass>,
129
+ ): Array<InstanceType<ComponentClass>>;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@types/govuk-frontend",
3
- "version": "5.6.0",
3
+ "version": "5.8.0",
4
4
  "description": "TypeScript definitions for govuk-frontend",
5
5
  "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/govuk-frontend",
6
6
  "license": "MIT",
@@ -32,6 +32,7 @@
32
32
  },
33
33
  "scripts": {},
34
34
  "dependencies": {},
35
- "typesPublisherContentHash": "e8f70663b9ffa7250d5ba386fc0ee65bffb85e8da2f64ce0a95445f8af7f19b1",
36
- "typeScriptVersion": "4.8"
35
+ "peerDependencies": {},
36
+ "typesPublisherContentHash": "3df1d321f714ae24feef605eab0f6302b12f0717613036605b5de394fb136253",
37
+ "typeScriptVersion": "5.0"
37
38
  }
@@ -1 +0,0 @@
1
- export * from "./normalise-dataset.js";
@@ -1,16 +0,0 @@
1
- import { type ObjectNested, type Schema } from "./index.js";
2
-
3
- /**
4
- * Normalise dataset
5
- *
6
- * Loop over an object and normalise each value using {@link normaliseString},
7
- * optionally expanding nested `i18n.field`
8
- *
9
- * @internal
10
- * @param {{ schema: Schema }} Component - Component class
11
- * @param {DOMStringMap} dataset - HTML element dataset
12
- * @returns {ObjectNested} Normalised dataset
13
- */
14
- export function normaliseDataset(Component: {
15
- schema: Schema;
16
- }, dataset: DOMStringMap): ObjectNested;
@@ -1 +0,0 @@
1
- export * from "./normalise-string.js";
@@ -1,22 +0,0 @@
1
- import { type SchemaProperty } from "./index.js";
2
-
3
- /**
4
- * Normalise string
5
- *
6
- * 'If it looks like a duck, and it quacks like a duck…' 🦆
7
- *
8
- * If the passed value looks like a boolean or a number, convert it to a boolean
9
- * or number.
10
- *
11
- * Designed to be used to convert config passed via data attributes (which are
12
- * always strings) into something sensible.
13
- *
14
- * @internal
15
- * @param {DOMStringMap[string]} value - The value to normalise
16
- * @param {SchemaProperty} [property] - Component schema property
17
- * @returns {string | boolean | number | undefined} Normalised data
18
- */
19
- export function normaliseString(
20
- value: DOMStringMap[string],
21
- property?: SchemaProperty,
22
- ): string | boolean | number | undefined;