govuk-frontend
Version:
GOV.UK Frontend contains the code you need to start building a user interface for government platforms and services.
74 lines (71 loc) • 2.2 kB
JavaScript
function getFragmentFromUrl(url) {
if (!url.includes('#')) {
return undefined;
}
return url.split('#').pop();
}
function getBreakpoint(name) {
const property = `--govuk-frontend-breakpoint-${name}`;
const value = window.getComputedStyle(document.documentElement).getPropertyValue(property);
return {
property,
value: value || undefined
};
}
function setFocus($element, options = {}) {
var _options$onBeforeFocu;
const isFocusable = $element.getAttribute('tabindex');
if (!isFocusable) {
$element.setAttribute('tabindex', '-1');
}
function onFocus() {
$element.addEventListener('blur', onBlur, {
once: true
});
}
function onBlur() {
var _options$onBlur;
(_options$onBlur = options.onBlur) == null || _options$onBlur.call($element);
if (!isFocusable) {
$element.removeAttribute('tabindex');
}
}
$element.addEventListener('focus', onFocus, {
once: true
});
(_options$onBeforeFocu = options.onBeforeFocus) == null || _options$onBeforeFocu.call($element);
$element.focus();
}
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
*/
export { formatErrorMessage, getBreakpoint, getFragmentFromUrl, isInitialised, isObject, isSupported, setFocus };
//# sourceMappingURL=index.mjs.map