UNPKG

@codegouvfr/react-dsfr

Version:

French State Design System React integration library

4 lines (3 loc) 4.05 kB
/*! DSFR v1.13.2 | SPDX-License-Identifier: MIT | License-Filename: LICENSE.md | restricted use (see terms and conditions) */ const e=window.dsfr,t=e.internals.ns.selector("nav__item"),i=e.internals.ns.selector("collapse"),s={NAVIGATION:e.internals.ns.selector("nav"),COLLAPSE:`${t} > ${i}, ${t} > *:not(${t}):not(${i}) > ${i}, ${t} > *:not(${t}):not(${i}) > *:not(${t}):not(${i}) > ${i}`,COLLAPSE_LEGACY:`${t} ${i}`,ITEM:t,ITEM_RIGHT:`${t}--align-right`,MENU:e.internals.ns.selector("menu"),BUTTON:e.internals.ns.selector("nav__btn"),TRANSLATE_BUTTON:e.internals.ns.selector("translate__btn")};class n extends e.core.Instance{constructor(){super(),this._isRightAligned=!1}static get instanceClassName(){return"NavigationItem"}init(){this.addAscent(e.core.DisclosureEmission.ADDED,this.calculate.bind(this)),this.addAscent(e.core.DisclosureEmission.REMOVED,this.calculate.bind(this)),this.isResizing=!0,this.calculate()}resize(){this.calculate()}calculate(){const t=this.element.getDescendantInstances(e.core.Collapse.instanceClassName,null,!0)[0];if(t&&this.isBreakpoint(e.core.Breakpoints.LG)&&t.element.node.matches(s.MENU)){const e=this.element.node.parentElement.getBoundingClientRect().right,i=t.element.node.getBoundingClientRect().width,s=this.element.node.getBoundingClientRect().left;this.isRightAligned=s+i>e}else this.isRightAligned=!1}get isRightAligned(){return this._isRightAligned}set isRightAligned(t){this._isRightAligned!==t&&(this._isRightAligned=t,t?e.internals.dom.addClass(this.element.node,s.ITEM_RIGHT):e.internals.dom.removeClass(this.element.node,s.ITEM_RIGHT))}get collapsePrimary(){return this.element.children.map((e=>e.getInstance("CollapseButton"))).filter((e=>null!==e&&(e.hasClass(s.BUTTON)||e.hasClass(s.TRANSLATE_BUTTON))))[0]}}const o={NONE:-1,INSIDE:0,OUTSIDE:1};class a extends e.core.CollapsesGroup{static get instanceClassName(){return"Navigation"}init(){super.init(),this.clicked=!1,this.out=!1,this.addEmission(e.core.RootEmission.CLICK,this._handleRootClick.bind(this)),this.listen("mousedown",this.handleMouseDown.bind(this)),this.addEmission(e.core.RootEmission.KEYDOWN,this._keydown.bind(this)),this.listenClick({capture:!0}),this.isResizing=!0}validate(t){return super.validate(t)&&t.element.node.matches(e.internals.legacy.isLegacy?s.COLLAPSE_LEGACY:s.COLLAPSE)}get hasOpenedMenu(){return this.isBreakpoint(e.core.Breakpoints.LG)&&this.index>-1}_keydown(t){switch(t){case e.core.KeyCodes.ESCAPE:if(!this.hasOpenedMenu)return;this.index=-1;break;case e.core.KeyCodes.TAB:if(!this.hasOpenedMenu)return;this.request((()=>{this.current.node.contains(document.activeElement)||(this.index=-1)}))}}handleMouseDown(e){this.hasOpenedMenu&&(this.position=this.current.node.contains(e.target)?o.INSIDE:o.OUTSIDE,this.requestPosition())}handleClick(t){!t.target.matches("a, button")||t.target.matches("[aria-controls]")||t.target.matches(e.core.DisclosureSelector.PREVENT_CONCEAL)||(this.index=-1)}_handleRootClick(t){this.isBreakpoint(e.core.Breakpoints.LG)&&(this.node.contains(t)||(this.out=!0,this.requestPosition()))}requestPosition(){this.isRequesting||(this.isRequesting=!0,this.request(this.getPosition.bind(this)))}getPosition(){if(this.out)switch(this.position){case o.OUTSIDE:this.index=-1;break;case o.INSIDE:this.current&&!this.current.node.contains(document.activeElement)&&this.current.focus();break;default:this.index>-1&&!this.current.hasFocus&&(this.index=-1)}this.request(this.requested.bind(this))}requested(){this.position=o.NONE,this.out=!1,this.isRequesting=!1}get index(){return super.index}set index(e){-1===e&&this.current&&this.current.hasFocus&&this.current.focus(),super.index=e}get canUngroup(){return!this.isBreakpoint(e.core.Breakpoints.LG)}resize(){this.update()}}e.navigation={Navigation:a,NavigationItem:n,NavigationMousePosition:o,NavigationSelector:s},e.internals.register(e.navigation.NavigationSelector.NAVIGATION,e.navigation.Navigation),e.internals.register(e.navigation.NavigationSelector.ITEM,e.navigation.NavigationItem); //# sourceMappingURL=navigation.module.min.js.map