@data-slot/select 0.2.138 → 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/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
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`),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`,le=o.align??g(`align`,n)??`start`,ue=o.sideOffset??_(`sideOffset`)??4,de=o.alignOffset??_(`alignOffset`)??0,y=o.avoidCollisions??se(`avoidCollisions`)??!0,b=o.collisionPadding??_(`collisionPadding`)??8,x=o.lockScroll??(0,e.getDataBool)(a,`lockScroll`)??!0,S=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,M=``,N=!1,P=[],F=[],I=[],L=new Map,R=null,z=!1,B=(0,e.createPortalLifecycle)({content:l,root:a,wrapperSlot:f?void 0:`select-positioner`,container:f??void 0,mountTarget:f?m??f:void 0}),fe=!1,V=!0,H=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,pe=e=>e.pointerType!==`touch`,me=(0,e.ensureId)(c,`select-trigger`),he=(0,e.ensureId)(l,`select-content`);c.setAttribute(`role`,`combobox`),c.setAttribute(`aria-haspopup`,`listbox`),c.setAttribute(`aria-controls`,he),c.hasAttribute(`type`)||c.setAttribute(`type`,`button`),l.setAttribute(`role`,`listbox`),l.setAttribute(`aria-labelledby`,me),l.tabIndex=-1;let U=document.querySelector(`label[for="${CSS.escape(me)}"]`);if(U){let t=(0,e.ensureId)(U,`select-label`),n=c.getAttribute(`aria-labelledby`);c.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),P.push((0,e.on)(U,`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&&(R=document.createElement(`input`),R.type=`hidden`,R.name=ie,R.value=w??``,a.appendChild(R));let ge=()=>{F=(0,e.getParts)(l,`select-item`);for(let t of F)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`));I=F.filter(e=>!H(e)),L=new Map(I.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)}}},_e=()=>(0,e.getPart)(l,`select-viewport`),ve=()=>_e()??l,ye=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(ye(e))??be(e)??t:t,xe=()=>{l.setAttribute(`data-position`,v),l.setAttribute(`data-align-trigger`,v===`item-aligned`?`true`:`false`);let e=_e();e&&e.setAttribute(`data-position`,v)},Se=e=>{if(!e)return null;let t=e.getBoundingClientRect();return t.width>0||t.height>0?t:null},Ce=e=>{let t=Se(u);return t?{top:t.top,bottom:t.bottom,height:t.height,left:e.left,right:e.right,width:e.width}:e},we=(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},Te=(e,t,n)=>we(e,l,t.top,n.scrollTop),Ee=e=>{let t=ye(e);return Se(t)?t:e},De=(e,t,n)=>{let r=E>=0?I[E]:void 0,i=F.find(e=>e.dataset.value===w)??r??I[0],a=Ce(e),o=e.left,s,c=0,u=a.height;if(i){let e=Ee(i);c=Te(e,t,n),u=e.getBoundingClientRect().height||e.offsetHeight||i.getBoundingClientRect().height||i.offsetHeight||a.height,s=a.top+a.height/2-l.clientTop-c-u/2}else s=e.top;return{x:o,y:s,alignItem:i,anchorTopInContent:c,anchorHeight:u,triggerAlignmentRect:a}},G=()=>{let n=B.container,r=a.ownerDocument.defaultView??window,i=c.getBoundingClientRect(),o=ve();l.style.minWidth=`${i.width}px`;let s=(0,e.measurePopupContentRect)(l),u,d=`bottom`,f=i;if(v===`item-aligned`){let e=De(i,s,o);f=e.triggerAlignmentRect,u={x:e.x,y:e.y};let t=e.triggerAlignmentRect.top+e.triggerAlignmentRect.height/2,n=b,a=r.innerHeight-s.height-b,c=e=>y?a<n?n:Math.min(Math.max(e,n),a):e,p=b,m=r.innerWidth-s.width-b;if(u.x=(e=>y?m<p?p:Math.min(Math.max(e,p),m):e)(u.x),e.alignItem){let n=Math.max(0,o.scrollHeight-o.clientHeight),r=e=>t-(e+l.clientTop),i=t=>e.anchorTopInContent+e.anchorHeight/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.anchorTopInContent-r+e.anchorHeight/2)),r=Math.min(Math.max(i(u.y),0),n),o.scrollTop=r,u.y=c(t-(l.clientTop+e.anchorTopInContent-r+e.anchorHeight/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:ce,align:le,sideOffset:ue,alignOffset:de,avoidCollisions:y,collisionPadding:b,allowedSides:t});u={x:n.x,y:n.y},d=n.side}let p=v===`item-aligned`?`center`:le,m=(0,e.computeFloatingTransformOrigin)({side:d,align:p,anchorRect:f,popupX:u.x,popupY:u.y});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.setProperty(`--transform-origin`,m),n.style.willChange=`transform`,n.style.margin=`0`,xe(),l.setAttribute(`data-side`,d),l.setAttribute(`data-align`,p),n!==l&&(n.setAttribute(`data-side`,d),n.setAttribute(`data-align`,p))},K=(0,e.createPositionSync)({observedElements:[c,l],isActive:()=>C,ancestorScroll:x,onUpdate:G,ignoreScrollTarget:e=>e instanceof Node&&l.contains(e)}),q=(t,n=!0,r=!0)=>{let i=ve();for(let a=0;a<I.length;a++){let o=I[a];a===t?(o.setAttribute(`data-highlighted`,``),r&&(0,e.ensureItemVisibleInContainer)(o,i),n&&o.focus()):o.removeAttribute(`data-highlighted`)}E=t},J=()=>{for(let e of F)e.removeAttribute(`data-highlighted`);E=-1},Oe=()=>{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`))},ke=()=>{requestAnimationFrame(()=>{T&&document.contains(T)?(0,e.focusElement)(T):c&&document.contains(c)&&(0,e.focusElement)(c),T=null})},Ae=()=>{fe||(B.restore(),l.hidden=!0,V?ke():T=null)},X=(0,e.createPresenceLifecycle)({element:l,onExitComplete:Ae}),je=()=>{u&&(w===null?(u.textContent=ne,c.setAttribute(`data-placeholder`,``)):(u.textContent=W(F.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=N;N=!1,V=!0,T=document.activeElement,C=!0,(0,e.setAria)(c,`expanded`,!0),B.mount(),l.hidden=!1,Y(`open`),X.enter(),x&&!z&&((0,e.lockScroll)(),z=!0),ge(),k=!1;let n=I.findIndex(e=>e.dataset.value===w);n>=0?q(n,!1,!1):J(),K.start(),G(),K.update(),requestAnimationFrame(()=>{if(C&&(G(),K.update(),t&&S&&M!==`touch`&&(A!==0||j!==0))){let e=document.elementFromPoint(A,j)?.closest?.(`[data-slot="select-item"]`);if(e&&!H(e)&&l.contains(e)){let t=L.get(e);t!==void 0&&q(t,!0,!1)}}}),l.focus()}else C=!1,N=!1,A=0,j=0,M=``,(0,e.setAria)(c,`expanded`,!1),Y(`closed`),J(),D=``,k=!1,V=!r,z&&=((0,e.unlockScroll)(),!1),K.stop(),i?(X.cleanup(),Ae()):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,R&&(R.value=t??``),t===null?a.removeAttribute(`data-value`):a.setAttribute(`data-value`,t);for(let n of F)n.dataset.value===t?((0,e.setAria)(n,`selected`,!0),n.setAttribute(`data-selected`,``)):((0,e.setAria)(n,`selected`,!1),n.removeAttribute(`data-selected`));je(),!n&&r!==t&&((0,e.emit)(a,`select:change`,{value:t}),ae?.(t))},Me=e=>{if(H(e))return;let t=e.dataset.value;t!==void 0&&(Q(t),Z(!1,{immediate:!0}))},Ne=e=>{let t=I.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),k=!0,q(E===-1?0:Math.min(E+1,t-1));break;case`ArrowUp`:e.preventDefault(),k=!0,q(E===-1?t-1:Math.max(E-1,0));break;case`Home`:e.preventDefault(),k=!0,q(0);break;case`End`:e.preventDefault(),k=!0,q(t-1);break;case`Enter`:case` `:e.preventDefault(),E>=0&&Me(I[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(),Pe(e.key.toLowerCase()))}},Pe=e=>{O&&clearTimeout(O),O=setTimeout(()=>{D=``},500),D+=e;let t=I.findIndex(e=>W(e).toLowerCase().startsWith(D));if(t===-1&&D.length===1){let n=E+1;for(let r=0;r<I.length;r++){let i=(n+r)%I.length;if(W(I[i]).toLowerCase().startsWith(e)){t=i;break}}}t!==-1&&(k=!0,q(t))};(0,e.setAria)(c,`expanded`,!1),l.hidden=!0,xe(),Y(`closed`),ge(),Q(w,!0),P.push((0,e.on)(c,`pointerdown`,e=>{A=e.clientX,j=e.clientY,M=e.pointerType,N=!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(),N=!1,Z(!0);break}})),P.push((0,e.on)(l,`keydown`,Ne),(0,e.on)(l,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&Me(t)}),(0,e.on)(l,`pointermove`,e=>{if(!S||!pe(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(k&&(k=!1,t&&L.get(t)===E)))if(t&&!H(t)){let e=L.get(t);e!==void 0&&e!==E&&q(e,!0)}else Oe()}),(0,e.on)(l,`pointerleave`,e=>{!S||!pe(e)||k||Oe()})),P.push((0,e.createDismissLayer)({root:a,isOpen:()=>C,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),P.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:()=>{fe=!0,O&&clearTimeout(O),K.stop(),X.cleanup(),B.cleanup(),z&&=((0,e.unlockScroll)(),!1),P.forEach(e=>e()),P.length=0,R&&R.parentNode&&R.parentNode.removeChild(R),(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;
|
|
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 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,D=(e,t)=>f(C,e,t)??(T?f(T,e,t):void 0)??f(g,e,t),ye=e=>p(C,e)??(T?p(T,e):void 0)??p(g,e),be=e=>d(C,e)??(T?d(T,e):void 0)??d(g,e),O=_.position??D(`position`,ce)??`item-aligned`,xe=_.side??D(`side`,oe)??`bottom`,Se=_.align??D(`align`,se)??`start`,Ce=_.sideOffset??ye(`sideOffset`)??4,we=_.alignOffset??ye(`alignOffset`)??0,Te=_.avoidCollisions??be(`avoidCollisions`)??!0,k=_.collisionPadding??ye(`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||Z(!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=()=>{C.setAttribute(`data-position`,O),C.setAttribute(`data-align-trigger`,O===`item-aligned`?`true`:`false`);let e=Ie();e&&e.setAttribute(`data-position`,O)},He=e=>{if(!e)return null;let t=e.getBoundingClientRect();return t.width>0||t.height>0?t:null},Ue=e=>{let t=He(w);return t?{top:t.top,bottom:t.bottom,height:t.height,left:e.left,right:e.right,width:e.width}:e},We=(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},Ge=(e,t,n)=>We(e,C,t.top,n.scrollTop),Ke=e=>{let t=Re(e);return He(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=Ue(e),o=e.left,s,c=0,l=a.height;if(i){let e=Ke(i);c=Ge(e,t,n),l=e.getBoundingClientRect().height||e.offsetHeight||i.getBoundingClientRect().height||i.offsetHeight||a.height,s=a.top+a.height/2-C.clientTop-c-l/2}else s=e.top;return{x:o,y:s,alignItem:i,anchorTopInContent:c,anchorHeight:l,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=i;if(O===`item-aligned`){let e=qe(i,o,a);l=e.triggerAlignmentRect,s={x:e.x,y:e.y};let t=e.triggerAlignmentRect.top+e.triggerAlignmentRect.height/2,n=k,u=r.innerHeight-o.height-k,d=e=>Te?u<n?n:Math.min(Math.max(e,n),u):e,f=k,p=r.innerWidth-o.width-k;if(s.x=(e=>Te?p<f?f:Math.min(Math.max(e,f),p):e)(s.x),e.alignItem){let n=Math.max(0,a.scrollHeight-a.clientHeight),r=e=>t-(e+C.clientTop),i=t=>e.anchorTopInContent+e.anchorHeight/2-r(t);if(n>0){s.y=d(t-o.height/2);let r=Math.min(Math.max(i(s.y),0),n);a.scrollTop=r,s.y=d(t-(C.clientTop+e.anchorTopInContent-r+e.anchorHeight/2)),r=Math.min(Math.max(i(s.y),0),n),a.scrollTop=r,s.y=d(t-(C.clientTop+e.anchorTopInContent-r+e.anchorHeight/2))}else a.scrollTop=0,s.y=d(e.y)}else a.scrollTop=0,s.y=d(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:oe});s={x:e.x,y:e.y},c=e.side}let u=O===`item-aligned`?`center`:Se,d=n({side:c,align:u,anchorRect:l,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`,d),e.style.willChange=`transform`,e.style.margin=`0`,Ve(),C.setAttribute(`data-side`,c),C.setAttribute(`data-align`,u),e!==C&&(e.setAttribute(`data-side`,c),e.setAttribute(`data-align`,u))},J=a({observedElements:[S,C],isActive:()=>A,ancestorScroll:Ee,onUpdate:Je,ignoreScrollTarget:e=>e instanceof Node&&C.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},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)},X=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`)))},Z=(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`),X.enter(),Ee&&!G&&(te(),G=!0),Fe(),I=!1;let t=H.findIndex(e=>e.dataset.value===j);t>=0?Y(t,!1,!1):Ye(),J.start(),Je(),J.update(),requestAnimationFrame(()=>{if(A&&(Je(),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)&&C.contains(e)){let t=U.get(e);t!==void 0&&Y(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),J.stop(),r?(X.cleanup(),$e()):X.exit();s(g,`select:open-change`,{open:A}),ve?.(A)}},Q=(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&&(Q(t),Z(!1,{immediate:!0}))},nt=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&&tt(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(),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,Y(t))};y(S,`expanded`,!1),C.hidden=!0,Ve(),Ze(`closed`),Fe(),Q(j,!0),B.push(v(S,`pointerdown`,e=>{L=e.clientX,R=e.clientY,Oe=e.pointerType,z=!0}),v(S,`click`,()=>{E||Z(!A)}),v(S,`keydown`,e=>{if(!E)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),z=!1,Z(!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&&Y(e,!0)}else Xe()}),v(C,`pointerleave`,e=>{!De||!je(e)||I||Xe()})),B.push(r({root:g,isOpen:()=>A,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),B.push(v(g,`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&&=(ae(),!1),B.forEach(e=>e()),B.length=0,W&&W.parentNode&&W.parentNode.removeChild(W),e(g,b,$)}};return ie(g,b,$),pe&&Z(!0),$}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};
|
|
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
|
}
|