Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions types/govuk-frontend/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!**/*.d.ts
!**/*.d.cts
!**/*.d.mts
!**/*.d.*.ts
14 changes: 14 additions & 0 deletions types/govuk-frontend/dist/govuk/all.bundle.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export { version } from "./common/govuk-frontend-version.mjs";
export { Accordion } from "./components/accordion/accordion.mjs";
export { Button } from "./components/button/button.mjs";
export { CharacterCount } from "./components/character-count/character-count.mjs";
export { Checkboxes } from "./components/checkboxes/checkboxes.mjs";
export { ErrorSummary } from "./components/error-summary/error-summary.mjs";
export { ExitThisPage } from "./components/exit-this-page/exit-this-page.mjs";
export { Header } from "./components/header/header.mjs";
export { NotificationBanner } from "./components/notification-banner/notification-banner.mjs";
export { PasswordInput } from "./components/password-input/password-input.mjs";
export { Radios } from "./components/radios/radios.mjs";
export { SkipLink } from "./components/skip-link/skip-link.mjs";
export { Tabs } from "./components/tabs/tabs.mjs";
export { type Config, type ConfigKey, createAll, initAll } from "./init.mjs";
39 changes: 39 additions & 0 deletions types/govuk-frontend/dist/govuk/all.bundle.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { version } from "./common/govuk-frontend-version.js";
import { Accordion } from "./components/accordion/accordion.js";
import { Button } from "./components/button/button.js";
import { CharacterCount } from "./components/character-count/character-count.js";
import { Checkboxes } from "./components/checkboxes/checkboxes.js";
import { ErrorSummary } from "./components/error-summary/error-summary.js";
import { ExitThisPage } from "./components/exit-this-page/exit-this-page.js";
import { Header } from "./components/header/header.js";
import { NotificationBanner } from "./components/notification-banner/notification-banner.js";
import { PasswordInput } from "./components/password-input/password-input.js";
import { Radios } from "./components/radios/radios.js";
import { SkipLink } from "./components/skip-link/skip-link.js";
import { Tabs } from "./components/tabs/tabs.js";
import { type Config as ConfigImport, type ConfigKey as ConfigKeyImport, createAll, initAll } from "./init.js";

declare namespace GOVUKFrontend {
type Config = ConfigImport;
type ConfigKey = ConfigKeyImport;
}

declare const GOVUKFrontend: {
version: typeof version;
Accordion: typeof Accordion;
Button: typeof Button;
CharacterCount: typeof CharacterCount;
Checkboxes: typeof Checkboxes;
ErrorSummary: typeof ErrorSummary;
ExitThisPage: typeof ExitThisPage;
Header: typeof Header;
NotificationBanner: typeof NotificationBanner;
PasswordInput: typeof PasswordInput;
Radios: typeof Radios;
SkipLink: typeof SkipLink;
Tabs: typeof Tabs;
createAll: typeof createAll;
initAll: typeof initAll;
};

export = GOVUKFrontend;
1 change: 1 addition & 0 deletions types/govuk-frontend/dist/govuk/all.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./all.bundle.mjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./closest-attribute-value.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Returns the value of the given attribute closest to the given element (including itself)
*
* @internal
* @param {Element} $element - The element to start walking the DOM tree up
* @param {string} attributeName - The name of the attribute
* @returns {string | null} Attribute value
*/
export function closestAttributeValue($element: Element, attributeName: string): string | null;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./govuk-frontend-version.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* GOV.UK Frontend release version
*
* {@link https://github.com/alphagov/govuk-frontend/releases}
*/
export const version: "development";
1 change: 1 addition & 0 deletions types/govuk-frontend/dist/govuk/common/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./index.js";
159 changes: 159 additions & 0 deletions types/govuk-frontend/dist/govuk/common/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* Common helpers which do not require polyfill.
*
* IMPORTANT: If a helper require a polyfill, please isolate it in its own module
* so that the polyfill can be properly tree-shaken and does not burden
* the components that do not need that helper
*/
/**
* Config merging function
*
* Takes any number of objects and combines them together, with
* greatest priority on the LAST item passed in.
*
* @internal
* @param {...{ [key: string]: unknown }} configObjects - Config objects to merge
* @returns {{ [key: string]: unknown }} A merged config object
*/
export function mergeConfigs(
...configObjects: Array<{
[key: string]: unknown;
}>
): {
[key: string]: unknown;
};

/**
* Extracts keys starting with a particular namespace from dataset ('data-*')
* object, removing the namespace in the process, normalising all values
*
* @internal
* @param {{ schema: Schema }} Component - Component class
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's minor, but .d.ts jsdoc shouldn't have types since they're redundant with the types which are actually checked -- which means that it's easier for them to fall out of date in addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sandersn, it's a shame the compiler leaves these in

Alternatives were removeComments (all comments removed) or edit all JSDoc by hand

The output is from source JavaScript via tsc --build so they'll always match the types, e.g.

https://github.com/alphagov/govuk-frontend/blob/953f15903426a8dc508592ed7dad90a253b058d4/packages/govuk-frontend/src/govuk/common/index.mjs#L50-L53

* @param {DOMStringMap} dataset - The object to extract key-value pairs from
* @param {string} namespace - The namespace to filter keys with
* @returns {ObjectNested | undefined} Nested object with dot-separated key namespace removed
*/
export function extractConfigByNamespace(
Component: {
schema: Schema;
},
dataset: DOMStringMap,
namespace: string,
): ObjectNested | undefined;

/**
* Get hash fragment from URL
*
* Extract the hash fragment (everything after the hash) from a URL,
* but not including the hash symbol
*
* @param {string} url - URL
* @returns {string | undefined} Fragment from URL, without the hash
*/
export function getFragmentFromUrl(url: string): string | undefined;

/**
* Get GOV.UK Frontend breakpoint value from CSS custom property
*
* @param {string} name - Breakpoint name
* @returns {{ property: string, value?: string }} Breakpoint object
*/
export function getBreakpoint(name: string): {
property: string;
value?: string;
};

/**
* Move focus to element
*
* Sets tabindex to -1 to make the element programmatically focusable,
* but removes it on blur as the element doesn't need to be focused again.
*
* @template {HTMLElement} FocusElement
* @param {FocusElement} $element - HTML element
* @param {object} [options] - Handler options
* @param {function(this: FocusElement): void} [options.onBeforeFocus] - Callback before focus
* @param {function(this: FocusElement): void} [options.onBlur] - Callback on blur
*/
export function setFocus<FocusElement extends HTMLElement>(
$element: FocusElement,
options?: {
onBeforeFocus?: ((this: FocusElement) => void) | undefined;
onBlur?: ((this: FocusElement) => void) | undefined;
},
): void;

/**
* Checks if GOV.UK Frontend is supported on this page
*
* Some browsers will load and run our JavaScript but GOV.UK Frontend
* won't be supported.
*
* @internal
* @param {HTMLElement | null} [$scope] - HTML element `<body>` checked for browser support
* @returns {boolean} Whether GOV.UK Frontend is supported on this page
*/
export function isSupported($scope?: HTMLElement | null): boolean;

/**
* Validate component config by schema
*
* Follows limited examples in JSON schema for wider support in future
*
* {@link https://ajv.js.org/json-schema.html#compound-keywords}
* {@link https://ajv.js.org/packages/ajv-errors.html#single-message}
*
* @internal
* @param {Schema} schema - Config schema
* @param {{ [key: string]: unknown }} config - Component config
* @returns {string[]} List of validation errors
*/
export function validateConfig(schema: Schema, config: {
[key: string]: unknown;
}): string[];

/**
* Schema for component config
*/
export interface Schema {
/**
* - Schema properties
*/
properties: {
[field: string]: SchemaProperty | undefined;
};

/**
* - List of schema conditions
*/
anyOf?: SchemaCondition[] | undefined;
}

/**
* Schema property for component config
*/
export interface SchemaProperty {
/**
* - Property type
*/
type: "string" | "boolean" | "number" | "object";
}

/**
* Schema condition for component config
*/
export interface SchemaCondition {
/**
* - List of required config fields
*/
required: string[];

/**
* - Error message when required config fields not provided
*/
errorMessage: string;
}
export type NestedKey = keyof ObjectNested;
export interface ObjectNested {
[key: string]: string | boolean | number | ObjectNested | undefined;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./normalise-dataset.js";
16 changes: 16 additions & 0 deletions types/govuk-frontend/dist/govuk/common/normalise-dataset.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type ObjectNested, type Schema } from "./index.js";

/**
* Normalise dataset
*
* Loop over an object and normalise each value using {@link normaliseString},
* optionally expanding nested `i18n.field`
*
* @internal
* @param {{ schema: Schema }} Component - Component class
* @param {DOMStringMap} dataset - HTML element dataset
* @returns {ObjectNested} Normalised dataset
*/
export function normaliseDataset(Component: {
schema: Schema;
}, dataset: DOMStringMap): ObjectNested;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./normalise-string.js";
22 changes: 22 additions & 0 deletions types/govuk-frontend/dist/govuk/common/normalise-string.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type SchemaProperty } from "./index.js";

/**
* Normalise string
*
* 'If it looks like a duck, and it quacks like a duck…' 🦆
*
* If the passed value looks like a boolean or a number, convert it to a boolean
* or number.
*
* Designed to be used to convert config passed via data attributes (which are
* always strings) into something sensible.
*
* @internal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't enforced by the compiler, but ... are you intending to include so many functions marked internal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, rather than cause issues for internal GOV.UK teams I've removed @private but kept @internal

It's a balance between the public docs versus JSDoc generated internal docs

Thanks for looking closely though, appreciate it

* @param {DOMStringMap[string]} value - The value to normalise
* @param {SchemaProperty} [property] - Component schema property
* @returns {string | boolean | number | undefined} Normalised data
*/
export function normaliseString(
value: DOMStringMap[string],
property?: SchemaProperty,
): string | boolean | number | undefined;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./accordion.js";
Loading