{"version":3,"file":"skip-link.mjs","sources":["../../../../src/govuk/components/skip-link/skip-link.mjs"],"sourcesContent":["import { getFragmentFromUrl, setFocus } from '../../common/index.mjs'\nimport { ElementError } from '../../errors/index.mjs'\nimport { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'\n\n/**\n * Skip link component\n *\n * @preserve\n * @augments GOVUKFrontendComponent\n */\nexport class SkipLink extends GOVUKFrontendComponent {\n static elementType = HTMLAnchorElement\n\n /**\n * @param {Element | null} $root - HTML element to use for skip link\n * @throws {ElementError} when $root is not set or the wrong type\n * @throws {ElementError} when $root.hash does not contain a hash\n * @throws {ElementError} when the linked element is missing or the wrong type\n */\n constructor($root) {\n super($root)\n\n const hash = this.$root.hash\n const href = this.$root.getAttribute('href') ?? ''\n\n /** @type {URL | undefined} */\n let url\n\n /**\n * Check for valid link URL\n *\n * {@link https://caniuse.com/url}\n * {@link https://url.spec.whatwg.org}\n *\n */\n try {\n url = new window.URL(this.$root.href)\n } catch (error) {\n throw new ElementError(\n `Skip link: Target link (\\`href=\"${href}\"\\`) is invalid`\n )\n }\n\n // Return early for external URLs or links to other pages\n if (\n url.origin !== window.location.origin ||\n url.pathname !== window.location.pathname\n ) {\n return\n }\n\n const linkedElementId = getFragmentFromUrl(hash)\n\n // Check link path matching current page\n if (!linkedElementId) {\n throw new ElementError(\n `Skip link: Target link (\\`href=\"${href}\"\\`) has no hash fragment`\n )\n }\n\n const $linkedElement = document.getElementById(linkedElementId)\n\n // Check for link target element\n if (!$linkedElement) {\n throw new ElementError({\n component: SkipLink,\n element: $linkedElement,\n identifier: `Target content (\\`id=\"${linkedElementId}\"\\`)`\n })\n }\n\n /**\n * Focus the linked element on click\n *\n * Adds a helper CSS class to hide native focus styles,\n * but removes it on blur to restore native focus styles\n */\n this.$root.addEventListener('click', () =>\n setFocus($linkedElement, {\n onBeforeFocus() {\n $linkedElement.classList.add('govuk-skip-link-focused-element')\n },\n onBlur() {\n $linkedElement.classList.remove('govuk-skip-link-focused-element')\n }\n })\n )\n }\n\n /**\n * Name for the component used when initialising using data-module attributes.\n */\n static moduleName = 'govuk-skip-link'\n}\n"],"names":["SkipLink","GOVUKFrontendComponent","constructor","$root","_this$$root$getAttrib","hash","href","getAttribute","url","window","URL","error","ElementError","origin","location","pathname","linkedElementId","getFragmentFromUrl","$linkedElement","document","getElementById","component","element","identifier","addEventListener","setFocus","onBeforeFocus","classList","add","onBlur","remove","elementType","HTMLAnchorElement","moduleName"],"mappings":";;;;AAIA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,QAAQ,SAASC,sBAAsB,CAAC;AAGnD;AACF;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAACC,KAAK,EAAE;AAAA,IAAA,IAAAC,qBAAA,CAAA;IACjB,KAAK,CAACD,KAAK,CAAC,CAAA;AAEZ,IAAA,MAAME,IAAI,GAAG,IAAI,CAACF,KAAK,CAACE,IAAI,CAAA;AAC5B,IAAA,MAAMC,IAAI,GAAA,CAAAF,qBAAA,GAAG,IAAI,CAACD,KAAK,CAACI,YAAY,CAAC,MAAM,CAAC,KAAAH,IAAAA,GAAAA,qBAAA,GAAI,EAAE,CAAA;AAGlD,IAAA,IAAII,GAAG,CAAA;IASP,IAAI;MACFA,GAAG,GAAG,IAAIC,MAAM,CAACC,GAAG,CAAC,IAAI,CAACP,KAAK,CAACG,IAAI,CAAC,CAAA;KACtC,CAAC,OAAOK,KAAK,EAAE;AACd,MAAA,MAAM,IAAIC,YAAY,CACpB,CAAmCN,gCAAAA,EAAAA,IAAI,iBACzC,CAAC,CAAA;AACH,KAAA;AAGA,IAAA,IACEE,GAAG,CAACK,MAAM,KAAKJ,MAAM,CAACK,QAAQ,CAACD,MAAM,IACrCL,GAAG,CAACO,QAAQ,KAAKN,MAAM,CAACK,QAAQ,CAACC,QAAQ,EACzC;AACA,MAAA,OAAA;AACF,KAAA;AAEA,IAAA,MAAMC,eAAe,GAAGC,kBAAkB,CAACZ,IAAI,CAAC,CAAA;IAGhD,IAAI,C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