@data-slot/select 0.2.30 → 0.2.32

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
- const e=(e,t)=>e.querySelector(`[data-slot="${t}"]`),t=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)],n=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)],r=new WeakMap;function i(e,t,n){if(typeof process<`u`&&process.env?.NODE_ENV===`production`)return;let i=r.get(e);i||(i=new Set,r.set(e,i)),!i.has(t)&&(i.add(t),console.warn(`[@data-slot] ${n}`))}function a(e){let t=`data-${e.replace(/([A-Z])/g,`-$1`).toLowerCase()}`,n=`data-${e}`;return t===n?[t]:[t,n]}function o(e,t){for(let n of a(t))if(e.hasAttribute(n))return e.getAttribute(n);return null}function s(e,t){return a(t).some(t=>e.hasAttribute(t))}const ee=new Set([``,`true`,`1`,`yes`]),te=new Set([`false`,`0`,`no`]);function c(e,t){if(!s(e,t))return;let n=o(e,t);if(n===null)return;let r=n.toLowerCase();if(ee.has(r))return!0;if(te.has(r))return!1;i(e,t,`Invalid boolean value "${n}" for data-${t}. Expected: true/false/1/0/yes/no or empty.`)}function l(e,t){let n=o(e,t);if(n===null||n===``)return;let r=Number(n);if(Number.isNaN(r)||!Number.isFinite(r)){i(e,t,`Invalid number value "${n}" for data-${t}.`);return}return r}function u(e,t){if(s(e,t))return o(e,t)??void 0}function d(e,t,n){let r=o(e,t);if(r!==null){if(n.includes(r))return r;i(e,t,`Invalid value "${r}" for data-${t}. Expected one of: ${n.join(`, `)}.`)}}const f=new WeakMap,p=Symbol.for(`data-slot.portal-owner`),m=(e,t)=>{f.set(e,t),e[p]=t},ne=e=>{f.delete(e),delete e[p]};function re(e,t,n){if(n.portaled)return;let r=(t.ownerDocument??document)?.body;r&&(n.originalParent=e.parentNode,n.originalNextSibling=e.nextSibling,m(e,t),r.appendChild(e),n.portaled=!0)}function ie(e,t){if(!t.portaled)return;ne(e);let n=t.originalParent,r=t.originalNextSibling;n&&n.isConnected?r&&r.parentNode===n?n.insertBefore(e,r):n.appendChild(e):e.remove(),t.portaled=!1,t.originalParent=null,t.originalNextSibling=null}let ae=0;const h=(e,t)=>e.id||=`${t}-${++ae}`,g=(e,t,n)=>{n===null?e.removeAttribute(`aria-${t}`):e.setAttribute(`aria-${t}`,String(n))};function _(e,t,n,r){return e.addEventListener(t,n,r),()=>e.removeEventListener(t,n,r)}const v=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));let y=0,b=``,x=``;function oe(){if(y===0){let e=document.documentElement;b=e.style.overflow,x=e.style.scrollbarGutter,e.style.overflow=`hidden`,e.style.scrollbarGutter=`stable`}y++}function S(){if(y=Math.max(0,y-1),y===0){let e=document.documentElement;e.style.overflow=b,e.style.scrollbarGutter=x}}const C=[`top`,`bottom`],se=[`start`,`center`,`end`],ce=[`item-aligned`,`popper`],le={top:`bottom`,bottom:`top`};function w(n,r={}){let i=e(n,`select-trigger`),a=e(n,`select-content`),o=e(n,`select-value`);if(!i||!a)throw Error(`Select requires trigger and content slots`);let s=r.defaultValue??u(n,`defaultValue`)??null,ee=r.defaultOpen??c(n,`defaultOpen`)??!1,te=r.placeholder??u(n,`placeholder`)??(o?u(o,`placeholder`):void 0)??``,f=r.disabled??c(n,`disabled`)??!1,p=r.required??c(n,`required`)??!1,m=r.name??u(n,`name`)??null,ne=r.onValueChange,ae=r.onOpenChange,y=r.position??d(n,`position`,ce)??`item-aligned`,b=r.side??d(a,`side`,C)??d(n,`side`,C)??`bottom`,x=r.align??d(a,`align`,se)??d(n,`align`,se)??`start`,w=r.sideOffset??l(a,`sideOffset`)??l(n,`sideOffset`)??4,E=r.alignOffset??l(a,`alignOffset`)??l(n,`alignOffset`)??0,ue=r.avoidCollisions??c(a,`avoidCollisions`)??c(n,`avoidCollisions`)??!0,D=r.collisionPadding??l(a,`collisionPadding`)??l(n,`collisionPadding`)??8,de=r.lockScroll??c(n,`lockScroll`)??!0,O=!1,k=s,A=null,j=-1,M=``,N=null,P=!1,F=0,I=0,L=[],R=[],z=[],B=new Map,V=null,H=[],U=null,W=!1,G={originalParent:null,originalNextSibling:null,portaled:!1},K=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,fe=h(i,`select-trigger`),pe=h(a,`select-content`);i.setAttribute(`role`,`combobox`),i.setAttribute(`aria-haspopup`,`listbox`),i.setAttribute(`aria-controls`,pe),i.hasAttribute(`type`)||i.setAttribute(`type`,`button`),a.setAttribute(`role`,`listbox`),a.setAttribute(`aria-labelledby`,fe),a.tabIndex=-1;let q=document.querySelector(`label[for="${CSS.escape(fe)}"]`);if(q){let e=h(q,`select-label`),t=i.getAttribute(`aria-labelledby`);i.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),L.push(_(q,`click`,e=>{e.preventDefault(),f||Q(!O)}))}f&&(i.setAttribute(`aria-disabled`,`true`),i.setAttribute(`data-disabled`,``)),p&&i.setAttribute(`aria-required`,`true`),m&&(U=document.createElement(`input`),U.type=`hidden`,U.name=m,U.value=k??``,n.appendChild(U));let me=()=>{R=t(a,`select-item`);for(let e of R){e.setAttribute(`role`,`option`),e.hasAttribute(`data-disabled`)||e.hasAttribute(`disabled`)?e.setAttribute(`aria-disabled`,`true`):e.removeAttribute(`aria-disabled`),e.tabIndex=-1;let t=e.dataset.value;t===k?(g(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(g(e,`selected`,!1),e.removeAttribute(`data-selected`))}z=R.filter(e=>!K(e)),B=new Map(z.map((e,t)=>[e,t]));let n=t(a,`select-group`);for(let t of n){t.setAttribute(`role`,`group`);let n=e(t,`select-label`);if(n){let e=h(n,`select-label`);t.setAttribute(`aria-labelledby`,e)}}},he=(e,t,n,r)=>{let i=0,a=0;return a=e===`top`?n.top-r.height-w:n.bottom+w,i=t===`start`?n.left+E:t===`center`?n.left+n.width/2-r.width/2+E:n.right-r.width-E,{x:i,y:a}},ge=(e,t)=>{let n=R.find(e=>e.dataset.value===k),r=n??z[0],i=e.left,a,o=0;if(r){let n=r.getBoundingClientRect();o=n.top-t.top,a=e.top+e.height/2-n.height/2-o}else a=e.top;return{x:i,y:a,itemOffsetTop:o}},J=()=>{let e=i.getBoundingClientRect(),t=window.innerWidth,n=window.innerHeight;a.style.minWidth=`${e.width}px`;let r=a.getBoundingClientRect(),o,s=`bottom`;if(y===`item-aligned`){let i=ge(e,r);o={x:i.x,y:i.y},ue&&(o.y<D?o.y=D:o.y+r.height>n-D&&(o.y=n-r.height-D),o.x<D?o.x=D:o.x+r.width>t-D&&(o.x=t-r.width-D)),s=o.y<e.top?`top`:`bottom`}else if(s=b,o=he(s,x,e,r),ue){let i=(e,t)=>e===`top`?t.y<D:t.y+r.height>n-D;if(i(s,o)){let t=le[s],n=he(t,x,e,r);i(t,n)||(s=t,o=n)}o.x<D?o.x=D:o.x+r.width>t-D&&(o.x=t-r.width-D),o.y<D?o.y=D:o.y+r.height>n-D&&(o.y=n-r.height-D)}a.style.position=`fixed`,a.style.top=`${o.y}px`,a.style.left=`${o.x}px`,a.style.margin=`0`,a.setAttribute(`data-side`,s),a.setAttribute(`data-align`,y===`item-aligned`?`center`:x)},_e=()=>{V===null&&(V=requestAnimationFrame(()=>{V=null,O&&J()}))},ve=()=>{V!==null&&(cancelAnimationFrame(V),V=null),H.forEach(e=>e()),H.length=0},ye=()=>{if(H.length>0)return;let e=()=>_e(),t=e=>{e.target instanceof Node&&a.contains(e.target)||_e()};window.addEventListener(`resize`,e),window.addEventListener(`scroll`,t,!0),H.push(()=>window.removeEventListener(`resize`,e),()=>window.removeEventListener(`scroll`,t,!0));let n=new ResizeObserver(e);n.observe(i),n.observe(a),H.push(()=>n.disconnect())},Y=(e,t=!0)=>{for(let n=0;n<z.length;n++){let r=z[n];n===e?(r.setAttribute(`data-highlighted`,``),t&&r.focus()):r.removeAttribute(`data-highlighted`)}j=e},X=()=>{for(let e of R)e.removeAttribute(`data-highlighted`);j=-1},Z=e=>{n.setAttribute(`data-state`,e),i.setAttribute(`data-state`,e),a.setAttribute(`data-state`,e)},be=()=>{if(o)if(k===null)o.textContent=te,i.setAttribute(`data-placeholder`,``);else{let e=R.find(e=>e.dataset.value===k),t=e?.dataset.label??e?.textContent?.trim()??k;o.textContent=t,i.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(O!==e&&!(f&&e)){if(e){A=document.activeElement,O=!0,g(i,`expanded`,!0),re(a,n,G),a.hidden=!1,Z(`open`),de&&!W&&(oe(),W=!0),me(),P=!1;let e=z.findIndex(e=>e.dataset.value===k);e>=0?Y(e,!1):X(),ye(),J(),requestAnimationFrame(()=>{if(O&&(J(),F!==0||I!==0)){let e=document.elementFromPoint(F,I),t=e?.closest?.(`[data-slot="select-item"]`);if(t&&!K(t)&&a.contains(t)){let e=B.get(t);e!==void 0&&Y(e,!1)}}}),a.focus()}else O=!1,g(i,`expanded`,!1),ie(a,G),a.hidden=!0,Z(`closed`),X(),M=``,P=!1,W&&(S(),W=!1),ve(),t?A=null:requestAnimationFrame(()=>{A&&document.contains(A)?A.focus():i&&document.contains(i)&&i.focus(),A=null});v(n,`select:open-change`,{open:O}),ae?.(O)}},$=(e,t=!1)=>{if(k===e&&!t)return;let r=k;k=e,U&&(U.value=e??``),e===null?n.removeAttribute(`data-value`):n.setAttribute(`data-value`,e);for(let t of R){let n=t.dataset.value;n===e?(g(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(g(t,`selected`,!1),t.removeAttribute(`data-selected`))}be(),!t&&r!==e&&(v(n,`select:change`,{value:e}),ne?.(e))},xe=e=>{if(K(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},Se=e=>{let t=z.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),P=!0,Y(j===-1?0:(j+1)%t);break;case`ArrowUp`:e.preventDefault(),P=!0,Y(j===-1?t-1:(j-1+t)%t);break;case`Home`:e.preventDefault(),P=!0,Y(0);break;case`End`:e.preventDefault(),P=!0,Y(t-1);break;case`Enter`:case` `:e.preventDefault(),j>=0&&xe(z[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(),Ce(e.key.toLowerCase()))}},Ce=e=>{N&&clearTimeout(N),N=setTimeout(()=>{M=``},500),M+=e;let t=z.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(M));if(t===-1&&M.length===1){let n=j+1;for(let r=0;r<z.length;r++){let i=(n+r)%z.length;if((z[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(P=!0,Y(t))},we=e=>{if(!f)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),Q(!0);break}};g(i,`expanded`,!1),a.hidden=!0,Z(`closed`),me(),$(k,!0),L.push(_(i,`pointerdown`,e=>{F=e.clientX,I=e.clientY}),_(i,`click`,()=>{f||Q(!O)}),_(i,`keydown`,we)),L.push(_(a,`keydown`,Se),_(a,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&xe(t)}),_(a,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(P&&(P=!1,t&&B.get(t)===j)))if(t&&!K(t)){let e=B.get(t);e!==void 0&&e!==j&&Y(e,!1)}else X()}),_(a,`pointerleave`,()=>{P||X()})),L.push(_(document,`pointerdown`,e=>{let t=e.target;O&&!n.contains(t)&&!a.contains(t)&&Q(!1)})),L.push(_(n,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let Te={get value(){return k},get isOpen(){return O},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{N&&clearTimeout(N),ve(),ie(a,G),W&&(S(),W=!1),L.forEach(e=>e()),L.length=0,U&&U.parentNode&&U.parentNode.removeChild(U),T.delete(n)}};return ee&&Q(!0),Te}const T=new WeakSet;function E(e=document){let t=[];for(let r of n(e,`select`)){if(T.has(r))continue;T.add(r),t.push(w(r))}return t}exports.create=E,exports.createSelect=w;
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,ee=o.defaultOpen??(0,e.getDataBool)(i,`defaultOpen`)??!1,d=o.placeholder??(0,e.getDataString)(i,`placeholder`)??(l?(0,e.getDataString)(l,`placeholder`):void 0)??``,f=o.disabled??(0,e.getDataBool)(i,`disabled`)??!1,p=o.required??(0,e.getDataBool)(i,`required`)??!1,m=o.name??(0,e.getDataString)(i,`name`)??null,h=o.onValueChange,g=o.onOpenChange,_=o.position??(0,e.getDataEnum)(i,`position`,r)??`item-aligned`,v=o.side??(0,e.getDataEnum)(c,`side`,t)??(0,e.getDataEnum)(i,`side`,t)??`bottom`,y=o.align??(0,e.getDataEnum)(c,`align`,n)??(0,e.getDataEnum)(i,`align`,n)??`start`,te=o.sideOffset??(0,e.getDataNumber)(c,`sideOffset`)??(0,e.getDataNumber)(i,`sideOffset`)??4,b=o.alignOffset??(0,e.getDataNumber)(c,`alignOffset`)??(0,e.getDataNumber)(i,`alignOffset`)??0,x=o.avoidCollisions??(0,e.getDataBool)(c,`avoidCollisions`)??(0,e.getDataBool)(i,`avoidCollisions`)??!0,S=o.collisionPadding??(0,e.getDataNumber)(c,`collisionPadding`)??(0,e.getDataNumber)(i,`collisionPadding`)??8,C=o.lockScroll??(0,e.getDataBool)(i,`lockScroll`)??!0,w=!1,T=u,E=null,D=-1,O=``,k=null,A=!1,j=0,M=0,N=[],P=[],F=[],I=new Map,L=null,R=!1,z=(0,e.createPortalLifecycle)({content:c,root:i}),B=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,V=(0,e.ensureId)(s,`select-trigger`),H=(0,e.ensureId)(c,`select-content`);s.setAttribute(`role`,`combobox`),s.setAttribute(`aria-haspopup`,`listbox`),s.setAttribute(`aria-controls`,H),s.hasAttribute(`type`)||s.setAttribute(`type`,`button`),c.setAttribute(`role`,`listbox`),c.setAttribute(`aria-labelledby`,V),c.tabIndex=-1;let U=document.querySelector(`label[for="${CSS.escape(V)}"]`);if(U){let t=(0,e.ensureId)(U,`select-label`),n=s.getAttribute(`aria-labelledby`);s.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),N.push((0,e.on)(U,`click`,e=>{e.preventDefault(),f||Z(!w)}))}f&&(s.setAttribute(`aria-disabled`,`true`),s.setAttribute(`data-disabled`,``)),p&&s.setAttribute(`aria-required`,`true`),m&&(L=document.createElement(`input`),L.type=`hidden`,L.name=m,L.value=T??``,i.appendChild(L));let W=()=>{P=(0,e.getParts)(c,`select-item`);for(let t of P)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===T?((0,e.setAria)(t,`selected`,!0),t.setAttribute(`data-selected`,``)):((0,e.setAria)(t,`selected`,!1),t.removeAttribute(`data-selected`));F=P.filter(e=>!B(e)),I=new Map(F.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)}}},G=(e,t)=>{let n=P.find(e=>e.dataset.value===T)??F[0],r=e.left,i,a=0,o=e.height;if(n){let r=n.getBoundingClientRect();a=r.top-t.top+c.scrollTop,o=r.height||n.offsetHeight||e.height,i=e.top+e.height/2-o/2-a}else i=e.top;return{x:r,y:i,alignItem:n,itemTopInContent:a,itemHeight:o}},K=()=>{let n=i.ownerDocument.defaultView??window,r=s.getBoundingClientRect();c.style.minWidth=`${r.width}px`;let a=c.getBoundingClientRect(),o,l=`bottom`;if(_===`item-aligned`){let e=G(r,a);o={x:e.x,y:e.y};let t=r.top+r.height/2,n=S,i=window.innerHeight-a.height-S,s=e=>x?i<n?n:Math.min(Math.max(e,n),i):e;if(x){let e=window.innerWidth,t=window.innerHeight;o.y<S?o.y=S:o.y+a.height>t-S&&(o.y=t-a.height-S),o.x<S?o.x=S:o.x+a.width>e-S&&(o.x=e-a.width-S)}if(e.alignItem){let n=Math.max(0,c.scrollHeight-c.clientHeight);if(n>0){let r=n=>e.itemTopInContent+e.itemHeight/2-(t-n);o.y=s(t-a.height/2);let i=Math.min(Math.max(r(o.y),0),n);c.scrollTop=i,o.y=s(t-(e.itemTopInContent-i+e.itemHeight/2)),i=Math.min(Math.max(r(o.y),0),n),c.scrollTop=i}}else c.scrollTop=0;l=o.y<r.top?`top`:`bottom`}else{let n=(0,e.computeFloatingPosition)({anchorRect:r,contentRect:a,side:v,align:y,sideOffset:te,alignOffset:b,avoidCollisions:x,collisionPadding:S,allowedSides:t});o={x:n.x,y:n.y},l=n.side}C?(c.style.position=`fixed`,c.style.top=`${o.y}px`,c.style.left=`${o.x}px`):(c.style.position=`absolute`,c.style.top=`${o.y+n.scrollY}px`,c.style.left=`${o.x+n.scrollX}px`),c.style.margin=`0`,c.setAttribute(`data-side`,l),c.setAttribute(`data-align`,_===`item-aligned`?`center`:y)},q=(0,e.createPositionSync)({observedElements:[s,c],isActive:()=>w,ancestorScroll:C,onUpdate:K,ignoreScrollTarget:e=>e instanceof Node&&c.contains(e)}),J=(t,n=!0,r=!0)=>{for(let i=0;i<F.length;i++){let a=F[i];i===t?(a.setAttribute(`data-highlighted`,``),r&&(0,e.ensureItemVisibleInContainer)(a,c),n&&a.focus()):a.removeAttribute(`data-highlighted`)}D=t},Y=()=>{for(let e of P)e.removeAttribute(`data-highlighted`);D=-1},X=e=>{i.setAttribute(`data-state`,e),s.setAttribute(`data-state`,e),c.setAttribute(`data-state`,e)},ne=()=>{if(l)if(T===null)l.textContent=d,s.setAttribute(`data-placeholder`,``);else{let e=P.find(e=>e.dataset.value===T);l.textContent=e?.dataset.label??e?.textContent?.trim()??T,s.removeAttribute(`data-placeholder`)}},Z=(t,n=!1)=>{if(w!==t&&!(f&&t)){if(t){E=document.activeElement,w=!0,(0,e.setAria)(s,`expanded`,!0),z.mount(),c.hidden=!1,X(`open`),C&&!R&&((0,e.lockScroll)(),R=!0),W(),A=!1;let t=F.findIndex(e=>e.dataset.value===T);t>=0?J(t,!1,!1):Y(),q.start(),K(),q.update(),requestAnimationFrame(()=>{if(w&&(q.update(),j!==0||M!==0)){let e=document.elementFromPoint(j,M)?.closest?.(`[data-slot="select-item"]`);if(e&&!B(e)&&c.contains(e)){let t=I.get(e);t!==void 0&&J(t,!1)}}}),c.focus()}else w=!1,(0,e.setAria)(s,`expanded`,!1),z.restore(),c.hidden=!0,X(`closed`),Y(),O=``,A=!1,R&&=((0,e.unlockScroll)(),!1),q.stop(),n?E=null:requestAnimationFrame(()=>{E&&document.contains(E)?E.focus():s&&document.contains(s)&&s.focus(),E=null});(0,e.emit)(i,`select:open-change`,{open:w}),g?.(w)}},Q=(t,n=!1)=>{if(T===t&&!n)return;let r=T;T=t,L&&(L.value=t??``),t===null?i.removeAttribute(`data-value`):i.setAttribute(`data-value`,t);for(let n of P)n.dataset.value===t?((0,e.setAria)(n,`selected`,!0),n.setAttribute(`data-selected`,``)):((0,e.setAria)(n,`selected`,!1),n.removeAttribute(`data-selected`));ne(),!n&&r!==t&&((0,e.emit)(i,`select:change`,{value:t}),h?.(t))},$=e=>{if(B(e))return;let t=e.dataset.value;t!==void 0&&(Q(t),Z(!1))},re=e=>{let t=F.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),A=!0,J(D===-1?0:Math.min(D+1,t-1));break;case`ArrowUp`:e.preventDefault(),A=!0,J(D===-1?t-1:Math.max(D-1,0));break;case`Home`:e.preventDefault(),A=!0,J(0);break;case`End`:e.preventDefault(),A=!0,J(t-1);break;case`Enter`:case` `:e.preventDefault(),D>=0&&$(F[D]);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(),ie(e.key.toLowerCase()))}},ie=e=>{k&&clearTimeout(k),k=setTimeout(()=>{O=``},500),O+=e;let t=F.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(O));if(t===-1&&O.length===1){let n=D+1;for(let r=0;r<F.length;r++){let i=(n+r)%F.length;if((F[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(A=!0,J(t))};return(0,e.setAria)(s,`expanded`,!1),c.hidden=!0,X(`closed`),W(),Q(T,!0),N.push((0,e.on)(s,`pointerdown`,e=>{j=e.clientX,M=e.clientY}),(0,e.on)(s,`click`,()=>{f||Z(!w)}),(0,e.on)(s,`keydown`,e=>{if(!f)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),Z(!0);break}})),N.push((0,e.on)(c,`keydown`,re),(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(!(A&&(A=!1,t&&I.get(t)===D)))if(t&&!B(t)){let e=I.get(t);e!==void 0&&e!==D&&J(e,!1)}else Y()}),(0,e.on)(c,`pointerleave`,()=>{A||Y()})),N.push((0,e.createDismissLayer)({root:i,isOpen:()=>w,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),N.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)})),ee&&Z(!0),{get value(){return T},get isOpen(){return w},select:e=>Q(e),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{k&&clearTimeout(k),q.stop(),z.cleanup(),R&&=((0,e.unlockScroll)(),!1),N.forEach(e=>e()),N.length=0,L&&L.parentNode&&L.parentNode.removeChild(L),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
- const e=(e,t)=>e.querySelector(`[data-slot="${t}"]`),t=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)],n=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)],r=new WeakMap;function i(e,t,n){if(typeof process<`u`&&process.env?.NODE_ENV===`production`)return;let i=r.get(e);i||(i=new Set,r.set(e,i)),!i.has(t)&&(i.add(t),console.warn(`[@data-slot] ${n}`))}function a(e){let t=`data-${e.replace(/([A-Z])/g,`-$1`).toLowerCase()}`,n=`data-${e}`;return t===n?[t]:[t,n]}function o(e,t){for(let n of a(t))if(e.hasAttribute(n))return e.getAttribute(n);return null}function s(e,t){return a(t).some(t=>e.hasAttribute(t))}const ee=new Set([``,`true`,`1`,`yes`]),te=new Set([`false`,`0`,`no`]);function c(e,t){if(!s(e,t))return;let n=o(e,t);if(n===null)return;let r=n.toLowerCase();if(ee.has(r))return!0;if(te.has(r))return!1;i(e,t,`Invalid boolean value "${n}" for data-${t}. Expected: true/false/1/0/yes/no or empty.`)}function l(e,t){let n=o(e,t);if(n===null||n===``)return;let r=Number(n);if(Number.isNaN(r)||!Number.isFinite(r)){i(e,t,`Invalid number value "${n}" for data-${t}.`);return}return r}function u(e,t){if(s(e,t))return o(e,t)??void 0}function d(e,t,n){let r=o(e,t);if(r!==null){if(n.includes(r))return r;i(e,t,`Invalid value "${r}" for data-${t}. Expected one of: ${n.join(`, `)}.`)}}const f=new WeakMap,p=Symbol.for(`data-slot.portal-owner`),m=(e,t)=>{f.set(e,t),e[p]=t},ne=e=>{f.delete(e),delete e[p]};function re(e,t,n){if(n.portaled)return;let r=(t.ownerDocument??document)?.body;r&&(n.originalParent=e.parentNode,n.originalNextSibling=e.nextSibling,m(e,t),r.appendChild(e),n.portaled=!0)}function ie(e,t){if(!t.portaled)return;ne(e);let n=t.originalParent,r=t.originalNextSibling;n&&n.isConnected?r&&r.parentNode===n?n.insertBefore(e,r):n.appendChild(e):e.remove(),t.portaled=!1,t.originalParent=null,t.originalNextSibling=null}let ae=0;const h=(e,t)=>e.id||=`${t}-${++ae}`,g=(e,t,n)=>{n===null?e.removeAttribute(`aria-${t}`):e.setAttribute(`aria-${t}`,String(n))};function _(e,t,n,r){return e.addEventListener(t,n,r),()=>e.removeEventListener(t,n,r)}const v=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));let y=0,b=``,x=``;function oe(){if(y===0){let e=document.documentElement;b=e.style.overflow,x=e.style.scrollbarGutter,e.style.overflow=`hidden`,e.style.scrollbarGutter=`stable`}y++}function S(){if(y=Math.max(0,y-1),y===0){let e=document.documentElement;e.style.overflow=b,e.style.scrollbarGutter=x}}const C=[`top`,`bottom`],se=[`start`,`center`,`end`],ce=[`item-aligned`,`popper`],le={top:`bottom`,bottom:`top`};function w(n,r={}){let i=e(n,`select-trigger`),a=e(n,`select-content`),o=e(n,`select-value`);if(!i||!a)throw Error(`Select requires trigger and content slots`);let s=r.defaultValue??u(n,`defaultValue`)??null,ee=r.defaultOpen??c(n,`defaultOpen`)??!1,te=r.placeholder??u(n,`placeholder`)??(o?u(o,`placeholder`):void 0)??``,f=r.disabled??c(n,`disabled`)??!1,p=r.required??c(n,`required`)??!1,m=r.name??u(n,`name`)??null,ne=r.onValueChange,ae=r.onOpenChange,y=r.position??d(n,`position`,ce)??`item-aligned`,b=r.side??d(a,`side`,C)??d(n,`side`,C)??`bottom`,x=r.align??d(a,`align`,se)??d(n,`align`,se)??`start`,w=r.sideOffset??l(a,`sideOffset`)??l(n,`sideOffset`)??4,E=r.alignOffset??l(a,`alignOffset`)??l(n,`alignOffset`)??0,ue=r.avoidCollisions??c(a,`avoidCollisions`)??c(n,`avoidCollisions`)??!0,D=r.collisionPadding??l(a,`collisionPadding`)??l(n,`collisionPadding`)??8,de=r.lockScroll??c(n,`lockScroll`)??!0,O=!1,k=s,A=null,j=-1,M=``,N=null,P=!1,F=0,I=0,L=[],R=[],z=[],B=new Map,V=null,H=[],U=null,W=!1,G={originalParent:null,originalNextSibling:null,portaled:!1},K=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,fe=h(i,`select-trigger`),pe=h(a,`select-content`);i.setAttribute(`role`,`combobox`),i.setAttribute(`aria-haspopup`,`listbox`),i.setAttribute(`aria-controls`,pe),i.hasAttribute(`type`)||i.setAttribute(`type`,`button`),a.setAttribute(`role`,`listbox`),a.setAttribute(`aria-labelledby`,fe),a.tabIndex=-1;let q=document.querySelector(`label[for="${CSS.escape(fe)}"]`);if(q){let e=h(q,`select-label`),t=i.getAttribute(`aria-labelledby`);i.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),L.push(_(q,`click`,e=>{e.preventDefault(),f||Q(!O)}))}f&&(i.setAttribute(`aria-disabled`,`true`),i.setAttribute(`data-disabled`,``)),p&&i.setAttribute(`aria-required`,`true`),m&&(U=document.createElement(`input`),U.type=`hidden`,U.name=m,U.value=k??``,n.appendChild(U));let me=()=>{R=t(a,`select-item`);for(let e of R){e.setAttribute(`role`,`option`),e.hasAttribute(`data-disabled`)||e.hasAttribute(`disabled`)?e.setAttribute(`aria-disabled`,`true`):e.removeAttribute(`aria-disabled`),e.tabIndex=-1;let t=e.dataset.value;t===k?(g(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(g(e,`selected`,!1),e.removeAttribute(`data-selected`))}z=R.filter(e=>!K(e)),B=new Map(z.map((e,t)=>[e,t]));let n=t(a,`select-group`);for(let t of n){t.setAttribute(`role`,`group`);let n=e(t,`select-label`);if(n){let e=h(n,`select-label`);t.setAttribute(`aria-labelledby`,e)}}},he=(e,t,n,r)=>{let i=0,a=0;return a=e===`top`?n.top-r.height-w:n.bottom+w,i=t===`start`?n.left+E:t===`center`?n.left+n.width/2-r.width/2+E:n.right-r.width-E,{x:i,y:a}},ge=(e,t)=>{let n=R.find(e=>e.dataset.value===k),r=n??z[0],i=e.left,a,o=0;if(r){let n=r.getBoundingClientRect();o=n.top-t.top,a=e.top+e.height/2-n.height/2-o}else a=e.top;return{x:i,y:a,itemOffsetTop:o}},J=()=>{let e=i.getBoundingClientRect(),t=window.innerWidth,n=window.innerHeight;a.style.minWidth=`${e.width}px`;let r=a.getBoundingClientRect(),o,s=`bottom`;if(y===`item-aligned`){let i=ge(e,r);o={x:i.x,y:i.y},ue&&(o.y<D?o.y=D:o.y+r.height>n-D&&(o.y=n-r.height-D),o.x<D?o.x=D:o.x+r.width>t-D&&(o.x=t-r.width-D)),s=o.y<e.top?`top`:`bottom`}else if(s=b,o=he(s,x,e,r),ue){let i=(e,t)=>e===`top`?t.y<D:t.y+r.height>n-D;if(i(s,o)){let t=le[s],n=he(t,x,e,r);i(t,n)||(s=t,o=n)}o.x<D?o.x=D:o.x+r.width>t-D&&(o.x=t-r.width-D),o.y<D?o.y=D:o.y+r.height>n-D&&(o.y=n-r.height-D)}a.style.position=`fixed`,a.style.top=`${o.y}px`,a.style.left=`${o.x}px`,a.style.margin=`0`,a.setAttribute(`data-side`,s),a.setAttribute(`data-align`,y===`item-aligned`?`center`:x)},_e=()=>{V===null&&(V=requestAnimationFrame(()=>{V=null,O&&J()}))},ve=()=>{V!==null&&(cancelAnimationFrame(V),V=null),H.forEach(e=>e()),H.length=0},ye=()=>{if(H.length>0)return;let e=()=>_e(),t=e=>{e.target instanceof Node&&a.contains(e.target)||_e()};window.addEventListener(`resize`,e),window.addEventListener(`scroll`,t,!0),H.push(()=>window.removeEventListener(`resize`,e),()=>window.removeEventListener(`scroll`,t,!0));let n=new ResizeObserver(e);n.observe(i),n.observe(a),H.push(()=>n.disconnect())},Y=(e,t=!0)=>{for(let n=0;n<z.length;n++){let r=z[n];n===e?(r.setAttribute(`data-highlighted`,``),t&&r.focus()):r.removeAttribute(`data-highlighted`)}j=e},X=()=>{for(let e of R)e.removeAttribute(`data-highlighted`);j=-1},Z=e=>{n.setAttribute(`data-state`,e),i.setAttribute(`data-state`,e),a.setAttribute(`data-state`,e)},be=()=>{if(o)if(k===null)o.textContent=te,i.setAttribute(`data-placeholder`,``);else{let e=R.find(e=>e.dataset.value===k),t=e?.dataset.label??e?.textContent?.trim()??k;o.textContent=t,i.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(O!==e&&!(f&&e)){if(e){A=document.activeElement,O=!0,g(i,`expanded`,!0),re(a,n,G),a.hidden=!1,Z(`open`),de&&!W&&(oe(),W=!0),me(),P=!1;let e=z.findIndex(e=>e.dataset.value===k);e>=0?Y(e,!1):X(),ye(),J(),requestAnimationFrame(()=>{if(O&&(J(),F!==0||I!==0)){let e=document.elementFromPoint(F,I),t=e?.closest?.(`[data-slot="select-item"]`);if(t&&!K(t)&&a.contains(t)){let e=B.get(t);e!==void 0&&Y(e,!1)}}}),a.focus()}else O=!1,g(i,`expanded`,!1),ie(a,G),a.hidden=!0,Z(`closed`),X(),M=``,P=!1,W&&(S(),W=!1),ve(),t?A=null:requestAnimationFrame(()=>{A&&document.contains(A)?A.focus():i&&document.contains(i)&&i.focus(),A=null});v(n,`select:open-change`,{open:O}),ae?.(O)}},$=(e,t=!1)=>{if(k===e&&!t)return;let r=k;k=e,U&&(U.value=e??``),e===null?n.removeAttribute(`data-value`):n.setAttribute(`data-value`,e);for(let t of R){let n=t.dataset.value;n===e?(g(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(g(t,`selected`,!1),t.removeAttribute(`data-selected`))}be(),!t&&r!==e&&(v(n,`select:change`,{value:e}),ne?.(e))},xe=e=>{if(K(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},Se=e=>{let t=z.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),P=!0,Y(j===-1?0:(j+1)%t);break;case`ArrowUp`:e.preventDefault(),P=!0,Y(j===-1?t-1:(j-1+t)%t);break;case`Home`:e.preventDefault(),P=!0,Y(0);break;case`End`:e.preventDefault(),P=!0,Y(t-1);break;case`Enter`:case` `:e.preventDefault(),j>=0&&xe(z[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(),Ce(e.key.toLowerCase()))}},Ce=e=>{N&&clearTimeout(N),N=setTimeout(()=>{M=``},500),M+=e;let t=z.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(M));if(t===-1&&M.length===1){let n=j+1;for(let r=0;r<z.length;r++){let i=(n+r)%z.length;if((z[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(P=!0,Y(t))},we=e=>{if(!f)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),Q(!0);break}};g(i,`expanded`,!1),a.hidden=!0,Z(`closed`),me(),$(k,!0),L.push(_(i,`pointerdown`,e=>{F=e.clientX,I=e.clientY}),_(i,`click`,()=>{f||Q(!O)}),_(i,`keydown`,we)),L.push(_(a,`keydown`,Se),_(a,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&xe(t)}),_(a,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(P&&(P=!1,t&&B.get(t)===j)))if(t&&!K(t)){let e=B.get(t);e!==void 0&&e!==j&&Y(e,!1)}else X()}),_(a,`pointerleave`,()=>{P||X()})),L.push(_(document,`pointerdown`,e=>{let t=e.target;O&&!n.contains(t)&&!a.contains(t)&&Q(!1)})),L.push(_(n,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let Te={get value(){return k},get isOpen(){return O},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{N&&clearTimeout(N),ve(),ie(a,G),W&&(S(),W=!1),L.forEach(e=>e()),L.length=0,U&&U.parentNode&&U.parentNode.removeChild(U),T.delete(n)}};return ee&&Q(!0),Te}const T=new WeakSet;function E(e=document){let t=[];for(let r of n(e,`select`)){if(T.has(r))continue;T.add(r),t.push(w(r))}return t}export{E as create,w as createSelect};
1
+ import{computeFloatingPosition as e,createDismissLayer as t,createPortalLifecycle as n,createPositionSync as r,emit as i,ensureId as a,ensureItemVisibleInContainer as o,getDataBool as s,getDataEnum as c,getDataNumber as l,getDataString as u,getPart as d,getParts as f,getRoots as p,lockScroll as ee,on as m,setAria as h,unlockScroll as g}from"@data-slot/core";const _=[`top`,`bottom`],v=[`start`,`center`,`end`],te=[`item-aligned`,`popper`];function y(p,y={}){let x=d(p,`select-trigger`),S=d(p,`select-content`),C=d(p,`select-value`);if(!x||!S)throw Error(`Select requires trigger and content slots`);let ne=y.defaultValue??u(p,`defaultValue`)??null,re=y.defaultOpen??s(p,`defaultOpen`)??!1,ie=y.placeholder??u(p,`placeholder`)??(C?u(C,`placeholder`):void 0)??``,w=y.disabled??s(p,`disabled`)??!1,ae=y.required??s(p,`required`)??!1,T=y.name??u(p,`name`)??null,oe=y.onValueChange,se=y.onOpenChange,E=y.position??c(p,`position`,te)??`item-aligned`,ce=y.side??c(S,`side`,_)??c(p,`side`,_)??`bottom`,le=y.align??c(S,`align`,v)??c(p,`align`,v)??`start`,ue=y.sideOffset??l(S,`sideOffset`)??l(p,`sideOffset`)??4,de=y.alignOffset??l(S,`alignOffset`)??l(p,`alignOffset`)??0,D=y.avoidCollisions??s(S,`avoidCollisions`)??s(p,`avoidCollisions`)??!0,O=y.collisionPadding??l(S,`collisionPadding`)??l(p,`collisionPadding`)??8,k=y.lockScroll??s(p,`lockScroll`)??!0,A=!1,j=ne,M=null,N=-1,P=``,F=null,I=!1,L=0,R=0,z=[],B=[],V=[],H=new Map,U=null,W=!1,G=n({content:S,root:p}),K=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,fe=a(x,`select-trigger`),pe=a(S,`select-content`);x.setAttribute(`role`,`combobox`),x.setAttribute(`aria-haspopup`,`listbox`),x.setAttribute(`aria-controls`,pe),x.hasAttribute(`type`)||x.setAttribute(`type`,`button`),S.setAttribute(`role`,`listbox`),S.setAttribute(`aria-labelledby`,fe),S.tabIndex=-1;let q=document.querySelector(`label[for="${CSS.escape(fe)}"]`);if(q){let e=a(q,`select-label`),t=x.getAttribute(`aria-labelledby`);x.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),z.push(m(q,`click`,e=>{e.preventDefault(),w||Q(!A)}))}w&&(x.setAttribute(`aria-disabled`,`true`),x.setAttribute(`data-disabled`,``)),ae&&x.setAttribute(`aria-required`,`true`),T&&(U=document.createElement(`input`),U.type=`hidden`,U.name=T,U.value=j??``,p.appendChild(U));let me=()=>{B=f(S,`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===j?(h(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(h(e,`selected`,!1),e.removeAttribute(`data-selected`));V=B.filter(e=>!K(e)),H=new Map(V.map((e,t)=>[e,t]));let e=f(S,`select-group`);for(let t of e){t.setAttribute(`role`,`group`);let e=d(t,`select-label`);if(e){let n=a(e,`select-label`);t.setAttribute(`aria-labelledby`,n)}}},he=(e,t)=>{let n=B.find(e=>e.dataset.value===j)??V[0],r=e.left,i,a=0,o=e.height;if(n){let r=n.getBoundingClientRect();a=r.top-t.top+S.scrollTop,o=r.height||n.offsetHeight||e.height,i=e.top+e.height/2-o/2-a}else i=e.top;return{x:r,y:i,alignItem:n,itemTopInContent:a,itemHeight:o}},ge=()=>{let t=p.ownerDocument.defaultView??window,n=x.getBoundingClientRect();S.style.minWidth=`${n.width}px`;let r=S.getBoundingClientRect(),i,a=`bottom`;if(E===`item-aligned`){let e=he(n,r);i={x:e.x,y:e.y};let t=n.top+n.height/2,o=O,s=window.innerHeight-r.height-O,c=e=>D?s<o?o:Math.min(Math.max(e,o),s):e;if(D){let e=window.innerWidth,t=window.innerHeight;i.y<O?i.y=O:i.y+r.height>t-O&&(i.y=t-r.height-O),i.x<O?i.x=O:i.x+r.width>e-O&&(i.x=e-r.width-O)}if(e.alignItem){let n=Math.max(0,S.scrollHeight-S.clientHeight);if(n>0){let a=n=>e.itemTopInContent+e.itemHeight/2-(t-n);i.y=c(t-r.height/2);let o=Math.min(Math.max(a(i.y),0),n);S.scrollTop=o,i.y=c(t-(e.itemTopInContent-o+e.itemHeight/2)),o=Math.min(Math.max(a(i.y),0),n),S.scrollTop=o}}else S.scrollTop=0;a=i.y<n.top?`top`:`bottom`}else{let t=e({anchorRect:n,contentRect:r,side:ce,align:le,sideOffset:ue,alignOffset:de,avoidCollisions:D,collisionPadding:O,allowedSides:_});i={x:t.x,y:t.y},a=t.side}k?(S.style.position=`fixed`,S.style.top=`${i.y}px`,S.style.left=`${i.x}px`):(S.style.position=`absolute`,S.style.top=`${i.y+t.scrollY}px`,S.style.left=`${i.x+t.scrollX}px`),S.style.margin=`0`,S.setAttribute(`data-side`,a),S.setAttribute(`data-align`,E===`item-aligned`?`center`:le)},J=r({observedElements:[x,S],isActive:()=>A,ancestorScroll:k,onUpdate:ge,ignoreScrollTarget:e=>e instanceof Node&&S.contains(e)}),Y=(e,t=!0,n=!0)=>{for(let r=0;r<V.length;r++){let i=V[r];r===e?(i.setAttribute(`data-highlighted`,``),n&&o(i,S),t&&i.focus()):i.removeAttribute(`data-highlighted`)}N=e},X=()=>{for(let e of B)e.removeAttribute(`data-highlighted`);N=-1},Z=e=>{p.setAttribute(`data-state`,e),x.setAttribute(`data-state`,e),S.setAttribute(`data-state`,e)},_e=()=>{if(C)if(j===null)C.textContent=ie,x.setAttribute(`data-placeholder`,``);else{let e=B.find(e=>e.dataset.value===j);C.textContent=e?.dataset.label??e?.textContent?.trim()??j,x.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(A!==e&&!(w&&e)){if(e){M=document.activeElement,A=!0,h(x,`expanded`,!0),G.mount(),S.hidden=!1,Z(`open`),k&&!W&&(ee(),W=!0),me(),I=!1;let e=V.findIndex(e=>e.dataset.value===j);e>=0?Y(e,!1,!1):X(),J.start(),ge(),J.update(),requestAnimationFrame(()=>{if(A&&(J.update(),L!==0||R!==0)){let e=document.elementFromPoint(L,R)?.closest?.(`[data-slot="select-item"]`);if(e&&!K(e)&&S.contains(e)){let t=H.get(e);t!==void 0&&Y(t,!1)}}}),S.focus()}else A=!1,h(x,`expanded`,!1),G.restore(),S.hidden=!0,Z(`closed`),X(),P=``,I=!1,W&&=(g(),!1),J.stop(),t?M=null:requestAnimationFrame(()=>{M&&document.contains(M)?M.focus():x&&document.contains(x)&&x.focus(),M=null});i(p,`select:open-change`,{open:A}),se?.(A)}},$=(e,t=!1)=>{if(j===e&&!t)return;let n=j;j=e,U&&(U.value=e??``),e===null?p.removeAttribute(`data-value`):p.setAttribute(`data-value`,e);for(let t of B)t.dataset.value===e?(h(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(h(t,`selected`,!1),t.removeAttribute(`data-selected`));_e(),!t&&n!==e&&(i(p,`select:change`,{value:e}),oe?.(e))},ve=e=>{if(K(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},ye=e=>{let t=V.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),I=!0,Y(N===-1?0:Math.min(N+1,t-1));break;case`ArrowUp`:e.preventDefault(),I=!0,Y(N===-1?t-1:Math.max(N-1,0));break;case`Home`:e.preventDefault(),I=!0,Y(0);break;case`End`:e.preventDefault(),I=!0,Y(t-1);break;case`Enter`:case` `:e.preventDefault(),N>=0&&ve(V[N]);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(),be(e.key.toLowerCase()))}},be=e=>{F&&clearTimeout(F),F=setTimeout(()=>{P=``},500),P+=e;let t=V.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(P));if(t===-1&&P.length===1){let n=N+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&&(I=!0,Y(t))};return h(x,`expanded`,!1),S.hidden=!0,Z(`closed`),me(),$(j,!0),z.push(m(x,`pointerdown`,e=>{L=e.clientX,R=e.clientY}),m(x,`click`,()=>{w||Q(!A)}),m(x,`keydown`,e=>{if(!w)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),Q(!0);break}})),z.push(m(S,`keydown`,ye),m(S,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&ve(t)}),m(S,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(I&&(I=!1,t&&H.get(t)===N)))if(t&&!K(t)){let e=H.get(t);e!==void 0&&e!==N&&Y(e,!1)}else X()}),m(S,`pointerleave`,()=>{I||X()})),z.push(t({root:p,isOpen:()=>A,onDismiss:()=>Q(!1),closeOnClickOutside:!0,closeOnEscape:!1})),z.push(m(p,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)})),re&&Q(!0),{get value(){return j},get isOpen(){return A},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{F&&clearTimeout(F),J.stop(),G.cleanup(),W&&=(g(),!1),z.forEach(e=>e()),z.length=0,U&&U.parentNode&&U.parentNode.removeChild(U),b.delete(p)}}}const b=new WeakSet;function x(e=document){let t=[];for(let n of p(e,`select`))b.has(n)||(b.add(n),t.push(y(n)));return t}export{x as create,y as createSelect};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-slot/select",
3
- "version": "0.2.30",
3
+ "version": "0.2.32",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.cjs",
@@ -24,9 +24,6 @@
24
24
  "scripts": {
25
25
  "build": "tsdown"
26
26
  },
27
- "devDependencies": {
28
- "@data-slot/core": "workspace:*"
29
- },
30
27
  "repository": {
31
28
  "type": "git",
32
29
  "url": "https://github.com/bejamas/data-slot",
@@ -41,5 +38,8 @@
41
38
  "vanilla",
42
39
  "data-slot"
43
40
  ],
44
- "license": "MIT"
41
+ "license": "MIT",
42
+ "dependencies": {
43
+ "@data-slot/core": "workspace:*"
44
+ }
45
45
  }
File without changes
File without changes