@data-slot/select 0.2.21 → 0.2.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -66,9 +66,31 @@ controller.destroy();
66
66
  | `select-content` | Popup container for options |
67
67
  | `select-item` | Individual selectable option |
68
68
  | `select-group` | Groups related items |
69
- | `select-label` | Label for a group |
69
+ | `select-label` | Field label (outside content) or group label (inside a group) |
70
70
  | `select-separator` | Visual divider between items/groups |
71
71
 
72
+ ### Field Label vs Group Label
73
+
74
+ The `select-label` slot serves two purposes depending on where it appears:
75
+
76
+ - **Field label** (outside `select-content`): Acts as the accessible label for the entire select. Linked to the trigger via `aria-labelledby`. If using a `<label>` element, `htmlFor` is set automatically. For non-label elements, clicking the label focuses the trigger.
77
+ - **Group label** (inside a `select-group`): Labels a group of items, linked to the group via `aria-labelledby`.
78
+
79
+ ```html
80
+ <div data-slot="select">
81
+ <label data-slot="select-label">Choose a fruit</label> <!-- field label -->
82
+ <button data-slot="select-trigger">
83
+ <span data-slot="select-value"></span>
84
+ </button>
85
+ <div data-slot="select-content" hidden>
86
+ <div data-slot="select-group">
87
+ <div data-slot="select-label">Fruits</div> <!-- group label -->
88
+ <div data-slot="select-item" data-value="apple">Apple</div>
89
+ </div>
90
+ </div>
91
+ </div>
92
+ ```
93
+
72
94
  ## Options
73
95
 
74
96
  Options can be passed via JavaScript or data attributes (JS takes precedence).
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 c=new Set([``,`true`,`1`,`yes`]),l=new Set([`false`,`0`,`no`]);function u(e,t){if(!s(e,t))return;let n=o(e,t);if(n===null)return;let r=n.toLowerCase();if(c.has(r))return!0;if(l.has(r))return!1;i(e,t,`Invalid boolean value "${n}" for data-${t}. Expected: true/false/1/0/yes/no or empty.`)}function d(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 f(e,t){if(s(e,t))return o(e,t)??void 0}function p(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(`, `)}.`)}}let m=0;const h=(e,t)=>e.id||=`${t}-${++m}`,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 ee=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));let v=0,y=``,b=``;function te(){if(v===0){let e=window.innerWidth-document.documentElement.clientWidth;y=document.body.style.overflow,b=document.body.style.paddingRight;let t=parseFloat(getComputedStyle(document.body).paddingRight)||0;document.body.style.paddingRight=`${t+e}px`,document.body.style.overflow=`hidden`}v++}function x(){v=Math.max(0,v-1),v===0&&(document.body.style.overflow=y,document.body.style.paddingRight=b)}const S=[`top`,`bottom`],ne=[`start`,`center`,`end`],re=[`item-aligned`,`popper`],ie={top:`bottom`,bottom:`top`};function C(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??f(n,`defaultValue`)??null,c=r.defaultOpen??u(n,`defaultOpen`)??!1,l=r.placeholder??f(n,`placeholder`)??(o?f(o,`placeholder`):void 0)??``,m=r.disabled??u(n,`disabled`)??!1,v=r.required??u(n,`required`)??!1,y=r.name??f(n,`name`)??null,b=r.onValueChange,C=r.onOpenChange,T=r.position??p(n,`position`,re)??`item-aligned`,ae=r.side??p(a,`side`,S)??p(n,`side`,S)??`bottom`,E=r.align??p(a,`align`,ne)??p(n,`align`,ne)??`start`,oe=r.sideOffset??d(a,`sideOffset`)??d(n,`sideOffset`)??4,D=r.alignOffset??d(a,`alignOffset`)??d(n,`alignOffset`)??0,se=r.avoidCollisions??u(a,`avoidCollisions`)??u(n,`avoidCollisions`)??!0,O=r.collisionPadding??d(a,`collisionPadding`)??d(n,`collisionPadding`)??8,ce=r.lockScroll??u(n,`lockScroll`)??!0,k=!1,A=s,j=null,M=-1,N=``,P=null,F=!1,I=[],L=[],R=[],z=new Map,B=null,V=[],H=null,U=!1,W=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,le=h(i,`select-trigger`),ue=h(a,`select-content`);i.setAttribute(`role`,`combobox`),i.setAttribute(`aria-haspopup`,`listbox`),i.setAttribute(`aria-controls`,ue),i.hasAttribute(`type`)||i.setAttribute(`type`,`button`),a.setAttribute(`role`,`listbox`),a.setAttribute(`aria-labelledby`,le),a.tabIndex=-1,m&&(i.setAttribute(`aria-disabled`,`true`),i.setAttribute(`data-disabled`,``)),v&&i.setAttribute(`aria-required`,`true`),y&&(H=document.createElement(`input`),H.type=`hidden`,H.name=y,H.value=A??``,n.appendChild(H));let G=()=>{L=t(a,`select-item`);for(let e of L){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===A?(g(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(g(e,`selected`,!1),e.removeAttribute(`data-selected`))}R=L.filter(e=>!W(e)),z=new Map(R.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)}}},K=(e,t,n,r)=>{let i=0,a=0;return a=e===`top`?n.top-r.height-oe:n.bottom+oe,i=t===`start`?n.left+D:t===`center`?n.left+n.width/2-r.width/2+D:n.right-r.width-D,{x:i,y:a}},de=(e,t)=>{let n=L.find(e=>e.dataset.value===A),r=n??R[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}},q=()=>{let e=i.getBoundingClientRect(),t=window.innerWidth,n=window.innerHeight;a.style.minWidth=`${e.width}px`;let r=a.getBoundingClientRect(),o,s=`bottom`;if(T===`item-aligned`){let i=de(e,r);o={x:i.x,y:i.y},se&&(o.y<O?o.y=O:o.y+r.height>n-O&&(o.y=n-r.height-O),o.x<O?o.x=O:o.x+r.width>t-O&&(o.x=t-r.width-O)),s=o.y<e.top?`top`:`bottom`}else if(s=ae,o=K(s,E,e,r),se){let i=(e,t)=>e===`top`?t.y<O:t.y+r.height>n-O;if(i(s,o)){let t=ie[s],n=K(t,E,e,r);i(t,n)||(s=t,o=n)}o.x<O?o.x=O:o.x+r.width>t-O&&(o.x=t-r.width-O),o.y<O?o.y=O:o.y+r.height>n-O&&(o.y=n-r.height-O)}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`,T===`item-aligned`?`center`:E)},J=()=>{B===null&&(B=requestAnimationFrame(()=>{B=null,k&&q()}))},fe=()=>{B!==null&&(cancelAnimationFrame(B),B=null),V.forEach(e=>e()),V.length=0},pe=()=>{if(V.length>0)return;let e=()=>J(),t=e=>{e.target instanceof Node&&a.contains(e.target)||J()};window.addEventListener(`resize`,e),window.addEventListener(`scroll`,t,!0),V.push(()=>window.removeEventListener(`resize`,e),()=>window.removeEventListener(`scroll`,t,!0));let n=new ResizeObserver(e);n.observe(i),n.observe(a),V.push(()=>n.disconnect())},Y=(e,t=!0)=>{for(let n=0;n<R.length;n++){let r=R[n];n===e?(r.setAttribute(`data-highlighted`,``),t&&r.focus()):r.removeAttribute(`data-highlighted`)}M=e},X=()=>{for(let e of L)e.removeAttribute(`data-highlighted`);M=-1},Z=e=>{n.setAttribute(`data-state`,e),i.setAttribute(`data-state`,e),a.setAttribute(`data-state`,e)},me=()=>{if(o)if(A===null)o.textContent=l,i.setAttribute(`data-placeholder`,``);else{let e=L.find(e=>e.dataset.value===A),t=e?.dataset.label??e?.textContent?.trim()??A;o.textContent=t,i.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(k!==e&&!(m&&e)){if(e){j=document.activeElement,k=!0,g(i,`expanded`,!0),a.hidden=!1,Z(`open`),ce&&!U&&(te(),U=!0),G(),F=!1;let e=R.findIndex(e=>e.dataset.value===A);e>=0?Y(e,!1):X(),pe(),q(),a.focus()}else k=!1,g(i,`expanded`,!1),a.hidden=!0,Z(`closed`),X(),N=``,F=!1,U&&(x(),U=!1),fe(),t?j=null:requestAnimationFrame(()=>{j&&document.contains(j)?j.focus():i&&document.contains(i)&&i.focus(),j=null});ee(n,`select:open-change`,{open:k}),C?.(k)}},$=(e,t=!1)=>{if(A===e&&!t)return;let r=A;A=e,H&&(H.value=e??``),e===null?n.removeAttribute(`data-value`):n.setAttribute(`data-value`,e);for(let t of L){let n=t.dataset.value;n===e?(g(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(g(t,`selected`,!1),t.removeAttribute(`data-selected`))}me(),!t&&r!==e&&(ee(n,`select:change`,{value:e}),b?.(e))},he=e=>{if(W(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},ge=e=>{let t=R.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),F=!0,Y(M===-1?0:(M+1)%t);break;case`ArrowUp`:e.preventDefault(),F=!0,Y(M===-1?t-1:(M-1+t)%t);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&&he(R[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(),_e(e.key.toLowerCase()))}},_e=e=>{P&&clearTimeout(P),P=setTimeout(()=>{N=``},500),N+=e;let t=R.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(N));if(t===-1&&N.length===1){let n=M+1;for(let r=0;r<R.length;r++){let i=(n+r)%R.length;if((R[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(F=!0,Y(t))},ve=e=>{if(!m)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),Q(!0);break}};g(i,`expanded`,!1),a.hidden=!0,Z(`closed`),G(),$(A,!0),I.push(_(i,`click`,()=>{m||Q(!k)}),_(i,`keydown`,ve)),I.push(_(a,`keydown`,ge),_(a,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&he(t)}),_(a,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(F&&(F=!1,t&&z.get(t)===M)))if(t&&!W(t)){let e=z.get(t);e!==void 0&&e!==M&&Y(e,!1)}else X()}),_(a,`pointerleave`,()=>{F||X()})),I.push(_(document,`pointerdown`,e=>{let t=e.target;k&&!n.contains(t)&&!a.contains(t)&&Q(!1)})),I.push(_(n,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let ye={get value(){return A},get isOpen(){return k},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{P&&clearTimeout(P),fe(),U&&(x(),U=!1),I.forEach(e=>e()),I.length=0,H&&H.parentNode&&H.parentNode.removeChild(H),w.delete(n)}};return c&&Q(!0),ye}const w=new WeakSet;function T(e=document){let t=[];for(let r of n(e,`select`)){if(w.has(r))continue;w.add(r),t.push(C(r))}return t}exports.create=T,exports.createSelect=C;
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 c=new Set([``,`true`,`1`,`yes`]),ee=new Set([`false`,`0`,`no`]);function l(e,t){if(!s(e,t))return;let n=o(e,t);if(n===null)return;let r=n.toLowerCase();if(c.has(r))return!0;if(ee.has(r))return!1;i(e,t,`Invalid boolean value "${n}" for data-${t}. Expected: true/false/1/0/yes/no or empty.`)}function u(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 d(e,t){if(s(e,t))return o(e,t)??void 0}function f(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(`, `)}.`)}}let p=0;const m=(e,t)=>e.id||=`${t}-${++p}`,h=(e,t,n)=>{n===null?e.removeAttribute(`aria-${t}`):e.setAttribute(`aria-${t}`,String(n))};function g(e,t,n,r){return e.addEventListener(t,n,r),()=>e.removeEventListener(t,n,r)}const te=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));let _=0,v=``,y=``;function ne(){if(_===0){let e=window.innerWidth-document.documentElement.clientWidth;v=document.body.style.overflow,y=document.body.style.paddingRight;let t=parseFloat(getComputedStyle(document.body).paddingRight)||0;document.body.style.paddingRight=`${t+e}px`,document.body.style.overflow=`hidden`}_++}function b(){_=Math.max(0,_-1),_===0&&(document.body.style.overflow=v,document.body.style.paddingRight=y)}const re=[`top`,`bottom`],ie=[`start`,`center`,`end`],ae=[`item-aligned`,`popper`],oe={top:`bottom`,bottom:`top`};function x(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??d(n,`defaultValue`)??null,c=r.defaultOpen??l(n,`defaultOpen`)??!1,ee=r.placeholder??d(n,`placeholder`)??(o?d(o,`placeholder`):void 0)??``,p=r.disabled??l(n,`disabled`)??!1,_=r.required??l(n,`required`)??!1,v=r.name??d(n,`name`)??null,y=r.onValueChange,x=r.onOpenChange,C=r.position??f(n,`position`,ae)??`item-aligned`,se=r.side??f(a,`side`,re)??f(n,`side`,re)??`bottom`,w=r.align??f(a,`align`,ie)??f(n,`align`,ie)??`start`,T=r.sideOffset??u(a,`sideOffset`)??u(n,`sideOffset`)??4,E=r.alignOffset??u(a,`alignOffset`)??u(n,`alignOffset`)??0,D=r.avoidCollisions??l(a,`avoidCollisions`)??l(n,`avoidCollisions`)??!0,O=r.collisionPadding??u(a,`collisionPadding`)??u(n,`collisionPadding`)??8,ce=r.lockScroll??l(n,`lockScroll`)??!0,k=!1,A=s,j=null,M=-1,N=``,P=null,F=!1,I=0,L=0,R=[],z=[],B=[],V=new Map,H=null,U=[],W=null,G=!1,K=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,le=m(i,`select-trigger`),ue=m(a,`select-content`);i.setAttribute(`role`,`combobox`),i.setAttribute(`aria-haspopup`,`listbox`),i.setAttribute(`aria-controls`,ue),i.hasAttribute(`type`)||i.setAttribute(`type`,`button`),a.setAttribute(`role`,`listbox`),a.setAttribute(`aria-labelledby`,le),a.tabIndex=-1;let de=t(n,`select-label`),q=de.find(e=>!a.contains(e))??null;if(q){let e=m(q,`select-label`),t=i.getAttribute(`aria-labelledby`);i.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),q.tagName===`LABEL`?q.htmlFor=le:R.push(g(q,`click`,()=>i.focus()))}p&&(i.setAttribute(`aria-disabled`,`true`),i.setAttribute(`data-disabled`,``)),_&&i.setAttribute(`aria-required`,`true`),v&&(W=document.createElement(`input`),W.type=`hidden`,W.name=v,W.value=A??``,n.appendChild(W));let fe=()=>{z=t(a,`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;let t=e.dataset.value;t===A?(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 n=t(a,`select-group`);for(let t of n){t.setAttribute(`role`,`group`);let n=e(t,`select-label`);if(n){let e=m(n,`select-label`);t.setAttribute(`aria-labelledby`,e)}}},pe=(e,t,n,r)=>{let i=0,a=0;return a=e===`top`?n.top-r.height-T:n.bottom+T,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}},me=(e,t)=>{let n=z.find(e=>e.dataset.value===A),r=n??B[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(C===`item-aligned`){let i=me(e,r);o={x:i.x,y:i.y},D&&(o.y<O?o.y=O:o.y+r.height>n-O&&(o.y=n-r.height-O),o.x<O?o.x=O:o.x+r.width>t-O&&(o.x=t-r.width-O)),s=o.y<e.top?`top`:`bottom`}else if(s=se,o=pe(s,w,e,r),D){let i=(e,t)=>e===`top`?t.y<O:t.y+r.height>n-O;if(i(s,o)){let t=oe[s],n=pe(t,w,e,r);i(t,n)||(s=t,o=n)}o.x<O?o.x=O:o.x+r.width>t-O&&(o.x=t-r.width-O),o.y<O?o.y=O:o.y+r.height>n-O&&(o.y=n-r.height-O)}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`,C===`item-aligned`?`center`:w)},he=()=>{H===null&&(H=requestAnimationFrame(()=>{H=null,k&&J()}))},ge=()=>{H!==null&&(cancelAnimationFrame(H),H=null),U.forEach(e=>e()),U.length=0},_e=()=>{if(U.length>0)return;let e=()=>he(),t=e=>{e.target instanceof Node&&a.contains(e.target)||he()};window.addEventListener(`resize`,e),window.addEventListener(`scroll`,t,!0),U.push(()=>window.removeEventListener(`resize`,e),()=>window.removeEventListener(`scroll`,t,!0));let n=new ResizeObserver(e);n.observe(i),n.observe(a),U.push(()=>n.disconnect())},Y=(e,t=!0)=>{for(let n=0;n<B.length;n++){let r=B[n];n===e?(r.setAttribute(`data-highlighted`,``),t&&r.focus()):r.removeAttribute(`data-highlighted`)}M=e},X=()=>{for(let e of z)e.removeAttribute(`data-highlighted`);M=-1},Z=e=>{n.setAttribute(`data-state`,e),i.setAttribute(`data-state`,e),a.setAttribute(`data-state`,e)},ve=()=>{if(o)if(A===null)o.textContent=ee,i.setAttribute(`data-placeholder`,``);else{let e=z.find(e=>e.dataset.value===A),t=e?.dataset.label??e?.textContent?.trim()??A;o.textContent=t,i.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(k!==e&&!(p&&e)){if(e){j=document.activeElement,k=!0,h(i,`expanded`,!0),a.hidden=!1,Z(`open`),ce&&!G&&(ne(),G=!0),fe(),F=!1;let e=B.findIndex(e=>e.dataset.value===A);e>=0?Y(e,!1):X(),_e(),J(),requestAnimationFrame(()=>{if(k&&(J(),I!==0||L!==0)){let e=document.elementFromPoint(I,L),t=e?.closest?.(`[data-slot="select-item"]`);if(t&&!K(t)&&a.contains(t)){let e=V.get(t);e!==void 0&&Y(e,!1)}}}),a.focus()}else k=!1,h(i,`expanded`,!1),a.hidden=!0,Z(`closed`),X(),N=``,F=!1,G&&(b(),G=!1),ge(),t?j=null:requestAnimationFrame(()=>{j&&document.contains(j)?j.focus():i&&document.contains(i)&&i.focus(),j=null});te(n,`select:open-change`,{open:k}),x?.(k)}},$=(e,t=!1)=>{if(A===e&&!t)return;let r=A;A=e,W&&(W.value=e??``),e===null?n.removeAttribute(`data-value`):n.setAttribute(`data-value`,e);for(let t of z){let n=t.dataset.value;n===e?(h(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(h(t,`selected`,!1),t.removeAttribute(`data-selected`))}ve(),!t&&r!==e&&(te(n,`select:change`,{value:e}),y?.(e))},ye=e=>{if(K(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},be=e=>{let t=B.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),F=!0,Y(M===-1?0:(M+1)%t);break;case`ArrowUp`:e.preventDefault(),F=!0,Y(M===-1?t-1:(M-1+t)%t);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&&ye(B[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(),xe(e.key.toLowerCase()))}},xe=e=>{P&&clearTimeout(P),P=setTimeout(()=>{N=``},500),N+=e;let t=B.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(N));if(t===-1&&N.length===1){let n=M+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&&(F=!0,Y(t))},Se=e=>{if(!p)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),Q(!0);break}};h(i,`expanded`,!1),a.hidden=!0,Z(`closed`),fe(),$(A,!0),R.push(g(i,`pointerdown`,e=>{I=e.clientX,L=e.clientY}),g(i,`click`,()=>{p||Q(!k)}),g(i,`keydown`,Se)),R.push(g(a,`keydown`,be),g(a,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&ye(t)}),g(a,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(F&&(F=!1,t&&V.get(t)===M)))if(t&&!K(t)){let e=V.get(t);e!==void 0&&e!==M&&Y(e,!1)}else X()}),g(a,`pointerleave`,()=>{F||X()})),R.push(g(document,`pointerdown`,e=>{let t=e.target;k&&!n.contains(t)&&!a.contains(t)&&Q(!1)})),R.push(g(n,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let Ce={get value(){return A},get isOpen(){return k},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{P&&clearTimeout(P),ge(),G&&(b(),G=!1),R.forEach(e=>e()),R.length=0,W&&W.parentNode&&W.parentNode.removeChild(W),S.delete(n)}};return c&&Q(!0),Ce}const S=new WeakSet;function C(e=document){let t=[];for(let r of n(e,`select`)){if(S.has(r))continue;S.add(r),t.push(x(r))}return t}exports.create=C,exports.createSelect=x;
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 c=new Set([``,`true`,`1`,`yes`]),l=new Set([`false`,`0`,`no`]);function u(e,t){if(!s(e,t))return;let n=o(e,t);if(n===null)return;let r=n.toLowerCase();if(c.has(r))return!0;if(l.has(r))return!1;i(e,t,`Invalid boolean value "${n}" for data-${t}. Expected: true/false/1/0/yes/no or empty.`)}function d(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 f(e,t){if(s(e,t))return o(e,t)??void 0}function p(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(`, `)}.`)}}let m=0;const h=(e,t)=>e.id||=`${t}-${++m}`,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 ee=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));let v=0,y=``,b=``;function te(){if(v===0){let e=window.innerWidth-document.documentElement.clientWidth;y=document.body.style.overflow,b=document.body.style.paddingRight;let t=parseFloat(getComputedStyle(document.body).paddingRight)||0;document.body.style.paddingRight=`${t+e}px`,document.body.style.overflow=`hidden`}v++}function x(){v=Math.max(0,v-1),v===0&&(document.body.style.overflow=y,document.body.style.paddingRight=b)}const S=[`top`,`bottom`],ne=[`start`,`center`,`end`],re=[`item-aligned`,`popper`],ie={top:`bottom`,bottom:`top`};function C(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??f(n,`defaultValue`)??null,c=r.defaultOpen??u(n,`defaultOpen`)??!1,l=r.placeholder??f(n,`placeholder`)??(o?f(o,`placeholder`):void 0)??``,m=r.disabled??u(n,`disabled`)??!1,v=r.required??u(n,`required`)??!1,y=r.name??f(n,`name`)??null,b=r.onValueChange,C=r.onOpenChange,T=r.position??p(n,`position`,re)??`item-aligned`,ae=r.side??p(a,`side`,S)??p(n,`side`,S)??`bottom`,E=r.align??p(a,`align`,ne)??p(n,`align`,ne)??`start`,oe=r.sideOffset??d(a,`sideOffset`)??d(n,`sideOffset`)??4,D=r.alignOffset??d(a,`alignOffset`)??d(n,`alignOffset`)??0,se=r.avoidCollisions??u(a,`avoidCollisions`)??u(n,`avoidCollisions`)??!0,O=r.collisionPadding??d(a,`collisionPadding`)??d(n,`collisionPadding`)??8,ce=r.lockScroll??u(n,`lockScroll`)??!0,k=!1,A=s,j=null,M=-1,N=``,P=null,F=!1,I=[],L=[],R=[],z=new Map,B=null,V=[],H=null,U=!1,W=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,le=h(i,`select-trigger`),ue=h(a,`select-content`);i.setAttribute(`role`,`combobox`),i.setAttribute(`aria-haspopup`,`listbox`),i.setAttribute(`aria-controls`,ue),i.hasAttribute(`type`)||i.setAttribute(`type`,`button`),a.setAttribute(`role`,`listbox`),a.setAttribute(`aria-labelledby`,le),a.tabIndex=-1,m&&(i.setAttribute(`aria-disabled`,`true`),i.setAttribute(`data-disabled`,``)),v&&i.setAttribute(`aria-required`,`true`),y&&(H=document.createElement(`input`),H.type=`hidden`,H.name=y,H.value=A??``,n.appendChild(H));let G=()=>{L=t(a,`select-item`);for(let e of L){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===A?(g(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(g(e,`selected`,!1),e.removeAttribute(`data-selected`))}R=L.filter(e=>!W(e)),z=new Map(R.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)}}},K=(e,t,n,r)=>{let i=0,a=0;return a=e===`top`?n.top-r.height-oe:n.bottom+oe,i=t===`start`?n.left+D:t===`center`?n.left+n.width/2-r.width/2+D:n.right-r.width-D,{x:i,y:a}},de=(e,t)=>{let n=L.find(e=>e.dataset.value===A),r=n??R[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}},q=()=>{let e=i.getBoundingClientRect(),t=window.innerWidth,n=window.innerHeight;a.style.minWidth=`${e.width}px`;let r=a.getBoundingClientRect(),o,s=`bottom`;if(T===`item-aligned`){let i=de(e,r);o={x:i.x,y:i.y},se&&(o.y<O?o.y=O:o.y+r.height>n-O&&(o.y=n-r.height-O),o.x<O?o.x=O:o.x+r.width>t-O&&(o.x=t-r.width-O)),s=o.y<e.top?`top`:`bottom`}else if(s=ae,o=K(s,E,e,r),se){let i=(e,t)=>e===`top`?t.y<O:t.y+r.height>n-O;if(i(s,o)){let t=ie[s],n=K(t,E,e,r);i(t,n)||(s=t,o=n)}o.x<O?o.x=O:o.x+r.width>t-O&&(o.x=t-r.width-O),o.y<O?o.y=O:o.y+r.height>n-O&&(o.y=n-r.height-O)}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`,T===`item-aligned`?`center`:E)},J=()=>{B===null&&(B=requestAnimationFrame(()=>{B=null,k&&q()}))},fe=()=>{B!==null&&(cancelAnimationFrame(B),B=null),V.forEach(e=>e()),V.length=0},pe=()=>{if(V.length>0)return;let e=()=>J(),t=e=>{e.target instanceof Node&&a.contains(e.target)||J()};window.addEventListener(`resize`,e),window.addEventListener(`scroll`,t,!0),V.push(()=>window.removeEventListener(`resize`,e),()=>window.removeEventListener(`scroll`,t,!0));let n=new ResizeObserver(e);n.observe(i),n.observe(a),V.push(()=>n.disconnect())},Y=(e,t=!0)=>{for(let n=0;n<R.length;n++){let r=R[n];n===e?(r.setAttribute(`data-highlighted`,``),t&&r.focus()):r.removeAttribute(`data-highlighted`)}M=e},X=()=>{for(let e of L)e.removeAttribute(`data-highlighted`);M=-1},Z=e=>{n.setAttribute(`data-state`,e),i.setAttribute(`data-state`,e),a.setAttribute(`data-state`,e)},me=()=>{if(o)if(A===null)o.textContent=l,i.setAttribute(`data-placeholder`,``);else{let e=L.find(e=>e.dataset.value===A),t=e?.dataset.label??e?.textContent?.trim()??A;o.textContent=t,i.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(k!==e&&!(m&&e)){if(e){j=document.activeElement,k=!0,g(i,`expanded`,!0),a.hidden=!1,Z(`open`),ce&&!U&&(te(),U=!0),G(),F=!1;let e=R.findIndex(e=>e.dataset.value===A);e>=0?Y(e,!1):X(),pe(),q(),a.focus()}else k=!1,g(i,`expanded`,!1),a.hidden=!0,Z(`closed`),X(),N=``,F=!1,U&&(x(),U=!1),fe(),t?j=null:requestAnimationFrame(()=>{j&&document.contains(j)?j.focus():i&&document.contains(i)&&i.focus(),j=null});ee(n,`select:open-change`,{open:k}),C?.(k)}},$=(e,t=!1)=>{if(A===e&&!t)return;let r=A;A=e,H&&(H.value=e??``),e===null?n.removeAttribute(`data-value`):n.setAttribute(`data-value`,e);for(let t of L){let n=t.dataset.value;n===e?(g(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(g(t,`selected`,!1),t.removeAttribute(`data-selected`))}me(),!t&&r!==e&&(ee(n,`select:change`,{value:e}),b?.(e))},he=e=>{if(W(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},ge=e=>{let t=R.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),F=!0,Y(M===-1?0:(M+1)%t);break;case`ArrowUp`:e.preventDefault(),F=!0,Y(M===-1?t-1:(M-1+t)%t);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&&he(R[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(),_e(e.key.toLowerCase()))}},_e=e=>{P&&clearTimeout(P),P=setTimeout(()=>{N=``},500),N+=e;let t=R.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(N));if(t===-1&&N.length===1){let n=M+1;for(let r=0;r<R.length;r++){let i=(n+r)%R.length;if((R[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(F=!0,Y(t))},ve=e=>{if(!m)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),Q(!0);break}};g(i,`expanded`,!1),a.hidden=!0,Z(`closed`),G(),$(A,!0),I.push(_(i,`click`,()=>{m||Q(!k)}),_(i,`keydown`,ve)),I.push(_(a,`keydown`,ge),_(a,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&he(t)}),_(a,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(F&&(F=!1,t&&z.get(t)===M)))if(t&&!W(t)){let e=z.get(t);e!==void 0&&e!==M&&Y(e,!1)}else X()}),_(a,`pointerleave`,()=>{F||X()})),I.push(_(document,`pointerdown`,e=>{let t=e.target;k&&!n.contains(t)&&!a.contains(t)&&Q(!1)})),I.push(_(n,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let ye={get value(){return A},get isOpen(){return k},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{P&&clearTimeout(P),fe(),U&&(x(),U=!1),I.forEach(e=>e()),I.length=0,H&&H.parentNode&&H.parentNode.removeChild(H),w.delete(n)}};return c&&Q(!0),ye}const w=new WeakSet;function T(e=document){let t=[];for(let r of n(e,`select`)){if(w.has(r))continue;w.add(r),t.push(C(r))}return t}export{T as create,C as createSelect};
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 c=new Set([``,`true`,`1`,`yes`]),ee=new Set([`false`,`0`,`no`]);function l(e,t){if(!s(e,t))return;let n=o(e,t);if(n===null)return;let r=n.toLowerCase();if(c.has(r))return!0;if(ee.has(r))return!1;i(e,t,`Invalid boolean value "${n}" for data-${t}. Expected: true/false/1/0/yes/no or empty.`)}function u(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 d(e,t){if(s(e,t))return o(e,t)??void 0}function f(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(`, `)}.`)}}let p=0;const m=(e,t)=>e.id||=`${t}-${++p}`,h=(e,t,n)=>{n===null?e.removeAttribute(`aria-${t}`):e.setAttribute(`aria-${t}`,String(n))};function g(e,t,n,r){return e.addEventListener(t,n,r),()=>e.removeEventListener(t,n,r)}const te=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));let _=0,v=``,y=``;function ne(){if(_===0){let e=window.innerWidth-document.documentElement.clientWidth;v=document.body.style.overflow,y=document.body.style.paddingRight;let t=parseFloat(getComputedStyle(document.body).paddingRight)||0;document.body.style.paddingRight=`${t+e}px`,document.body.style.overflow=`hidden`}_++}function b(){_=Math.max(0,_-1),_===0&&(document.body.style.overflow=v,document.body.style.paddingRight=y)}const re=[`top`,`bottom`],ie=[`start`,`center`,`end`],ae=[`item-aligned`,`popper`],oe={top:`bottom`,bottom:`top`};function x(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??d(n,`defaultValue`)??null,c=r.defaultOpen??l(n,`defaultOpen`)??!1,ee=r.placeholder??d(n,`placeholder`)??(o?d(o,`placeholder`):void 0)??``,p=r.disabled??l(n,`disabled`)??!1,_=r.required??l(n,`required`)??!1,v=r.name??d(n,`name`)??null,y=r.onValueChange,x=r.onOpenChange,C=r.position??f(n,`position`,ae)??`item-aligned`,se=r.side??f(a,`side`,re)??f(n,`side`,re)??`bottom`,w=r.align??f(a,`align`,ie)??f(n,`align`,ie)??`start`,T=r.sideOffset??u(a,`sideOffset`)??u(n,`sideOffset`)??4,E=r.alignOffset??u(a,`alignOffset`)??u(n,`alignOffset`)??0,D=r.avoidCollisions??l(a,`avoidCollisions`)??l(n,`avoidCollisions`)??!0,O=r.collisionPadding??u(a,`collisionPadding`)??u(n,`collisionPadding`)??8,ce=r.lockScroll??l(n,`lockScroll`)??!0,k=!1,A=s,j=null,M=-1,N=``,P=null,F=!1,I=0,L=0,R=[],z=[],B=[],V=new Map,H=null,U=[],W=null,G=!1,K=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,le=m(i,`select-trigger`),ue=m(a,`select-content`);i.setAttribute(`role`,`combobox`),i.setAttribute(`aria-haspopup`,`listbox`),i.setAttribute(`aria-controls`,ue),i.hasAttribute(`type`)||i.setAttribute(`type`,`button`),a.setAttribute(`role`,`listbox`),a.setAttribute(`aria-labelledby`,le),a.tabIndex=-1;let de=t(n,`select-label`),q=de.find(e=>!a.contains(e))??null;if(q){let e=m(q,`select-label`),t=i.getAttribute(`aria-labelledby`);i.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),q.tagName===`LABEL`?q.htmlFor=le:R.push(g(q,`click`,()=>i.focus()))}p&&(i.setAttribute(`aria-disabled`,`true`),i.setAttribute(`data-disabled`,``)),_&&i.setAttribute(`aria-required`,`true`),v&&(W=document.createElement(`input`),W.type=`hidden`,W.name=v,W.value=A??``,n.appendChild(W));let fe=()=>{z=t(a,`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;let t=e.dataset.value;t===A?(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 n=t(a,`select-group`);for(let t of n){t.setAttribute(`role`,`group`);let n=e(t,`select-label`);if(n){let e=m(n,`select-label`);t.setAttribute(`aria-labelledby`,e)}}},pe=(e,t,n,r)=>{let i=0,a=0;return a=e===`top`?n.top-r.height-T:n.bottom+T,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}},me=(e,t)=>{let n=z.find(e=>e.dataset.value===A),r=n??B[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(C===`item-aligned`){let i=me(e,r);o={x:i.x,y:i.y},D&&(o.y<O?o.y=O:o.y+r.height>n-O&&(o.y=n-r.height-O),o.x<O?o.x=O:o.x+r.width>t-O&&(o.x=t-r.width-O)),s=o.y<e.top?`top`:`bottom`}else if(s=se,o=pe(s,w,e,r),D){let i=(e,t)=>e===`top`?t.y<O:t.y+r.height>n-O;if(i(s,o)){let t=oe[s],n=pe(t,w,e,r);i(t,n)||(s=t,o=n)}o.x<O?o.x=O:o.x+r.width>t-O&&(o.x=t-r.width-O),o.y<O?o.y=O:o.y+r.height>n-O&&(o.y=n-r.height-O)}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`,C===`item-aligned`?`center`:w)},he=()=>{H===null&&(H=requestAnimationFrame(()=>{H=null,k&&J()}))},ge=()=>{H!==null&&(cancelAnimationFrame(H),H=null),U.forEach(e=>e()),U.length=0},_e=()=>{if(U.length>0)return;let e=()=>he(),t=e=>{e.target instanceof Node&&a.contains(e.target)||he()};window.addEventListener(`resize`,e),window.addEventListener(`scroll`,t,!0),U.push(()=>window.removeEventListener(`resize`,e),()=>window.removeEventListener(`scroll`,t,!0));let n=new ResizeObserver(e);n.observe(i),n.observe(a),U.push(()=>n.disconnect())},Y=(e,t=!0)=>{for(let n=0;n<B.length;n++){let r=B[n];n===e?(r.setAttribute(`data-highlighted`,``),t&&r.focus()):r.removeAttribute(`data-highlighted`)}M=e},X=()=>{for(let e of z)e.removeAttribute(`data-highlighted`);M=-1},Z=e=>{n.setAttribute(`data-state`,e),i.setAttribute(`data-state`,e),a.setAttribute(`data-state`,e)},ve=()=>{if(o)if(A===null)o.textContent=ee,i.setAttribute(`data-placeholder`,``);else{let e=z.find(e=>e.dataset.value===A),t=e?.dataset.label??e?.textContent?.trim()??A;o.textContent=t,i.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(k!==e&&!(p&&e)){if(e){j=document.activeElement,k=!0,h(i,`expanded`,!0),a.hidden=!1,Z(`open`),ce&&!G&&(ne(),G=!0),fe(),F=!1;let e=B.findIndex(e=>e.dataset.value===A);e>=0?Y(e,!1):X(),_e(),J(),requestAnimationFrame(()=>{if(k&&(J(),I!==0||L!==0)){let e=document.elementFromPoint(I,L),t=e?.closest?.(`[data-slot="select-item"]`);if(t&&!K(t)&&a.contains(t)){let e=V.get(t);e!==void 0&&Y(e,!1)}}}),a.focus()}else k=!1,h(i,`expanded`,!1),a.hidden=!0,Z(`closed`),X(),N=``,F=!1,G&&(b(),G=!1),ge(),t?j=null:requestAnimationFrame(()=>{j&&document.contains(j)?j.focus():i&&document.contains(i)&&i.focus(),j=null});te(n,`select:open-change`,{open:k}),x?.(k)}},$=(e,t=!1)=>{if(A===e&&!t)return;let r=A;A=e,W&&(W.value=e??``),e===null?n.removeAttribute(`data-value`):n.setAttribute(`data-value`,e);for(let t of z){let n=t.dataset.value;n===e?(h(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(h(t,`selected`,!1),t.removeAttribute(`data-selected`))}ve(),!t&&r!==e&&(te(n,`select:change`,{value:e}),y?.(e))},ye=e=>{if(K(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},be=e=>{let t=B.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),F=!0,Y(M===-1?0:(M+1)%t);break;case`ArrowUp`:e.preventDefault(),F=!0,Y(M===-1?t-1:(M-1+t)%t);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&&ye(B[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(),xe(e.key.toLowerCase()))}},xe=e=>{P&&clearTimeout(P),P=setTimeout(()=>{N=``},500),N+=e;let t=B.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(N));if(t===-1&&N.length===1){let n=M+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&&(F=!0,Y(t))},Se=e=>{if(!p)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),Q(!0);break}};h(i,`expanded`,!1),a.hidden=!0,Z(`closed`),fe(),$(A,!0),R.push(g(i,`pointerdown`,e=>{I=e.clientX,L=e.clientY}),g(i,`click`,()=>{p||Q(!k)}),g(i,`keydown`,Se)),R.push(g(a,`keydown`,be),g(a,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&ye(t)}),g(a,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(F&&(F=!1,t&&V.get(t)===M)))if(t&&!K(t)){let e=V.get(t);e!==void 0&&e!==M&&Y(e,!1)}else X()}),g(a,`pointerleave`,()=>{F||X()})),R.push(g(document,`pointerdown`,e=>{let t=e.target;k&&!n.contains(t)&&!a.contains(t)&&Q(!1)})),R.push(g(n,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let Ce={get value(){return A},get isOpen(){return k},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{P&&clearTimeout(P),ge(),G&&(b(),G=!1),R.forEach(e=>e()),R.length=0,W&&W.parentNode&&W.parentNode.removeChild(W),S.delete(n)}};return c&&Q(!0),Ce}const S=new WeakSet;function C(e=document){let t=[];for(let r of n(e,`select`)){if(S.has(r))continue;S.add(r),t.push(x(r))}return t}export{C as create,x as createSelect};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-slot/select",
3
- "version": "0.2.21",
3
+ "version": "0.2.23",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.cjs",