@data-slot/navigation-menu 0.2.54 → 0.2.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -145,14 +145,16 @@ Can be set on:
|
|
|
145
145
|
|
|
146
146
|
- `navigation-menu-indicator` - Animated highlight that follows the hovered trigger
|
|
147
147
|
- `navigation-menu-viewport` - Container for content with size transitions
|
|
148
|
-
- `navigation-menu-positioner` -
|
|
149
|
-
- `navigation-menu-viewport-positioner` -
|
|
148
|
+
- `navigation-menu-positioner` - Positioning wrapper for active content (generated if not authored)
|
|
149
|
+
- `navigation-menu-viewport-positioner` - Positioning wrapper for viewport (generated if not authored)
|
|
150
|
+
- `navigation-menu-portal` - Optional authored portal wrapper that can contain positioners
|
|
150
151
|
|
|
151
152
|
## Styling
|
|
152
153
|
|
|
153
154
|
Active `navigation-menu-content` and `navigation-menu-viewport` are portaled to `document.body`
|
|
154
|
-
while open.
|
|
155
|
-
|
|
155
|
+
while open. If authored `navigation-menu-portal` / positioner slots are present, they are reused.
|
|
156
|
+
Otherwise, `navigation-menu-positioner` and `navigation-menu-viewport-positioner` wrappers are
|
|
157
|
+
generated and positioned at the navigation root so submenu layers are not clipped by local
|
|
156
158
|
stacking contexts.
|
|
157
159
|
|
|
158
160
|
### Basic Styling
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@data-slot/core`);const t=[`start`,`center`,`end`];function n(n,r={}){let i=r.delayOpen??(0,e.getDataNumber)(n,`delayOpen`)??200,a=r.delayClose??(0,e.getDataNumber)(n,`delayClose`)??150,o=r.openOnFocus??(0,e.getDataBool)(n,`openOnFocus`)??!0,s=r.onValueChange,c=e=>e.replace(/[^a-z0-9\-_:.]/gi,`-`),l=(e,t)=>{`inert`in e&&(e.inert=t)},u=(0,e.getPart)(n,`navigation-menu-list`),d=(0,e.getParts)(n,`navigation-menu-item`),f=(0,e.getPart)(n,`navigation-menu-viewport`),p=(0,e.getPart)(n,`navigation-menu-indicator`);if(!u||d.length===0)throw Error(`NavigationMenu requires navigation-menu-list and at least one navigation-menu-item`);let m=null,h=null,g=-1,_=null,v=null,y=null,b=!1,x=!1,S=!1,C=[],w=new Map,T=f?(0,e.createPortalLifecycle)({content:f,root:n,enabled:!0,wrapperSlot:`navigation-menu-viewport-positioner`}):null,E=e=>{let t=w.get(e);if(!t)return;let r=t.container,i=n.ownerDocument.defaultView??window,a=n.getBoundingClientRect();r.style.position=`absolute`,r.style.top=`0px`,r.style.left=`0px`,r.style.transform=`translate3d(${a.left+i.scrollX}px, ${a.top+i.scrollY}px, 0)`,r.style.width=`${a.width}px`,r.style.height=`${a.height}px`,r.style.margin=`0`,r.style.willChange=`transform`},D=()=>{if(!f||!T)return;let e=T.container,t=n.ownerDocument.defaultView??window,r=n.getBoundingClientRect();e.style.position=`absolute`,e.style.top=`0px`,e.style.left=`0px`,e.style.transform=`translate3d(${r.left+t.scrollX}px, ${r.top+t.scrollY}px, 0)`,e.style.width=`${r.width}px`,e.style.height=`${r.height}px`,e.style.margin=`0`,e.style.willChange=`transform`},O=null,k=e=>{O?.disconnect(),O=null,!(!f||!e)&&(O=new ResizeObserver(()=>R(e.content,e.trigger,e.align)),O.observe(e.content))};C.push(()=>O?.disconnect()),C.push(()=>{w.forEach(e=>e.cleanup()),w.clear(),T?.cleanup()});let A=new Map,j=0;d.forEach(r=>{let i=r.dataset.value;if(!i)return;let a=(0,e.getPart)(r,`navigation-menu-trigger`),o=(0,e.getPart)(r,`navigation-menu-content`);if(a&&o){let s=(0,e.getDataEnum)(o,`align`,t)??(0,e.getDataEnum)(r,`align`,t)??(0,e.getDataEnum)(n,`align`,t)??`start`;A.set(i,{item:r,trigger:a,content:o,index:j++,align:s}),w.set(o,(0,e.createPortalLifecycle)({content:o,root:n,enabled:!0,wrapperSlot:`navigation-menu-positioner`}));let l=c(i),u=(0,e.ensureId)(a,`nav-menu-trigger-${l}`),d=(0,e.ensureId)(o,`nav-menu-content-${l}`);a.setAttribute(`aria-haspopup`,`true`),a.setAttribute(`aria-controls`,d),o.setAttribute(`aria-labelledby`,u)}});let M=Array.from(A.values()).map(e=>e.trigger),N=new Map;for(let[e,t]of A)N.set(t.trigger,e);let P=e=>Array.from(e.querySelectorAll(`a, button, input, select, textarea, [tabindex]:not([tabindex="-1"])`)).filter(e=>!e.hidden&&!e.closest(`[hidden]`)),F=()=>{_&&=(clearTimeout(_),null),v&&=(clearTimeout(v),null)},I=null,L=()=>(I||(I=document.createElement(`div`),I.setAttribute(`data-slot`,`navigation-menu-bridge`),I.style.cssText=`position: absolute; left: 0; right: 0; top: 0; pointer-events: auto; z-index: -1;`,f&&f.insertBefore(I,f.firstChild),C.push((0,e.on)(I,`pointerenter`,()=>{F()}))),I),R=(e,t,r)=>{f&&requestAnimationFrame(()=>{let i=e.firstElementChild,a=i?.getBoundingClientRect(),o=i?getComputedStyle(i):null,s=o&&parseFloat(o.marginTop)||0,c=o&&parseFloat(o.marginBottom)||0,l=a??e.getBoundingClientRect(),u=l.height+s+c,d=getComputedStyle(f),p=parseFloat(d.marginTop)||0,m=e.getBoundingClientRect();f.style.setProperty(`--viewport-width`,`${m.width}px`),f.style.setProperty(`--viewport-height`,`${u}px`);let h=n.getBoundingClientRect(),g=t.getBoundingClientRect(),_;_=r===`center`?g.left-h.left+g.width/2-l.width/2:r===`end`?g.right-h.left-l.width:g.left-h.left,f.style.setProperty(`--viewport-left`,`${_}px`),f.style.left=`${_}px`,e.style.left=`${_}px`,E(e),D();let v=s+p;if(v>0){let e=L();e.style.height=`${v}px`,e.style.transform=`translateY(-${v}px)`}else I&&(I.style.height=`0`)})},z=e=>g===-1||e>g?`right`:`left`,B=e=>{if(!p)return;if(y=e,!e){p.setAttribute(`data-state`,`hidden`);return}let t=u.getBoundingClientRect(),n=e.getBoundingClientRect();p.style.setProperty(`--indicator-left`,`${n.left-t.left}px`),p.style.setProperty(`--indicator-width`,`${n.width}px`),p.style.setProperty(`--indicator-top`,`${n.top-t.top}px`);let r=(f&&parseFloat(getComputedStyle(f).marginTop)||0)<1?1:0;p.style.setProperty(`--indicator-height`,`${n.height-r}px`),p.setAttribute(`data-state`,`visible`)},V=(t,r=!1)=>{if(t===m){F();return}if(t!==null&&t===h){F();return}F(),h=t===null?null:t;let o=()=>{let r=m,i=t?A.get(t):null,a=r!==null&&t!==null&&r!==t,o=a&&i?z(i.index):null,c=document.activeElement;if(t===null&&c&&(0,e.containsWithPortals)(n,c)){let e=r?A.get(r)?.trigger:null;e&&e.focus()}if(A.forEach(({trigger:n,content:i,item:a},s)=>{let c=s===t,u=s===r;if((0,e.setAria)(n,`expanded`,c),n.setAttribute(`data-state`,c?`open`:`closed`),c||u&&t===null?n.tabIndex=0:n.tabIndex=-1,a.setAttribute(`data-state`,c?`open`:`closed`),!c)if(w.get(i)?.restore(),i.setAttribute(`data-state`,`inactive`),i.setAttribute(`aria-hidden`,`true`),l(i,!0),i.hidden=!0,u&&o){let e=o===`right`?`to-left`:`to-right`;i.setAttribute(`data-motion`,e)}else i.removeAttribute(`data-motion`)}),i){if(T?.mount(),w.get(i.content)?.mount(),o){let e=o===`right`?`from-right`:`from-left`;i.content.setAttribute(`data-motion`,e)}else i.content.removeAttribute(`data-motion`);i.content.setAttribute(`data-state`,`active`),i.content.removeAttribute(`aria-hidden`),l(i.content,!1),i.content.hidden=!1,g=i.index,R(i.content,i.trigger,i.align),k(i),B(i.trigger)}else T?.restore(),k(null);let u=t!==null;n.setAttribute(`data-state`,u?`open`:`closed`),o?n.setAttribute(`data-motion`,o===`right`?`from-right`:`from-left`):n.removeAttribute(`data-motion`),f&&(f.setAttribute(`data-state`,u?`open`:`closed`),u&&!a?f.setAttribute(`data-instant`,``):a&&f.removeAttribute(`data-instant`),o&&f.style.setProperty(`--motion-direction`,o===`right`?`1`:`-1`)),m=t,h=null,t===null&&B(null),(0,e.emit)(n,`navigation-menu:change`,{value:t}),s?.(t)};r?o():t!==null&&m===null?_=setTimeout(o,i):t!==null&&m!==null?o():v=setTimeout(o,a)};n.setAttribute(`data-state`,`closed`),f&&f.setAttribute(`data-state`,`closed`),p&&p.setAttribute(`data-state`,`hidden`),A.forEach(({trigger:t,content:n,item:r})=>{t.tagName===`BUTTON`&&!t.hasAttribute(`type`)&&(t.type=`button`),(0,e.setAria)(t,`expanded`,!1),t.setAttribute(`data-state`,`closed`),t.tabIndex=t===M[0]?0:-1,r.setAttribute(`data-state`,`closed`),n.setAttribute(`data-state`,`inactive`),n.setAttribute(`aria-hidden`,`true`),n.tabIndex=-1,l(n,!0),n.hidden=!0}),A.forEach(({item:t,trigger:n},r)=>{C.push((0,e.on)(n,`pointerenter`,()=>{b||B(n)})),C.push((0,e.on)(t,`pointerenter`,()=>{b||V(r)})),C.push((0,e.on)(t,`pointerleave`,()=>{h===r&&m===null&&(F(),h=null)})),C.push((0,e.on)(n,`focus`,()=>{x||(o&&V(r,!0),B(n))})),C.push((0,e.on)(n,`pointerdown`,()=>{x=!0})),C.push((0,e.on)(n,`click`,()=>{F(),m===r&&b?(b=!1,V(null,!0),B(null)):m===r&&!b?(b=!0,B(n)):(b=!0,V(r,!0),B(n)),x=!1}))}),C.push((0,e.on)(n,`pointerenter`,()=>{S=!0}),(0,e.on)(n,`pointerleave`,()=>{S=!1,b||(V(null),B(null))}),(0,e.on)(n,`pointerdown`,F)),f&&C.push((0,e.on)(f,`pointerenter`,()=>{F()}),(0,e.on)(f,`transitionend`,e=>{if(e.target!==f)return;let t=m?A.get(m):null;t&&R(t.content,t.trigger,t.align)})),A.forEach(({content:t})=>{C.push((0,e.on)(t,`pointerenter`,()=>{F()}),(0,e.on)(t,`pointerleave`,t=>{if(b)return;let r=t.relatedTarget;(0,e.containsWithPortals)(n,r)||(V(null),B(null))}))}),C.push((0,e.on)(u,`keydown`,e=>{let t=e.target,n=M.indexOf(t);if(n===-1)return;let r=N.get(t)??null,i=n;switch(e.key){case`ArrowLeft`:i=n-1,i<0&&(i=M.length-1);break;case`ArrowRight`:i=n+1,i>=M.length&&(i=0);break;case`ArrowDown`:e.preventDefault(),r&&(b=!0,V(r,!0),requestAnimationFrame(()=>{let e=A.get(r);if(!e)return;let t=P(e.content)[0];t?t.focus():e.content.focus()}));return;case`Home`:i=0;break;case`End`:i=M.length-1;break;case`Escape`:b=!1,V(null,!0),B(null);return;default:return}e.preventDefault();let a=M[i];a&&(M.forEach(e=>e.tabIndex=e===a?0:-1),a.focus(),B(a))})),A.forEach(({content:t,trigger:n})=>{C.push((0,e.on)(t,`keydown`,e=>{let r=e.target,i=P(t),a=i.indexOf(r);if(a!==-1)switch(e.key){case`ArrowDown`:case`ArrowRight`:{e.preventDefault();let t=a+1;t<i.length&&i[t]?.focus();break}case`ArrowUp`:case`ArrowLeft`:e.preventDefault(),a===0?n.focus():i[a-1]?.focus();break;case`Escape`:e.preventDefault(),b=!1,V(null,!0),B(null),n.focus();break}}))});let H=()=>(0,e.containsWithPortals)(n,document.activeElement)||S||b,U=()=>{b=!1,V(null,!0),B(null)};return C.push((0,e.on)(document,`pointerup`,()=>{x=!1},{capture:!0}),(0,e.on)(document,`pointercancel`,()=>{x=!1},{capture:!0})),C.push((0,e.on)(document,`focusin`,t=>{m!==null&&((0,e.containsWithPortals)(n,t.target)||U())})),C.push((0,e.createDismissLayer)({root:n,isOpen:()=>m!==null&&H(),onDismiss:U,closeOnClickOutside:!0,closeOnEscape:!0,preventEscapeDefault:!1,isInside:t=>!!t&&(0,e.containsWithPortals)(n,t)})),C.push((0,e.on)(window,`resize`,()=>{if(m&&requestAnimationFrame(()=>D()),m){let e=A.get(m);e&&requestAnimationFrame(()=>E(e.content))}y&&requestAnimationFrame(()=>B(y))}),(0,e.on)(u,`scroll`,()=>{y&&requestAnimationFrame(()=>B(y))})),C.push((0,e.on)(n,`navigation-menu:set`,e=>{let t=e.detail;if(t?.value!==void 0){if(t.value===null)U();else if(A.has(t.value)){b=!0,V(t.value,!0);let e=A.get(t.value);e&&B(e.trigger)}}})),{get value(){return m},open:e=>V(e,!0),close:()=>V(null,!0),destroy:()=>{F(),C.forEach(e=>e()),C.length=0}}}const r=new WeakSet;function i(t=document){let i=[];for(let a of(0,e.getRoots)(t,`navigation-menu`))r.has(a)||(r.add(a),i.push(n(a)));return i}exports.create=i,exports.createNavigationMenu=n;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@data-slot/core`);const t=[`start`,`center`,`end`];function n(n,r={}){let i=r.delayOpen??(0,e.getDataNumber)(n,`delayOpen`)??200,a=r.delayClose??(0,e.getDataNumber)(n,`delayClose`)??150,o=r.openOnFocus??(0,e.getDataBool)(n,`openOnFocus`)??!0,s=r.onValueChange,c=e=>e.replace(/[^a-z0-9\-_:.]/gi,`-`),l=(e,t)=>{`inert`in e&&(e.inert=t)},u=(0,e.getPart)(n,`navigation-menu-list`),d=(0,e.getParts)(n,`navigation-menu-item`),f=(0,e.getPart)(n,`navigation-menu-viewport`),p=(0,e.getPart)(n,`navigation-menu-indicator`),m=(e,t)=>{let r=e.parentElement;for(;r&&r!==n;){if(r.getAttribute(`data-slot`)===t)return r;r=r.parentElement}return null};if(!u||d.length===0)throw Error(`NavigationMenu requires navigation-menu-list and at least one navigation-menu-item`);let h=null,g=null,_=-1,v=null,y=null,b=null,x=!1,S=!1,C=!1,w=[],T=new Map,E=new Set,D=f?m(f,`navigation-menu-viewport-positioner`):null,O=D?m(D,`navigation-menu-portal`):null,k=f?(0,e.createPortalLifecycle)({content:f,root:n,enabled:!0,wrapperSlot:D?void 0:`navigation-menu-viewport-positioner`,container:D??void 0,mountTarget:D?O??D:void 0}):null,A=e=>{let t=T.get(e);if(!t)return;let r=t.container,i=n.ownerDocument.defaultView??window,a=n.getBoundingClientRect();r.style.position=`absolute`,r.style.top=`0px`,r.style.left=`0px`,r.style.transform=`translate3d(${a.left+i.scrollX}px, ${a.top+i.scrollY}px, 0)`,r.style.width=`${a.width}px`,r.style.height=`${a.height}px`,r.style.margin=`0`,r.style.willChange=`transform`,r.style.pointerEvents=`none`},j=()=>{if(!f||!k)return;let e=k.container,t=n.ownerDocument.defaultView??window,r=n.getBoundingClientRect();e.style.position=`absolute`,e.style.top=`0px`,e.style.left=`0px`,e.style.transform=`translate3d(${r.left+t.scrollX}px, ${r.top+t.scrollY}px, 0)`,e.style.width=`${r.width}px`,e.style.height=`${r.height}px`,e.style.margin=`0`,e.style.willChange=`transform`,e.style.pointerEvents=`none`},M=null,N=e=>{M?.disconnect(),M=null,!(!f||!e)&&(M=new ResizeObserver(()=>H(e.content,e.trigger,e.align)),M.observe(e.content))};w.push(()=>M?.disconnect()),w.push(()=>{T.forEach(e=>e.cleanup()),T.clear(),k?.cleanup()});let P=new Map,F=0;d.forEach(r=>{let i=r.dataset.value;if(!i)return;let a=(0,e.getPart)(r,`navigation-menu-trigger`),o=(0,e.getPart)(r,`navigation-menu-content`);if(a&&o){let s=(0,e.getDataEnum)(o,`align`,t)??(0,e.getDataEnum)(r,`align`,t)??(0,e.getDataEnum)(n,`align`,t)??`start`;P.set(i,{item:r,trigger:a,content:o,index:F++,align:s});let l=m(o,`navigation-menu-positioner`);l&&E.has(l)&&(l=null),l&&E.add(l);let u=l?m(l,`navigation-menu-portal`):null;T.set(o,(0,e.createPortalLifecycle)({content:o,root:n,enabled:!0,wrapperSlot:l?void 0:`navigation-menu-positioner`,container:l??void 0,mountTarget:l?u??l:void 0}));let d=c(i),f=(0,e.ensureId)(a,`nav-menu-trigger-${d}`),p=(0,e.ensureId)(o,`nav-menu-content-${d}`);a.setAttribute(`aria-haspopup`,`true`),a.setAttribute(`aria-controls`,p),o.setAttribute(`aria-labelledby`,f)}});let I=Array.from(P.values()).map(e=>e.trigger),L=new Map;for(let[e,t]of P)L.set(t.trigger,e);let R=e=>Array.from(e.querySelectorAll(`a, button, input, select, textarea, [tabindex]:not([tabindex="-1"])`)).filter(e=>!e.hidden&&!e.closest(`[hidden]`)),z=()=>{v&&=(clearTimeout(v),null),y&&=(clearTimeout(y),null)},B=null,V=()=>(B||(B=document.createElement(`div`),B.setAttribute(`data-slot`,`navigation-menu-bridge`),B.style.cssText=`position: absolute; left: 0; right: 0; top: 0; pointer-events: auto; z-index: -1;`,f&&f.insertBefore(B,f.firstChild),w.push((0,e.on)(B,`pointerenter`,()=>{z()}))),B),H=(e,t,r)=>{f&&requestAnimationFrame(()=>{let i=e.firstElementChild,a=i?.getBoundingClientRect(),o=i?getComputedStyle(i):null,s=o&&parseFloat(o.marginTop)||0,c=o&&parseFloat(o.marginBottom)||0,l=a??e.getBoundingClientRect(),u=l.height+s+c,d=getComputedStyle(f),p=parseFloat(d.marginTop)||0,m=e.getBoundingClientRect();f.style.setProperty(`--viewport-width`,`${m.width}px`),f.style.setProperty(`--viewport-height`,`${u}px`);let h=n.getBoundingClientRect(),g=t.getBoundingClientRect(),_;_=r===`center`?g.left-h.left+g.width/2-l.width/2:r===`end`?g.right-h.left-l.width:g.left-h.left,f.style.setProperty(`--viewport-left`,`${_}px`),f.style.left=`${_}px`,e.style.left=`${_}px`,A(e),j();let v=s+p;if(v>0){let e=V();e.style.height=`${v}px`,e.style.transform=`translateY(-${v}px)`}else B&&(B.style.height=`0`)})},U=e=>_===-1||e>_?`right`:`left`,W=e=>{if(!p)return;if(b=e,!e){p.setAttribute(`data-state`,`hidden`);return}let t=u.getBoundingClientRect(),n=e.getBoundingClientRect();p.style.setProperty(`--indicator-left`,`${n.left-t.left}px`),p.style.setProperty(`--indicator-width`,`${n.width}px`),p.style.setProperty(`--indicator-top`,`${n.top-t.top}px`);let r=(f&&parseFloat(getComputedStyle(f).marginTop)||0)<1?1:0;p.style.setProperty(`--indicator-height`,`${n.height-r}px`),p.setAttribute(`data-state`,`visible`)},G=(t,r=!1)=>{if(t===h){z();return}if(t!==null&&t===g){z();return}z(),g=t===null?null:t;let o=()=>{let r=h,i=t?P.get(t):null,a=r!==null&&t!==null&&r!==t,o=a&&i?U(i.index):null,c=document.activeElement;if(t===null&&c&&(0,e.containsWithPortals)(n,c)){let e=r?P.get(r)?.trigger:null;e&&e.focus()}if(P.forEach(({trigger:n,content:i,item:a},s)=>{let c=s===t,u=s===r;if((0,e.setAria)(n,`expanded`,c),n.setAttribute(`data-state`,c?`open`:`closed`),c||u&&t===null?n.tabIndex=0:n.tabIndex=-1,a.setAttribute(`data-state`,c?`open`:`closed`),!c)if(T.get(i)?.restore(),i.setAttribute(`data-state`,`inactive`),i.setAttribute(`aria-hidden`,`true`),l(i,!0),i.hidden=!0,u&&o){let e=o===`right`?`to-left`:`to-right`;i.setAttribute(`data-motion`,e)}else i.removeAttribute(`data-motion`)}),i){if(k?.mount(),T.get(i.content)?.mount(),o){let e=o===`right`?`from-right`:`from-left`;i.content.setAttribute(`data-motion`,e)}else i.content.removeAttribute(`data-motion`);i.content.setAttribute(`data-state`,`active`),i.content.removeAttribute(`aria-hidden`),l(i.content,!1),i.content.hidden=!1,i.content.style.pointerEvents=`auto`,_=i.index,H(i.content,i.trigger,i.align),N(i),W(i.trigger)}else k?.restore(),N(null);let u=t!==null;n.setAttribute(`data-state`,u?`open`:`closed`),o?n.setAttribute(`data-motion`,o===`right`?`from-right`:`from-left`):n.removeAttribute(`data-motion`),f&&(f.setAttribute(`data-state`,u?`open`:`closed`),f.style.pointerEvents=u?`auto`:`none`,u&&!a?f.setAttribute(`data-instant`,``):a&&f.removeAttribute(`data-instant`),o&&f.style.setProperty(`--motion-direction`,o===`right`?`1`:`-1`)),h=t,g=null,t===null&&W(null),(0,e.emit)(n,`navigation-menu:change`,{value:t}),s?.(t)};r?o():t!==null&&h===null?v=setTimeout(o,i):t!==null&&h!==null?o():y=setTimeout(o,a)};n.setAttribute(`data-state`,`closed`),f&&f.setAttribute(`data-state`,`closed`),p&&p.setAttribute(`data-state`,`hidden`),P.forEach(({trigger:t,content:n,item:r})=>{t.tagName===`BUTTON`&&!t.hasAttribute(`type`)&&(t.type=`button`),(0,e.setAria)(t,`expanded`,!1),t.setAttribute(`data-state`,`closed`),t.tabIndex=t===I[0]?0:-1,r.setAttribute(`data-state`,`closed`),n.setAttribute(`data-state`,`inactive`),n.setAttribute(`aria-hidden`,`true`),n.tabIndex=-1,l(n,!0),n.hidden=!0}),P.forEach(({item:t,trigger:n},r)=>{w.push((0,e.on)(n,`pointerenter`,()=>{x||W(n)})),w.push((0,e.on)(t,`pointerenter`,()=>{x||G(r)})),w.push((0,e.on)(t,`pointerleave`,()=>{g===r&&h===null&&(z(),g=null)})),w.push((0,e.on)(n,`focus`,()=>{S||(o&&G(r,!0),W(n))})),w.push((0,e.on)(n,`pointerdown`,()=>{S=!0})),w.push((0,e.on)(n,`click`,()=>{z(),h===r&&x?(x=!1,G(null,!0),W(null)):h===r&&!x?(x=!0,W(n)):(x=!0,G(r,!0),W(n)),S=!1}))}),w.push((0,e.on)(n,`pointerenter`,()=>{C=!0}),(0,e.on)(n,`pointerleave`,t=>{let r=t.relatedTarget;(0,e.containsWithPortals)(n,r)||(C=!1,x||(G(null),W(null)))}),(0,e.on)(n,`pointerdown`,z)),f&&w.push((0,e.on)(f,`pointerenter`,()=>{z()}),(0,e.on)(f,`transitionend`,e=>{if(e.target!==f)return;let t=h?P.get(h):null;t&&H(t.content,t.trigger,t.align)})),P.forEach(({content:t})=>{w.push((0,e.on)(t,`pointerenter`,()=>{z()}),(0,e.on)(t,`pointerleave`,t=>{if(x)return;let r=t.relatedTarget;(0,e.containsWithPortals)(n,r)||(G(null),W(null))}))}),w.push((0,e.on)(u,`keydown`,e=>{let t=e.target,n=I.indexOf(t);if(n===-1)return;let r=L.get(t)??null,i=n;switch(e.key){case`ArrowLeft`:i=n-1,i<0&&(i=I.length-1);break;case`ArrowRight`:i=n+1,i>=I.length&&(i=0);break;case`ArrowDown`:e.preventDefault(),r&&(x=!0,G(r,!0),requestAnimationFrame(()=>{let e=P.get(r);if(!e)return;let t=R(e.content)[0];t?t.focus():e.content.focus()}));return;case`Home`:i=0;break;case`End`:i=I.length-1;break;case`Escape`:x=!1,G(null,!0),W(null);return;default:return}e.preventDefault();let a=I[i];a&&(I.forEach(e=>e.tabIndex=e===a?0:-1),a.focus(),W(a))})),P.forEach(({content:t,trigger:n})=>{w.push((0,e.on)(t,`keydown`,e=>{let r=e.target,i=R(t),a=i.indexOf(r);if(a!==-1)switch(e.key){case`ArrowDown`:case`ArrowRight`:{e.preventDefault();let t=a+1;t<i.length&&i[t]?.focus();break}case`ArrowUp`:case`ArrowLeft`:e.preventDefault(),a===0?n.focus():i[a-1]?.focus();break;case`Escape`:e.preventDefault(),x=!1,G(null,!0),W(null),n.focus();break}}))});let K=()=>(0,e.containsWithPortals)(n,document.activeElement)||C||x,q=()=>{x=!1,G(null,!0),W(null)};return w.push((0,e.on)(document,`pointerup`,()=>{S=!1},{capture:!0}),(0,e.on)(document,`pointercancel`,()=>{S=!1},{capture:!0})),w.push((0,e.on)(document,`focusin`,t=>{h!==null&&((0,e.containsWithPortals)(n,t.target)||q())})),w.push((0,e.createDismissLayer)({root:n,isOpen:()=>h!==null&&K(),onDismiss:q,closeOnClickOutside:!0,closeOnEscape:!0,preventEscapeDefault:!1,isInside:t=>!!t&&(0,e.containsWithPortals)(n,t)})),w.push((0,e.on)(window,`resize`,()=>{if(h&&requestAnimationFrame(()=>j()),h){let e=P.get(h);e&&requestAnimationFrame(()=>A(e.content))}b&&requestAnimationFrame(()=>W(b))}),(0,e.on)(u,`scroll`,()=>{b&&requestAnimationFrame(()=>W(b))})),w.push((0,e.on)(n,`navigation-menu:set`,e=>{let t=e.detail;if(t?.value!==void 0){if(t.value===null)q();else if(P.has(t.value)){x=!0,G(t.value,!0);let e=P.get(t.value);e&&W(e.trigger)}}})),{get value(){return h},open:e=>G(e,!0),close:()=>G(null,!0),destroy:()=>{z(),w.forEach(e=>e()),w.length=0}}}const r=new WeakSet;function i(t=document){let i=[];for(let a of(0,e.getRoots)(t,`navigation-menu`))r.has(a)||(r.add(a),i.push(n(a)));return i}exports.create=i,exports.createNavigationMenu=n;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{containsWithPortals as e,createDismissLayer as t,createPortalLifecycle as n,emit as r,ensureId as i,getDataBool as a,getDataEnum as o,getDataNumber as s,getPart as c,getParts as l,getRoots as u,on as d,setAria as f}from"@data-slot/core";const p=[`start`,`center`,`end`];function m(u,m={}){let h=m.delayOpen??s(u,`delayOpen`)??200,g=m.delayClose??s(u,`delayClose`)??150,_=m.openOnFocus??a(u,`openOnFocus`)??!0,
|
|
1
|
+
import{containsWithPortals as e,createDismissLayer as t,createPortalLifecycle as n,emit as r,ensureId as i,getDataBool as a,getDataEnum as o,getDataNumber as s,getPart as c,getParts as l,getRoots as u,on as d,setAria as f}from"@data-slot/core";const p=[`start`,`center`,`end`];function m(u,m={}){let h=m.delayOpen??s(u,`delayOpen`)??200,g=m.delayClose??s(u,`delayClose`)??150,_=m.openOnFocus??a(u,`openOnFocus`)??!0,ee=m.onValueChange,v=e=>e.replace(/[^a-z0-9\-_:.]/gi,`-`),y=(e,t)=>{`inert`in e&&(e.inert=t)},b=c(u,`navigation-menu-list`),x=l(u,`navigation-menu-item`),S=c(u,`navigation-menu-viewport`),C=c(u,`navigation-menu-indicator`),w=(e,t)=>{let n=e.parentElement;for(;n&&n!==u;){if(n.getAttribute(`data-slot`)===t)return n;n=n.parentElement}return null};if(!b||x.length===0)throw Error(`NavigationMenu requires navigation-menu-list and at least one navigation-menu-item`);let T=null,E=null,D=-1,O=null,k=null,A=null,j=!1,M=!1,N=!1,P=[],F=new Map,I=new Set,L=S?w(S,`navigation-menu-viewport-positioner`):null,R=L?w(L,`navigation-menu-portal`):null,z=S?n({content:S,root:u,enabled:!0,wrapperSlot:L?void 0:`navigation-menu-viewport-positioner`,container:L??void 0,mountTarget:L?R??L:void 0}):null,B=e=>{let t=F.get(e);if(!t)return;let n=t.container,r=u.ownerDocument.defaultView??window,i=u.getBoundingClientRect();n.style.position=`absolute`,n.style.top=`0px`,n.style.left=`0px`,n.style.transform=`translate3d(${i.left+r.scrollX}px, ${i.top+r.scrollY}px, 0)`,n.style.width=`${i.width}px`,n.style.height=`${i.height}px`,n.style.margin=`0`,n.style.willChange=`transform`,n.style.pointerEvents=`none`},V=()=>{if(!S||!z)return;let e=z.container,t=u.ownerDocument.defaultView??window,n=u.getBoundingClientRect();e.style.position=`absolute`,e.style.top=`0px`,e.style.left=`0px`,e.style.transform=`translate3d(${n.left+t.scrollX}px, ${n.top+t.scrollY}px, 0)`,e.style.width=`${n.width}px`,e.style.height=`${n.height}px`,e.style.margin=`0`,e.style.willChange=`transform`,e.style.pointerEvents=`none`},H=null,U=e=>{H?.disconnect(),H=null,!(!S||!e)&&(H=new ResizeObserver(()=>X(e.content,e.trigger,e.align)),H.observe(e.content))};P.push(()=>H?.disconnect()),P.push(()=>{F.forEach(e=>e.cleanup()),F.clear(),z?.cleanup()});let W=new Map,te=0;x.forEach(e=>{let t=e.dataset.value;if(!t)return;let r=c(e,`navigation-menu-trigger`),a=c(e,`navigation-menu-content`);if(r&&a){let s=o(a,`align`,p)??o(e,`align`,p)??o(u,`align`,p)??`start`;W.set(t,{item:e,trigger:r,content:a,index:te++,align:s});let c=w(a,`navigation-menu-positioner`);c&&I.has(c)&&(c=null),c&&I.add(c);let l=c?w(c,`navigation-menu-portal`):null;F.set(a,n({content:a,root:u,enabled:!0,wrapperSlot:c?void 0:`navigation-menu-positioner`,container:c??void 0,mountTarget:c?l??c:void 0}));let d=v(t),f=i(r,`nav-menu-trigger-${d}`),m=i(a,`nav-menu-content-${d}`);r.setAttribute(`aria-haspopup`,`true`),r.setAttribute(`aria-controls`,m),a.setAttribute(`aria-labelledby`,f)}});let G=Array.from(W.values()).map(e=>e.trigger),K=new Map;for(let[e,t]of W)K.set(t.trigger,e);let q=e=>Array.from(e.querySelectorAll(`a, button, input, select, textarea, [tabindex]:not([tabindex="-1"])`)).filter(e=>!e.hidden&&!e.closest(`[hidden]`)),J=()=>{O&&=(clearTimeout(O),null),k&&=(clearTimeout(k),null)},Y=null,ne=()=>(Y||(Y=document.createElement(`div`),Y.setAttribute(`data-slot`,`navigation-menu-bridge`),Y.style.cssText=`position: absolute; left: 0; right: 0; top: 0; pointer-events: auto; z-index: -1;`,S&&S.insertBefore(Y,S.firstChild),P.push(d(Y,`pointerenter`,()=>{J()}))),Y),X=(e,t,n)=>{S&&requestAnimationFrame(()=>{let r=e.firstElementChild,i=r?.getBoundingClientRect(),a=r?getComputedStyle(r):null,o=a&&parseFloat(a.marginTop)||0,s=a&&parseFloat(a.marginBottom)||0,c=i??e.getBoundingClientRect(),l=c.height+o+s,d=getComputedStyle(S),f=parseFloat(d.marginTop)||0,p=e.getBoundingClientRect();S.style.setProperty(`--viewport-width`,`${p.width}px`),S.style.setProperty(`--viewport-height`,`${l}px`);let m=u.getBoundingClientRect(),h=t.getBoundingClientRect(),g;g=n===`center`?h.left-m.left+h.width/2-c.width/2:n===`end`?h.right-m.left-c.width:h.left-m.left,S.style.setProperty(`--viewport-left`,`${g}px`),S.style.left=`${g}px`,e.style.left=`${g}px`,B(e),V();let _=o+f;if(_>0){let e=ne();e.style.height=`${_}px`,e.style.transform=`translateY(-${_}px)`}else Y&&(Y.style.height=`0`)})},re=e=>D===-1||e>D?`right`:`left`,Z=e=>{if(!C)return;if(A=e,!e){C.setAttribute(`data-state`,`hidden`);return}let t=b.getBoundingClientRect(),n=e.getBoundingClientRect();C.style.setProperty(`--indicator-left`,`${n.left-t.left}px`),C.style.setProperty(`--indicator-width`,`${n.width}px`),C.style.setProperty(`--indicator-top`,`${n.top-t.top}px`);let r=(S&&parseFloat(getComputedStyle(S).marginTop)||0)<1?1:0;C.style.setProperty(`--indicator-height`,`${n.height-r}px`),C.setAttribute(`data-state`,`visible`)},Q=(t,n=!1)=>{if(t===T){J();return}if(t!==null&&t===E){J();return}J(),E=t===null?null:t;let i=()=>{let n=T,i=t?W.get(t):null,a=n!==null&&t!==null&&n!==t,o=a&&i?re(i.index):null,s=document.activeElement;if(t===null&&s&&e(u,s)){let e=n?W.get(n)?.trigger:null;e&&e.focus()}if(W.forEach(({trigger:e,content:r,item:i},a)=>{let s=a===t,c=a===n;if(f(e,`expanded`,s),e.setAttribute(`data-state`,s?`open`:`closed`),s||c&&t===null?e.tabIndex=0:e.tabIndex=-1,i.setAttribute(`data-state`,s?`open`:`closed`),!s)if(F.get(r)?.restore(),r.setAttribute(`data-state`,`inactive`),r.setAttribute(`aria-hidden`,`true`),y(r,!0),r.hidden=!0,c&&o){let e=o===`right`?`to-left`:`to-right`;r.setAttribute(`data-motion`,e)}else r.removeAttribute(`data-motion`)}),i){if(z?.mount(),F.get(i.content)?.mount(),o){let e=o===`right`?`from-right`:`from-left`;i.content.setAttribute(`data-motion`,e)}else i.content.removeAttribute(`data-motion`);i.content.setAttribute(`data-state`,`active`),i.content.removeAttribute(`aria-hidden`),y(i.content,!1),i.content.hidden=!1,i.content.style.pointerEvents=`auto`,D=i.index,X(i.content,i.trigger,i.align),U(i),Z(i.trigger)}else z?.restore(),U(null);let c=t!==null;u.setAttribute(`data-state`,c?`open`:`closed`),o?u.setAttribute(`data-motion`,o===`right`?`from-right`:`from-left`):u.removeAttribute(`data-motion`),S&&(S.setAttribute(`data-state`,c?`open`:`closed`),S.style.pointerEvents=c?`auto`:`none`,c&&!a?S.setAttribute(`data-instant`,``):a&&S.removeAttribute(`data-instant`),o&&S.style.setProperty(`--motion-direction`,o===`right`?`1`:`-1`)),T=t,E=null,t===null&&Z(null),r(u,`navigation-menu:change`,{value:t}),ee?.(t)};n?i():t!==null&&T===null?O=setTimeout(i,h):t!==null&&T!==null?i():k=setTimeout(i,g)};u.setAttribute(`data-state`,`closed`),S&&S.setAttribute(`data-state`,`closed`),C&&C.setAttribute(`data-state`,`hidden`),W.forEach(({trigger:e,content:t,item:n})=>{e.tagName===`BUTTON`&&!e.hasAttribute(`type`)&&(e.type=`button`),f(e,`expanded`,!1),e.setAttribute(`data-state`,`closed`),e.tabIndex=e===G[0]?0:-1,n.setAttribute(`data-state`,`closed`),t.setAttribute(`data-state`,`inactive`),t.setAttribute(`aria-hidden`,`true`),t.tabIndex=-1,y(t,!0),t.hidden=!0}),W.forEach(({item:e,trigger:t},n)=>{P.push(d(t,`pointerenter`,()=>{j||Z(t)})),P.push(d(e,`pointerenter`,()=>{j||Q(n)})),P.push(d(e,`pointerleave`,()=>{E===n&&T===null&&(J(),E=null)})),P.push(d(t,`focus`,()=>{M||(_&&Q(n,!0),Z(t))})),P.push(d(t,`pointerdown`,()=>{M=!0})),P.push(d(t,`click`,()=>{J(),T===n&&j?(j=!1,Q(null,!0),Z(null)):T===n&&!j?(j=!0,Z(t)):(j=!0,Q(n,!0),Z(t)),M=!1}))}),P.push(d(u,`pointerenter`,()=>{N=!0}),d(u,`pointerleave`,t=>{let n=t.relatedTarget;e(u,n)||(N=!1,j||(Q(null),Z(null)))}),d(u,`pointerdown`,J)),S&&P.push(d(S,`pointerenter`,()=>{J()}),d(S,`transitionend`,e=>{if(e.target!==S)return;let t=T?W.get(T):null;t&&X(t.content,t.trigger,t.align)})),W.forEach(({content:t})=>{P.push(d(t,`pointerenter`,()=>{J()}),d(t,`pointerleave`,t=>{if(j)return;let n=t.relatedTarget;e(u,n)||(Q(null),Z(null))}))}),P.push(d(b,`keydown`,e=>{let t=e.target,n=G.indexOf(t);if(n===-1)return;let r=K.get(t)??null,i=n;switch(e.key){case`ArrowLeft`:i=n-1,i<0&&(i=G.length-1);break;case`ArrowRight`:i=n+1,i>=G.length&&(i=0);break;case`ArrowDown`:e.preventDefault(),r&&(j=!0,Q(r,!0),requestAnimationFrame(()=>{let e=W.get(r);if(!e)return;let t=q(e.content)[0];t?t.focus():e.content.focus()}));return;case`Home`:i=0;break;case`End`:i=G.length-1;break;case`Escape`:j=!1,Q(null,!0),Z(null);return;default:return}e.preventDefault();let a=G[i];a&&(G.forEach(e=>e.tabIndex=e===a?0:-1),a.focus(),Z(a))})),W.forEach(({content:e,trigger:t})=>{P.push(d(e,`keydown`,n=>{let r=n.target,i=q(e),a=i.indexOf(r);if(a!==-1)switch(n.key){case`ArrowDown`:case`ArrowRight`:{n.preventDefault();let e=a+1;e<i.length&&i[e]?.focus();break}case`ArrowUp`:case`ArrowLeft`:n.preventDefault(),a===0?t.focus():i[a-1]?.focus();break;case`Escape`:n.preventDefault(),j=!1,Q(null,!0),Z(null),t.focus();break}}))});let ie=()=>e(u,document.activeElement)||N||j,$=()=>{j=!1,Q(null,!0),Z(null)};return P.push(d(document,`pointerup`,()=>{M=!1},{capture:!0}),d(document,`pointercancel`,()=>{M=!1},{capture:!0})),P.push(d(document,`focusin`,t=>{T!==null&&(e(u,t.target)||$())})),P.push(t({root:u,isOpen:()=>T!==null&&ie(),onDismiss:$,closeOnClickOutside:!0,closeOnEscape:!0,preventEscapeDefault:!1,isInside:t=>!!t&&e(u,t)})),P.push(d(window,`resize`,()=>{if(T&&requestAnimationFrame(()=>V()),T){let e=W.get(T);e&&requestAnimationFrame(()=>B(e.content))}A&&requestAnimationFrame(()=>Z(A))}),d(b,`scroll`,()=>{A&&requestAnimationFrame(()=>Z(A))})),P.push(d(u,`navigation-menu:set`,e=>{let t=e.detail;if(t?.value!==void 0){if(t.value===null)$();else if(W.has(t.value)){j=!0,Q(t.value,!0);let e=W.get(t.value);e&&Z(e.trigger)}}})),{get value(){return T},open:e=>Q(e,!0),close:()=>Q(null,!0),destroy:()=>{J(),P.forEach(e=>e()),P.length=0}}}const h=new WeakSet;function g(e=document){let t=[];for(let n of u(e,`navigation-menu`))h.has(n)||(h.add(n),t.push(m(n)));return t}export{g as create,m as createNavigationMenu};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-slot/navigation-menu",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.56",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -39,6 +39,6 @@
|
|
|
39
39
|
],
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@data-slot/core": "0.2.
|
|
42
|
+
"@data-slot/core": "0.2.56"
|
|
43
43
|
}
|
|
44
44
|
}
|