@data-slot/select 0.2.89 → 0.2.91

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