@data-slot/select 0.2.45 → 0.2.47
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/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@data-slot/core`);const t=[`top`,`bottom`],n=[`start`,`center`,`end`],r=[`item-aligned`,`popper`];function i(i,o={}){let s=(0,e.getPart)(i,`select-trigger`),c=(0,e.getPart)(i,`select-content`),l=(0,e.getPart)(i,`select-value`);if(!s||!c)throw Error(`Select requires trigger and content slots`);let u=o.defaultValue??(0,e.getDataString)(i,`defaultValue`)??null,d=o.defaultOpen??(0,e.getDataBool)(i,`defaultOpen`)??!1,f=o.placeholder??(0,e.getDataString)(i,`placeholder`)??(l?(0,e.getDataString)(l,`placeholder`):void 0)??``,p=o.disabled??(0,e.getDataBool)(i,`disabled`)??!1,ee=o.required??(0,e.getDataBool)(i,`required`)??!1,m=o.name??(0,e.getDataString)(i,`name`)??null,te=o.onValueChange,ne=o.onOpenChange,h=o.position??(0,e.getDataEnum)(i,`position`,r)??`item-aligned`,re=o.side??(0,e.getDataEnum)(c,`side`,t)??(0,e.getDataEnum)(i,`side`,t)??`bottom`,g=o.align??(0,e.getDataEnum)(c,`align`,n)??(0,e.getDataEnum)(i,`align`,n)??`start`,_=o.sideOffset??(0,e.getDataNumber)(c,`sideOffset`)??(0,e.getDataNumber)(i,`sideOffset`)??4,ie=o.alignOffset??(0,e.getDataNumber)(c,`alignOffset`)??(0,e.getDataNumber)(i,`alignOffset`)??0,v=o.avoidCollisions??(0,e.getDataBool)(c,`avoidCollisions`)??(0,e.getDataBool)(i,`avoidCollisions`)??!0,y=o.collisionPadding??(0,e.getDataNumber)(c,`collisionPadding`)??(0,e.getDataNumber)(i,`collisionPadding`)??8,b=o.lockScroll??(0,e.getDataBool)(i,`lockScroll`)??!0,x=!1,S=u,C=null,w=-1,T=``,E=null,D=!1,O=0,k=0,A=!1,j=[],M=[],N=[],P=new Map,F=null,I=!1,L=(0,e.createPortalLifecycle)({content:c,root:i,wrapperSlot:`select-positioner`}),R=!1,z=!0,B=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,V=(0,e.ensureId)(s,`select-trigger`),ae=(0,e.ensureId)(c,`select-content`);s.setAttribute(`role`,`combobox`),s.setAttribute(`aria-haspopup`,`listbox`),s.setAttribute(`aria-controls`,ae),s.hasAttribute(`type`)||s.setAttribute(`type`,`button`),c.setAttribute(`role`,`listbox`),c.setAttribute(`aria-labelledby`,V),c.tabIndex=-1;let H=document.querySelector(`label[for="${CSS.escape(V)}"]`);if(H){let t=(0,e.ensureId)(H,`select-label`),n=s.getAttribute(`aria-labelledby`);s.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),j.push((0,e.on)(H,`click`,e=>{e.preventDefault(),p||Z(!x)}))}p&&(s.setAttribute(`aria-disabled`,`true`),s.setAttribute(`data-disabled`,``)),ee&&s.setAttribute(`aria-required`,`true`),m&&(F=document.createElement(`input`),F.type=`hidden`,F.name=m,F.value=S??``,i.appendChild(F));let U=()=>{M=(0,e.getParts)(c,`select-item`);for(let t of M)t.setAttribute(`role`,`option`),t.hasAttribute(`data-disabled`)||t.hasAttribute(`disabled`)?t.setAttribute(`aria-disabled`,`true`):t.removeAttribute(`aria-disabled`),t.tabIndex=-1,t.dataset.value===S?((0,e.setAria)(t,`selected`,!0),t.setAttribute(`data-selected`,``)):((0,e.setAria)(t,`selected`,!1),t.removeAttribute(`data-selected`));N=M.filter(e=>!B(e)),P=new Map(N.map((e,t)=>[e,t]));let t=(0,e.getParts)(c,`select-group`);for(let n of t){n.setAttribute(`role`,`group`);let t=(0,e.getPart)(n,`select-label`);if(t){let r=(0,e.ensureId)(t,`select-label`);n.setAttribute(`aria-labelledby`,r)}}},W=()=>(0,e.getPart)(c,`select-viewport`)??c,oe=(e,t,n)=>{let r=0,i=e;for(;i&&i!==c;){r+=i.offsetTop;let e=i.offsetParent;if(!(e instanceof HTMLElement)){r=NaN;break}i=e}return i===c&&Number.isFinite(r)?r:e.getBoundingClientRect().top-t.top+n.scrollTop},se=(e,t,n)=>{let r=w>=0?N[w]:void 0,i=M.find(e=>e.dataset.value===S),a=r??i??N[0],o=e.left,s,c=0,l=e.height;return a?(c=oe(a,t,n),l=a.getBoundingClientRect().height||a.offsetHeight||e.height,s=e.top+e.height/2-l/2-c):s=e.top,{x:o,y:s,alignItem:a,itemTopInContent:c,itemHeight:l}},G=()=>{let n=L.container,r=i.ownerDocument.defaultView??window,a=s.getBoundingClientRect(),o=W();c.style.minWidth=`${a.width}px`;let l=c.getBoundingClientRect(),u,d=`bottom`;if(h===`item-aligned`){let e=se(a,l,o);u={x:e.x,y:e.y};let t=a.top+a.height/2,n=y,i=r.innerHeight-l.height-y,s=e=>v?i<n?n:Math.min(Math.max(e,n),i):e,c=y,f=r.innerWidth-l.width-y;if(u.x=(e=>v?f<c?c:Math.min(Math.max(e,c),f):e)(u.x),
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@data-slot/core`);const t=[`top`,`bottom`],n=[`start`,`center`,`end`],r=[`item-aligned`,`popper`];function i(i,o={}){let s=(0,e.getPart)(i,`select-trigger`),c=(0,e.getPart)(i,`select-content`),l=(0,e.getPart)(i,`select-value`);if(!s||!c)throw Error(`Select requires trigger and content slots`);let u=o.defaultValue??(0,e.getDataString)(i,`defaultValue`)??null,d=o.defaultOpen??(0,e.getDataBool)(i,`defaultOpen`)??!1,f=o.placeholder??(0,e.getDataString)(i,`placeholder`)??(l?(0,e.getDataString)(l,`placeholder`):void 0)??``,p=o.disabled??(0,e.getDataBool)(i,`disabled`)??!1,ee=o.required??(0,e.getDataBool)(i,`required`)??!1,m=o.name??(0,e.getDataString)(i,`name`)??null,te=o.onValueChange,ne=o.onOpenChange,h=o.position??(0,e.getDataEnum)(i,`position`,r)??`item-aligned`,re=o.side??(0,e.getDataEnum)(c,`side`,t)??(0,e.getDataEnum)(i,`side`,t)??`bottom`,g=o.align??(0,e.getDataEnum)(c,`align`,n)??(0,e.getDataEnum)(i,`align`,n)??`start`,_=o.sideOffset??(0,e.getDataNumber)(c,`sideOffset`)??(0,e.getDataNumber)(i,`sideOffset`)??4,ie=o.alignOffset??(0,e.getDataNumber)(c,`alignOffset`)??(0,e.getDataNumber)(i,`alignOffset`)??0,v=o.avoidCollisions??(0,e.getDataBool)(c,`avoidCollisions`)??(0,e.getDataBool)(i,`avoidCollisions`)??!0,y=o.collisionPadding??(0,e.getDataNumber)(c,`collisionPadding`)??(0,e.getDataNumber)(i,`collisionPadding`)??8,b=o.lockScroll??(0,e.getDataBool)(i,`lockScroll`)??!0,x=!1,S=u,C=null,w=-1,T=``,E=null,D=!1,O=0,k=0,A=!1,j=[],M=[],N=[],P=new Map,F=null,I=!1,L=(0,e.createPortalLifecycle)({content:c,root:i,wrapperSlot:`select-positioner`}),R=!1,z=!0,B=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,V=(0,e.ensureId)(s,`select-trigger`),ae=(0,e.ensureId)(c,`select-content`);s.setAttribute(`role`,`combobox`),s.setAttribute(`aria-haspopup`,`listbox`),s.setAttribute(`aria-controls`,ae),s.hasAttribute(`type`)||s.setAttribute(`type`,`button`),c.setAttribute(`role`,`listbox`),c.setAttribute(`aria-labelledby`,V),c.tabIndex=-1;let H=document.querySelector(`label[for="${CSS.escape(V)}"]`);if(H){let t=(0,e.ensureId)(H,`select-label`),n=s.getAttribute(`aria-labelledby`);s.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),j.push((0,e.on)(H,`click`,e=>{e.preventDefault(),p||Z(!x)}))}p&&(s.setAttribute(`aria-disabled`,`true`),s.setAttribute(`data-disabled`,``)),ee&&s.setAttribute(`aria-required`,`true`),m&&(F=document.createElement(`input`),F.type=`hidden`,F.name=m,F.value=S??``,i.appendChild(F));let U=()=>{M=(0,e.getParts)(c,`select-item`);for(let t of M)t.setAttribute(`role`,`option`),t.hasAttribute(`data-disabled`)||t.hasAttribute(`disabled`)?t.setAttribute(`aria-disabled`,`true`):t.removeAttribute(`aria-disabled`),t.tabIndex=-1,t.dataset.value===S?((0,e.setAria)(t,`selected`,!0),t.setAttribute(`data-selected`,``)):((0,e.setAria)(t,`selected`,!1),t.removeAttribute(`data-selected`));N=M.filter(e=>!B(e)),P=new Map(N.map((e,t)=>[e,t]));let t=(0,e.getParts)(c,`select-group`);for(let n of t){n.setAttribute(`role`,`group`);let t=(0,e.getPart)(n,`select-label`);if(t){let r=(0,e.ensureId)(t,`select-label`);n.setAttribute(`aria-labelledby`,r)}}},W=()=>(0,e.getPart)(c,`select-viewport`)??c,oe=(e,t,n)=>{let r=0,i=e;for(;i&&i!==c;){r+=i.offsetTop;let e=i.offsetParent;if(!(e instanceof HTMLElement)){r=NaN;break}i=e}return i===c&&Number.isFinite(r)?r:e.getBoundingClientRect().top-t.top+n.scrollTop},se=(e,t,n)=>{let r=w>=0?N[w]:void 0,i=M.find(e=>e.dataset.value===S),a=r??i??N[0],o=e.left,s,c=0,l=e.height;return a?(c=oe(a,t,n),l=a.getBoundingClientRect().height||a.offsetHeight||e.height,s=e.top+e.height/2-l/2-c):s=e.top,{x:o,y:s,alignItem:a,itemTopInContent:c,itemHeight:l}},G=()=>{let n=L.container,r=i.ownerDocument.defaultView??window,a=s.getBoundingClientRect(),o=W();c.style.minWidth=`${a.width}px`;let l=c.getBoundingClientRect(),u,d=`bottom`;if(h===`item-aligned`){let e=se(a,l,o);u={x:e.x,y:e.y};let t=a.top+a.height/2,n=y,i=r.innerHeight-l.height-y,s=e=>v?i<n?n:Math.min(Math.max(e,n),i):e,c=y,f=r.innerWidth-l.width-y;if(u.x=(e=>v?f<c?c:Math.min(Math.max(e,c),f):e)(u.x),e.alignItem){let n=Math.max(0,o.scrollHeight-o.clientHeight),r=n=>e.itemTopInContent+e.itemHeight/2-(t-n);if(n>0){u.y=s(t-l.height/2);let i=Math.min(Math.max(r(u.y),0),n);o.scrollTop=i,u.y=s(t-(e.itemTopInContent-i+e.itemHeight/2)),i=Math.min(Math.max(r(u.y),0),n),o.scrollTop=i,u.y=s(t-(e.itemTopInContent-i+e.itemHeight/2))}else o.scrollTop=0,u.y=s(e.y)}else o.scrollTop=0,u.y=s(e.y);d=u.y<a.top?`top`:`bottom`}else{let n=(0,e.computeFloatingPosition)({anchorRect:a,contentRect:l,side:re,align:g,sideOffset:_,alignOffset:ie,avoidCollisions:v,collisionPadding:y,allowedSides:t});u={x:n.x,y:n.y},d=n.side}b?(n.style.position=`fixed`,n.style.top=`0px`,n.style.left=`0px`,n.style.transform=`translate3d(${u.x}px, ${u.y}px, 0)`):(n.style.position=`absolute`,n.style.top=`0px`,n.style.left=`0px`,n.style.transform=`translate3d(${u.x+r.scrollX}px, ${u.y+r.scrollY}px, 0)`),n.style.willChange=`transform`,n.style.margin=`0`,c.setAttribute(`data-side`,d),c.setAttribute(`data-align`,h===`item-aligned`?`center`:g),n!==c&&(n.setAttribute(`data-side`,d),n.setAttribute(`data-align`,h===`item-aligned`?`center`:g))},K=(0,e.createPositionSync)({observedElements:[s,c],isActive:()=>x,ancestorScroll:b,onUpdate:G,ignoreScrollTarget:e=>e instanceof Node&&c.contains(e)}),q=(t,n=!0,r=!0)=>{let i=W();for(let a=0;a<N.length;a++){let o=N[a];a===t?(o.setAttribute(`data-highlighted`,``),r&&(0,e.ensureItemVisibleInContainer)(o,i),n&&o.focus()):o.removeAttribute(`data-highlighted`)}w=t},J=()=>{for(let e of M)e.removeAttribute(`data-highlighted`);w=-1},Y=e=>{i.setAttribute(`data-state`,e),s.setAttribute(`data-state`,e),c.setAttribute(`data-state`,e),e===`open`?(i.setAttribute(`data-open`,``),s.setAttribute(`data-open`,``),c.setAttribute(`data-open`,``),i.removeAttribute(`data-closed`),s.removeAttribute(`data-closed`),c.removeAttribute(`data-closed`)):(i.setAttribute(`data-closed`,``),s.setAttribute(`data-closed`,``),c.setAttribute(`data-closed`,``),i.removeAttribute(`data-open`),s.removeAttribute(`data-open`),c.removeAttribute(`data-open`))},ce=()=>{requestAnimationFrame(()=>{C&&document.contains(C)?C.focus():s&&document.contains(s)&&s.focus(),C=null})},X=(0,e.createPresenceLifecycle)({element:c,onExitComplete:()=>{R||(L.restore(),c.hidden=!0,z?ce():C=null)}}),le=()=>{if(l)if(S===null)l.textContent=f,s.setAttribute(`data-placeholder`,``);else{let e=M.find(e=>e.dataset.value===S);l.textContent=e?.dataset.label??e?.textContent?.trim()??S,s.removeAttribute(`data-placeholder`)}},Z=(t,n=!1)=>{if(x!==t&&!(p&&t)){if(t){let t=A;A=!1,z=!0,C=document.activeElement,x=!0,(0,e.setAria)(s,`expanded`,!0),L.mount(),c.hidden=!1,Y(`open`),X.enter(),b&&!I&&((0,e.lockScroll)(),I=!0),U(),D=!1;let n=N.findIndex(e=>e.dataset.value===S);n>=0?q(n,!1,!1):J(),K.start(),G(),K.update(),requestAnimationFrame(()=>{if(x&&(G(),K.update(),t&&(O!==0||k!==0))){let e=document.elementFromPoint(O,k)?.closest?.(`[data-slot="select-item"]`);if(e&&!B(e)&&c.contains(e)){let t=P.get(e);t!==void 0&&q(t,!1,!1)}}}),c.focus()}else x=!1,A=!1,O=0,k=0,(0,e.setAria)(s,`expanded`,!1),Y(`closed`),J(),T=``,D=!1,z=!n,I&&=((0,e.unlockScroll)(),!1),K.stop(),X.exit();(0,e.emit)(i,`select:open-change`,{open:x}),ne?.(x)}},Q=(t,n=!1)=>{if(S===t&&!n)return;let r=S;S=t,F&&(F.value=t??``),t===null?i.removeAttribute(`data-value`):i.setAttribute(`data-value`,t);for(let n of M)n.dataset.value===t?((0,e.setAria)(n,`selected`,!0),n.setAttribute(`data-selected`,``)):((0,e.setAria)(n,`selected`,!1),n.removeAttribute(`data-selected`));le(),!n&&r!==t&&((0,e.emit)(i,`select:change`,{value:t}),te?.(t))},$=e=>{if(B(e))return;let t=e.dataset.value;t!==void 0&&(Q(t),Z(!1))},ue=e=>{let t=N.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),D=!0,q(w===-1?0:Math.min(w+1,t-1));break;case`ArrowUp`:e.preventDefault(),D=!0,q(w===-1?t-1:Math.max(w-1,0));break;case`Home`:e.preventDefault(),D=!0,q(0);break;case`End`:e.preventDefault(),D=!0,q(t-1);break;case`Enter`:case` `:e.preventDefault(),w>=0&&$(N[w]);break;case`Tab`:Z(!1,!0);break;case`Escape`:e.preventDefault(),Z(!1);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),de(e.key.toLowerCase()))}},de=e=>{E&&clearTimeout(E),E=setTimeout(()=>{T=``},500),T+=e;let t=N.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(T));if(t===-1&&T.length===1){let n=w+1;for(let r=0;r<N.length;r++){let i=(n+r)%N.length;if((N[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(D=!0,q(t))};return(0,e.setAria)(s,`expanded`,!1),c.hidden=!0,Y(`closed`),U(),Q(S,!0),j.push((0,e.on)(s,`pointerdown`,e=>{O=e.clientX,k=e.clientY,A=!0}),(0,e.on)(s,`click`,()=>{p||Z(!x)}),(0,e.on)(s,`keydown`,e=>{if(!p)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),A=!1,Z(!0);break}})),j.push((0,e.on)(c,`keydown`,ue),(0,e.on)(c,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&$(t)}),(0,e.on)(c,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(D&&(D=!1,t&&P.get(t)===w)))if(t&&!B(t)){let e=P.get(t);e!==void 0&&e!==w&&q(e,!1)}else J()}),(0,e.on)(c,`pointerleave`,()=>{D||J()})),j.push((0,e.createDismissLayer)({root:i,isOpen:()=>x,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),j.push((0,e.on)(i,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&Q(t.value),t?.open!==void 0&&Z(t.open)})),d&&Z(!0),{get value(){return S},get isOpen(){return x},select:e=>Q(e),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{R=!0,E&&clearTimeout(E),K.stop(),X.cleanup(),L.cleanup(),I&&=((0,e.unlockScroll)(),!1),j.forEach(e=>e()),j.length=0,F&&F.parentNode&&F.parentNode.removeChild(F),a.delete(i)}}}const a=new WeakSet;function o(t=document){let n=[];for(let r of(0,e.getRoots)(t,`select`))a.has(r)||(a.add(r),n.push(i(r)));return n}exports.create=o,exports.createSelect=i;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{computeFloatingPosition as e,createDismissLayer as t,createPortalLifecycle as n,createPositionSync as r,createPresenceLifecycle as i,emit as a,ensureId as o,ensureItemVisibleInContainer as s,getDataBool as c,getDataEnum as l,getDataNumber as u,getDataString as d,getPart as f,getParts as ee,getRoots as p,lockScroll as te,on as m,setAria as h,unlockScroll as ne}from"@data-slot/core";const g=[`top`,`bottom`],re=[`start`,`center`,`end`],ie=[`item-aligned`,`popper`];function _(p,_={}){let y=f(p,`select-trigger`),b=f(p,`select-content`),x=f(p,`select-value`);if(!y||!b)throw Error(`Select requires trigger and content slots`);let ae=_.defaultValue??d(p,`defaultValue`)??null,oe=_.defaultOpen??c(p,`defaultOpen`)??!1,se=_.placeholder??d(p,`placeholder`)??(x?d(x,`placeholder`):void 0)??``,S=_.disabled??c(p,`disabled`)??!1,ce=_.required??c(p,`required`)??!1,le=_.name??d(p,`name`)??null,ue=_.onValueChange,de=_.onOpenChange,C=_.position??l(p,`position`,ie)??`item-aligned`,fe=_.side??l(b,`side`,g)??l(p,`side`,g)??`bottom`,w=_.align??l(b,`align`,re)??l(p,`align`,re)??`start`,pe=_.sideOffset??u(b,`sideOffset`)??u(p,`sideOffset`)??4,me=_.alignOffset??u(b,`alignOffset`)??u(p,`alignOffset`)??0,T=_.avoidCollisions??c(b,`avoidCollisions`)??c(p,`avoidCollisions`)??!0,E=_.collisionPadding??u(b,`collisionPadding`)??u(p,`collisionPadding`)??8,D=_.lockScroll??c(p,`lockScroll`)??!0,O=!1,k=ae,A=null,j=-1,M=``,N=null,P=!1,F=0,I=0,L=!1,R=[],z=[],B=[],V=new Map,H=null,U=!1,W=n({content:b,root:p,wrapperSlot:`select-positioner`}),he=!1,G=!0,K=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,ge=o(y,`select-trigger`),_e=o(b,`select-content`);y.setAttribute(`role`,`combobox`),y.setAttribute(`aria-haspopup`,`listbox`),y.setAttribute(`aria-controls`,_e),y.hasAttribute(`type`)||y.setAttribute(`type`,`button`),b.setAttribute(`role`,`listbox`),b.setAttribute(`aria-labelledby`,ge),b.tabIndex=-1;let q=document.querySelector(`label[for="${CSS.escape(ge)}"]`);if(q){let e=o(q,`select-label`),t=y.getAttribute(`aria-labelledby`);y.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),R.push(m(q,`click`,e=>{e.preventDefault(),S||Q(!O)}))}S&&(y.setAttribute(`aria-disabled`,`true`),y.setAttribute(`data-disabled`,``)),ce&&y.setAttribute(`aria-required`,`true`),le&&(H=document.createElement(`input`),H.type=`hidden`,H.name=le,H.value=k??``,p.appendChild(H));let ve=()=>{z=ee(b,`select-item`);for(let e of z)e.setAttribute(`role`,`option`),e.hasAttribute(`data-disabled`)||e.hasAttribute(`disabled`)?e.setAttribute(`aria-disabled`,`true`):e.removeAttribute(`aria-disabled`),e.tabIndex=-1,e.dataset.value===k?(h(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(h(e,`selected`,!1),e.removeAttribute(`data-selected`));B=z.filter(e=>!K(e)),V=new Map(B.map((e,t)=>[e,t]));let e=ee(b,`select-group`);for(let t of e){t.setAttribute(`role`,`group`);let e=f(t,`select-label`);if(e){let n=o(e,`select-label`);t.setAttribute(`aria-labelledby`,n)}}},ye=()=>f(b,`select-viewport`)??b,be=(e,t,n)=>{let r=0,i=e;for(;i&&i!==b;){r+=i.offsetTop;let e=i.offsetParent;if(!(e instanceof HTMLElement)){r=NaN;break}i=e}return i===b&&Number.isFinite(r)?r:e.getBoundingClientRect().top-t.top+n.scrollTop},xe=(e,t,n)=>{let r=j>=0?B[j]:void 0,i=z.find(e=>e.dataset.value===k),a=r??i??B[0],o=e.left,s,c=0,l=e.height;return a?(c=be(a,t,n),l=a.getBoundingClientRect().height||a.offsetHeight||e.height,s=e.top+e.height/2-l/2-c):s=e.top,{x:o,y:s,alignItem:a,itemTopInContent:c,itemHeight:l}},J=()=>{let t=W.container,n=p.ownerDocument.defaultView??window,r=y.getBoundingClientRect(),i=ye();b.style.minWidth=`${r.width}px`;let a=b.getBoundingClientRect(),o,s=`bottom`;if(C===`item-aligned`){let e=xe(r,a,i);o={x:e.x,y:e.y};let t=r.top+r.height/2,c=E,l=n.innerHeight-a.height-E,u=e=>T?l<c?c:Math.min(Math.max(e,c),l):e,d=E,f=n.innerWidth-a.width-E;if(o.x=(e=>T?f<d?d:Math.min(Math.max(e,d),f):e)(o.x),
|
|
1
|
+
import{computeFloatingPosition as e,createDismissLayer as t,createPortalLifecycle as n,createPositionSync as r,createPresenceLifecycle as i,emit as a,ensureId as o,ensureItemVisibleInContainer as s,getDataBool as c,getDataEnum as l,getDataNumber as u,getDataString as d,getPart as f,getParts as ee,getRoots as p,lockScroll as te,on as m,setAria as h,unlockScroll as ne}from"@data-slot/core";const g=[`top`,`bottom`],re=[`start`,`center`,`end`],ie=[`item-aligned`,`popper`];function _(p,_={}){let y=f(p,`select-trigger`),b=f(p,`select-content`),x=f(p,`select-value`);if(!y||!b)throw Error(`Select requires trigger and content slots`);let ae=_.defaultValue??d(p,`defaultValue`)??null,oe=_.defaultOpen??c(p,`defaultOpen`)??!1,se=_.placeholder??d(p,`placeholder`)??(x?d(x,`placeholder`):void 0)??``,S=_.disabled??c(p,`disabled`)??!1,ce=_.required??c(p,`required`)??!1,le=_.name??d(p,`name`)??null,ue=_.onValueChange,de=_.onOpenChange,C=_.position??l(p,`position`,ie)??`item-aligned`,fe=_.side??l(b,`side`,g)??l(p,`side`,g)??`bottom`,w=_.align??l(b,`align`,re)??l(p,`align`,re)??`start`,pe=_.sideOffset??u(b,`sideOffset`)??u(p,`sideOffset`)??4,me=_.alignOffset??u(b,`alignOffset`)??u(p,`alignOffset`)??0,T=_.avoidCollisions??c(b,`avoidCollisions`)??c(p,`avoidCollisions`)??!0,E=_.collisionPadding??u(b,`collisionPadding`)??u(p,`collisionPadding`)??8,D=_.lockScroll??c(p,`lockScroll`)??!0,O=!1,k=ae,A=null,j=-1,M=``,N=null,P=!1,F=0,I=0,L=!1,R=[],z=[],B=[],V=new Map,H=null,U=!1,W=n({content:b,root:p,wrapperSlot:`select-positioner`}),he=!1,G=!0,K=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,ge=o(y,`select-trigger`),_e=o(b,`select-content`);y.setAttribute(`role`,`combobox`),y.setAttribute(`aria-haspopup`,`listbox`),y.setAttribute(`aria-controls`,_e),y.hasAttribute(`type`)||y.setAttribute(`type`,`button`),b.setAttribute(`role`,`listbox`),b.setAttribute(`aria-labelledby`,ge),b.tabIndex=-1;let q=document.querySelector(`label[for="${CSS.escape(ge)}"]`);if(q){let e=o(q,`select-label`),t=y.getAttribute(`aria-labelledby`);y.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),R.push(m(q,`click`,e=>{e.preventDefault(),S||Q(!O)}))}S&&(y.setAttribute(`aria-disabled`,`true`),y.setAttribute(`data-disabled`,``)),ce&&y.setAttribute(`aria-required`,`true`),le&&(H=document.createElement(`input`),H.type=`hidden`,H.name=le,H.value=k??``,p.appendChild(H));let ve=()=>{z=ee(b,`select-item`);for(let e of z)e.setAttribute(`role`,`option`),e.hasAttribute(`data-disabled`)||e.hasAttribute(`disabled`)?e.setAttribute(`aria-disabled`,`true`):e.removeAttribute(`aria-disabled`),e.tabIndex=-1,e.dataset.value===k?(h(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(h(e,`selected`,!1),e.removeAttribute(`data-selected`));B=z.filter(e=>!K(e)),V=new Map(B.map((e,t)=>[e,t]));let e=ee(b,`select-group`);for(let t of e){t.setAttribute(`role`,`group`);let e=f(t,`select-label`);if(e){let n=o(e,`select-label`);t.setAttribute(`aria-labelledby`,n)}}},ye=()=>f(b,`select-viewport`)??b,be=(e,t,n)=>{let r=0,i=e;for(;i&&i!==b;){r+=i.offsetTop;let e=i.offsetParent;if(!(e instanceof HTMLElement)){r=NaN;break}i=e}return i===b&&Number.isFinite(r)?r:e.getBoundingClientRect().top-t.top+n.scrollTop},xe=(e,t,n)=>{let r=j>=0?B[j]:void 0,i=z.find(e=>e.dataset.value===k),a=r??i??B[0],o=e.left,s,c=0,l=e.height;return a?(c=be(a,t,n),l=a.getBoundingClientRect().height||a.offsetHeight||e.height,s=e.top+e.height/2-l/2-c):s=e.top,{x:o,y:s,alignItem:a,itemTopInContent:c,itemHeight:l}},J=()=>{let t=W.container,n=p.ownerDocument.defaultView??window,r=y.getBoundingClientRect(),i=ye();b.style.minWidth=`${r.width}px`;let a=b.getBoundingClientRect(),o,s=`bottom`;if(C===`item-aligned`){let e=xe(r,a,i);o={x:e.x,y:e.y};let t=r.top+r.height/2,c=E,l=n.innerHeight-a.height-E,u=e=>T?l<c?c:Math.min(Math.max(e,c),l):e,d=E,f=n.innerWidth-a.width-E;if(o.x=(e=>T?f<d?d:Math.min(Math.max(e,d),f):e)(o.x),e.alignItem){let n=Math.max(0,i.scrollHeight-i.clientHeight),r=n=>e.itemTopInContent+e.itemHeight/2-(t-n);if(n>0){o.y=u(t-a.height/2);let s=Math.min(Math.max(r(o.y),0),n);i.scrollTop=s,o.y=u(t-(e.itemTopInContent-s+e.itemHeight/2)),s=Math.min(Math.max(r(o.y),0),n),i.scrollTop=s,o.y=u(t-(e.itemTopInContent-s+e.itemHeight/2))}else i.scrollTop=0,o.y=u(e.y)}else i.scrollTop=0,o.y=u(e.y);s=o.y<r.top?`top`:`bottom`}else{let t=e({anchorRect:r,contentRect:a,side:fe,align:w,sideOffset:pe,alignOffset:me,avoidCollisions:T,collisionPadding:E,allowedSides:g});o={x:t.x,y:t.y},s=t.side}D?(t.style.position=`fixed`,t.style.top=`0px`,t.style.left=`0px`,t.style.transform=`translate3d(${o.x}px, ${o.y}px, 0)`):(t.style.position=`absolute`,t.style.top=`0px`,t.style.left=`0px`,t.style.transform=`translate3d(${o.x+n.scrollX}px, ${o.y+n.scrollY}px, 0)`),t.style.willChange=`transform`,t.style.margin=`0`,b.setAttribute(`data-side`,s),b.setAttribute(`data-align`,C===`item-aligned`?`center`:w),t!==b&&(t.setAttribute(`data-side`,s),t.setAttribute(`data-align`,C===`item-aligned`?`center`:w))},Y=r({observedElements:[y,b],isActive:()=>O,ancestorScroll:D,onUpdate:J,ignoreScrollTarget:e=>e instanceof Node&&b.contains(e)}),X=(e,t=!0,n=!0)=>{let r=ye();for(let i=0;i<B.length;i++){let a=B[i];i===e?(a.setAttribute(`data-highlighted`,``),n&&s(a,r),t&&a.focus()):a.removeAttribute(`data-highlighted`)}j=e},Z=()=>{for(let e of z)e.removeAttribute(`data-highlighted`);j=-1},Se=e=>{p.setAttribute(`data-state`,e),y.setAttribute(`data-state`,e),b.setAttribute(`data-state`,e),e===`open`?(p.setAttribute(`data-open`,``),y.setAttribute(`data-open`,``),b.setAttribute(`data-open`,``),p.removeAttribute(`data-closed`),y.removeAttribute(`data-closed`),b.removeAttribute(`data-closed`)):(p.setAttribute(`data-closed`,``),y.setAttribute(`data-closed`,``),b.setAttribute(`data-closed`,``),p.removeAttribute(`data-open`),y.removeAttribute(`data-open`),b.removeAttribute(`data-open`))},Ce=()=>{requestAnimationFrame(()=>{A&&document.contains(A)?A.focus():y&&document.contains(y)&&y.focus(),A=null})},we=i({element:b,onExitComplete:()=>{he||(W.restore(),b.hidden=!0,G?Ce():A=null)}}),Te=()=>{if(x)if(k===null)x.textContent=se,y.setAttribute(`data-placeholder`,``);else{let e=z.find(e=>e.dataset.value===k);x.textContent=e?.dataset.label??e?.textContent?.trim()??k,y.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(O!==e&&!(S&&e)){if(e){let e=L;L=!1,G=!0,A=document.activeElement,O=!0,h(y,`expanded`,!0),W.mount(),b.hidden=!1,Se(`open`),we.enter(),D&&!U&&(te(),U=!0),ve(),P=!1;let t=B.findIndex(e=>e.dataset.value===k);t>=0?X(t,!1,!1):Z(),Y.start(),J(),Y.update(),requestAnimationFrame(()=>{if(O&&(J(),Y.update(),e&&(F!==0||I!==0))){let e=document.elementFromPoint(F,I)?.closest?.(`[data-slot="select-item"]`);if(e&&!K(e)&&b.contains(e)){let t=V.get(e);t!==void 0&&X(t,!1,!1)}}}),b.focus()}else O=!1,L=!1,F=0,I=0,h(y,`expanded`,!1),Se(`closed`),Z(),M=``,P=!1,G=!t,U&&=(ne(),!1),Y.stop(),we.exit();a(p,`select:open-change`,{open:O}),de?.(O)}},$=(e,t=!1)=>{if(k===e&&!t)return;let n=k;k=e,H&&(H.value=e??``),e===null?p.removeAttribute(`data-value`):p.setAttribute(`data-value`,e);for(let t of z)t.dataset.value===e?(h(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(h(t,`selected`,!1),t.removeAttribute(`data-selected`));Te(),!t&&n!==e&&(a(p,`select:change`,{value:e}),ue?.(e))},Ee=e=>{if(K(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},De=e=>{let t=B.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),P=!0,X(j===-1?0:Math.min(j+1,t-1));break;case`ArrowUp`:e.preventDefault(),P=!0,X(j===-1?t-1:Math.max(j-1,0));break;case`Home`:e.preventDefault(),P=!0,X(0);break;case`End`:e.preventDefault(),P=!0,X(t-1);break;case`Enter`:case` `:e.preventDefault(),j>=0&&Ee(B[j]);break;case`Tab`:Q(!1,!0);break;case`Escape`:e.preventDefault(),Q(!1);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),Oe(e.key.toLowerCase()))}},Oe=e=>{N&&clearTimeout(N),N=setTimeout(()=>{M=``},500),M+=e;let t=B.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(M));if(t===-1&&M.length===1){let n=j+1;for(let r=0;r<B.length;r++){let i=(n+r)%B.length;if((B[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(P=!0,X(t))};return h(y,`expanded`,!1),b.hidden=!0,Se(`closed`),ve(),$(k,!0),R.push(m(y,`pointerdown`,e=>{F=e.clientX,I=e.clientY,L=!0}),m(y,`click`,()=>{S||Q(!O)}),m(y,`keydown`,e=>{if(!S)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),L=!1,Q(!0);break}})),R.push(m(b,`keydown`,De),m(b,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&Ee(t)}),m(b,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(P&&(P=!1,t&&V.get(t)===j)))if(t&&!K(t)){let e=V.get(t);e!==void 0&&e!==j&&X(e,!1)}else Z()}),m(b,`pointerleave`,()=>{P||Z()})),R.push(t({root:p,isOpen:()=>O,onDismiss:()=>Q(!1),closeOnClickOutside:!0,closeOnEscape:!1})),R.push(m(p,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)})),oe&&Q(!0),{get value(){return k},get isOpen(){return O},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{he=!0,N&&clearTimeout(N),Y.stop(),we.cleanup(),W.cleanup(),U&&=(ne(),!1),R.forEach(e=>e()),R.length=0,H&&H.parentNode&&H.parentNode.removeChild(H),v.delete(p)}}}const v=new WeakSet;function y(e=document){let t=[];for(let n of p(e,`select`))v.has(n)||(v.add(n),t.push(_(n)));return t}export{y as create,_ as createSelect};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-slot/select",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.47",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -40,6 +40,6 @@
|
|
|
40
40
|
],
|
|
41
41
|
"license": "MIT",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@data-slot/core": "0.2.
|
|
43
|
+
"@data-slot/core": "0.2.47"
|
|
44
44
|
}
|
|
45
45
|
}
|