@types/govuk-frontend 5.4.1 → 5.7.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.
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: Mon, 07 Oct 2024 17:07:30 GMT
11
+ * Last updated: Mon, 14 Oct 2024 16:41:00 GMT
12
12
  * Dependencies: none
13
13
 
14
14
  # Credits
@@ -1,4 +1,5 @@
1
1
  export { version } from "./common/govuk-frontend-version.mjs";
2
+ export { isSupported } from "./common/index.mjs";
2
3
  export { Accordion } from "./components/accordion/accordion.mjs";
3
4
  export { Button } from "./components/button/button.mjs";
4
5
  export { CharacterCount } from "./components/character-count/character-count.mjs";
@@ -9,6 +10,8 @@ export { Header } from "./components/header/header.mjs";
9
10
  export { NotificationBanner } from "./components/notification-banner/notification-banner.mjs";
10
11
  export { PasswordInput } from "./components/password-input/password-input.mjs";
11
12
  export { Radios } from "./components/radios/radios.mjs";
13
+ export { ServiceNavigation } from "./components/service-navigation/service-navigation.mjs";
12
14
  export { SkipLink } from "./components/skip-link/skip-link.mjs";
13
15
  export { Tabs } from "./components/tabs/tabs.mjs";
16
+ export { GOVUKFrontendComponent as Component } from "./govuk-frontend-component.mjs";
14
17
  export { type Config, type ConfigKey, createAll, initAll } from "./init.mjs";
@@ -1,4 +1,5 @@
1
1
  import { version } from "./common/govuk-frontend-version.js";
2
+ import { isSupported } from "./common/index.js";
2
3
  import { Accordion } from "./components/accordion/accordion.js";
3
4
  import { Button } from "./components/button/button.js";
4
5
  import { CharacterCount } from "./components/character-count/character-count.js";
@@ -9,8 +10,10 @@ import { Header } from "./components/header/header.js";
9
10
  import { NotificationBanner } from "./components/notification-banner/notification-banner.js";
10
11
  import { PasswordInput } from "./components/password-input/password-input.js";
11
12
  import { Radios } from "./components/radios/radios.js";
13
+ import { ServiceNavigation } from "./components/service-navigation/service-navigation.js";
12
14
  import { SkipLink } from "./components/skip-link/skip-link.js";
13
15
  import { Tabs } from "./components/tabs/tabs.js";
16
+ import { GOVUKFrontendComponent as Component } from "./govuk-frontend-component.js";
14
17
  import { type Config as ConfigImport, type ConfigKey as ConfigKeyImport, createAll, initAll } from "./init.js";
15
18
 
16
19
  declare namespace GOVUKFrontend {
@@ -20,6 +23,7 @@ declare namespace GOVUKFrontend {
20
23
 
21
24
  declare const GOVUKFrontend: {
22
25
  version: typeof version;
26
+ isSupported: typeof isSupported;
23
27
  Accordion: typeof Accordion;
24
28
  Button: typeof Button;
25
29
  CharacterCount: typeof CharacterCount;
@@ -30,8 +34,10 @@ declare const GOVUKFrontend: {
30
34
  NotificationBanner: typeof NotificationBanner;
31
35
  PasswordInput: typeof PasswordInput;
32
36
  Radios: typeof Radios;
37
+ ServiceNavigation: typeof ServiceNavigation;
33
38
  SkipLink: typeof SkipLink;
34
39
  Tabs: typeof Tabs;
40
+ Component: typeof Component;
35
41
  createAll: typeof createAll;
36
42
  initAll: typeof initAll;
37
43
  };
@@ -83,14 +83,23 @@ export function setFocus<FocusElement extends HTMLElement>(
83
83
  },
84
84
  ): void;
85
85
 
86
+ /**
87
+ * Checks if component is already initialised
88
+ *
89
+ * @internal
90
+ * @param {Element} $root - HTML element to be checked
91
+ * @param {string} moduleName - name of component module
92
+ * @returns {boolean} Whether component is already initialised
93
+ */
94
+ export function isInitialised($root: Element, moduleName: string): boolean;
95
+
86
96
  /**
87
97
  * Checks if GOV.UK Frontend is supported on this page
88
98
  *
89
99
  * Some browsers will load and run our JavaScript but GOV.UK Frontend
90
100
  * won't be supported.
91
101
  *
92
- * @internal
93
- * @param {HTMLElement | null} [$scope] - HTML element `<body>` checked for browser support
102
+ * @param {HTMLElement | null} [$scope] - (internal) `<body>` HTML element checked for browser support
94
103
  * @returns {boolean} Whether GOV.UK Frontend is supported on this page
95
104
  */
96
105
  export function isSupported($scope?: HTMLElement | null): boolean;
@@ -112,6 +121,16 @@ export function validateConfig(schema: Schema, config: {
112
121
  [key: string]: unknown;
113
122
  }): string[];
114
123
 
124
+ /**
125
+ * Format error message
126
+ *
127
+ * @internal
128
+ * @param {ComponentWithModuleName} Component - Component that threw the error
129
+ * @param {string} message - Error message
130
+ * @returns {string} - Formatted error message
131
+ */
132
+ export function formatErrorMessage(Component: ComponentWithModuleName, message: string): string;
133
+
115
134
  /**
116
135
  * Schema for component config
117
136
  */
@@ -153,7 +172,16 @@ export interface SchemaCondition {
153
172
  */
154
173
  errorMessage: string;
155
174
  }
175
+
156
176
  export type NestedKey = keyof ObjectNested;
177
+
157
178
  export interface ObjectNested {
158
179
  [key: string]: string | boolean | number | ObjectNested | undefined;
159
180
  }
181
+
182
+ export interface ComponentWithModuleName {
183
+ /**
184
+ * - Name of the component
185
+ */
186
+ moduleName: string;
187
+ }
@@ -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 GOVUKFrontendComponent<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
@@ -3,7 +3,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * JavaScript enhancements for the Button component
5
5
  */
6
- export class Button extends GOVUKFrontendComponent {
6
+ export class Button extends GOVUKFrontendComponent<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
  /**
@@ -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 GOVUKFrontendComponent<HTMLElement> {
15
15
  /**
16
16
  * Name for the component used when initialising using data-module attributes.
17
17
  */
@@ -51,10 +51,10 @@ 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
58
  }
59
59
 
60
60
  /**
@@ -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
  }
@@ -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 GOVUKFrontendComponent<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
  /**
@@ -3,7 +3,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Exit this page component
5
5
  */
6
- export class ExitThisPage extends GOVUKFrontendComponent {
6
+ export class ExitThisPage extends GOVUKFrontendComponent<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
  }
@@ -3,7 +3,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Notification Banner component
5
5
  */
6
- export class NotificationBanner extends GOVUKFrontendComponent {
6
+ export class NotificationBanner extends GOVUKFrontendComponent<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
  /**
@@ -3,7 +3,7 @@ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
3
3
  /**
4
4
  * Password input component
5
5
  */
6
- export class PasswordInput extends GOVUKFrontendComponent {
6
+ export class PasswordInput extends GOVUKFrontendComponent<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
  }
@@ -0,0 +1 @@
1
+ export * from "./service-navigation.js";
@@ -0,0 +1,16 @@
1
+ import { GOVUKFrontendComponent } from "../../govuk-frontend-component.js";
2
+
3
+ /**
4
+ * Service Navigation component
5
+ */
6
+ export class ServiceNavigation extends GOVUKFrontendComponent<HTMLElement> {
7
+ /**
8
+ * Name for the component used when initialising using data-module attributes.
9
+ */
10
+ static moduleName: string;
11
+
12
+ /**
13
+ * @param {Element | null} $root - HTML element to use for header
14
+ */
15
+ constructor($root: Element | null);
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
  }
@@ -57,17 +57,49 @@ export interface Config {
57
57
  */
58
58
  export type ConfigKey = keyof Config;
59
59
 
60
+ export interface ErrorContext<T extends CompatibleClass> {
61
+ /**
62
+ * - Element used for component module initialisation
63
+ */
64
+ element?: Element | undefined;
65
+
66
+ /**
67
+ * - Class of component
68
+ */
69
+ component?: T | undefined;
70
+
71
+ /**
72
+ * - Config supplied to component
73
+ */
74
+ config: T["defaults"];
75
+ }
76
+
77
+ export type OnErrorCallback<T extends CompatibleClass> = (error: unknown, context: ErrorContext<T>) => any;
78
+
79
+ export interface CreateAllOptions<T extends CompatibleClass> {
80
+ /**
81
+ * - scope of the document to search within
82
+ */
83
+ scope?: Document | Element | undefined;
84
+
85
+ /**
86
+ * - callback function if error throw by component on init
87
+ */
88
+ onError?: OnErrorCallback<T> | undefined;
89
+ }
90
+
60
91
  /**
61
92
  * Initialise all components
62
93
  *
63
94
  * Use the `data-module` attributes to find, instantiate and init all of the
64
95
  * components provided as part of GOV.UK Frontend.
65
96
  *
66
- * @param {Config & { scope?: Element }} [config] - Config for all components (with optional scope)
97
+ * @param {Config & { scope?: Element, onError?: OnErrorCallback<CompatibleClass> }} [config] - Config for all components (with optional scope)
67
98
  */
68
99
  export function initAll(
69
100
  config?: Config & {
70
101
  scope?: Element;
102
+ onError?: OnErrorCallback<CompatibleClass>;
71
103
  },
72
104
  ): void;
73
105
 
@@ -82,12 +114,12 @@ export function initAll(
82
114
  *
83
115
  * @template {CompatibleClass} T
84
116
  * @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
117
+ * @param {T["defaults"]} [config] - Config supplied to component
118
+ * @param {OnErrorCallback<T> | Element | Document | CreateAllOptions<T> } [createAllOptions] - options for createAll including scope of the document to search within and callback function if error throw by component on init
87
119
  * @returns {Array<InstanceType<T>>} - array of instantiated components
88
120
  */
89
121
  export function createAll<T extends CompatibleClass>(
90
122
  Component: T,
91
123
  config?: T["defaults"],
92
- $scope?: Document | Element,
124
+ createAllOptions?: Document | Element | OnErrorCallback<T> | CreateAllOptions<T>,
93
125
  ): Array<InstanceType<T>>;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@types/govuk-frontend",
3
- "version": "5.4.1",
3
+ "version": "5.7.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,6 @@
32
32
  },
33
33
  "scripts": {},
34
34
  "dependencies": {},
35
- "typesPublisherContentHash": "119b3d995ab239093ec36b6c242fab216abb9c9f5a66c8b30f4c296b4de1d951",
35
+ "typesPublisherContentHash": "08d1cc6a671dadd0e0e9c1113ea085b28a6b2510f52991c817bfb8d71bc308ae",
36
36
  "typeScriptVersion": "4.8"
37
37
  }