UNPKG

govuk-frontend

Version:

GOV.UK Frontend contains the code you need to start building a user interface for government platforms and services.

263 lines (251 loc) 8.74 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.GOVUKFrontend = global.GOVUKFrontend || {})); })(this, (function (exports) { 'use strict'; function getBreakpoint(name) { const property = `--govuk-breakpoint-${name}`; const value = window.getComputedStyle(document.documentElement).getPropertyValue(property); return { property, value: value || undefined }; } function isInitialised($root, moduleName) { return $root instanceof HTMLElement && $root.hasAttribute(`data-${moduleName}-init`); } /** * 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. * * @param {HTMLElement | null} [$scope] - (internal) `<body>` HTML element checked for browser support * @returns {boolean} Whether GOV.UK Frontend is supported on this page */ function isSupported($scope = document.body) { if (!$scope) { return false; } return $scope.classList.contains('govuk-frontend-supported'); } function isArray(option) { return Array.isArray(option); } function isObject(option) { return !!option && typeof option === 'object' && !isArray(option); } function formatErrorMessage(Component, message) { return `${Component.moduleName}: ${message}`; } /** * @typedef ComponentWithModuleName * @property {string} moduleName - Name of the component */ class GOVUKFrontendError extends Error { constructor(...args) { super(...args); this.name = 'GOVUKFrontendError'; } } class SupportError extends GOVUKFrontendError { /** * Checks if GOV.UK Frontend is supported on this page * * @param {HTMLElement | null} [$scope] - HTML element `<body>` checked for browser support */ constructor($scope = document.body) { const supportMessage = 'noModule' in HTMLScriptElement.prototype ? 'GOV.UK Frontend initialised without `<body class="govuk-frontend-supported">` from template `<script>` snippet' : 'GOV.UK Frontend is not supported in this browser'; super($scope ? supportMessage : 'GOV.UK Frontend initialised without `<script type="module">`'); this.name = 'SupportError'; } } class ElementError extends GOVUKFrontendError { constructor(messageOrOptions) { let message = typeof messageOrOptions === 'string' ? messageOrOptions : ''; if (isObject(messageOrOptions)) { const { component, identifier, element, expectedType } = messageOrOptions; message = identifier; message += element ? ` is not of type ${expectedType != null ? expectedType : 'HTMLElement'}` : ' not found'; if (component) { message = formatErrorMessage(component, message); } } super(message); this.name = 'ElementError'; } } class InitError extends GOVUKFrontendError { constructor(componentOrMessage) { const message = typeof componentOrMessage === 'string' ? componentOrMessage : formatErrorMessage(componentOrMessage, `Root element (\`$root\`) already initialised`); super(message); this.name = 'InitError'; } } /** * @import { ComponentWithModuleName } from '../common/index.mjs' */ class Component { /** * Returns the root element of the component * * @protected * @returns {RootElementType} - the root element of component */ get $root() { return this._$root; } constructor($root) { this._$root = void 0; const childConstructor = this.constructor; if (typeof childConstructor.moduleName !== 'string') { throw new InitError(`\`moduleName\` not defined in component`); } if (!($root instanceof childConstructor.elementType)) { throw new ElementError({ element: $root, component: childConstructor, identifier: 'Root element (`$root`)', expectedType: childConstructor.elementType.name }); } else { this._$root = $root; } childConstructor.checkSupport(); this.checkInitialised(); const moduleName = childConstructor.moduleName; this.$root.setAttribute(`data-${moduleName}-init`, ''); } checkInitialised() { const constructor = this.constructor; const moduleName = constructor.moduleName; if (moduleName && isInitialised(this.$root, moduleName)) { throw new InitError(constructor); } } static checkSupport() { if (!isSupported()) { throw new SupportError(); } } } /** * @typedef ChildClass * @property {string} moduleName - The module name that'll be looked for in the DOM when initialising the component */ /** * @typedef {typeof Component & ChildClass} ChildClassConstructor */ Component.elementType = HTMLElement; /** * Service Navigation component * * @preserve */ class ServiceNavigation extends Component { /** * @param {Element | null} $root - HTML element to use for header */ constructor($root) { super($root); this.$menuButton = void 0; this.$menu = void 0; this.menuIsOpen = false; this.mql = null; const $menuButton = this.$root.querySelector('.govuk-js-service-navigation-toggle'); if (!$menuButton) { return this; } const menuId = $menuButton.getAttribute('aria-controls'); if (!menuId) { throw new ElementError({ component: ServiceNavigation, identifier: 'Navigation button (`<button class="govuk-js-service-navigation-toggle">`) attribute (`aria-controls`)' }); } const $menu = document.getElementById(menuId); if (!$menu) { throw new ElementError({ component: ServiceNavigation, element: $menu, identifier: `Navigation (\`<ul id="${menuId}">\`)` }); } this.$menu = $menu; this.$menuButton = $menuButton; this.setupResponsiveChecks(); this.$menuButton.addEventListener('click', () => this.handleMenuButtonClick()); } setupResponsiveChecks() { const breakpoint = getBreakpoint('tablet'); if (!breakpoint.value) { throw new ElementError({ component: ServiceNavigation, identifier: `CSS custom property (\`${breakpoint.property}\`) on pseudo-class \`:root\`` }); } this.mql = window.matchMedia(`(min-width: ${breakpoint.value})`); if ('addEventListener' in this.mql) { this.mql.addEventListener('change', () => this.checkMode()); } else { this.mql.addListener(() => this.checkMode()); } this.checkMode(); } checkMode() { if (!this.mql || !this.$menu || !this.$menuButton) { return; } if (this.mql.matches) { this.$menu.removeAttribute('hidden'); setAttributes(this.$menuButton, attributesForHidingButton); } else { removeAttributes(this.$menuButton, Object.keys(attributesForHidingButton)); this.$menuButton.setAttribute('aria-expanded', this.menuIsOpen.toString()); if (this.menuIsOpen) { this.$menu.removeAttribute('hidden'); } else { this.$menu.setAttribute('hidden', ''); } } } handleMenuButtonClick() { this.menuIsOpen = !this.menuIsOpen; this.checkMode(); } } ServiceNavigation.moduleName = 'govuk-service-navigation'; const attributesForHidingButton = { hidden: '', 'aria-hidden': 'true' }; /** * Sets a group of attributes on the given element * * @param {Element} $element - The element to set the attribute on * @param {{[attributeName: string]: string}} attributes - The attributes to set */ function setAttributes($element, attributes) { for (const attributeName in attributes) { $element.setAttribute(attributeName, attributes[attributeName]); } } /** * Removes a list of attributes from the given element * * @param {Element} $element - The element to remove the attributes from * @param {string[]} attributeNames - The names of the attributes to remove */ function removeAttributes($element, attributeNames) { for (const attributeName of attributeNames) { $element.removeAttribute(attributeName); } } exports.ServiceNavigation = ServiceNavigation; })); //# sourceMappingURL=service-navigation.bundle.js.map