|
| 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