UNPKG

@codegouvfr/react-dsfr

Version:

French State Design System React integration library

99 lines (82 loc) 3.52 kB
/*! DSFR v1.8.5 | SPDX-License-Identifier: MIT | License-Filename: LICENSE.md | restricted use (see terms and conditions) */ const config = { prefix: 'fr', namespace: 'dsfr', organisation: '@gouvfr', version: '1.8.5' }; const api = window[config.namespace]; const HeaderSelector = { HEADER: api.internals.ns.selector('header'), TOOLS_LINKS: api.internals.ns.selector('header__tools-links'), MENU_LINKS: api.internals.ns.selector('header__menu-links'), BUTTONS: `${api.internals.ns.selector('header__tools-links')} ${api.internals.ns.selector('btns-group')}, ${api.internals.ns.selector('header__tools-links')} ${api.internals.ns.selector('links-group')}`, MODALS: `${api.internals.ns.selector('header__search')}${api.internals.ns.selector('modal')}, ${api.internals.ns.selector('header__menu')}${api.internals.ns.selector('modal')}` }; class HeaderLinks extends api.core.Instance { static get instanceClassName () { return 'HeaderLinks'; } init () { const header = this.queryParentSelector(HeaderSelector.HEADER); this.toolsLinks = header.querySelector(HeaderSelector.TOOLS_LINKS); this.menuLinks = header.querySelector(HeaderSelector.MENU_LINKS); const copySuffix = '-mobile'; const toolsHtml = this.toolsLinks.innerHTML.replace(/ +/g, ' '); const menuHtml = this.menuLinks.innerHTML.replace(/ +/g, ' '); // Pour éviter de dupliquer des id, on ajoute un suffixe aux id et aria-controls duppliqués. let toolsHtmlDuplicateId = toolsHtml.replace(/(<nav[.\s\S]*-translate [.\s\S]*) id="(.*?)"([.\s\S]*<\/nav>)/gm, '$1 id="$2' + copySuffix + '"$3'); toolsHtmlDuplicateId = toolsHtmlDuplicateId.replace(/(<nav[.\s\S]*-translate [.\s\S]*) aria-controls="(.*?)"([.\s\S]*<\/nav>)/gm, '$1 aria-controls="$2' + copySuffix + '"$3'); if (toolsHtmlDuplicateId === menuHtml) return; switch (api.mode) { case api.Modes.ANGULAR: case api.Modes.REACT: case api.Modes.VUE: api.inspector.warn(`header__tools-links content is different from header__menu-links content. As you're using a dynamic framework, you should handle duplication of this content yourself, please refer to documentation: ${api.header.doc}`); break; default: this.menuLinks.innerHTML = toolsHtmlDuplicateId; } } } class HeaderModal extends api.core.Instance { static get instanceClassName () { return 'HeaderModal'; } init () { this.isResizing = true; } resize () { if (this.isBreakpoint(api.core.Breakpoints.LG)) this.unqualify(); else this.qualify(); } qualify () { this.setAttribute('role', 'dialog'); const modal = this.element.getInstance('Modal'); if (!modal) return; const buttons = modal.buttons; let id = ''; for (const button of buttons) { id = button.id || id; if (button.isPrimary && id) break; } this.setAttribute('aria-labelledby', id); } unqualify () { const modal = this.element.getInstance('Modal'); if (modal) modal.conceal(); this.removeAttribute('role'); this.removeAttribute('aria-labelledby'); } } api.header = { HeaderLinks: HeaderLinks, HeaderModal: HeaderModal, HeaderSelector: HeaderSelector, doc: 'https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/en-tete' }; api.internals.register(api.header.HeaderSelector.BUTTONS, api.header.HeaderLinks); api.internals.register(api.header.HeaderSelector.MODALS, api.header.HeaderModal); //# sourceMappingURL=header.module.js.map