@data-slot/select 0.2.122 → 0.2.123
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=new WeakMap,a=new WeakSet,o=e=>{typeof process<`u`&&process.env?.NODE_ENV===`production`||a.has(e)||(a.add(e),console.warn(`[@data-slot/select] createSelect() called more than once for the same root. Returning the existing controller. Destroy it before rebinding with new options.`))};function s(a,s={}){let c=i.get(a);if(c)return o(a),c;let l=(0,e.getPart)(a,`select-trigger`),u=(0,e.getPart)(a,`select-content`),d=(0,e.getPart)(a,`select-value`),f=(0,e.getPart)(a,`select-positioner`),p=f&&u&&f.contains(u)?f:null,m=(0,e.getPart)(a,`select-portal`),ee=m&&p&&m.contains(p)?m:null;if(!l||!u)throw Error(`Select requires trigger and content slots`);let te=s.defaultValue??(0,e.getDataString)(a,`defaultValue`)??null,ne=s.defaultOpen??(0,e.getDataBool)(a,`defaultOpen`)??!1,re=s.placeholder??(0,e.getDataString)(a,`placeholder`)??(d?(0,e.getDataString)(d,`placeholder`):void 0)??``,h=s.disabled??(0,e.getDataBool)(a,`disabled`)??!1,ie=s.required??(0,e.getDataBool)(a,`required`)??!1,g=s.name??(0,e.getDataString)(a,`name`)??null,ae=s.onValueChange,oe=s.onOpenChange,_=(t,n)=>(0,e.getDataEnum)(u,t,n)??(p?(0,e.getDataEnum)(p,t,n):void 0)??(0,e.getDataEnum)(a,t,n),v=t=>(0,e.getDataNumber)(u,t)??(p?(0,e.getDataNumber)(p,t):void 0)??(0,e.getDataNumber)(a,t),se=t=>(0,e.getDataBool)(u,t)??(p?(0,e.getDataBool)(p,t):void 0)??(0,e.getDataBool)(a,t),y=s.position??_(`position`,r)??`item-aligned`,ce=s.side??_(`side`,t)??`bottom`,b=s.align??_(`align`,n)??`start`,le=s.sideOffset??v(`sideOffset`)??4,ue=s.alignOffset??v(`alignOffset`)??0,x=s.avoidCollisions??se(`avoidCollisions`)??!0,S=s.collisionPadding??v(`collisionPadding`)??8,C=s.lockScroll??(0,e.getDataBool)(a,`lockScroll`)??!0,w=s.highlightItemOnHover??(0,e.getDataBool)(a,`highlightItemOnHover`)??!0,T=!1,E=te,D=null,O=-1,k=``,A=null,j=!1,M=0,N=0,P=``,F=!1,I=[],L=[],R=[],z=new Map,B=null,V=!1,H=(0,e.createPortalLifecycle)({content:u,root:a,wrapperSlot:p?void 0:`select-positioner`,container:p??void 0,mountTarget:p?ee??p:void 0}),de=!1,U=!0,W=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,fe=e=>e.pointerType!==`touch`,pe=(0,e.ensureId)(l,`select-trigger`),me=(0,e.ensureId)(u,`select-content`);l.setAttribute(`role`,`combobox`),l.setAttribute(`aria-haspopup`,`listbox`),l.setAttribute(`aria-controls`,me),l.hasAttribute(`type`)||l.setAttribute(`type`,`button`),u.setAttribute(`role`,`listbox`),u.setAttribute(`aria-labelledby`,pe),u.tabIndex=-1;let G=document.querySelector(`label[for="${CSS.escape(pe)}"]`);if(G){let t=(0,e.ensureId)(G,`select-label`),n=l.getAttribute(`aria-labelledby`);l.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),I.push((0,e.on)(G,`click`,e=>{e.preventDefault(),h||Q(!T)}))}h&&(l.setAttribute(`aria-disabled`,`true`),l.setAttribute(`data-disabled`,``),l instanceof HTMLButtonElement&&(l.disabled=!0)),ie&&l.setAttribute(`aria-required`,`true`),g&&(B=document.createElement(`input`),B.type=`hidden`,B.name=g,B.value=E??``,a.appendChild(B));let he=()=>{L=(0,e.getParts)(u,`select-item`);for(let t of L)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===E?((0,e.setAria)(t,`selected`,!0),t.setAttribute(`data-selected`,``)):((0,e.setAria)(t,`selected`,!1),t.removeAttribute(`data-selected`));R=L.filter(e=>!W(e)),z=new Map(R.map((e,t)=>[e,t]));let t=(0,e.getParts)(u,`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)(u,`select-viewport`),_e=()=>ge()??u,ve=()=>{u.setAttribute(`data-position`,y),u.setAttribute(`data-align-trigger`,y===`item-aligned`?`true`:`false`);let e=ge();e&&e.setAttribute(`data-position`,y)},ye=(e,t,n)=>{let r=0,i=e;for(;i&&i!==u;){r+=i.offsetTop;let e=i.offsetParent;if(!(e instanceof HTMLElement)){r=NaN;break}i=e}return i===u&&Number.isFinite(r)?r:e.getBoundingClientRect().top-t.top+n.scrollTop},be=(e,t,n)=>{let r=O>=0?R[O]:void 0,i=L.find(e=>e.dataset.value===E)??r??R[0],a=e.left,o,s=0,c=e.height;return i?(s=ye(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=H.container,r=a.ownerDocument.defaultView??window,i=l.getBoundingClientRect(),o=_e();u.style.minWidth=`${i.width}px`;let s=(0,e.measurePopupContentRect)(u),c,d=`bottom`;if(y===`item-aligned`){let e=be(i,s,o);c={x:e.x,y:e.y};let t=i.top+i.height/2,n=S,a=r.innerHeight-s.height-S,l=e=>x?a<n?n:Math.min(Math.max(e,n),a):e,u=S,f=r.innerWidth-s.width-S;if(c.x=(e=>x?f<u?u:Math.min(Math.max(e,u),f):e)(c.x),e.alignItem){let n=Math.max(0,o.scrollHeight-o.clientHeight),r=n=>e.itemTopInContent+e.itemHeight/2-(t-n);if(n>0){c.y=l(t-s.height/2);let i=Math.min(Math.max(r(c.y),0),n);o.scrollTop=i,c.y=l(t-(e.itemTopInContent-i+e.itemHeight/2)),i=Math.min(Math.max(r(c.y),0),n),o.scrollTop=i,c.y=l(t-(e.itemTopInContent-i+e.itemHeight/2))}else o.scrollTop=0,c.y=l(e.y)}else o.scrollTop=0,c.y=l(e.y);d=c.y<i.top?`top`:`bottom`}else{let n=(0,e.computeFloatingPosition)({anchorRect:i,contentRect:s,side:ce,align:b,sideOffset:le,alignOffset:ue,avoidCollisions:x,collisionPadding:S,allowedSides:t});c={x:n.x,y:n.y},d=n.side}let f=y===`item-aligned`?`center`:b,p=(0,e.computeFloatingTransformOrigin)({side:d,align:f,anchorRect:i,popupX:c.x,popupY:c.y});C?(n.style.position=`fixed`,n.style.top=`0px`,n.style.left=`0px`,n.style.transform=`translate3d(${c.x}px, ${c.y}px, 0)`):(n.style.position=`absolute`,n.style.top=`0px`,n.style.left=`0px`,n.style.transform=`translate3d(${c.x+r.scrollX}px, ${c.y+r.scrollY}px, 0)`),n.style.setProperty(`--transform-origin`,p),n.style.willChange=`transform`,n.style.margin=`0`,ve(),u.setAttribute(`data-side`,d),u.setAttribute(`data-align`,f),n!==u&&(n.setAttribute(`data-side`,d),n.setAttribute(`data-align`,f))},q=(0,e.createPositionSync)({observedElements:[l,u],isActive:()=>T,ancestorScroll:C,onUpdate:K,ignoreScrollTarget:e=>e instanceof Node&&u.contains(e)}),J=(t,n=!0,r=!0)=>{let i=_e();for(let a=0;a<R.length;a++){let o=R[a];a===t?(o.setAttribute(`data-highlighted`,``),r&&(0,e.ensureItemVisibleInContainer)(o,i),n&&o.focus()):o.removeAttribute(`data-highlighted`)}O=t},Y=()=>{for(let e of L)e.removeAttribute(`data-highlighted`);O=-1},xe=()=>{Y(),(0,e.focusElement)(u)},X=e=>{a.setAttribute(`data-state`,e),l.setAttribute(`data-state`,e),u.setAttribute(`data-state`,e),e===`open`?(a.setAttribute(`data-open`,``),l.setAttribute(`data-open`,``),u.setAttribute(`data-open`,``),a.removeAttribute(`data-closed`),l.removeAttribute(`data-closed`),u.removeAttribute(`data-closed`)):(a.setAttribute(`data-closed`,``),l.setAttribute(`data-closed`,``),u.setAttribute(`data-closed`,``),a.removeAttribute(`data-open`),l.removeAttribute(`data-open`),u.removeAttribute(`data-open`))},Se=()=>{requestAnimationFrame(()=>{D&&document.contains(D)?(0,e.focusElement)(D):l&&document.contains(l)&&(0,e.focusElement)(l),D=null})},Z=(0,e.createPresenceLifecycle)({element:u,onExitComplete:()=>{de||(H.restore(),u.hidden=!0,U?Se():D=null)}}),Ce=()=>{if(d)if(E===null)d.textContent=re,l.setAttribute(`data-placeholder`,``);else{let e=L.find(e=>e.dataset.value===E);d.textContent=e?.dataset.label??e?.textContent?.trim()??E,l.removeAttribute(`data-placeholder`)}},Q=(t,n=!1)=>{if(T!==t&&!(h&&t)){if(t){let t=F;F=!1,U=!0,D=document.activeElement,T=!0,(0,e.setAria)(l,`expanded`,!0),H.mount(),u.hidden=!1,X(`open`),Z.enter(),C&&!V&&((0,e.lockScroll)(),V=!0),he(),j=!1;let n=R.findIndex(e=>e.dataset.value===E);n>=0?J(n,!1,!1):Y(),q.start(),K(),q.update(),requestAnimationFrame(()=>{if(T&&(K(),q.update(),t&&w&&P!==`touch`&&(M!==0||N!==0))){let e=document.elementFromPoint(M,N)?.closest?.(`[data-slot="select-item"]`);if(e&&!W(e)&&u.contains(e)){let t=z.get(e);t!==void 0&&J(t,!0,!1)}}}),u.focus()}else T=!1,F=!1,M=0,N=0,P=``,(0,e.setAria)(l,`expanded`,!1),X(`closed`),Y(),k=``,j=!1,U=!n,V&&=((0,e.unlockScroll)(),!1),q.stop(),Z.exit();(0,e.emit)(a,`select:open-change`,{open:T}),oe?.(T)}},$=(t,n=!1)=>{if(E===t&&!n)return;let r=E;E=t,B&&(B.value=t??``),t===null?a.removeAttribute(`data-value`):a.setAttribute(`data-value`,t);for(let n of L)n.dataset.value===t?((0,e.setAria)(n,`selected`,!0),n.setAttribute(`data-selected`,``)):((0,e.setAria)(n,`selected`,!1),n.removeAttribute(`data-selected`));Ce(),!n&&r!==t&&((0,e.emit)(a,`select:change`,{value:t}),ae?.(t))},we=e=>{if(W(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},Te=e=>{let t=R.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),j=!0,J(O===-1?0:Math.min(O+1,t-1));break;case`ArrowUp`:e.preventDefault(),j=!0,J(O===-1?t-1:Math.max(O-1,0));break;case`Home`:e.preventDefault(),j=!0,J(0);break;case`End`:e.preventDefault(),j=!0,J(t-1);break;case`Enter`:case` `:e.preventDefault(),O>=0&&we(R[O]);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(),Ee(e.key.toLowerCase()))}},Ee=e=>{A&&clearTimeout(A),A=setTimeout(()=>{k=``},500),k+=e;let t=R.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(k));if(t===-1&&k.length===1){let n=O+1;for(let r=0;r<R.length;r++){let i=(n+r)%R.length;if((R[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(j=!0,J(t))};(0,e.setAria)(l,`expanded`,!1),u.hidden=!0,ve(),X(`closed`),he(),$(E,!0),I.push((0,e.on)(l,`pointerdown`,e=>{M=e.clientX,N=e.clientY,P=e.pointerType,F=!0}),(0,e.on)(l,`click`,()=>{h||Q(!T)}),(0,e.on)(l,`keydown`,e=>{if(!h)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),F=!1,Q(!0);break}})),I.push((0,e.on)(u,`keydown`,Te),(0,e.on)(u,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&we(t)}),(0,e.on)(u,`pointermove`,e=>{if(!w||!fe(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(j&&(j=!1,t&&z.get(t)===O)))if(t&&!W(t)){let e=z.get(t);e!==void 0&&e!==O&&J(e,!0)}else xe()}),(0,e.on)(u,`pointerleave`,e=>{!w||!fe(e)||j||xe()})),I.push((0,e.createDismissLayer)({root:a,isOpen:()=>T,onDismiss:()=>Q(!1),closeOnClickOutside:!0,closeOnEscape:!1})),I.push((0,e.on)(a,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let De={get value(){return E},get isOpen(){return T},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{de=!0,A&&clearTimeout(A),q.stop(),Z.cleanup(),H.cleanup(),V&&=((0,e.unlockScroll)(),!1),I.forEach(e=>e()),I.length=0,B&&B.parentNode&&B.parentNode.removeChild(B),i.delete(a)}};return i.set(a,De),ne&&Q(!0),De}function c(t=document){let n=[];for(let r of(0,e.getRoots)(t,`select`))i.has(r)||n.push(s(r));return n}exports.create=c,exports.createSelect=s;
|
|
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.getRootBinding)(a,i);if(s)return(0,e.warnRootBindingOnce)(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.`),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,h=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,C=o.highlightItemOnHover??(0,e.getDataBool)(a,`highlightItemOnHover`)??!0,w=!1,T=te,E=null,D=-1,O=``,k=null,A=!1,j=0,M=0,N=``,P=!1,F=[],I=[],L=[],R=new Map,z=null,B=!1,V=(0,e.createPortalLifecycle)({content:l,root:a,wrapperSlot:f?void 0:`select-positioner`,container:f??void 0,mountTarget:f?ee??f:void 0}),de=!1,H=!0,U=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,fe=e=>e.pointerType!==`touch`,pe=(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`,pe),l.tabIndex=-1;let W=document.querySelector(`label[for="${CSS.escape(pe)}"]`);if(W){let t=(0,e.ensureId)(W,`select-label`),n=c.getAttribute(`aria-labelledby`);c.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),F.push((0,e.on)(W,`click`,e=>{e.preventDefault(),m||Z(!w)}))}m&&(c.setAttribute(`aria-disabled`,`true`),c.setAttribute(`data-disabled`,``),c instanceof HTMLButtonElement&&(c.disabled=!0)),ie&&c.setAttribute(`aria-required`,`true`),h&&(z=document.createElement(`input`),z.type=`hidden`,z.name=h,z.value=T??``,a.appendChild(z));let he=()=>{I=(0,e.getParts)(l,`select-item`);for(let t of I)t.setAttribute(`role`,`option`),t.hasAttribute(`data-disabled`)||t.hasAttribute(`disabled`)?t.setAttribute(`aria-disabled`,`true`):t.removeAttribute(`aria-disabled`),t.tabIndex=-1,t.dataset.value===T?((0,e.setAria)(t,`selected`,!0),t.setAttribute(`data-selected`,``)):((0,e.setAria)(t,`selected`,!1),t.removeAttribute(`data-selected`));L=I.filter(e=>!U(e)),R=new Map(L.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`,v),l.setAttribute(`data-align-trigger`,v===`item-aligned`?`true`:`false`);let e=ge();e&&e.setAttribute(`data-position`,v)},ye=(e,t,n)=>{let r=0,i=e;for(;i&&i!==l;){r+=i.offsetTop;let e=i.offsetParent;if(!(e instanceof HTMLElement)){r=NaN;break}i=e}return i===l&&Number.isFinite(r)?r:e.getBoundingClientRect().top-t.top+n.scrollTop},be=(e,t,n)=>{let r=D>=0?L[D]:void 0,i=I.find(e=>e.dataset.value===T)??r??L[0],a=e.left,o,s=0,c=e.height;return i?(s=ye(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=V.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(v===`item-aligned`){let e=be(i,s,o);u={x:e.x,y:e.y};let t=i.top+i.height/2,n=x,a=r.innerHeight-s.height-x,c=e=>b?a<n?n:Math.min(Math.max(e,n),a):e,l=x,f=r.innerWidth-s.width-x;if(u.x=(e=>b?f<l?l:Math.min(Math.max(e,l),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=c(t-s.height/2);let i=Math.min(Math.max(r(u.y),0),n);o.scrollTop=i,u.y=c(t-(e.itemTopInContent-i+e.itemHeight/2)),i=Math.min(Math.max(r(u.y),0),n),o.scrollTop=i,u.y=c(t-(e.itemTopInContent-i+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:ce,align:y,sideOffset:le,alignOffset:ue,avoidCollisions:b,collisionPadding:x,allowedSides:t});u={x:n.x,y:n.y},d=n.side}let f=v===`item-aligned`?`center`:y,p=(0,e.computeFloatingTransformOrigin)({side:d,align:f,anchorRect:i,popupX:u.x,popupY:u.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`,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:()=>w,ancestorScroll:S,onUpdate:G,ignoreScrollTarget:e=>e instanceof Node&&l.contains(e)}),q=(t,n=!0,r=!0)=>{let i=_e();for(let a=0;a<L.length;a++){let o=L[a];a===t?(o.setAttribute(`data-highlighted`,``),r&&(0,e.ensureItemVisibleInContainer)(o,i),n&&o.focus()):o.removeAttribute(`data-highlighted`)}D=t},J=()=>{for(let e of I)e.removeAttribute(`data-highlighted`);D=-1},xe=()=>{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`))},Se=()=>{requestAnimationFrame(()=>{E&&document.contains(E)?(0,e.focusElement)(E):c&&document.contains(c)&&(0,e.focusElement)(c),E=null})},X=(0,e.createPresenceLifecycle)({element:l,onExitComplete:()=>{de||(V.restore(),l.hidden=!0,H?Se():E=null)}}),Ce=()=>{if(u)if(T===null)u.textContent=re,c.setAttribute(`data-placeholder`,``);else{let e=I.find(e=>e.dataset.value===T);u.textContent=e?.dataset.label??e?.textContent?.trim()??T,c.removeAttribute(`data-placeholder`)}},Z=(t,n=!1)=>{if(w!==t&&!(m&&t)){if(t){let t=P;P=!1,H=!0,E=document.activeElement,w=!0,(0,e.setAria)(c,`expanded`,!0),V.mount(),l.hidden=!1,Y(`open`),X.enter(),S&&!B&&((0,e.lockScroll)(),B=!0),he(),A=!1;let n=L.findIndex(e=>e.dataset.value===T);n>=0?q(n,!1,!1):J(),K.start(),G(),K.update(),requestAnimationFrame(()=>{if(w&&(G(),K.update(),t&&C&&N!==`touch`&&(j!==0||M!==0))){let e=document.elementFromPoint(j,M)?.closest?.(`[data-slot="select-item"]`);if(e&&!U(e)&&l.contains(e)){let t=R.get(e);t!==void 0&&q(t,!0,!1)}}}),l.focus()}else w=!1,P=!1,j=0,M=0,N=``,(0,e.setAria)(c,`expanded`,!1),Y(`closed`),J(),O=``,A=!1,H=!n,B&&=((0,e.unlockScroll)(),!1),K.stop(),X.exit();(0,e.emit)(a,`select:open-change`,{open:w}),oe?.(w)}},Q=(t,n=!1)=>{if(T===t&&!n)return;let r=T;T=t,z&&(z.value=t??``),t===null?a.removeAttribute(`data-value`):a.setAttribute(`data-value`,t);for(let n of I)n.dataset.value===t?((0,e.setAria)(n,`selected`,!0),n.setAttribute(`data-selected`,``)):((0,e.setAria)(n,`selected`,!1),n.removeAttribute(`data-selected`));Ce(),!n&&r!==t&&((0,e.emit)(a,`select:change`,{value:t}),ae?.(t))},we=e=>{if(U(e))return;let t=e.dataset.value;t!==void 0&&(Q(t),Z(!1))},Te=e=>{let t=L.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),A=!0,q(D===-1?0:Math.min(D+1,t-1));break;case`ArrowUp`:e.preventDefault(),A=!0,q(D===-1?t-1:Math.max(D-1,0));break;case`Home`:e.preventDefault(),A=!0,q(0);break;case`End`:e.preventDefault(),A=!0,q(t-1);break;case`Enter`:case` `:e.preventDefault(),D>=0&&we(L[D]);break;case`Tab`:Z(!1,!0);break;case`Escape`:e.preventDefault(),Z(!1);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),Ee(e.key.toLowerCase()))}},Ee=e=>{k&&clearTimeout(k),k=setTimeout(()=>{O=``},500),O+=e;let t=L.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(O));if(t===-1&&O.length===1){let n=D+1;for(let r=0;r<L.length;r++){let i=(n+r)%L.length;if((L[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(A=!0,q(t))};(0,e.setAria)(c,`expanded`,!1),l.hidden=!0,ve(),Y(`closed`),he(),Q(T,!0),F.push((0,e.on)(c,`pointerdown`,e=>{j=e.clientX,M=e.clientY,N=e.pointerType,P=!0}),(0,e.on)(c,`click`,()=>{m||Z(!w)}),(0,e.on)(c,`keydown`,e=>{if(!m)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),P=!1,Z(!0);break}})),F.push((0,e.on)(l,`keydown`,Te),(0,e.on)(l,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&we(t)}),(0,e.on)(l,`pointermove`,e=>{if(!C||!fe(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(A&&(A=!1,t&&R.get(t)===D)))if(t&&!U(t)){let e=R.get(t);e!==void 0&&e!==D&&q(e,!0)}else xe()}),(0,e.on)(l,`pointerleave`,e=>{!C||!fe(e)||A||xe()})),F.push((0,e.createDismissLayer)({root:a,isOpen:()=>w,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),F.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 T},get isOpen(){return w},select:e=>Q(e),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{de=!0,k&&clearTimeout(k),K.stop(),X.cleanup(),V.cleanup(),B&&=((0,e.unlockScroll)(),!1),F.forEach(e=>e()),F.length=0,z&&z.parentNode&&z.parentNode.removeChild(z),(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.getRootBinding)(r,i)||n.push(a(r));return n}exports.create=o,exports.createSelect=a;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{computeFloatingPosition as e,computeFloatingTransformOrigin as t,createDismissLayer as n,createPortalLifecycle as r,createPositionSync as i,createPresenceLifecycle as a,emit as o,ensureId as s,ensureItemVisibleInContainer as c,focusElement as l,getDataBool as u,getDataEnum as d,getDataNumber as f,getDataString as p,getPart as m,getParts as ee,getRoots as h,lockScroll as te,measurePopupContentRect as ne,on as g,setAria as _,unlockScroll as re}from"@data-slot/core";const ie=[`top`,`bottom`],ae=[`start`,`center`,`end`],oe=[`item-aligned`,`popper`],v=new WeakMap,y=new WeakSet,se=e=>{typeof process<`u`&&process.env?.NODE_ENV===`production`||y.has(e)||(y.add(e),console.warn(`[@data-slot/select] createSelect() called more than once for the same root. Returning the existing controller. Destroy it before rebinding with new options.`))};function b(h,y={}){let b=v.get(h);if(b)return se(h),b;let x=m(h,`select-trigger`),S=m(h,`select-content`),C=m(h,`select-value`),w=m(h,`select-positioner`),T=w&&S&&w.contains(S)?w:null,ce=m(h,`select-portal`),le=ce&&T&&ce.contains(T)?ce:null;if(!x||!S)throw Error(`Select requires trigger and content slots`);let ue=y.defaultValue??p(h,`defaultValue`)??null,de=y.defaultOpen??u(h,`defaultOpen`)??!1,fe=y.placeholder??p(h,`placeholder`)??(C?p(C,`placeholder`):void 0)??``,E=y.disabled??u(h,`disabled`)??!1,pe=y.required??u(h,`required`)??!1,me=y.name??p(h,`name`)??null,he=y.onValueChange,ge=y.onOpenChange,_e=(e,t)=>d(S,e,t)??(T?d(T,e,t):void 0)??d(h,e,t),ve=e=>f(S,e)??(T?f(T,e):void 0)??f(h,e),ye=e=>u(S,e)??(T?u(T,e):void 0)??u(h,e),D=y.position??_e(`position`,oe)??`item-aligned`,be=y.side??_e(`side`,ie)??`bottom`,xe=y.align??_e(`align`,ae)??`start`,Se=y.sideOffset??ve(`sideOffset`)??4,Ce=y.alignOffset??ve(`alignOffset`)??0,O=y.avoidCollisions??ye(`avoidCollisions`)??!0,k=y.collisionPadding??ve(`collisionPadding`)??8,A=y.lockScroll??u(h,`lockScroll`)??!0,j=y.highlightItemOnHover??u(h,`highlightItemOnHover`)??!0,M=!1,N=ue,P=null,F=-1,I=``,L=null,R=!1,z=0,B=0,we=``,V=!1,H=[],U=[],W=[],G=new Map,K=null,q=!1,J=r({content:S,root:h,wrapperSlot:T?void 0:`select-positioner`,container:T??void 0,mountTarget:T?le??T:void 0}),Te=!1,Ee=!0,Y=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,De=e=>e.pointerType!==`touch`,Oe=s(x,`select-trigger`),ke=s(S,`select-content`);x.setAttribute(`role`,`combobox`),x.setAttribute(`aria-haspopup`,`listbox`),x.setAttribute(`aria-controls`,ke),x.hasAttribute(`type`)||x.setAttribute(`type`,`button`),S.setAttribute(`role`,`listbox`),S.setAttribute(`aria-labelledby`,Oe),S.tabIndex=-1;let Ae=document.querySelector(`label[for="${CSS.escape(Oe)}"]`);if(Ae){let e=s(Ae,`select-label`),t=x.getAttribute(`aria-labelledby`);x.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),H.push(g(Ae,`click`,e=>{e.preventDefault(),E||Q(!M)}))}E&&(x.setAttribute(`aria-disabled`,`true`),x.setAttribute(`data-disabled`,``),x instanceof HTMLButtonElement&&(x.disabled=!0)),pe&&x.setAttribute(`aria-required`,`true`),me&&(K=document.createElement(`input`),K.type=`hidden`,K.name=me,K.value=N??``,h.appendChild(K));let je=()=>{U=ee(S,`select-item`);for(let e of U)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===N?(_(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(_(e,`selected`,!1),e.removeAttribute(`data-selected`));W=U.filter(e=>!Y(e)),G=new Map(W.map((e,t)=>[e,t]));let e=ee(S,`select-group`);for(let t of e){t.setAttribute(`role`,`group`);let e=m(t,`select-label`);if(e){let n=s(e,`select-label`);t.setAttribute(`aria-labelledby`,n)}}},Me=()=>m(S,`select-viewport`),Ne=()=>Me()??S,Pe=()=>{S.setAttribute(`data-position`,D),S.setAttribute(`data-align-trigger`,D===`item-aligned`?`true`:`false`);let e=Me();e&&e.setAttribute(`data-position`,D)},Fe=(e,t,n)=>{let r=0,i=e;for(;i&&i!==S;){r+=i.offsetTop;let e=i.offsetParent;if(!(e instanceof HTMLElement)){r=NaN;break}i=e}return i===S&&Number.isFinite(r)?r:e.getBoundingClientRect().top-t.top+n.scrollTop},Ie=(e,t,n)=>{let r=F>=0?W[F]:void 0,i=U.find(e=>e.dataset.value===N)??r??W[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}},Le=()=>{let n=J.container,r=h.ownerDocument.defaultView??window,i=x.getBoundingClientRect(),a=Ne();S.style.minWidth=`${i.width}px`;let o=ne(S),s,c=`bottom`;if(D===`item-aligned`){let e=Ie(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=>O?l<n?n:Math.min(Math.max(e,n),l):e,d=k,f=r.innerWidth-o.width-k;if(s.x=(e=>O?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=n=>e.itemTopInContent+e.itemHeight/2-(t-n);if(n>0){s.y=u(t-o.height/2);let i=Math.min(Math.max(r(s.y),0),n);a.scrollTop=i,s.y=u(t-(e.itemTopInContent-i+e.itemHeight/2)),i=Math.min(Math.max(r(s.y),0),n),a.scrollTop=i,s.y=u(t-(e.itemTopInContent-i+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 t=e({anchorRect:i,contentRect:o,side:be,align:xe,sideOffset:Se,alignOffset:Ce,avoidCollisions:O,collisionPadding:k,allowedSides:ie});s={x:t.x,y:t.y},c=t.side}let l=D===`item-aligned`?`center`:xe,u=t({side:c,align:l,anchorRect:i,popupX:s.x,popupY:s.y});A?(n.style.position=`fixed`,n.style.top=`0px`,n.style.left=`0px`,n.style.transform=`translate3d(${s.x}px, ${s.y}px, 0)`):(n.style.position=`absolute`,n.style.top=`0px`,n.style.left=`0px`,n.style.transform=`translate3d(${s.x+r.scrollX}px, ${s.y+r.scrollY}px, 0)`),n.style.setProperty(`--transform-origin`,u),n.style.willChange=`transform`,n.style.margin=`0`,Pe(),S.setAttribute(`data-side`,c),S.setAttribute(`data-align`,l),n!==S&&(n.setAttribute(`data-side`,c),n.setAttribute(`data-align`,l))},X=i({observedElements:[x,S],isActive:()=>M,ancestorScroll:A,onUpdate:Le,ignoreScrollTarget:e=>e instanceof Node&&S.contains(e)}),Z=(e,t=!0,n=!0)=>{let r=Ne();for(let i=0;i<W.length;i++){let a=W[i];i===e?(a.setAttribute(`data-highlighted`,``),n&&c(a,r),t&&a.focus()):a.removeAttribute(`data-highlighted`)}F=e},Re=()=>{for(let e of U)e.removeAttribute(`data-highlighted`);F=-1},ze=()=>{Re(),l(S)},Be=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`))},Ve=()=>{requestAnimationFrame(()=>{P&&document.contains(P)?l(P):x&&document.contains(x)&&l(x),P=null})},He=a({element:S,onExitComplete:()=>{Te||(J.restore(),S.hidden=!0,Ee?Ve():P=null)}}),Ue=()=>{if(C)if(N===null)C.textContent=fe,x.setAttribute(`data-placeholder`,``);else{let e=U.find(e=>e.dataset.value===N);C.textContent=e?.dataset.label??e?.textContent?.trim()??N,x.removeAttribute(`data-placeholder`)}},Q=(e,t=!1)=>{if(M!==e&&!(E&&e)){if(e){let e=V;V=!1,Ee=!0,P=document.activeElement,M=!0,_(x,`expanded`,!0),J.mount(),S.hidden=!1,Be(`open`),He.enter(),A&&!q&&(te(),q=!0),je(),R=!1;let t=W.findIndex(e=>e.dataset.value===N);t>=0?Z(t,!1,!1):Re(),X.start(),Le(),X.update(),requestAnimationFrame(()=>{if(M&&(Le(),X.update(),e&&j&&we!==`touch`&&(z!==0||B!==0))){let e=document.elementFromPoint(z,B)?.closest?.(`[data-slot="select-item"]`);if(e&&!Y(e)&&S.contains(e)){let t=G.get(e);t!==void 0&&Z(t,!0,!1)}}}),S.focus()}else M=!1,V=!1,z=0,B=0,we=``,_(x,`expanded`,!1),Be(`closed`),Re(),I=``,R=!1,Ee=!t,q&&=(re(),!1),X.stop(),He.exit();o(h,`select:open-change`,{open:M}),ge?.(M)}},$=(e,t=!1)=>{if(N===e&&!t)return;let n=N;N=e,K&&(K.value=e??``),e===null?h.removeAttribute(`data-value`):h.setAttribute(`data-value`,e);for(let t of U)t.dataset.value===e?(_(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(_(t,`selected`,!1),t.removeAttribute(`data-selected`));Ue(),!t&&n!==e&&(o(h,`select:change`,{value:e}),he?.(e))},We=e=>{if(Y(e))return;let t=e.dataset.value;t!==void 0&&($(t),Q(!1))},Ge=e=>{let t=W.length;if(t!==0)switch(e.key){case`ArrowDown`:e.preventDefault(),R=!0,Z(F===-1?0:Math.min(F+1,t-1));break;case`ArrowUp`:e.preventDefault(),R=!0,Z(F===-1?t-1:Math.max(F-1,0));break;case`Home`:e.preventDefault(),R=!0,Z(0);break;case`End`:e.preventDefault(),R=!0,Z(t-1);break;case`Enter`:case` `:e.preventDefault(),F>=0&&We(W[F]);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(),Ke(e.key.toLowerCase()))}},Ke=e=>{L&&clearTimeout(L),L=setTimeout(()=>{I=``},500),I+=e;let t=W.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(I));if(t===-1&&I.length===1){let n=F+1;for(let r=0;r<W.length;r++){let i=(n+r)%W.length;if((W[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(R=!0,Z(t))};_(x,`expanded`,!1),S.hidden=!0,Pe(),Be(`closed`),je(),$(N,!0),H.push(g(x,`pointerdown`,e=>{z=e.clientX,B=e.clientY,we=e.pointerType,V=!0}),g(x,`click`,()=>{E||Q(!M)}),g(x,`keydown`,e=>{if(!E)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),V=!1,Q(!0);break}})),H.push(g(S,`keydown`,Ge),g(S,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&We(t)}),g(S,`pointermove`,e=>{if(!j||!De(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(R&&(R=!1,t&&G.get(t)===F)))if(t&&!Y(t)){let e=G.get(t);e!==void 0&&e!==F&&Z(e,!0)}else ze()}),g(S,`pointerleave`,e=>{!j||!De(e)||R||ze()})),H.push(n({root:h,isOpen:()=>M,onDismiss:()=>Q(!1),closeOnClickOutside:!0,closeOnEscape:!1})),H.push(g(h,`select:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open)}));let qe={get value(){return N},get isOpen(){return M},select:e=>$(e),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{Te=!0,L&&clearTimeout(L),X.stop(),He.cleanup(),J.cleanup(),q&&=(re(),!1),H.forEach(e=>e()),H.length=0,K&&K.parentNode&&K.parentNode.removeChild(K),v.delete(h)}};return v.set(h,qe),de&&Q(!0),qe}function x(e=document){let t=[];for(let n of h(e,`select`))v.has(n)||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 ee,getDataString as p,getPart as m,getParts as te,getRootBinding as ne,getRoots as h,lockScroll as re,measurePopupContentRect as ie,on as g,setAria as _,setRootBinding as ae,unlockScroll as oe,warnRootBindingOnce as se}from"@data-slot/core";const ce=[`top`,`bottom`],le=[`start`,`center`,`end`],ue=[`item-aligned`,`popper`],v=`@data-slot/select`;function y(h,y={}){let de=ne(h,v);if(de)return se(h,v,`[@data-slot/select] createSelect() called more than once for the same root. Returning the existing controller. Destroy it before rebinding with new options.`),de;let b=m(h,`select-trigger`),x=m(h,`select-content`),S=m(h,`select-value`),fe=m(h,`select-positioner`),C=fe&&x&&fe.contains(x)?fe:null,pe=m(h,`select-portal`),me=pe&&C&&pe.contains(C)?pe:null;if(!b||!x)throw Error(`Select requires trigger and content slots`);let he=y.defaultValue??p(h,`defaultValue`)??null,ge=y.defaultOpen??d(h,`defaultOpen`)??!1,_e=y.placeholder??p(h,`placeholder`)??(S?p(S,`placeholder`):void 0)??``,w=y.disabled??d(h,`disabled`)??!1,ve=y.required??d(h,`required`)??!1,ye=y.name??p(h,`name`)??null,be=y.onValueChange,xe=y.onOpenChange,Se=(e,t)=>f(x,e,t)??(C?f(C,e,t):void 0)??f(h,e,t),Ce=e=>ee(x,e)??(C?ee(C,e):void 0)??ee(h,e),we=e=>d(x,e)??(C?d(C,e):void 0)??d(h,e),T=y.position??Se(`position`,ue)??`item-aligned`,Te=y.side??Se(`side`,ce)??`bottom`,Ee=y.align??Se(`align`,le)??`start`,De=y.sideOffset??Ce(`sideOffset`)??4,Oe=y.alignOffset??Ce(`alignOffset`)??0,E=y.avoidCollisions??we(`avoidCollisions`)??!0,D=y.collisionPadding??Ce(`collisionPadding`)??8,O=y.lockScroll??d(h,`lockScroll`)??!0,k=y.highlightItemOnHover??d(h,`highlightItemOnHover`)??!0,A=!1,j=he,M=null,N=-1,P=``,F=null,I=!1,L=0,R=0,z=``,B=!1,V=[],H=[],U=[],W=new Map,G=null,K=!1,q=i({content:x,root:h,wrapperSlot:C?void 0:`select-positioner`,container:C??void 0,mountTarget:C?me??C:void 0}),ke=!1,Ae=!0,J=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,je=e=>e.pointerType!==`touch`,Me=c(b,`select-trigger`),Ne=c(x,`select-content`);b.setAttribute(`role`,`combobox`),b.setAttribute(`aria-haspopup`,`listbox`),b.setAttribute(`aria-controls`,Ne),b.hasAttribute(`type`)||b.setAttribute(`type`,`button`),x.setAttribute(`role`,`listbox`),x.setAttribute(`aria-labelledby`,Me),x.tabIndex=-1;let Pe=document.querySelector(`label[for="${CSS.escape(Me)}"]`);if(Pe){let e=c(Pe,`select-label`),t=b.getAttribute(`aria-labelledby`);b.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),V.push(g(Pe,`click`,e=>{e.preventDefault(),w||Z(!A)}))}w&&(b.setAttribute(`aria-disabled`,`true`),b.setAttribute(`data-disabled`,``),b instanceof HTMLButtonElement&&(b.disabled=!0)),ve&&b.setAttribute(`aria-required`,`true`),ye&&(G=document.createElement(`input`),G.type=`hidden`,G.name=ye,G.value=j??``,h.appendChild(G));let Fe=()=>{H=te(x,`select-item`);for(let e of H)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?(_(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(_(e,`selected`,!1),e.removeAttribute(`data-selected`));U=H.filter(e=>!J(e)),W=new Map(U.map((e,t)=>[e,t]));let e=te(x,`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(x,`select-viewport`),Le=()=>Ie()??x,Re=()=>{x.setAttribute(`data-position`,T),x.setAttribute(`data-align-trigger`,T===`item-aligned`?`true`:`false`);let e=Ie();e&&e.setAttribute(`data-position`,T)},ze=(e,t,n)=>{let r=0,i=e;for(;i&&i!==x;){r+=i.offsetTop;let e=i.offsetParent;if(!(e instanceof HTMLElement)){r=NaN;break}i=e}return i===x&&Number.isFinite(r)?r:e.getBoundingClientRect().top-t.top+n.scrollTop},Be=(e,t,n)=>{let r=N>=0?U[N]:void 0,i=H.find(e=>e.dataset.value===j)??r??U[0],a=e.left,o,s=0,c=e.height;return i?(s=ze(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}},Ve=()=>{let e=q.container,r=h.ownerDocument.defaultView??window,i=b.getBoundingClientRect(),a=Le();x.style.minWidth=`${i.width}px`;let o=ie(x),s,c=`bottom`;if(T===`item-aligned`){let e=Be(i,o,a);s={x:e.x,y:e.y};let t=i.top+i.height/2,n=D,l=r.innerHeight-o.height-D,u=e=>E?l<n?n:Math.min(Math.max(e,n),l):e,d=D,f=r.innerWidth-o.width-D;if(s.x=(e=>E?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=n=>e.itemTopInContent+e.itemHeight/2-(t-n);if(n>0){s.y=u(t-o.height/2);let i=Math.min(Math.max(r(s.y),0),n);a.scrollTop=i,s.y=u(t-(e.itemTopInContent-i+e.itemHeight/2)),i=Math.min(Math.max(r(s.y),0),n),a.scrollTop=i,s.y=u(t-(e.itemTopInContent-i+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:Te,align:Ee,sideOffset:De,alignOffset:Oe,avoidCollisions:E,collisionPadding:D,allowedSides:ce});s={x:e.x,y:e.y},c=e.side}let l=T===`item-aligned`?`center`:Ee,u=n({side:c,align:l,anchorRect:i,popupX:s.x,popupY:s.y});O?(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(),x.setAttribute(`data-side`,c),x.setAttribute(`data-align`,l),e!==x&&(e.setAttribute(`data-side`,c),e.setAttribute(`data-align`,l))},Y=a({observedElements:[b,x],isActive:()=>A,ancestorScroll:O,onUpdate:Ve,ignoreScrollTarget:e=>e instanceof Node&&x.contains(e)}),X=(e,t=!0,n=!0)=>{let r=Le();for(let i=0;i<U.length;i++){let a=U[i];i===e?(a.setAttribute(`data-highlighted`,``),n&&l(a,r),t&&a.focus()):a.removeAttribute(`data-highlighted`)}N=e},He=()=>{for(let e of H)e.removeAttribute(`data-highlighted`);N=-1},Ue=()=>{He(),u(x)},We=e=>{h.setAttribute(`data-state`,e),b.setAttribute(`data-state`,e),x.setAttribute(`data-state`,e),e===`open`?(h.setAttribute(`data-open`,``),b.setAttribute(`data-open`,``),x.setAttribute(`data-open`,``),h.removeAttribute(`data-closed`),b.removeAttribute(`data-closed`),x.removeAttribute(`data-closed`)):(h.setAttribute(`data-closed`,``),b.setAttribute(`data-closed`,``),x.setAttribute(`data-closed`,``),h.removeAttribute(`data-open`),b.removeAttribute(`data-open`),x.removeAttribute(`data-open`))},Ge=()=>{requestAnimationFrame(()=>{M&&document.contains(M)?u(M):b&&document.contains(b)&&u(b),M=null})},Ke=o({element:x,onExitComplete:()=>{ke||(q.restore(),x.hidden=!0,Ae?Ge():M=null)}}),qe=()=>{if(S)if(j===null)S.textContent=_e,b.setAttribute(`data-placeholder`,``);else{let e=H.find(e=>e.dataset.value===j);S.textContent=e?.dataset.label??e?.textContent?.trim()??j,b.removeAttribute(`data-placeholder`)}},Z=(e,t=!1)=>{if(A!==e&&!(w&&e)){if(e){let e=B;B=!1,Ae=!0,M=document.activeElement,A=!0,_(b,`expanded`,!0),q.mount(),x.hidden=!1,We(`open`),Ke.enter(),O&&!K&&(re(),K=!0),Fe(),I=!1;let t=U.findIndex(e=>e.dataset.value===j);t>=0?X(t,!1,!1):He(),Y.start(),Ve(),Y.update(),requestAnimationFrame(()=>{if(A&&(Ve(),Y.update(),e&&k&&z!==`touch`&&(L!==0||R!==0))){let e=document.elementFromPoint(L,R)?.closest?.(`[data-slot="select-item"]`);if(e&&!J(e)&&x.contains(e)){let t=W.get(e);t!==void 0&&X(t,!0,!1)}}}),x.focus()}else A=!1,B=!1,L=0,R=0,z=``,_(b,`expanded`,!1),We(`closed`),He(),P=``,I=!1,Ae=!t,K&&=(oe(),!1),Y.stop(),Ke.exit();s(h,`select:open-change`,{open:A}),xe?.(A)}},Q=(e,t=!1)=>{if(j===e&&!t)return;let n=j;j=e,G&&(G.value=e??``),e===null?h.removeAttribute(`data-value`):h.setAttribute(`data-value`,e);for(let t of H)t.dataset.value===e?(_(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(_(t,`selected`,!1),t.removeAttribute(`data-selected`));qe(),!t&&n!==e&&(s(h,`select:change`,{value:e}),be?.(e))},Je=e=>{if(J(e))return;let t=e.dataset.value;t!==void 0&&(Q(t),Z(!1))},Ye=e=>{let t=U.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&&Je(U[N]);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(),Xe(e.key.toLowerCase()))}},Xe=e=>{F&&clearTimeout(F),F=setTimeout(()=>{P=``},500),P+=e;let t=U.findIndex(e=>(e.textContent?.trim().toLowerCase()||``).startsWith(P));if(t===-1&&P.length===1){let n=N+1;for(let r=0;r<U.length;r++){let i=(n+r)%U.length;if((U[i].textContent?.trim().toLowerCase()||``).startsWith(e)){t=i;break}}}t!==-1&&(I=!0,X(t))};_(b,`expanded`,!1),x.hidden=!0,Re(),We(`closed`),Fe(),Q(j,!0),V.push(g(b,`pointerdown`,e=>{L=e.clientX,R=e.clientY,z=e.pointerType,B=!0}),g(b,`click`,()=>{w||Z(!A)}),g(b,`keydown`,e=>{if(!w)switch(e.key){case`Enter`:case` `:case`ArrowDown`:case`ArrowUp`:e.preventDefault(),B=!1,Z(!0);break}})),V.push(g(x,`keydown`,Ye),g(x,`click`,e=>{let t=e.target.closest?.(`[data-slot="select-item"]`);t&&Je(t)}),g(x,`pointermove`,e=>{if(!k||!je(e))return;let t=e.target.closest?.(`[data-slot="select-item"]`);if(!(I&&(I=!1,t&&W.get(t)===N)))if(t&&!J(t)){let e=W.get(t);e!==void 0&&e!==N&&X(e,!0)}else Ue()}),g(x,`pointerleave`,e=>{!k||!je(e)||I||Ue()})),V.push(r({root:h,isOpen:()=>A,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),V.push(g(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),Y.stop(),Ke.cleanup(),q.cleanup(),K&&=(oe(),!1),V.forEach(e=>e()),V.length=0,G&&G.parentNode&&G.parentNode.removeChild(G),e(h,v,$)}};return ae(h,v,$),ge&&Z(!0),$}function de(e=document){let t=[];for(let n of h(e,`select`))ne(n,v)||t.push(y(n));return t}export{de as create,y as createSelect};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-slot/select",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.123",
|
|
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.123"
|
|
40
40
|
}
|
|
41
41
|
}
|