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