@data-slot/select 0.2.137 → 0.2.139
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 +15 -4
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -22,12 +22,20 @@ npm install @data-slot/select
|
|
|
22
22
|
<div data-slot="select-viewport">
|
|
23
23
|
<div data-slot="select-group">
|
|
24
24
|
<div data-slot="select-label">Fruits</div>
|
|
25
|
-
<div data-slot="select-item" data-value="apple">
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
<div data-slot="select-item" data-value="apple">
|
|
26
|
+
<span data-slot="select-item-text">Apple</span>
|
|
27
|
+
</div>
|
|
28
|
+
<div data-slot="select-item" data-value="banana">
|
|
29
|
+
<span data-slot="select-item-text">Banana</span>
|
|
30
|
+
</div>
|
|
31
|
+
<div data-slot="select-item" data-value="orange">
|
|
32
|
+
<span data-slot="select-item-text">Orange</span>
|
|
33
|
+
</div>
|
|
28
34
|
</div>
|
|
29
35
|
<div data-slot="select-separator"></div>
|
|
30
|
-
<div data-slot="select-item" data-value="other">
|
|
36
|
+
<div data-slot="select-item" data-value="other">
|
|
37
|
+
<span data-slot="select-item-text">Other</span>
|
|
38
|
+
</div>
|
|
31
39
|
</div>
|
|
32
40
|
</div>
|
|
33
41
|
</div>
|
|
@@ -70,6 +78,7 @@ controller.destroy();
|
|
|
70
78
|
| `select-content` | Popup container for options |
|
|
71
79
|
| `select-viewport` | Optional scroll container inside `select-content`; used for item-aligned scrolling when present |
|
|
72
80
|
| `select-item` | Individual selectable option |
|
|
81
|
+
| `select-item-text` | Optional text anchor inside `select-item`; preferred for exact item-aligned parity with Base/shadcn styles |
|
|
73
82
|
| `select-group` | Groups related items |
|
|
74
83
|
| `select-label` | Group label (inside a `select-group`) |
|
|
75
84
|
| `select-separator` | Visual divider between items/groups |
|
|
@@ -136,6 +145,8 @@ Placement attributes (`position`, `side`, `align`, `sideOffset`, `alignOffset`,
|
|
|
136
145
|
|
|
137
146
|
**`item-aligned` (default)**: The popup positions itself so the selected item aligns with the trigger, similar to native `<select>` elements. The popup width matches the trigger width.
|
|
138
147
|
|
|
148
|
+
For the closest Base/shadcn visual parity, wrap the visible label inside each item with `data-slot="select-item-text"`. When absent, the controller falls back to the whole `select-item` box for alignment, trigger display, and typeahead.
|
|
149
|
+
|
|
139
150
|
**`popper`**: The popup appears below or above the trigger like a dropdown menu. Additional options apply:
|
|
140
151
|
|
|
141
152
|
| Option | Data Attribute | Type | Default | Description |
|
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`],i=`@data-slot/select`;function a(a,o={}){let s=(0,e.reuseRootBinding)(a,i,`[@data-slot/select] createSelect() called more than once for the same root. Returning the existing controller. Destroy it before rebinding with new options.`);if(s)return s;let c=(0,e.getPart)(a,`select-trigger`),l=(0,e.getPart)(a,`select-content`),u=(0,e.getPart)(a,`select-value`),d=(0,e.getPart)(a,`select-positioner`),f=d&&l&&d.contains(l)?d:null,p=(0,e.getPart)(a,`select-portal`),ee=p&&f&&p.contains(f)?p:null;if(!c||!l)throw Error(`Select requires trigger and content slots`);let te=o.defaultValue??(0,e.getDataString)(a,`defaultValue`)??null,ne=o.defaultOpen??(0,e.getDataBool)(a,`defaultOpen`)??!1,re=o.placeholder??(0,e.getDataString)(a,`placeholder`)??(u?(0,e.getDataString)(u,`placeholder`):void 0)??``,m=o.disabled??(0,e.getDataBool)(a,`disabled`)??!1,ie=o.required??(0,e.getDataBool)(a,`required`)??!1,ae=o.name??(0,e.getDataString)(a,`name`)??null,oe=o.onValueChange,se=o.onOpenChange,h=(t,n)=>(0,e.getDataEnum)(l,t,n)??(f?(0,e.getDataEnum)(f,t,n):void 0)??(0,e.getDataEnum)(a,t,n),g=t=>(0,e.getDataNumber)(l,t)??(f?(0,e.getDataNumber)(f,t):void 0)??(0,e.getDataNumber)(a,t),ce=t=>(0,e.getDataBool)(l,t)??(f?(0,e.getDataBool)(f,t):void 0)??(0,e.getDataBool)(a,t),_=o.position??h(`position`,r)??`item-aligned`,le=o.side??h(`side`,t)??`bottom`,ue=o.align??h(`align`,n)??`start`,de=o.sideOffset??g(`sideOffset`)??4,fe=o.alignOffset??g(`alignOffset`)??0,v=o.avoidCollisions??ce(`avoidCollisions`)??!0,y=o.collisionPadding??g(`collisionPadding`)??8,b=o.lockScroll??(0,e.getDataBool)(a,`lockScroll`)??!0,x=o.highlightItemOnHover??(0,e.getDataBool)(a,`highlightItemOnHover`)??!0,S=!1,C=te,w=null,T=-1,E=``,D=null,O=!1,k=0,A=0,j=``,M=!1,N=[],P=[],F=[],I=new Map,L=null,R=!1,z=(0,e.createPortalLifecycle)({content:l,root:a,wrapperSlot:f?void 0:`select-positioner`,container:f??void 0,mountTarget:f?ee??f:void 0}),B=!1,V=!0,H=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,pe=e=>e.pointerType!==`touch`,U=(0,e.ensureId)(c,`select-trigger`),me=(0,e.ensureId)(l,`select-content`);c.setAttribute(`role`,`combobox`),c.setAttribute(`aria-haspopup`,`listbox`),c.setAttribute(`aria-controls`,me),c.hasAttribute(`type`)||c.setAttribute(`type`,`button`),l.setAttribute(`role`,`listbox`),l.setAttribute(`aria-labelledby`,U),l.tabIndex=-1;let W=document.querySelector(`label[for="${CSS.escape(U)}"]`);if(W){let t=(0,e.ensureId)(W,`select-label`),n=c.getAttribute(`aria-labelledby`);c.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),N.push((0,e.on)(W,`click`,e=>{e.preventDefault(),m||Z(!S)}))}m&&(c.setAttribute(`aria-disabled`,`true`),c.setAttribute(`data-disabled`,``),c instanceof HTMLButtonElement&&(c.disabled=!0)),ie&&c.setAttribute(`aria-required`,`true`),ae&&(L=document.createElement(`input`),L.type=`hidden`,L.name=ae,L.value=C??``,a.appendChild(L));let he=()=>{P=(0,e.getParts)(l,`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===C?((0,e.setAria)(t,`selected`,!0),t.setAttribute(`data-selected`,``)):((0,e.setAria)(t,`selected`,!1),t.removeAttribute(`data-selected`));F=P.filter(e=>!H(e)),I=new Map(F.map((e,t)=>[e,t]));let t=(0,e.getParts)(l,`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)}}},ge=()=>(0,e.getPart)(l,`select-viewport`),_e=()=>ge()??l,ve=()=>{l.setAttribute(`data-position`,_),l.setAttribute(`data-align-trigger`,_===`item-aligned`?`true`:`false`);let e=ge();e&&e.setAttribute(`data-position`,_)},ye=(e,t,n,r)=>{let i=0,a=e;for(;a&&a!==t;){i+=a.offsetTop;let e=a.offsetParent;if(!(e instanceof HTMLElement)){i=NaN;break}e!==t&&(i+=e.clientTop),a=e}return a===t&&Number.isFinite(i)?i:e.getBoundingClientRect().top-n-t.clientTop+r},be=(e,t,n)=>ye(e,l,t.top,n.scrollTop),xe=(e,t,n)=>{let r=T>=0?F[T]:void 0,i=P.find(e=>e.dataset.value===C)??r??F[0],a=e.left,o,s=0,c=e.height;return i?(s=be(i,t,n),c=i.getBoundingClientRect().height||i.offsetHeight||e.height,o=e.top+e.height/2-l.clientTop-s-c/2):o=e.top,{x:a,y:o,alignItem:i,itemTopInContent:s,itemHeight:c}},G=()=>{let n=z.container,r=a.ownerDocument.defaultView??window,i=c.getBoundingClientRect(),o=_e();l.style.minWidth=`${i.width}px`;let s=(0,e.measurePopupContentRect)(l),u,d=`bottom`;if(_===`item-aligned`){let e=xe(i,s,o);u={x:e.x,y:e.y};let t=i.top+i.height/2,n=y,a=r.innerHeight-s.height-y,c=e=>v?a<n?n:Math.min(Math.max(e,n),a):e,f=y,p=r.innerWidth-s.width-y;if(u.x=(e=>v?p<f?f:Math.min(Math.max(e,f),p):e)(u.x),e.alignItem){let n=Math.max(0,o.scrollHeight-o.clientHeight),r=e=>t-(e+l.clientTop),i=t=>e.itemTopInContent+e.itemHeight/2-r(t);if(n>0){u.y=c(t-s.height/2);let r=Math.min(Math.max(i(u.y),0),n);o.scrollTop=r,u.y=c(t-(l.clientTop+e.itemTopInContent-r+e.itemHeight/2)),r=Math.min(Math.max(i(u.y),0),n),o.scrollTop=r,u.y=c(t-(l.clientTop+e.itemTopInContent-r+e.itemHeight/2))}else o.scrollTop=0,u.y=c(e.y)}else o.scrollTop=0,u.y=c(e.y);d=u.y<i.top?`top`:`bottom`}else{let n=(0,e.computeFloatingPosition)({anchorRect:i,contentRect:s,side:le,align:ue,sideOffset:de,alignOffset:fe,avoidCollisions:v,collisionPadding:y,allowedSides:t});u={x:n.x,y:n.y},d=n.side}let f=_===`item-aligned`?`center`:ue,p=(0,e.computeFloatingTransformOrigin)({side:d,align:f,anchorRect:i,popupX:u.x,popupY:u.y});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.setProperty(`--transform-origin`,p),n.style.willChange=`transform`,n.style.margin=`0`,ve(),l.setAttribute(`data-side`,d),l.setAttribute(`data-align`,f),n!==l&&(n.setAttribute(`data-side`,d),n.setAttribute(`data-align`,f))},K=(0,e.createPositionSync)({observedElements:[c,l],isActive:()=>S,ancestorScroll:b,onUpdate:G,ignoreScrollTarget:e=>e instanceof Node&&l.contains(e)}),q=(t,n=!0,r=!0)=>{let i=_e();for(let a=0;a<F.length;a++){let o=F[a];a===t?(o.setAttribute(`data-highlighted`,``),r&&(0,e.ensureItemVisibleInContainer)(o,i),n&&o.focus()):o.removeAttribute(`data-highlighted`)}T=t},J=()=>{for(let e of P)e.removeAttribute(`data-highlighted`);T=-1},Se=()=>{J(),(0,e.focusElement)(l)},Y=e=>{a.setAttribute(`data-state`,e),c.setAttribute(`data-state`,e),l.setAttribute(`data-state`,e),e===`open`?(a.setAttribute(`data-open`,``),c.setAttribute(`data-open`,``),l.setAttribute(`data-open`,``),a.removeAttribute(`data-closed`),c.removeAttribute(`data-closed`),l.removeAttribute(`data-closed`)):(a.setAttribute(`data-closed`,``),c.setAttribute(`data-closed`,``),l.setAttribute(`data-closed`,``),a.removeAttribute(`data-open`),c.removeAttribute(`data-open`),l.removeAttribute(`data-open`))},Ce=()=>{requestAnimationFrame(()=>{w&&document.contains(w)?(0,e.focusElement)(w):c&&document.contains(c)&&(0,e.focusElement)(c),w=null})},we=()=>{B||(z.restore(),l.hidden=!0,V?Ce():w=null)},X=(0,e.createPresenceLifecycle)({element:l,onExitComplete:we}),Te=()=>{if(u)if(C===null)u.textContent=re,c.setAttribute(`data-placeholder`,``);else{let e=P.find(e=>e.dataset.value===C);u.textContent=e?.dataset.label??e?.textContent?.trim()??C,c.removeAttribute(`data-placeholder`)}},Z=(t,n={})=>{let{skipFocusRestore:r=!1,immediate:i=!1}=n;if(S!==t&&!(m&&t)){if(t){let t=M;M=!1,V=!0,w=document.activeElement,S=!0,(0,e.setAria)(c,`expanded`,!0),z.mount(),l.hidden=!1,Y(`open`),X.enter(),b&&!R&&((0,e.lockScroll)(),R=!0),he(),O=!1;let n=F.findIndex(e=>e.dataset.value===C);n>=0?q(n,!1,!1):J(),K.start(),G(),K.update(),requestAnimationFrame(()=>{if(S&&(G(),K.update(),t&&x&&j!==`touch`&&(k!==0||A!==0))){let e=document.elementFromPoint(k,A)?.closest?.(`[data-slot="select-item"]`);if(e&&!H(e)&&l.contains(e)){let t=I.get(e);t!==void 0&&q(t,!0,!1)}}}),l.focus()}else S=!1,M=!1,k=0,A=0,j=``,(0,e.setAria)(c,`expanded`,!1),Y(`closed`),J(),E=``,O=!1,V=!r,R&&=((0,e.unlockScroll)(),!1),K.stop(),i?(X.cleanup(),we()):X.exit();(0,e.emit)(a,`select:open-change`,{open:S}),se?.(S)}},Q=(t,n=!1)=>{if(C===t&&!n)return;let r=C;C=t,L&&(L.value=t??``),t===null?a.removeAttribute(`data-value`):a.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`));Te(),!n&&r!==t&&((0,e.emit)(a,`select:change`,{value:t}),oe?.(t))},Ee=e=>{if(H(e))return;let t=e.dataset.value;t!==void 0&&(Q(t),Z(!1,{immediate:!0}))},De=e=>{let t=F.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),O=!0,q(T===-1?0:Math.min(T+1,t-1));break;case`ArrowUp`:e.preventDefault(),O=!0,q(T===-1?t-1:Math.max(T-1,0));break;case`Home`:e.preventDefault(),O=!0,q(0);break;case`End`:e.preventDefault(),O=!0,q(t-1);break;case`Enter`:case` `:e.preventDefault(),T>=0&&Ee(F[T]);break;case`Tab`:Z(!1,{skipFocusRestore:!0});break;case`Escape`:e.preventDefault(),Z(!1);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),Oe(e.key.toLowerCase()))}},Oe=e=>{D&&clearTimeout(D),D=setTimeout(()=>{E=``},500),E+=e;let t=F.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(E));if(t===-1&&E.length===1){let n=T+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&&(O=!0,q(t))};(0,e.setAria)(c,`expanded`,!1),l.hidden=!0,ve(),Y(`closed`),he(),Q(C,!0),N.push((0,e.on)(c,`pointerdown`,e=>{k=e.clientX,A=e.clientY,j=e.pointerType,M=!0}),(0,e.on)(c,`click`,()=>{m||Z(!S)}),(0,e.on)(c,`keydown`,e=>{if(!m)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),M=!1,Z(!0);break}})),N.push((0,e.on)(l,`keydown`,De),(0,e.on)(l,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&Ee(t)}),(0,e.on)(l,`pointermove`,e=>{if(!x||!pe(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(O&&(O=!1,t&&I.get(t)===T)))if(t&&!H(t)){let e=I.get(t);e!==void 0&&e!==T&&q(e,!0)}else Se()}),(0,e.on)(l,`pointerleave`,e=>{!x||!pe(e)||O||Se()})),N.push((0,e.createDismissLayer)({root:a,isOpen:()=>S,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),N.push((0,e.on)(a,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&Q(t.value),t?.open!==void 0&&Z(t.open)}));let $={get value(){return C},get isOpen(){return S},select:e=>Q(e),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{B=!0,D&&clearTimeout(D),K.stop(),X.cleanup(),z.cleanup(),R&&=((0,e.unlockScroll)(),!1),N.forEach(e=>e()),N.length=0,L&&L.parentNode&&L.parentNode.removeChild(L),(0,e.clearRootBinding)(a,i,$)}};return(0,e.setRootBinding)(a,i,$),ne&&Z(!0),$}function o(t=document){let n=[];for(let r of(0,e.getRoots)(t,`select`))(0,e.hasRootBinding)(r,i)||n.push(a(r));return n}exports.create=o,exports.createSelect=a;
|
|
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`],i=`@data-slot/select`;function a(a,o={}){let s=(0,e.reuseRootBinding)(a,i,`[@data-slot/select] createSelect() called more than once for the same root. Returning the existing controller. Destroy it before rebinding with new options.`);if(s)return s;let c=(0,e.getPart)(a,`select-trigger`),l=(0,e.getPart)(a,`select-content`),u=(0,e.getPart)(a,`select-value`),d=(0,e.getPart)(a,`select-positioner`),f=d&&l&&d.contains(l)?d:null,p=(0,e.getPart)(a,`select-portal`),m=p&&f&&p.contains(f)?p:null;if(!c||!l)throw Error(`Select requires trigger and content slots`);let ee=o.defaultValue??(0,e.getDataString)(a,`defaultValue`)??null,te=o.defaultOpen??(0,e.getDataBool)(a,`defaultOpen`)??!1,ne=o.placeholder??(0,e.getDataString)(a,`placeholder`)??(u?(0,e.getDataString)(u,`placeholder`):void 0)??``,h=o.disabled??(0,e.getDataBool)(a,`disabled`)??!1,re=o.required??(0,e.getDataBool)(a,`required`)??!1,ie=o.name??(0,e.getDataString)(a,`name`)??null,ae=o.onValueChange,oe=o.onOpenChange,g=(t,n)=>(0,e.getDataEnum)(l,t,n)??(f?(0,e.getDataEnum)(f,t,n):void 0)??(0,e.getDataEnum)(a,t,n),_=t=>(0,e.getDataNumber)(l,t)??(f?(0,e.getDataNumber)(f,t):void 0)??(0,e.getDataNumber)(a,t),se=t=>(0,e.getDataBool)(l,t)??(f?(0,e.getDataBool)(f,t):void 0)??(0,e.getDataBool)(a,t),v=o.position??g(`position`,r)??`item-aligned`,ce=o.side??g(`side`,t)??`bottom`,y=o.align??g(`align`,n)??`start`,le=o.sideOffset??_(`sideOffset`)??4,ue=o.alignOffset??_(`alignOffset`)??0,b=o.avoidCollisions??se(`avoidCollisions`)??!0,x=o.collisionPadding??_(`collisionPadding`)??8,S=o.lockScroll??(0,e.getDataBool)(a,`lockScroll`)??!0,de=o.highlightItemOnHover??(0,e.getDataBool)(a,`highlightItemOnHover`)??!0,C=!1,w=ee,T=null,E=-1,D=``,O=null,k=!1,A=0,j=0,fe=``,M=!1,N=[],P=[],F=[],I=new Map,L=null,R=!1,z=(0,e.createPortalLifecycle)({content:l,root:a,wrapperSlot:f?void 0:`select-positioner`,container:f??void 0,mountTarget:f?m??f:void 0}),pe=!1,B=!0,V=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,me=e=>e.pointerType!==`touch`,he=(0,e.ensureId)(c,`select-trigger`),ge=(0,e.ensureId)(l,`select-content`);c.setAttribute(`role`,`combobox`),c.setAttribute(`aria-haspopup`,`listbox`),c.setAttribute(`aria-controls`,ge),c.hasAttribute(`type`)||c.setAttribute(`type`,`button`),l.setAttribute(`role`,`listbox`),l.setAttribute(`aria-labelledby`,he),l.tabIndex=-1;let H=document.querySelector(`label[for="${CSS.escape(he)}"]`);if(H){let t=(0,e.ensureId)(H,`select-label`),n=c.getAttribute(`aria-labelledby`);c.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),N.push((0,e.on)(H,`click`,e=>{e.preventDefault(),h||Z(!C)}))}h&&(c.setAttribute(`aria-disabled`,`true`),c.setAttribute(`data-disabled`,``),c instanceof HTMLButtonElement&&(c.disabled=!0)),re&&c.setAttribute(`aria-required`,`true`),ie&&(L=document.createElement(`input`),L.type=`hidden`,L.name=ie,L.value=w??``,a.appendChild(L));let _e=()=>{P=(0,e.getParts)(l,`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===w?((0,e.setAria)(t,`selected`,!0),t.setAttribute(`data-selected`,``)):((0,e.setAria)(t,`selected`,!1),t.removeAttribute(`data-selected`));F=P.filter(e=>!V(e)),I=new Map(F.map((e,t)=>[e,t]));let t=(0,e.getParts)(l,`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)}}},ve=()=>(0,e.getPart)(l,`select-viewport`),ye=()=>ve()??l,U=t=>(0,e.getPart)(t,`select-item-text`),be=e=>e?.textContent?.trim()||void 0,W=(e,t=``)=>e?(e.dataset.label?.trim()||void 0)??be(U(e))??be(e)??t:t,xe=(e=v===`item-aligned`)=>{l.setAttribute(`data-position`,v),l.setAttribute(`data-align-trigger`,e?`true`:`false`);let t=ve();t&&t.setAttribute(`data-position`,v)},G=e=>{if(!e)return null;let t=e.getBoundingClientRect();return t.width>0||t.height>0?t:null},Se=e=>G(u)??e,Ce=(e,t,n,r,i)=>{let a=i===`top`?`offsetTop`:`offsetLeft`,o=i===`top`?`clientTop`:`clientLeft`,s=i===`top`?`top`:`left`,c=0,l=e;for(;l&&l!==t;){c+=l[a];let e=l.offsetParent;if(!(e instanceof HTMLElement)){c=NaN;break}e!==t&&(c+=e[o]),l=e}return l===t&&Number.isFinite(c)?c:e.getBoundingClientRect()[s]-n[s]-t[o]+r},we=(e,t,n,r,i)=>Ce(e,t,n,r,i)+(i===`top`?t.clientTop:t.clientLeft),Te=(e,t,n)=>we(e,l,t,n.scrollTop,`top`),Ee=e=>{let t=U(e);return G(t)?t:e},De=(e,t,n)=>{let r=E>=0?F[E]:void 0,i=P.find(e=>e.dataset.value===w)??r??F[0],a=Se(e),o=G(u),s=e.left,c,l=0,d=a.height;if(i){let e=U(i),r=!!(o&&G(e)),u=r&&e?e:Ee(i),f=G(u)??u.getBoundingClientRect();l=Te(u,t,n),d=f.height||u.offsetHeight||i.getBoundingClientRect().height||i.offsetHeight||a.height,r&&o&&(s=o.left-(f.left-t.left)),c=a.top+a.height/2-l-d/2}else c=e.top;return{x:s,y:c,alignItem:i,anchorTopInContent:l,anchorHeight:d,triggerAlignmentRect:a}},K=()=>{let n=z.container,r=a.ownerDocument.defaultView??window,i=c.getBoundingClientRect(),o=ye();l.style.minWidth=`${i.width}px`;let s=(0,e.measurePopupContentRect)(l),u,d=`bottom`,f,p=v===`item-aligned`;if(v===`item-aligned`){let n=r.getComputedStyle(l),a=Number.parseFloat(n.minHeight)||0,c=Math.max(0,r.innerHeight-x*2),m=(i.width>0||i.height>0)&&(i.top<x+20||i.bottom>r.innerHeight-x-20),ee=s.height>0&&(o.scrollHeight<=o.clientHeight&&s.height>c+.5||a>0&&c+.5<Math.min(o.scrollHeight||s.height,a));if(m||ee){p=!1;let n=(0,e.computeFloatingPosition)({anchorRect:i,contentRect:s,side:ce,align:y,sideOffset:le,alignOffset:ue,avoidCollisions:b,collisionPadding:x,allowedSides:t});u={x:n.x,y:n.y},d=n.side,f=(0,e.computeFloatingTransformOrigin)({side:d,align:n.align,anchorRect:i,popupX:u.x,popupY:u.y})}else{let e=De(i,s,o);u={x:e.x,y:e.y};let t=e.triggerAlignmentRect.left+e.triggerAlignmentRect.width/2,n=e.triggerAlignmentRect.top+e.triggerAlignmentRect.height/2,a=x,c=r.innerHeight-s.height-x,l=e=>b?c<a?a:Math.min(Math.max(e,a),c):e,p=x,m=r.innerWidth-s.width-x;if(u.x=(e=>b?m<p?p:Math.min(Math.max(e,p),m):e)(u.x),e.alignItem){let t=Math.max(0,o.scrollHeight-o.clientHeight),r=e=>n-e,i=t=>e.anchorTopInContent+e.anchorHeight/2-r(t);if(t>0){u.y=l(n-s.height/2);let r=Math.min(Math.max(i(u.y),0),t);o.scrollTop=r,u.y=l(n-(e.anchorTopInContent-r+e.anchorHeight/2)),r=Math.min(Math.max(i(u.y),0),t),o.scrollTop=r,u.y=l(n-(e.anchorTopInContent-r+e.anchorHeight/2))}else o.scrollTop=0,u.y=l(e.y)}else o.scrollTop=0,u.y=l(e.y);d=u.y<i.top?`top`:`bottom`,f=`${Math.min(Math.max(t-u.x,0),s.width)}px ${Math.min(Math.max(n-u.y,0),s.height)}px`}}else{let n=(0,e.computeFloatingPosition)({anchorRect:i,contentRect:s,side:ce,align:y,sideOffset:le,alignOffset:ue,avoidCollisions:b,collisionPadding:x,allowedSides:t});u={x:n.x,y:n.y},d=n.side,f=(0,e.computeFloatingTransformOrigin)({side:d,align:n.align,anchorRect:i,popupX:u.x,popupY:u.y})}let m=v===`item-aligned`&&p?`center`:y;S?(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.setProperty(`--transform-origin`,f),n.style.willChange=`transform`,n.style.margin=`0`,xe(p),l.setAttribute(`data-side`,d),l.setAttribute(`data-align`,m),n!==l&&(n.setAttribute(`data-side`,d),n.setAttribute(`data-align`,m))},q=(0,e.createPositionSync)({observedElements:[c,l],isActive:()=>C,ancestorScroll:S,onUpdate:K,ignoreScrollTarget:e=>e instanceof Node&&l.contains(e)}),J=(t,n=!0,r=!0)=>{let i=ye();for(let a=0;a<F.length;a++){let o=F[a];a===t?(o.setAttribute(`data-highlighted`,``),r&&(0,e.ensureItemVisibleInContainer)(o,i),n&&o.focus()):o.removeAttribute(`data-highlighted`)}E=t},Y=()=>{for(let e of P)e.removeAttribute(`data-highlighted`);E=-1},Oe=()=>{Y(),(0,e.focusElement)(l)},ke=e=>{a.setAttribute(`data-state`,e),c.setAttribute(`data-state`,e),l.setAttribute(`data-state`,e),e===`open`?(a.setAttribute(`data-open`,``),c.setAttribute(`data-open`,``),l.setAttribute(`data-open`,``),a.removeAttribute(`data-closed`),c.removeAttribute(`data-closed`),l.removeAttribute(`data-closed`)):(a.setAttribute(`data-closed`,``),c.setAttribute(`data-closed`,``),l.setAttribute(`data-closed`,``),a.removeAttribute(`data-open`),c.removeAttribute(`data-open`),l.removeAttribute(`data-open`))},Ae=()=>{requestAnimationFrame(()=>{T&&document.contains(T)?(0,e.focusElement)(T):c&&document.contains(c)&&(0,e.focusElement)(c),T=null})},je=()=>{pe||(z.restore(),l.hidden=!0,B?Ae():T=null)},X=(0,e.createPresenceLifecycle)({element:l,onExitComplete:je}),Me=()=>{u&&(w===null?(u.textContent=ne,c.setAttribute(`data-placeholder`,``)):(u.textContent=W(P.find(e=>e.dataset.value===w),w),c.removeAttribute(`data-placeholder`)))},Z=(t,n={})=>{let{skipFocusRestore:r=!1,immediate:i=!1}=n;if(C!==t&&!(h&&t)){if(t){let t=M;M=!1,B=!0,T=document.activeElement,C=!0,(0,e.setAria)(c,`expanded`,!0),z.mount(),l.hidden=!1,ke(`open`),X.enter(),S&&!R&&((0,e.lockScroll)(),R=!0),_e(),k=!1;let n=F.findIndex(e=>e.dataset.value===w);n>=0?J(n,!1,!1):Y(),q.start(),K(),q.update(),requestAnimationFrame(()=>{if(C&&(K(),q.update(),t&&de&&fe!==`touch`&&(A!==0||j!==0))){let e=document.elementFromPoint(A,j)?.closest?.(`[data-slot="select-item"]`);if(e&&!V(e)&&l.contains(e)){let t=I.get(e);t!==void 0&&J(t,!0,!1)}}}),l.focus()}else C=!1,M=!1,A=0,j=0,fe=``,(0,e.setAria)(c,`expanded`,!1),ke(`closed`),Y(),D=``,k=!1,B=!r,R&&=((0,e.unlockScroll)(),!1),q.stop(),i?(X.cleanup(),je()):X.exit();(0,e.emit)(a,`select:open-change`,{open:C}),oe?.(C)}},Q=(t,n=!1)=>{if(w===t&&!n)return;let r=w;w=t,L&&(L.value=t??``),t===null?a.removeAttribute(`data-value`):a.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`));Me(),!n&&r!==t&&((0,e.emit)(a,`select:change`,{value:t}),ae?.(t))},Ne=e=>{if(V(e))return;let t=e.dataset.value;t!==void 0&&(Q(t),Z(!1,{immediate:!0}))},Pe=e=>{let t=F.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),k=!0,J(E===-1?0:Math.min(E+1,t-1));break;case`ArrowUp`:e.preventDefault(),k=!0,J(E===-1?t-1:Math.max(E-1,0));break;case`Home`:e.preventDefault(),k=!0,J(0);break;case`End`:e.preventDefault(),k=!0,J(t-1);break;case`Enter`:case` `:e.preventDefault(),E>=0&&Ne(F[E]);break;case`Tab`:Z(!1,{skipFocusRestore:!0});break;case`Escape`:e.preventDefault(),Z(!1);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),Fe(e.key.toLowerCase()))}},Fe=e=>{O&&clearTimeout(O),O=setTimeout(()=>{D=``},500),D+=e;let t=F.findIndex(e=>W(e).toLowerCase().startsWith(D));if(t===-1&&D.length===1){let n=E+1;for(let r=0;r<F.length;r++){let i=(n+r)%F.length;if(W(F[i]).toLowerCase().startsWith(e)){t=i;break}}}t!==-1&&(k=!0,J(t))};(0,e.setAria)(c,`expanded`,!1),l.hidden=!0,xe(),ke(`closed`),_e(),Q(w,!0),N.push((0,e.on)(c,`pointerdown`,e=>{A=e.clientX,j=e.clientY,fe=e.pointerType,M=!0}),(0,e.on)(c,`click`,()=>{h||Z(!C)}),(0,e.on)(c,`keydown`,e=>{if(!h)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),M=!1,Z(!0);break}})),N.push((0,e.on)(l,`keydown`,Pe),(0,e.on)(l,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&Ne(t)}),(0,e.on)(l,`pointermove`,e=>{if(!de||!me(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(k&&(k=!1,t&&I.get(t)===E)))if(t&&!V(t)){let e=I.get(t);e!==void 0&&e!==E&&J(e,!0)}else Oe()}),(0,e.on)(l,`pointerleave`,e=>{!de||!me(e)||k||Oe()})),N.push((0,e.createDismissLayer)({root:a,isOpen:()=>C,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),N.push((0,e.on)(a,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&Q(t.value),t?.open!==void 0&&Z(t.open)}));let $={get value(){return w},get isOpen(){return C},select:e=>Q(e),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{pe=!0,O&&clearTimeout(O),q.stop(),X.cleanup(),z.cleanup(),R&&=((0,e.unlockScroll)(),!1),N.forEach(e=>e()),N.length=0,L&&L.parentNode&&L.parentNode.removeChild(L),(0,e.clearRootBinding)(a,i,$)}};return(0,e.setRootBinding)(a,i,$),te&&Z(!0),$}function o(t=document){let n=[];for(let r of(0,e.getRoots)(t,`select`))(0,e.hasRootBinding)(r,i)||n.push(a(r));return n}exports.create=o,exports.createSelect=a;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{clearRootBinding as e,computeFloatingPosition as t,computeFloatingTransformOrigin as n,createDismissLayer as r,createPortalLifecycle as i,createPositionSync as a,createPresenceLifecycle as o,emit as s,ensureId as c,ensureItemVisibleInContainer as l,focusElement as u,getDataBool as d,getDataEnum as f,getDataNumber as ee,getDataString as p,getPart as m,getParts as te,getRoots as h,hasRootBinding as g,lockScroll as ne,measurePopupContentRect as re,on as _,reuseRootBinding as ie,setAria as v,setRootBinding as ae,unlockScroll as oe}from"@data-slot/core";const se=[`top`,`bottom`],ce=[`start`,`center`,`end`],le=[`item-aligned`,`popper`],y=`@data-slot/select`;function b(h,g={}){let b=ie(h,y,`[@data-slot/select] createSelect() called more than once for the same root. Returning the existing controller. Destroy it before rebinding with new options.`);if(b)return b;let x=m(h,`select-trigger`),S=m(h,`select-content`),C=m(h,`select-value`),ue=m(h,`select-positioner`),w=ue&&S&&ue.contains(S)?ue:null,de=m(h,`select-portal`),fe=de&&w&&de.contains(w)?de:null;if(!x||!S)throw Error(`Select requires trigger and content slots`);let pe=g.defaultValue??p(h,`defaultValue`)??null,me=g.defaultOpen??d(h,`defaultOpen`)??!1,he=g.placeholder??p(h,`placeholder`)??(C?p(C,`placeholder`):void 0)??``,T=g.disabled??d(h,`disabled`)??!1,ge=g.required??d(h,`required`)??!1,_e=g.name??p(h,`name`)??null,ve=g.onValueChange,ye=g.onOpenChange,E=(e,t)=>f(S,e,t)??(w?f(w,e,t):void 0)??f(h,e,t),D=e=>ee(S,e)??(w?ee(w,e):void 0)??ee(h,e),be=e=>d(S,e)??(w?d(w,e):void 0)??d(h,e),O=g.position??E(`position`,le)??`item-aligned`,xe=g.side??E(`side`,se)??`bottom`,Se=g.align??E(`align`,ce)??`start`,Ce=g.sideOffset??D(`sideOffset`)??4,we=g.alignOffset??D(`alignOffset`)??0,Te=g.avoidCollisions??be(`avoidCollisions`)??!0,k=g.collisionPadding??D(`collisionPadding`)??8,Ee=g.lockScroll??d(h,`lockScroll`)??!0,De=g.highlightItemOnHover??d(h,`highlightItemOnHover`)??!0,A=!1,j=pe,M=null,N=-1,P=``,F=null,I=!1,L=0,R=0,Oe=``,z=!1,B=[],V=[],H=[],U=new Map,W=null,G=!1,K=i({content:S,root:h,wrapperSlot:w?void 0:`select-positioner`,container:w??void 0,mountTarget:w?fe??w:void 0}),ke=!1,Ae=!0,q=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,je=e=>e.pointerType!==`touch`,Me=c(x,`select-trigger`),Ne=c(S,`select-content`);x.setAttribute(`role`,`combobox`),x.setAttribute(`aria-haspopup`,`listbox`),x.setAttribute(`aria-controls`,Ne),x.hasAttribute(`type`)||x.setAttribute(`type`,`button`),S.setAttribute(`role`,`listbox`),S.setAttribute(`aria-labelledby`,Me),S.tabIndex=-1;let Pe=document.querySelector(`label[for="${CSS.escape(Me)}"]`);if(Pe){let e=c(Pe,`select-label`),t=x.getAttribute(`aria-labelledby`);x.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),B.push(_(Pe,`click`,e=>{e.preventDefault(),T||Z(!A)}))}T&&(x.setAttribute(`aria-disabled`,`true`),x.setAttribute(`data-disabled`,``),x instanceof HTMLButtonElement&&(x.disabled=!0)),ge&&x.setAttribute(`aria-required`,`true`),_e&&(W=document.createElement(`input`),W.type=`hidden`,W.name=_e,W.value=j??``,h.appendChild(W));let Fe=()=>{V=te(S,`select-item`);for(let e of V)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?(v(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(v(e,`selected`,!1),e.removeAttribute(`data-selected`));H=V.filter(e=>!q(e)),U=new Map(H.map((e,t)=>[e,t]));let e=te(S,`select-group`);for(let t of e){t.setAttribute(`role`,`group`);let e=m(t,`select-label`);if(e){let n=c(e,`select-label`);t.setAttribute(`aria-labelledby`,n)}}},Ie=()=>m(S,`select-viewport`),Le=()=>Ie()??S,Re=()=>{S.setAttribute(`data-position`,O),S.setAttribute(`data-align-trigger`,O===`item-aligned`?`true`:`false`);let e=Ie();e&&e.setAttribute(`data-position`,O)},ze=(e,t,n,r)=>{let i=0,a=e;for(;a&&a!==t;){i+=a.offsetTop;let e=a.offsetParent;if(!(e instanceof HTMLElement)){i=NaN;break}e!==t&&(i+=e.clientTop),a=e}return a===t&&Number.isFinite(i)?i:e.getBoundingClientRect().top-n-t.clientTop+r},Be=(e,t,n)=>ze(e,S,t.top,n.scrollTop),Ve=(e,t,n)=>{let r=N>=0?H[N]:void 0,i=V.find(e=>e.dataset.value===j)??r??H[0],a=e.left,o,s=0,c=e.height;return i?(s=Be(i,t,n),c=i.getBoundingClientRect().height||i.offsetHeight||e.height,o=e.top+e.height/2-S.clientTop-s-c/2):o=e.top,{x:a,y:o,alignItem:i,itemTopInContent:s,itemHeight:c}},He=()=>{let e=K.container,r=h.ownerDocument.defaultView??window,i=x.getBoundingClientRect(),a=Le();S.style.minWidth=`${i.width}px`;let o=re(S),s,c=`bottom`;if(O===`item-aligned`){let e=Ve(i,o,a);s={x:e.x,y:e.y};let t=i.top+i.height/2,n=k,l=r.innerHeight-o.height-k,u=e=>Te?l<n?n:Math.min(Math.max(e,n),l):e,d=k,f=r.innerWidth-o.width-k;if(s.x=(e=>Te?f<d?d:Math.min(Math.max(e,d),f):e)(s.x),e.alignItem){let n=Math.max(0,a.scrollHeight-a.clientHeight),r=e=>t-(e+S.clientTop),i=t=>e.itemTopInContent+e.itemHeight/2-r(t);if(n>0){s.y=u(t-o.height/2);let r=Math.min(Math.max(i(s.y),0),n);a.scrollTop=r,s.y=u(t-(S.clientTop+e.itemTopInContent-r+e.itemHeight/2)),r=Math.min(Math.max(i(s.y),0),n),a.scrollTop=r,s.y=u(t-(S.clientTop+e.itemTopInContent-r+e.itemHeight/2))}else a.scrollTop=0,s.y=u(e.y)}else a.scrollTop=0,s.y=u(e.y);c=s.y<i.top?`top`:`bottom`}else{let e=t({anchorRect:i,contentRect:o,side:xe,align:Se,sideOffset:Ce,alignOffset:we,avoidCollisions:Te,collisionPadding:k,allowedSides:se});s={x:e.x,y:e.y},c=e.side}let l=O===`item-aligned`?`center`:Se,u=n({side:c,align:l,anchorRect:i,popupX:s.x,popupY:s.y});Ee?(e.style.position=`fixed`,e.style.top=`0px`,e.style.left=`0px`,e.style.transform=`translate3d(${s.x}px, ${s.y}px, 0)`):(e.style.position=`absolute`,e.style.top=`0px`,e.style.left=`0px`,e.style.transform=`translate3d(${s.x+r.scrollX}px, ${s.y+r.scrollY}px, 0)`),e.style.setProperty(`--transform-origin`,u),e.style.willChange=`transform`,e.style.margin=`0`,Re(),S.setAttribute(`data-side`,c),S.setAttribute(`data-align`,l),e!==S&&(e.setAttribute(`data-side`,c),e.setAttribute(`data-align`,l))},J=a({observedElements:[x,S],isActive:()=>A,ancestorScroll:Ee,onUpdate:He,ignoreScrollTarget:e=>e instanceof Node&&S.contains(e)}),Y=(e,t=!0,n=!0)=>{let r=Le();for(let i=0;i<H.length;i++){let a=H[i];i===e?(a.setAttribute(`data-highlighted`,``),n&&l(a,r),t&&a.focus()):a.removeAttribute(`data-highlighted`)}N=e},Ue=()=>{for(let e of V)e.removeAttribute(`data-highlighted`);N=-1},We=()=>{Ue(),u(S)},Ge=e=>{h.setAttribute(`data-state`,e),x.setAttribute(`data-state`,e),S.setAttribute(`data-state`,e),e===`open`?(h.setAttribute(`data-open`,``),x.setAttribute(`data-open`,``),S.setAttribute(`data-open`,``),h.removeAttribute(`data-closed`),x.removeAttribute(`data-closed`),S.removeAttribute(`data-closed`)):(h.setAttribute(`data-closed`,``),x.setAttribute(`data-closed`,``),S.setAttribute(`data-closed`,``),h.removeAttribute(`data-open`),x.removeAttribute(`data-open`),S.removeAttribute(`data-open`))},Ke=()=>{requestAnimationFrame(()=>{M&&document.contains(M)?u(M):x&&document.contains(x)&&u(x),M=null})},qe=()=>{ke||(K.restore(),S.hidden=!0,Ae?Ke():M=null)},X=o({element:S,onExitComplete:qe}),Je=()=>{if(C)if(j===null)C.textContent=he,x.setAttribute(`data-placeholder`,``);else{let e=V.find(e=>e.dataset.value===j);C.textContent=e?.dataset.label??e?.textContent?.trim()??j,x.removeAttribute(`data-placeholder`)}},Z=(e,t={})=>{let{skipFocusRestore:n=!1,immediate:r=!1}=t;if(A!==e&&!(T&&e)){if(e){let e=z;z=!1,Ae=!0,M=document.activeElement,A=!0,v(x,`expanded`,!0),K.mount(),S.hidden=!1,Ge(`open`),X.enter(),Ee&&!G&&(ne(),G=!0),Fe(),I=!1;let t=H.findIndex(e=>e.dataset.value===j);t>=0?Y(t,!1,!1):Ue(),J.start(),He(),J.update(),requestAnimationFrame(()=>{if(A&&(He(),J.update(),e&&De&&Oe!==`touch`&&(L!==0||R!==0))){let e=document.elementFromPoint(L,R)?.closest?.(`[data-slot="select-item"]`);if(e&&!q(e)&&S.contains(e)){let t=U.get(e);t!==void 0&&Y(t,!0,!1)}}}),S.focus()}else A=!1,z=!1,L=0,R=0,Oe=``,v(x,`expanded`,!1),Ge(`closed`),Ue(),P=``,I=!1,Ae=!n,G&&=(oe(),!1),J.stop(),r?(X.cleanup(),qe()):X.exit();s(h,`select:open-change`,{open:A}),ye?.(A)}},Q=(e,t=!1)=>{if(j===e&&!t)return;let n=j;j=e,W&&(W.value=e??``),e===null?h.removeAttribute(`data-value`):h.setAttribute(`data-value`,e);for(let t of V)t.dataset.value===e?(v(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(v(t,`selected`,!1),t.removeAttribute(`data-selected`));Je(),!t&&n!==e&&(s(h,`select:change`,{value:e}),ve?.(e))},Ye=e=>{if(q(e))return;let t=e.dataset.value;t!==void 0&&(Q(t),Z(!1,{immediate:!0}))},Xe=e=>{let t=H.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&&Ye(H[N]);break;case`Tab`:Z(!1,{skipFocusRestore:!0});break;case`Escape`:e.preventDefault(),Z(!1);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),Ze(e.key.toLowerCase()))}},Ze=e=>{F&&clearTimeout(F),F=setTimeout(()=>{P=``},500),P+=e;let t=H.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(P));if(t===-1&&P.length===1){let n=N+1;for(let r=0;r<H.length;r++){let i=(n+r)%H.length;if((H[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(I=!0,Y(t))};v(x,`expanded`,!1),S.hidden=!0,Re(),Ge(`closed`),Fe(),Q(j,!0),B.push(_(x,`pointerdown`,e=>{L=e.clientX,R=e.clientY,Oe=e.pointerType,z=!0}),_(x,`click`,()=>{T||Z(!A)}),_(x,`keydown`,e=>{if(!T)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),z=!1,Z(!0);break}})),B.push(_(S,`keydown`,Xe),_(S,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&Ye(t)}),_(S,`pointermove`,e=>{if(!De||!je(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(I&&(I=!1,t&&U.get(t)===N)))if(t&&!q(t)){let e=U.get(t);e!==void 0&&e!==N&&Y(e,!0)}else We()}),_(S,`pointerleave`,e=>{!De||!je(e)||I||We()})),B.push(r({root:h,isOpen:()=>A,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),B.push(_(h,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&Q(t.value),t?.open!==void 0&&Z(t.open)}));let $={get value(){return j},get isOpen(){return A},select:e=>Q(e),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{ke=!0,F&&clearTimeout(F),J.stop(),X.cleanup(),K.cleanup(),G&&=(oe(),!1),B.forEach(e=>e()),B.length=0,W&&W.parentNode&&W.parentNode.removeChild(W),e(h,y,$)}};return ae(h,y,$),me&&Z(!0),$}function x(e=document){let t=[];for(let n of h(e,`select`))g(n,y)||t.push(b(n));return t}export{x as create,b as createSelect};
|
|
1
|
+
import{clearRootBinding as e,computeFloatingPosition as t,computeFloatingTransformOrigin as n,createDismissLayer as r,createPortalLifecycle as i,createPositionSync as a,createPresenceLifecycle as o,emit as s,ensureId as c,ensureItemVisibleInContainer as l,focusElement as u,getDataBool as d,getDataEnum as f,getDataNumber as p,getDataString as m,getPart as h,getParts as ee,getRoots as g,hasRootBinding as _,lockScroll as te,measurePopupContentRect as ne,on as v,reuseRootBinding as re,setAria as y,setRootBinding as ie,unlockScroll as ae}from"@data-slot/core";const oe=[`top`,`bottom`],se=[`start`,`center`,`end`],ce=[`item-aligned`,`popper`],b=`@data-slot/select`;function x(g,_={}){let x=re(g,b,`[@data-slot/select] createSelect() called more than once for the same root. Returning the existing controller. Destroy it before rebinding with new options.`);if(x)return x;let S=h(g,`select-trigger`),C=h(g,`select-content`),w=h(g,`select-value`),le=h(g,`select-positioner`),T=le&&C&&le.contains(C)?le:null,ue=h(g,`select-portal`),de=ue&&T&&ue.contains(T)?ue:null;if(!S||!C)throw Error(`Select requires trigger and content slots`);let fe=_.defaultValue??m(g,`defaultValue`)??null,pe=_.defaultOpen??d(g,`defaultOpen`)??!1,me=_.placeholder??m(g,`placeholder`)??(w?m(w,`placeholder`):void 0)??``,E=_.disabled??d(g,`disabled`)??!1,he=_.required??d(g,`required`)??!1,ge=_.name??m(g,`name`)??null,_e=_.onValueChange,ve=_.onOpenChange,ye=(e,t)=>f(C,e,t)??(T?f(T,e,t):void 0)??f(g,e,t),be=e=>p(C,e)??(T?p(T,e):void 0)??p(g,e),xe=e=>d(C,e)??(T?d(T,e):void 0)??d(g,e),D=_.position??ye(`position`,ce)??`item-aligned`,Se=_.side??ye(`side`,oe)??`bottom`,Ce=_.align??ye(`align`,se)??`start`,we=_.sideOffset??be(`sideOffset`)??4,Te=_.alignOffset??be(`alignOffset`)??0,O=_.avoidCollisions??xe(`avoidCollisions`)??!0,k=_.collisionPadding??be(`collisionPadding`)??8,Ee=_.lockScroll??d(g,`lockScroll`)??!0,De=_.highlightItemOnHover??d(g,`highlightItemOnHover`)??!0,A=!1,j=fe,M=null,N=-1,P=``,F=null,I=!1,L=0,R=0,Oe=``,z=!1,B=[],V=[],H=[],U=new Map,W=null,G=!1,K=i({content:C,root:g,wrapperSlot:T?void 0:`select-positioner`,container:T??void 0,mountTarget:T?de??T:void 0}),ke=!1,Ae=!0,q=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,je=e=>e.pointerType!==`touch`,Me=c(S,`select-trigger`),Ne=c(C,`select-content`);S.setAttribute(`role`,`combobox`),S.setAttribute(`aria-haspopup`,`listbox`),S.setAttribute(`aria-controls`,Ne),S.hasAttribute(`type`)||S.setAttribute(`type`,`button`),C.setAttribute(`role`,`listbox`),C.setAttribute(`aria-labelledby`,Me),C.tabIndex=-1;let Pe=document.querySelector(`label[for="${CSS.escape(Me)}"]`);if(Pe){let e=c(Pe,`select-label`),t=S.getAttribute(`aria-labelledby`);S.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),B.push(v(Pe,`click`,e=>{e.preventDefault(),E||Q(!A)}))}E&&(S.setAttribute(`aria-disabled`,`true`),S.setAttribute(`data-disabled`,``),S instanceof HTMLButtonElement&&(S.disabled=!0)),he&&S.setAttribute(`aria-required`,`true`),ge&&(W=document.createElement(`input`),W.type=`hidden`,W.name=ge,W.value=j??``,g.appendChild(W));let Fe=()=>{V=ee(C,`select-item`);for(let e of V)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?(y(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(y(e,`selected`,!1),e.removeAttribute(`data-selected`));H=V.filter(e=>!q(e)),U=new Map(H.map((e,t)=>[e,t]));let e=ee(C,`select-group`);for(let t of e){t.setAttribute(`role`,`group`);let e=h(t,`select-label`);if(e){let n=c(e,`select-label`);t.setAttribute(`aria-labelledby`,n)}}},Ie=()=>h(C,`select-viewport`),Le=()=>Ie()??C,Re=e=>h(e,`select-item-text`),ze=e=>e?.textContent?.trim()||void 0,Be=(e,t=``)=>e?(e.dataset.label?.trim()||void 0)??ze(Re(e))??ze(e)??t:t,Ve=(e=D===`item-aligned`)=>{C.setAttribute(`data-position`,D),C.setAttribute(`data-align-trigger`,e?`true`:`false`);let t=Ie();t&&t.setAttribute(`data-position`,D)},J=e=>{if(!e)return null;let t=e.getBoundingClientRect();return t.width>0||t.height>0?t:null},He=e=>J(w)??e,Ue=(e,t,n,r,i)=>{let a=i===`top`?`offsetTop`:`offsetLeft`,o=i===`top`?`clientTop`:`clientLeft`,s=i===`top`?`top`:`left`,c=0,l=e;for(;l&&l!==t;){c+=l[a];let e=l.offsetParent;if(!(e instanceof HTMLElement)){c=NaN;break}e!==t&&(c+=e[o]),l=e}return l===t&&Number.isFinite(c)?c:e.getBoundingClientRect()[s]-n[s]-t[o]+r},We=(e,t,n,r,i)=>Ue(e,t,n,r,i)+(i===`top`?t.clientTop:t.clientLeft),Ge=(e,t,n)=>We(e,C,t,n.scrollTop,`top`),Ke=e=>{let t=Re(e);return J(t)?t:e},qe=(e,t,n)=>{let r=N>=0?H[N]:void 0,i=V.find(e=>e.dataset.value===j)??r??H[0],a=He(e),o=J(w),s=e.left,c,l=0,u=a.height;if(i){let e=Re(i),r=!!(o&&J(e)),d=r&&e?e:Ke(i),f=J(d)??d.getBoundingClientRect();l=Ge(d,t,n),u=f.height||d.offsetHeight||i.getBoundingClientRect().height||i.offsetHeight||a.height,r&&o&&(s=o.left-(f.left-t.left)),c=a.top+a.height/2-l-u/2}else c=e.top;return{x:s,y:c,alignItem:i,anchorTopInContent:l,anchorHeight:u,triggerAlignmentRect:a}},Je=()=>{let e=K.container,r=g.ownerDocument.defaultView??window,i=S.getBoundingClientRect(),a=Le();C.style.minWidth=`${i.width}px`;let o=ne(C),s,c=`bottom`,l,u=D===`item-aligned`;if(D===`item-aligned`){let e=r.getComputedStyle(C),d=Number.parseFloat(e.minHeight)||0,f=Math.max(0,r.innerHeight-k*2),p=(i.width>0||i.height>0)&&(i.top<k+20||i.bottom>r.innerHeight-k-20),m=o.height>0&&(a.scrollHeight<=a.clientHeight&&o.height>f+.5||d>0&&f+.5<Math.min(a.scrollHeight||o.height,d));if(p||m){u=!1;let e=t({anchorRect:i,contentRect:o,side:Se,align:Ce,sideOffset:we,alignOffset:Te,avoidCollisions:O,collisionPadding:k,allowedSides:oe});s={x:e.x,y:e.y},c=e.side,l=n({side:c,align:e.align,anchorRect:i,popupX:s.x,popupY:s.y})}else{let e=qe(i,o,a);s={x:e.x,y:e.y};let t=e.triggerAlignmentRect.left+e.triggerAlignmentRect.width/2,n=e.triggerAlignmentRect.top+e.triggerAlignmentRect.height/2,u=k,d=r.innerHeight-o.height-k,f=e=>O?d<u?u:Math.min(Math.max(e,u),d):e,p=k,m=r.innerWidth-o.width-k;if(s.x=(e=>O?m<p?p:Math.min(Math.max(e,p),m):e)(s.x),e.alignItem){let t=Math.max(0,a.scrollHeight-a.clientHeight),r=e=>n-e,i=t=>e.anchorTopInContent+e.anchorHeight/2-r(t);if(t>0){s.y=f(n-o.height/2);let r=Math.min(Math.max(i(s.y),0),t);a.scrollTop=r,s.y=f(n-(e.anchorTopInContent-r+e.anchorHeight/2)),r=Math.min(Math.max(i(s.y),0),t),a.scrollTop=r,s.y=f(n-(e.anchorTopInContent-r+e.anchorHeight/2))}else a.scrollTop=0,s.y=f(e.y)}else a.scrollTop=0,s.y=f(e.y);c=s.y<i.top?`top`:`bottom`,l=`${Math.min(Math.max(t-s.x,0),o.width)}px ${Math.min(Math.max(n-s.y,0),o.height)}px`}}else{let e=t({anchorRect:i,contentRect:o,side:Se,align:Ce,sideOffset:we,alignOffset:Te,avoidCollisions:O,collisionPadding:k,allowedSides:oe});s={x:e.x,y:e.y},c=e.side,l=n({side:c,align:e.align,anchorRect:i,popupX:s.x,popupY:s.y})}let d=D===`item-aligned`&&u?`center`:Ce;Ee?(e.style.position=`fixed`,e.style.top=`0px`,e.style.left=`0px`,e.style.transform=`translate3d(${s.x}px, ${s.y}px, 0)`):(e.style.position=`absolute`,e.style.top=`0px`,e.style.left=`0px`,e.style.transform=`translate3d(${s.x+r.scrollX}px, ${s.y+r.scrollY}px, 0)`),e.style.setProperty(`--transform-origin`,l),e.style.willChange=`transform`,e.style.margin=`0`,Ve(u),C.setAttribute(`data-side`,c),C.setAttribute(`data-align`,d),e!==C&&(e.setAttribute(`data-side`,c),e.setAttribute(`data-align`,d))},Y=a({observedElements:[S,C],isActive:()=>A,ancestorScroll:Ee,onUpdate:Je,ignoreScrollTarget:e=>e instanceof Node&&C.contains(e)}),X=(e,t=!0,n=!0)=>{let r=Le();for(let i=0;i<H.length;i++){let a=H[i];i===e?(a.setAttribute(`data-highlighted`,``),n&&l(a,r),t&&a.focus()):a.removeAttribute(`data-highlighted`)}N=e},Ye=()=>{for(let e of V)e.removeAttribute(`data-highlighted`);N=-1},Xe=()=>{Ye(),u(C)},Ze=e=>{g.setAttribute(`data-state`,e),S.setAttribute(`data-state`,e),C.setAttribute(`data-state`,e),e===`open`?(g.setAttribute(`data-open`,``),S.setAttribute(`data-open`,``),C.setAttribute(`data-open`,``),g.removeAttribute(`data-closed`),S.removeAttribute(`data-closed`),C.removeAttribute(`data-closed`)):(g.setAttribute(`data-closed`,``),S.setAttribute(`data-closed`,``),C.setAttribute(`data-closed`,``),g.removeAttribute(`data-open`),S.removeAttribute(`data-open`),C.removeAttribute(`data-open`))},Qe=()=>{requestAnimationFrame(()=>{M&&document.contains(M)?u(M):S&&document.contains(S)&&u(S),M=null})},$e=()=>{ke||(K.restore(),C.hidden=!0,Ae?Qe():M=null)},Z=o({element:C,onExitComplete:$e}),et=()=>{w&&(j===null?(w.textContent=me,S.setAttribute(`data-placeholder`,``)):(w.textContent=Be(V.find(e=>e.dataset.value===j),j),S.removeAttribute(`data-placeholder`)))},Q=(e,t={})=>{let{skipFocusRestore:n=!1,immediate:r=!1}=t;if(A!==e&&!(E&&e)){if(e){let e=z;z=!1,Ae=!0,M=document.activeElement,A=!0,y(S,`expanded`,!0),K.mount(),C.hidden=!1,Ze(`open`),Z.enter(),Ee&&!G&&(te(),G=!0),Fe(),I=!1;let t=H.findIndex(e=>e.dataset.value===j);t>=0?X(t,!1,!1):Ye(),Y.start(),Je(),Y.update(),requestAnimationFrame(()=>{if(A&&(Je(),Y.update(),e&&De&&Oe!==`touch`&&(L!==0||R!==0))){let e=document.elementFromPoint(L,R)?.closest?.(`[data-slot="select-item"]`);if(e&&!q(e)&&C.contains(e)){let t=U.get(e);t!==void 0&&X(t,!0,!1)}}}),C.focus()}else A=!1,z=!1,L=0,R=0,Oe=``,y(S,`expanded`,!1),Ze(`closed`),Ye(),P=``,I=!1,Ae=!n,G&&=(ae(),!1),Y.stop(),r?(Z.cleanup(),$e()):Z.exit();s(g,`select:open-change`,{open:A}),ve?.(A)}},$=(e,t=!1)=>{if(j===e&&!t)return;let n=j;j=e,W&&(W.value=e??``),e===null?g.removeAttribute(`data-value`):g.setAttribute(`data-value`,e);for(let t of V)t.dataset.value===e?(y(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(y(t,`selected`,!1),t.removeAttribute(`data-selected`));et(),!t&&n!==e&&(s(g,`select:change`,{value:e}),_e?.(e))},tt=e=>{if(q(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1,{immediate:!0}))},nt=e=>{let t=H.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),I=!0,X(N===-1?0:Math.min(N+1,t-1));break;case`ArrowUp`:e.preventDefault(),I=!0,X(N===-1?t-1:Math.max(N-1,0));break;case`Home`:e.preventDefault(),I=!0,X(0);break;case`End`:e.preventDefault(),I=!0,X(t-1);break;case`Enter`:case` `:e.preventDefault(),N>=0&&tt(H[N]);break;case`Tab`:Q(!1,{skipFocusRestore:!0});break;case`Escape`:e.preventDefault(),Q(!1);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),rt(e.key.toLowerCase()))}},rt=e=>{F&&clearTimeout(F),F=setTimeout(()=>{P=``},500),P+=e;let t=H.findIndex(e=>Be(e).toLowerCase().startsWith(P));if(t===-1&&P.length===1){let n=N+1;for(let r=0;r<H.length;r++){let i=(n+r)%H.length;if(Be(H[i]).toLowerCase().startsWith(e)){t=i;break}}}t!==-1&&(I=!0,X(t))};y(S,`expanded`,!1),C.hidden=!0,Ve(),Ze(`closed`),Fe(),$(j,!0),B.push(v(S,`pointerdown`,e=>{L=e.clientX,R=e.clientY,Oe=e.pointerType,z=!0}),v(S,`click`,()=>{E||Q(!A)}),v(S,`keydown`,e=>{if(!E)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),z=!1,Q(!0);break}})),B.push(v(C,`keydown`,nt),v(C,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&tt(t)}),v(C,`pointermove`,e=>{if(!De||!je(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(I&&(I=!1,t&&U.get(t)===N)))if(t&&!q(t)){let e=U.get(t);e!==void 0&&e!==N&&X(e,!0)}else Xe()}),v(C,`pointerleave`,e=>{!De||!je(e)||I||Xe()})),B.push(r({root:g,isOpen:()=>A,onDismiss:()=>Q(!1),closeOnClickOutside:!0,closeOnEscape:!1})),B.push(v(g,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let it={get value(){return j},get isOpen(){return A},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{ke=!0,F&&clearTimeout(F),Y.stop(),Z.cleanup(),K.cleanup(),G&&=(ae(),!1),B.forEach(e=>e()),B.length=0,W&&W.parentNode&&W.parentNode.removeChild(W),e(g,b,it)}};return ie(g,b,it),pe&&Q(!0),it}function S(e=document){let t=[];for(let n of g(e,`select`))_(n,b)||t.push(x(n));return t}export{S 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.
|
|
3
|
+
"version": "0.2.139",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -36,6 +36,6 @@
|
|
|
36
36
|
],
|
|
37
37
|
"license": "MIT",
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@data-slot/core": "0.2.
|
|
39
|
+
"@data-slot/core": "0.2.139"
|
|
40
40
|
}
|
|
41
41
|
}
|