@data-slot/combobox 0.2.31 → 0.2.32

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 CHANGED
@@ -1 +1 @@
1
- var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=s(require(`@data-slot/core`)),l=[`top`,`bottom`],u=[`start`,`center`,`end`];function d(e,t={}){let n=(0,c.getPart)(e,`combobox-input`),r=(0,c.getPart)(e,`combobox-content`),i=(0,c.getPart)(e,`combobox-list`)??(0,c.getPart)(r??e,`combobox-list`),a=(0,c.getPart)(e,`combobox-trigger`),o=(0,c.getPart)(i??r??e,`combobox-empty`);if(!n||!r)throw Error(`Combobox requires combobox-input and combobox-content slots`);let s=t.defaultValue??(0,c.getDataString)(e,`defaultValue`)??null,d=t.defaultOpen??(0,c.getDataBool)(e,`defaultOpen`)??!1,p=t.placeholder??(0,c.getDataString)(e,`placeholder`)??``,m=t.disabled??(0,c.getDataBool)(e,`disabled`)??!1,h=t.required??(0,c.getDataBool)(e,`required`)??!1,g=t.name??(0,c.getDataString)(e,`name`)??null,_=t.openOnFocus??(0,c.getDataBool)(e,`openOnFocus`)??!0,v=t.autoHighlight??(0,c.getDataBool)(e,`autoHighlight`)??!0,ee=t.filter??null,te=t.onValueChange,y=t.onOpenChange,ne=t.onInputValueChange,re=t.side??(0,c.getDataEnum)(r,`side`,l)??(0,c.getDataEnum)(e,`side`,l)??`bottom`,ie=t.align??(0,c.getDataEnum)(r,`align`,u)??(0,c.getDataEnum)(e,`align`,u)??`start`,b=t.sideOffset??(0,c.getDataNumber)(r,`sideOffset`)??(0,c.getDataNumber)(e,`sideOffset`)??4,ae=t.alignOffset??(0,c.getDataNumber)(r,`alignOffset`)??(0,c.getDataNumber)(e,`alignOffset`)??0,oe=t.avoidCollisions??(0,c.getDataBool)(r,`avoidCollisions`)??(0,c.getDataBool)(e,`avoidCollisions`)??!0,x=t.collisionPadding??(0,c.getDataNumber)(r,`collisionPadding`)??(0,c.getDataNumber)(e,`collisionPadding`)??8,S=!1,C=s,w=-1,T=!1,E=[],D=[],O=[],k=[],A=new Map,j=null,M=(0,c.createPortalLifecycle)({content:r,root:e}),N=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,P=e=>{if(e.dataset.label)return e.dataset.label;let t=``;for(let n of e.childNodes)n.nodeType===Node.TEXT_NODE&&(t+=n.textContent);let n=t.trim();return n||(e.textContent?.trim()??``)},F=e=>e.hasAttribute(`data-value`)?e.getAttribute(`data-value`):void 0,I=e=>{if(e===null)return``;let t=i??r,n=(0,c.getParts)(t,`combobox-item`),a=n.find(t=>F(t)===e);return a?P(a):``},se=(0,c.ensureId)(n,`combobox-input`),L=i??r,ce=(0,c.ensureId)(L,`combobox-list`);n.setAttribute(`role`,`combobox`),n.setAttribute(`aria-autocomplete`,`list`),n.setAttribute(`autocomplete`,`off`),n.setAttribute(`aria-controls`,ce),i?i.setAttribute(`role`,`listbox`):r.setAttribute(`role`,`listbox`),a&&(a.hasAttribute(`type`)||a.setAttribute(`type`,`button`),a.tabIndex=-1,a.setAttribute(`aria-label`,`Toggle`));let R=document.querySelector(`label[for="${CSS.escape(se)}"]`);if(R){let e=(0,c.ensureId)(R,`combobox-label`),t=n.getAttribute(`aria-labelledby`);n.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),L.setAttribute(`aria-labelledby`,e)}m&&(n.setAttribute(`aria-disabled`,`true`),n.disabled=!0,a&&(a.setAttribute(`aria-disabled`,`true`),a.setAttribute(`data-disabled`,``))),h&&(n.setAttribute(`aria-required`,`true`),n.required=!0);let z=()=>{h&&n.setCustomValidity(C===null?`Please select a value`:``)};p&&(n.placeholder=p),g&&(n.name&&n.removeAttribute(`name`),j=document.createElement(`input`),j.type=`hidden`,j.name=g,j.value=C??``,e.appendChild(j));let B=(e,t,n)=>n.toLowerCase().includes(e.toLowerCase()),V=ee??B,H=()=>{let e=i??r;D=(0,c.getParts)(e,`combobox-item`);for(let e of D){e.setAttribute(`role`,`option`),(0,c.ensureId)(e,`combobox-item`),N(e)?e.setAttribute(`aria-disabled`,`true`):e.removeAttribute(`aria-disabled`);let t=F(e);t===C?((0,c.setAria)(e,`selected`,!0),e.setAttribute(`data-selected`,``)):((0,c.setAria)(e,`selected`,!1),e.removeAttribute(`data-selected`))}let t=(0,c.getParts)(e,`combobox-group`);for(let e of t){e.setAttribute(`role`,`group`);let t=(0,c.getPart)(e,`combobox-label`);if(t){let n=(0,c.ensureId)(t,`combobox-label`);e.setAttribute(`aria-labelledby`,n)}}U()},U=()=>{O=D.filter(e=>!e.hidden),k=O.filter(e=>!N(e)),A=new Map(k.map((e,t)=>[e,t]))},W=(e,t)=>{let n=t===`previous`?e.previousElementSibling:e.nextElementSibling;for(;n;){if(n instanceof HTMLElement&&!n.hidden&&n.dataset.slot!==`combobox-separator`)return!0;n=t===`previous`?n.previousElementSibling:n.nextElementSibling}return!1},G=e=>{let t=i??r,n=e.trim(),a=0;for(let e of D){let t=F(e)??``,r=P(e),i=n===``||V(n,t,r);e.hidden=!i,i&&a++}let s=(0,c.getParts)(t,`combobox-group`);for(let e of s){let t=(0,c.getParts)(e,`combobox-item`),n=t.some(e=>!e.hidden);e.hidden=!n}let l=(0,c.getParts)(t,`combobox-separator`);for(let e of l)e.hidden=!W(e,`previous`)||!W(e,`next`);o&&(o.hidden=a>0),a===0?r.setAttribute(`data-empty`,``):r.removeAttribute(`data-empty`),U()},K=()=>{let t=e.ownerDocument.defaultView??window,n=e.getBoundingClientRect();r.style.minWidth=`${n.width}px`;let i=r.getBoundingClientRect(),a=(0,c.computeFloatingPosition)({anchorRect:n,contentRect:i,side:re,align:ie,sideOffset:b,alignOffset:ae,avoidCollisions:oe,collisionPadding:x,allowedSides:l});r.style.position=`absolute`,r.style.top=`${a.y+t.scrollY}px`,r.style.left=`${a.x+t.scrollX}px`,r.style.margin=`0`,r.setAttribute(`data-side`,a.side),r.setAttribute(`data-align`,a.align)},q=(0,c.createPositionSync)({observedElements:[e,r],isActive:()=>S,ancestorScroll:!1,onUpdate:K,ignoreScrollTarget:e=>e instanceof Node&&r.contains(e)}),le=e=>i&&i.contains(e)&&i.scrollHeight>i.clientHeight?i:r,J=e=>{for(let t=0;t<k.length;t++){let r=k[t];t===e?(r.setAttribute(`data-highlighted`,``),n.setAttribute(`aria-activedescendant`,r.id),(0,c.ensureItemVisibleInContainer)(r,le(r))):r.removeAttribute(`data-highlighted`)}w=e},Y=()=>{for(let e of D)e.removeAttribute(`data-highlighted`);w=-1,n.removeAttribute(`aria-activedescendant`)},X=t=>{e.setAttribute(`data-state`,t),r.setAttribute(`data-state`,t),a&&a.setAttribute(`data-state`,t)},Z=(t,i=!1)=>{if(S!==t&&!(m&&t)){if(t){S=!0,(0,c.setAria)(n,`expanded`,!0),M.mount(),r.hidden=!1,X(`open`),H(),T=!1,G(n.value);let e=k.findIndex(e=>F(e)===C);e>=0?J(e):v&&k.length>0?J(0):Y(),q.start(),K(),q.update(),requestAnimationFrame(()=>{S&&q.update()})}else{S=!1,(0,c.setAria)(n,`expanded`,!1),M.restore(),r.hidden=!0,X(`closed`),Y(),T=!1,q.stop();let e=I(C);n.value=e}(0,c.emit)(e,`combobox:open-change`,{open:S}),y?.(S)}},Q=(t,a=!1)=>{if(C===t&&!a)return;let o=C;C=t,z(),j&&(j.value=t??``),t===null?e.removeAttribute(`data-value`):e.setAttribute(`data-value`,t);let s=i??r,l=D.length>0?D:(0,c.getParts)(s,`combobox-item`);for(let e of l){let n=F(e);n===t?((0,c.setAria)(e,`selected`,!0),e.setAttribute(`data-selected`,``)):((0,c.setAria)(e,`selected`,!1),e.removeAttribute(`data-selected`))}n.value=I(t),!a&&o!==t&&((0,c.emit)(e,`combobox:change`,{value:t}),te?.(t))},$=e=>{if(N(e))return;let t=F(e);t!==void 0&&(Q(t),Z(!1))},ue=e=>{if(!m)switch(e.key){case`ArrowDown`:{if(e.preventDefault(),!S){Z(!0);return}T=!0;let t=k.length;if(t===0)return;J(w===-1?0:(w+1)%t);break}case`ArrowUp`:{if(e.preventDefault(),!S){Z(!0);return}T=!0;let t=k.length;if(t===0)return;J(w===-1?t-1:(w-1+t)%t);break}case`Home`:if(!S)return;e.preventDefault(),T=!0,k.length>0&&J(0);break;case`End`:if(!S)return;e.preventDefault(),T=!0,k.length>0&&J(k.length-1);break;case`Enter`:if(!S)return;e.preventDefault(),w>=0&&w<k.length&&$(k[w]);break;case`Escape`:S?(e.preventDefault(),Z(!1)):C!==null&&(e.preventDefault(),Q(null));break;case`Tab`:S&&Z(!1,!0);break}},de=()=>{let t=n.value;(0,c.emit)(e,`combobox:input-change`,{inputValue:t}),ne?.(t),S?(G(t),v&&k.length>0?J(0):Y(),q.update()):Z(!0)},fe=()=>{m||(n.select(),_&&!S&&Z(!0))};(0,c.setAria)(n,`expanded`,!1),r.hidden=!0,X(`closed`),Q(C,!0),E.push((0,c.on)(n,`input`,de),(0,c.on)(n,`keydown`,ue),(0,c.on)(n,`focus`,fe)),a&&E.push((0,c.on)(a,`click`,()=>{m||(S?Z(!1):(Z(!0),n.focus()))})),E.push((0,c.on)(r,`click`,e=>{let t=e.target.closest?.(`[data-slot="combobox-item"]`);t&&!t.hidden&&$(t)}),(0,c.on)(r,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="combobox-item"]`);if(!(T&&(T=!1,t&&A.get(t)===w)))if(t&&!N(t)&&!t.hidden){let e=A.get(t);e!==void 0&&e!==w&&J(e)}else Y()}),(0,c.on)(r,`pointerleave`,()=>{T||Y()}),(0,c.on)(r,`mousedown`,e=>{e.preventDefault()})),E.push((0,c.createDismissLayer)({root:e,isOpen:()=>S,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),E.push((0,c.on)(e,`combobox:set`,e=>{let t=e.detail;t?.value!==void 0&&Q(t.value),t?.open!==void 0&&Z(t.open),t?.inputValue!==void 0&&(n.value=t.inputValue)}));let pe={get value(){return C},get inputValue(){return n.value},get isOpen(){return S},select:e=>Q(e),clear:()=>Q(null),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{q.stop(),M.cleanup(),E.forEach(e=>e()),E.length=0,j&&j.parentNode&&j.parentNode.removeChild(j),f.delete(e)}};return d&&Z(!0),pe}const f=new WeakSet;function p(e=document){let t=[];for(let n of(0,c.getRoots)(e,`combobox`)){if(f.has(n))continue;f.add(n),t.push(d(n))}return t}exports.create=p,exports.createCombobox=d;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@data-slot/core`);const t=[`top`,`bottom`],n=[`start`,`center`,`end`];function r(r,a={}){let o=(0,e.getPart)(r,`combobox-input`),s=(0,e.getPart)(r,`combobox-content`),c=(0,e.getPart)(r,`combobox-list`)??(0,e.getPart)(s??r,`combobox-list`),l=(0,e.getPart)(r,`combobox-trigger`),u=(0,e.getPart)(c??s??r,`combobox-empty`);if(!o||!s)throw Error(`Combobox requires combobox-input and combobox-content slots`);let d=a.defaultValue??(0,e.getDataString)(r,`defaultValue`)??null,ee=a.defaultOpen??(0,e.getDataBool)(r,`defaultOpen`)??!1,f=a.placeholder??(0,e.getDataString)(r,`placeholder`)??``,p=a.disabled??(0,e.getDataBool)(r,`disabled`)??!1,m=a.required??(0,e.getDataBool)(r,`required`)??!1,h=a.name??(0,e.getDataString)(r,`name`)??null,te=a.openOnFocus??(0,e.getDataBool)(r,`openOnFocus`)??!0,g=a.autoHighlight??(0,e.getDataBool)(r,`autoHighlight`)??!0,_=a.filter??null,v=a.onValueChange,y=a.onOpenChange,b=a.onInputValueChange,x=a.side??(0,e.getDataEnum)(s,`side`,t)??(0,e.getDataEnum)(r,`side`,t)??`bottom`,ne=a.align??(0,e.getDataEnum)(s,`align`,n)??(0,e.getDataEnum)(r,`align`,n)??`start`,re=a.sideOffset??(0,e.getDataNumber)(s,`sideOffset`)??(0,e.getDataNumber)(r,`sideOffset`)??4,ie=a.alignOffset??(0,e.getDataNumber)(s,`alignOffset`)??(0,e.getDataNumber)(r,`alignOffset`)??0,ae=a.avoidCollisions??(0,e.getDataBool)(s,`avoidCollisions`)??(0,e.getDataBool)(r,`avoidCollisions`)??!0,oe=a.collisionPadding??(0,e.getDataNumber)(s,`collisionPadding`)??(0,e.getDataNumber)(r,`collisionPadding`)??8,S=!1,C=d,w=-1,T=!1,E=[],D=[],O=[],k=[],A=new Map,j=null,M=(0,e.createPortalLifecycle)({content:s,root:r}),N=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,P=e=>{if(e.dataset.label)return e.dataset.label;let t=``;for(let n of e.childNodes)n.nodeType===Node.TEXT_NODE&&(t+=n.textContent);return t.trim()||(e.textContent?.trim()??``)},F=e=>e.hasAttribute(`data-value`)?e.getAttribute(`data-value`):void 0,I=t=>{if(t===null)return``;let n=(0,e.getParts)(c??s,`combobox-item`).find(e=>F(e)===t);return n?P(n):``},se=(0,e.ensureId)(o,`combobox-input`),L=c??s,R=(0,e.ensureId)(L,`combobox-list`);o.setAttribute(`role`,`combobox`),o.setAttribute(`aria-autocomplete`,`list`),o.setAttribute(`autocomplete`,`off`),o.setAttribute(`aria-controls`,R),c?c.setAttribute(`role`,`listbox`):s.setAttribute(`role`,`listbox`),l&&(l.hasAttribute(`type`)||l.setAttribute(`type`,`button`),l.tabIndex=-1,l.setAttribute(`aria-label`,`Toggle`));let z=document.querySelector(`label[for="${CSS.escape(se)}"]`);if(z){let t=(0,e.ensureId)(z,`combobox-label`),n=o.getAttribute(`aria-labelledby`);o.setAttribute(`aria-labelledby`,n?`${n} ${t}`:t),L.setAttribute(`aria-labelledby`,t)}p&&(o.setAttribute(`aria-disabled`,`true`),o.disabled=!0,l&&(l.setAttribute(`aria-disabled`,`true`),l.setAttribute(`data-disabled`,``))),m&&(o.setAttribute(`aria-required`,`true`),o.required=!0);let B=()=>{m&&o.setCustomValidity(C===null?`Please select a value`:``)};f&&(o.placeholder=f),h&&(o.name&&o.removeAttribute(`name`),j=document.createElement(`input`),j.type=`hidden`,j.name=h,j.value=C??``,r.appendChild(j));let V=_??((e,t,n)=>n.toLowerCase().includes(e.toLowerCase())),H=()=>{let t=c??s;D=(0,e.getParts)(t,`combobox-item`);for(let t of D)t.setAttribute(`role`,`option`),(0,e.ensureId)(t,`combobox-item`),N(t)?t.setAttribute(`aria-disabled`,`true`):t.removeAttribute(`aria-disabled`),F(t)===C?((0,e.setAria)(t,`selected`,!0),t.setAttribute(`data-selected`,``)):((0,e.setAria)(t,`selected`,!1),t.removeAttribute(`data-selected`));let n=(0,e.getParts)(t,`combobox-group`);for(let t of n){t.setAttribute(`role`,`group`);let n=(0,e.getPart)(t,`combobox-label`);if(n){let r=(0,e.ensureId)(n,`combobox-label`);t.setAttribute(`aria-labelledby`,r)}}U()},U=()=>{O=D.filter(e=>!e.hidden),k=O.filter(e=>!N(e)),A=new Map(k.map((e,t)=>[e,t]))},W=(e,t)=>{let n=t===`previous`?e.previousElementSibling:e.nextElementSibling;for(;n;){if(n instanceof HTMLElement&&!n.hidden&&n.dataset.slot!==`combobox-separator`)return!0;n=t===`previous`?n.previousElementSibling:n.nextElementSibling}return!1},G=t=>{let n=c??s,r=t.trim(),i=0;for(let e of D){let t=F(e)??``,n=P(e),a=r===``||V(r,t,n);e.hidden=!a,a&&i++}let a=(0,e.getParts)(n,`combobox-group`);for(let t of a)t.hidden=!(0,e.getParts)(t,`combobox-item`).some(e=>!e.hidden);let o=(0,e.getParts)(n,`combobox-separator`);for(let e of o)e.hidden=!W(e,`previous`)||!W(e,`next`);u&&(u.hidden=i>0),i===0?s.setAttribute(`data-empty`,``):s.removeAttribute(`data-empty`),U()},K=()=>{let n=r.ownerDocument.defaultView??window,i=r.getBoundingClientRect();s.style.minWidth=`${i.width}px`;let a=(0,e.computeFloatingPosition)({anchorRect:i,contentRect:s.getBoundingClientRect(),side:x,align:ne,sideOffset:re,alignOffset:ie,avoidCollisions:ae,collisionPadding:oe,allowedSides:t});s.style.position=`absolute`,s.style.top=`${a.y+n.scrollY}px`,s.style.left=`${a.x+n.scrollX}px`,s.style.margin=`0`,s.setAttribute(`data-side`,a.side),s.setAttribute(`data-align`,a.align)},q=(0,e.createPositionSync)({observedElements:[r,s],isActive:()=>S,ancestorScroll:!1,onUpdate:K,ignoreScrollTarget:e=>e instanceof Node&&s.contains(e)}),ce=e=>c&&c.contains(e)&&c.scrollHeight>c.clientHeight?c:s,J=t=>{for(let n=0;n<k.length;n++){let r=k[n];n===t?(r.setAttribute(`data-highlighted`,``),o.setAttribute(`aria-activedescendant`,r.id),(0,e.ensureItemVisibleInContainer)(r,ce(r))):r.removeAttribute(`data-highlighted`)}w=t},Y=()=>{for(let e of D)e.removeAttribute(`data-highlighted`);w=-1,o.removeAttribute(`aria-activedescendant`)},X=e=>{r.setAttribute(`data-state`,e),s.setAttribute(`data-state`,e),l&&l.setAttribute(`data-state`,e)},Z=(t,n=!1)=>{if(S!==t&&!(p&&t)){if(t){S=!0,(0,e.setAria)(o,`expanded`,!0),M.mount(),s.hidden=!1,X(`open`),H(),T=!1,G(o.value);let t=k.findIndex(e=>F(e)===C);t>=0?J(t):g&&k.length>0?J(0):Y(),q.start(),K(),q.update(),requestAnimationFrame(()=>{S&&q.update()})}else S=!1,(0,e.setAria)(o,`expanded`,!1),M.restore(),s.hidden=!0,X(`closed`),Y(),T=!1,q.stop(),o.value=I(C);(0,e.emit)(r,`combobox:open-change`,{open:S}),y?.(S)}},Q=(t,n=!1)=>{if(C===t&&!n)return;let i=C;C=t,B(),j&&(j.value=t??``),t===null?r.removeAttribute(`data-value`):r.setAttribute(`data-value`,t);let a=c??s,l=D.length>0?D:(0,e.getParts)(a,`combobox-item`);for(let n of l)F(n)===t?((0,e.setAria)(n,`selected`,!0),n.setAttribute(`data-selected`,``)):((0,e.setAria)(n,`selected`,!1),n.removeAttribute(`data-selected`));o.value=I(t),!n&&i!==t&&((0,e.emit)(r,`combobox:change`,{value:t}),v?.(t))},$=e=>{if(N(e))return;let t=F(e);t!==void 0&&(Q(t),Z(!1))};return(0,e.setAria)(o,`expanded`,!1),s.hidden=!0,X(`closed`),Q(C,!0),E.push((0,e.on)(o,`input`,()=>{let t=o.value;(0,e.emit)(r,`combobox:input-change`,{inputValue:t}),b?.(t),S?(G(t),g&&k.length>0?J(0):Y(),q.update()):Z(!0)}),(0,e.on)(o,`keydown`,e=>{if(!p)switch(e.key){case`ArrowDown`:{if(e.preventDefault(),!S){Z(!0);return}T=!0;let t=k.length;if(t===0)return;J(w===-1?0:(w+1)%t);break}case`ArrowUp`:{if(e.preventDefault(),!S){Z(!0);return}T=!0;let t=k.length;if(t===0)return;J(w===-1?t-1:(w-1+t)%t);break}case`Home`:if(!S)return;e.preventDefault(),T=!0,k.length>0&&J(0);break;case`End`:if(!S)return;e.preventDefault(),T=!0,k.length>0&&J(k.length-1);break;case`Enter`:if(!S)return;e.preventDefault(),w>=0&&w<k.length&&$(k[w]);break;case`Escape`:S?(e.preventDefault(),Z(!1)):C!==null&&(e.preventDefault(),Q(null));break;case`Tab`:S&&Z(!1,!0);break}}),(0,e.on)(o,`focus`,()=>{p||(o.select(),te&&!S&&Z(!0))})),l&&E.push((0,e.on)(l,`click`,()=>{p||(S?Z(!1):(Z(!0),o.focus()))})),E.push((0,e.on)(s,`click`,e=>{let t=e.target.closest?.(`[data-slot="combobox-item"]`);t&&!t.hidden&&$(t)}),(0,e.on)(s,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="combobox-item"]`);if(!(T&&(T=!1,t&&A.get(t)===w)))if(t&&!N(t)&&!t.hidden){let e=A.get(t);e!==void 0&&e!==w&&J(e)}else Y()}),(0,e.on)(s,`pointerleave`,()=>{T||Y()}),(0,e.on)(s,`mousedown`,e=>{e.preventDefault()})),E.push((0,e.createDismissLayer)({root:r,isOpen:()=>S,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),E.push((0,e.on)(r,`combobox:set`,e=>{let t=e.detail;t?.value!==void 0&&Q(t.value),t?.open!==void 0&&Z(t.open),t?.inputValue!==void 0&&(o.value=t.inputValue)})),ee&&Z(!0),{get value(){return C},get inputValue(){return o.value},get isOpen(){return S},select:e=>Q(e),clear:()=>Q(null),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{q.stop(),M.cleanup(),E.forEach(e=>e()),E.length=0,j&&j.parentNode&&j.parentNode.removeChild(j),i.delete(r)}}}const i=new WeakSet;function a(t=document){let n=[];for(let a of(0,e.getRoots)(t,`combobox`))i.has(a)||(i.add(a),n.push(r(a)));return n}exports.create=a,exports.createCombobox=r;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{computeFloatingPosition as e,createDismissLayer as t,createPortalLifecycle as n,createPositionSync as r,emit as i,ensureId as a,ensureItemVisibleInContainer as ee,getDataBool as o,getDataEnum as s,getDataNumber as c,getDataString as l,getPart as u,getParts as d,getRoots as f,on as p,setAria as m}from"@data-slot/core";const h=[`top`,`bottom`],g=[`start`,`center`,`end`];function _(f,_={}){let y=u(f,`combobox-input`),b=u(f,`combobox-content`),x=u(f,`combobox-list`)??u(b??f,`combobox-list`),S=u(f,`combobox-trigger`),C=u(x??b??f,`combobox-empty`);if(!y||!b)throw Error(`Combobox requires combobox-input and combobox-content slots`);let te=_.defaultValue??l(f,`defaultValue`)??null,ne=_.defaultOpen??o(f,`defaultOpen`)??!1,w=_.placeholder??l(f,`placeholder`)??``,T=_.disabled??o(f,`disabled`)??!1,E=_.required??o(f,`required`)??!1,re=_.name??l(f,`name`)??null,ie=_.openOnFocus??o(f,`openOnFocus`)??!0,ae=_.autoHighlight??o(f,`autoHighlight`)??!0,oe=_.filter??null,se=_.onValueChange,ce=_.onOpenChange,le=_.onInputValueChange,ue=_.side??s(b,`side`,h)??s(f,`side`,h)??`bottom`,de=_.align??s(b,`align`,g)??s(f,`align`,g)??`start`,fe=_.sideOffset??c(b,`sideOffset`)??c(f,`sideOffset`)??4,pe=_.alignOffset??c(b,`alignOffset`)??c(f,`alignOffset`)??0,me=_.avoidCollisions??o(b,`avoidCollisions`)??o(f,`avoidCollisions`)??!0,he=_.collisionPadding??c(b,`collisionPadding`)??c(f,`collisionPadding`)??8,D=!1,O=te,k=-1,A=!1,j=[],M=[],N=[],P=[],F=new Map,I=null,L=n({content:b,root:f}),R=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,z=e=>{if(e.dataset.label)return e.dataset.label;let t=``;for(let n of e.childNodes)n.nodeType===Node.TEXT_NODE&&(t+=n.textContent);let n=t.trim();return n||(e.textContent?.trim()??``)},B=e=>e.hasAttribute(`data-value`)?e.getAttribute(`data-value`):void 0,V=e=>{if(e===null)return``;let t=x??b,n=d(t,`combobox-item`),r=n.find(t=>B(t)===e);return r?z(r):``},ge=a(y,`combobox-input`),H=x??b,_e=a(H,`combobox-list`);y.setAttribute(`role`,`combobox`),y.setAttribute(`aria-autocomplete`,`list`),y.setAttribute(`autocomplete`,`off`),y.setAttribute(`aria-controls`,_e),x?x.setAttribute(`role`,`listbox`):b.setAttribute(`role`,`listbox`),S&&(S.hasAttribute(`type`)||S.setAttribute(`type`,`button`),S.tabIndex=-1,S.setAttribute(`aria-label`,`Toggle`));let U=document.querySelector(`label[for="${CSS.escape(ge)}"]`);if(U){let e=a(U,`combobox-label`),t=y.getAttribute(`aria-labelledby`);y.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),H.setAttribute(`aria-labelledby`,e)}T&&(y.setAttribute(`aria-disabled`,`true`),y.disabled=!0,S&&(S.setAttribute(`aria-disabled`,`true`),S.setAttribute(`data-disabled`,``))),E&&(y.setAttribute(`aria-required`,`true`),y.required=!0);let ve=()=>{E&&y.setCustomValidity(O===null?`Please select a value`:``)};w&&(y.placeholder=w),re&&(y.name&&y.removeAttribute(`name`),I=document.createElement(`input`),I.type=`hidden`,I.name=re,I.value=O??``,f.appendChild(I));let ye=(e,t,n)=>n.toLowerCase().includes(e.toLowerCase()),be=oe??ye,xe=()=>{let e=x??b;M=d(e,`combobox-item`);for(let e of M){e.setAttribute(`role`,`option`),a(e,`combobox-item`),R(e)?e.setAttribute(`aria-disabled`,`true`):e.removeAttribute(`aria-disabled`);let t=B(e);t===O?(m(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(m(e,`selected`,!1),e.removeAttribute(`data-selected`))}let t=d(e,`combobox-group`);for(let e of t){e.setAttribute(`role`,`group`);let t=u(e,`combobox-label`);if(t){let n=a(t,`combobox-label`);e.setAttribute(`aria-labelledby`,n)}}W()},W=()=>{N=M.filter(e=>!e.hidden),P=N.filter(e=>!R(e)),F=new Map(P.map((e,t)=>[e,t]))},G=(e,t)=>{let n=t===`previous`?e.previousElementSibling:e.nextElementSibling;for(;n;){if(n instanceof HTMLElement&&!n.hidden&&n.dataset.slot!==`combobox-separator`)return!0;n=t===`previous`?n.previousElementSibling:n.nextElementSibling}return!1},K=e=>{let t=x??b,n=e.trim(),r=0;for(let e of M){let t=B(e)??``,i=z(e),a=n===``||be(n,t,i);e.hidden=!a,a&&r++}let i=d(t,`combobox-group`);for(let e of i){let t=d(e,`combobox-item`),n=t.some(e=>!e.hidden);e.hidden=!n}let a=d(t,`combobox-separator`);for(let e of a)e.hidden=!G(e,`previous`)||!G(e,`next`);C&&(C.hidden=r>0),r===0?b.setAttribute(`data-empty`,``):b.removeAttribute(`data-empty`),W()},Se=()=>{let t=f.ownerDocument.defaultView??window,n=f.getBoundingClientRect();b.style.minWidth=`${n.width}px`;let r=b.getBoundingClientRect(),i=e({anchorRect:n,contentRect:r,side:ue,align:de,sideOffset:fe,alignOffset:pe,avoidCollisions:me,collisionPadding:he,allowedSides:h});b.style.position=`absolute`,b.style.top=`${i.y+t.scrollY}px`,b.style.left=`${i.x+t.scrollX}px`,b.style.margin=`0`,b.setAttribute(`data-side`,i.side),b.setAttribute(`data-align`,i.align)},q=r({observedElements:[f,b],isActive:()=>D,ancestorScroll:!1,onUpdate:Se,ignoreScrollTarget:e=>e instanceof Node&&b.contains(e)}),Ce=e=>x&&x.contains(e)&&x.scrollHeight>x.clientHeight?x:b,J=e=>{for(let t=0;t<P.length;t++){let n=P[t];t===e?(n.setAttribute(`data-highlighted`,``),y.setAttribute(`aria-activedescendant`,n.id),ee(n,Ce(n))):n.removeAttribute(`data-highlighted`)}k=e},Y=()=>{for(let e of M)e.removeAttribute(`data-highlighted`);k=-1,y.removeAttribute(`aria-activedescendant`)},X=e=>{f.setAttribute(`data-state`,e),b.setAttribute(`data-state`,e),S&&S.setAttribute(`data-state`,e)},Z=(e,t=!1)=>{if(D!==e&&!(T&&e)){if(e){D=!0,m(y,`expanded`,!0),L.mount(),b.hidden=!1,X(`open`),xe(),A=!1,K(y.value);let e=P.findIndex(e=>B(e)===O);e>=0?J(e):ae&&P.length>0?J(0):Y(),q.start(),Se(),q.update(),requestAnimationFrame(()=>{D&&q.update()})}else{D=!1,m(y,`expanded`,!1),L.restore(),b.hidden=!0,X(`closed`),Y(),A=!1,q.stop();let e=V(O);y.value=e}i(f,`combobox:open-change`,{open:D}),ce?.(D)}},Q=(e,t=!1)=>{if(O===e&&!t)return;let n=O;O=e,ve(),I&&(I.value=e??``),e===null?f.removeAttribute(`data-value`):f.setAttribute(`data-value`,e);let r=x??b,a=M.length>0?M:d(r,`combobox-item`);for(let t of a){let n=B(t);n===e?(m(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(m(t,`selected`,!1),t.removeAttribute(`data-selected`))}y.value=V(e),!t&&n!==e&&(i(f,`combobox:change`,{value:e}),se?.(e))},$=e=>{if(R(e))return;let t=B(e);t!==void 0&&(Q(t),Z(!1))},we=e=>{if(!T)switch(e.key){case`ArrowDown`:{if(e.preventDefault(),!D){Z(!0);return}A=!0;let t=P.length;if(t===0)return;J(k===-1?0:(k+1)%t);break}case`ArrowUp`:{if(e.preventDefault(),!D){Z(!0);return}A=!0;let t=P.length;if(t===0)return;J(k===-1?t-1:(k-1+t)%t);break}case`Home`:if(!D)return;e.preventDefault(),A=!0,P.length>0&&J(0);break;case`End`:if(!D)return;e.preventDefault(),A=!0,P.length>0&&J(P.length-1);break;case`Enter`:if(!D)return;e.preventDefault(),k>=0&&k<P.length&&$(P[k]);break;case`Escape`:D?(e.preventDefault(),Z(!1)):O!==null&&(e.preventDefault(),Q(null));break;case`Tab`:D&&Z(!1,!0);break}},Te=()=>{let e=y.value;i(f,`combobox:input-change`,{inputValue:e}),le?.(e),D?(K(e),ae&&P.length>0?J(0):Y(),q.update()):Z(!0)},Ee=()=>{T||(y.select(),ie&&!D&&Z(!0))};m(y,`expanded`,!1),b.hidden=!0,X(`closed`),Q(O,!0),j.push(p(y,`input`,Te),p(y,`keydown`,we),p(y,`focus`,Ee)),S&&j.push(p(S,`click`,()=>{T||(D?Z(!1):(Z(!0),y.focus()))})),j.push(p(b,`click`,e=>{let t=e.target.closest?.(`[data-slot="combobox-item"]`);t&&!t.hidden&&$(t)}),p(b,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="combobox-item"]`);if(!(A&&(A=!1,t&&F.get(t)===k)))if(t&&!R(t)&&!t.hidden){let e=F.get(t);e!==void 0&&e!==k&&J(e)}else Y()}),p(b,`pointerleave`,()=>{A||Y()}),p(b,`mousedown`,e=>{e.preventDefault()})),j.push(t({root:f,isOpen:()=>D,onDismiss:()=>Z(!1),closeOnClickOutside:!0,closeOnEscape:!1})),j.push(p(f,`combobox:set`,e=>{let t=e.detail;t?.value!==void 0&&Q(t.value),t?.open!==void 0&&Z(t.open),t?.inputValue!==void 0&&(y.value=t.inputValue)}));let De={get value(){return O},get inputValue(){return y.value},get isOpen(){return D},select:e=>Q(e),clear:()=>Q(null),open:()=>Z(!0),close:()=>Z(!1),destroy:()=>{q.stop(),L.cleanup(),j.forEach(e=>e()),j.length=0,I&&I.parentNode&&I.parentNode.removeChild(I),v.delete(f)}};return ne&&Z(!0),De}const v=new WeakSet;function y(e=document){let t=[];for(let n of f(e,`combobox`)){if(v.has(n))continue;v.add(n),t.push(_(n))}return t}export{y as create,_ as createCombobox};
1
+ import{computeFloatingPosition as e,createDismissLayer as t,createPortalLifecycle as n,createPositionSync as r,emit as i,ensureId as a,ensureItemVisibleInContainer as ee,getDataBool as o,getDataEnum as s,getDataNumber as c,getDataString as l,getPart as u,getParts as d,getRoots as f,on as p,setAria as m}from"@data-slot/core";const h=[`top`,`bottom`],g=[`start`,`center`,`end`];function _(f,_={}){let y=u(f,`combobox-input`),b=u(f,`combobox-content`),x=u(f,`combobox-list`)??u(b??f,`combobox-list`),S=u(f,`combobox-trigger`),C=u(x??b??f,`combobox-empty`);if(!y||!b)throw Error(`Combobox requires combobox-input and combobox-content slots`);let te=_.defaultValue??l(f,`defaultValue`)??null,ne=_.defaultOpen??o(f,`defaultOpen`)??!1,w=_.placeholder??l(f,`placeholder`)??``,T=_.disabled??o(f,`disabled`)??!1,re=_.required??o(f,`required`)??!1,ie=_.name??l(f,`name`)??null,ae=_.openOnFocus??o(f,`openOnFocus`)??!0,E=_.autoHighlight??o(f,`autoHighlight`)??!0,oe=_.filter??null,se=_.onValueChange,ce=_.onOpenChange,le=_.onInputValueChange,ue=_.side??s(b,`side`,h)??s(f,`side`,h)??`bottom`,de=_.align??s(b,`align`,g)??s(f,`align`,g)??`start`,fe=_.sideOffset??c(b,`sideOffset`)??c(f,`sideOffset`)??4,pe=_.alignOffset??c(b,`alignOffset`)??c(f,`alignOffset`)??0,me=_.avoidCollisions??o(b,`avoidCollisions`)??o(f,`avoidCollisions`)??!0,he=_.collisionPadding??c(b,`collisionPadding`)??c(f,`collisionPadding`)??8,D=!1,O=te,k=-1,A=!1,j=[],M=[],N=[],P=[],F=new Map,I=null,L=n({content:b,root:f}),R=e=>e.hasAttribute(`disabled`)||e.hasAttribute(`data-disabled`)||e.getAttribute(`aria-disabled`)===`true`,z=e=>{if(e.dataset.label)return e.dataset.label;let t=``;for(let n of e.childNodes)n.nodeType===Node.TEXT_NODE&&(t+=n.textContent);return t.trim()||(e.textContent?.trim()??``)},B=e=>e.hasAttribute(`data-value`)?e.getAttribute(`data-value`):void 0,V=e=>{if(e===null)return``;let t=d(x??b,`combobox-item`).find(t=>B(t)===e);return t?z(t):``},ge=a(y,`combobox-input`),H=x??b,_e=a(H,`combobox-list`);y.setAttribute(`role`,`combobox`),y.setAttribute(`aria-autocomplete`,`list`),y.setAttribute(`autocomplete`,`off`),y.setAttribute(`aria-controls`,_e),x?x.setAttribute(`role`,`listbox`):b.setAttribute(`role`,`listbox`),S&&(S.hasAttribute(`type`)||S.setAttribute(`type`,`button`),S.tabIndex=-1,S.setAttribute(`aria-label`,`Toggle`));let U=document.querySelector(`label[for="${CSS.escape(ge)}"]`);if(U){let e=a(U,`combobox-label`),t=y.getAttribute(`aria-labelledby`);y.setAttribute(`aria-labelledby`,t?`${t} ${e}`:e),H.setAttribute(`aria-labelledby`,e)}T&&(y.setAttribute(`aria-disabled`,`true`),y.disabled=!0,S&&(S.setAttribute(`aria-disabled`,`true`),S.setAttribute(`data-disabled`,``))),re&&(y.setAttribute(`aria-required`,`true`),y.required=!0);let ve=()=>{re&&y.setCustomValidity(O===null?`Please select a value`:``)};w&&(y.placeholder=w),ie&&(y.name&&y.removeAttribute(`name`),I=document.createElement(`input`),I.type=`hidden`,I.name=ie,I.value=O??``,f.appendChild(I));let ye=oe??((e,t,n)=>n.toLowerCase().includes(e.toLowerCase())),be=()=>{let e=x??b;M=d(e,`combobox-item`);for(let e of M)e.setAttribute(`role`,`option`),a(e,`combobox-item`),R(e)?e.setAttribute(`aria-disabled`,`true`):e.removeAttribute(`aria-disabled`),B(e)===O?(m(e,`selected`,!0),e.setAttribute(`data-selected`,``)):(m(e,`selected`,!1),e.removeAttribute(`data-selected`));let t=d(e,`combobox-group`);for(let e of t){e.setAttribute(`role`,`group`);let t=u(e,`combobox-label`);if(t){let n=a(t,`combobox-label`);e.setAttribute(`aria-labelledby`,n)}}W()},W=()=>{N=M.filter(e=>!e.hidden),P=N.filter(e=>!R(e)),F=new Map(P.map((e,t)=>[e,t]))},G=(e,t)=>{let n=t===`previous`?e.previousElementSibling:e.nextElementSibling;for(;n;){if(n instanceof HTMLElement&&!n.hidden&&n.dataset.slot!==`combobox-separator`)return!0;n=t===`previous`?n.previousElementSibling:n.nextElementSibling}return!1},K=e=>{let t=x??b,n=e.trim(),r=0;for(let e of M){let t=B(e)??``,i=z(e),a=n===``||ye(n,t,i);e.hidden=!a,a&&r++}let i=d(t,`combobox-group`);for(let e of i)e.hidden=!d(e,`combobox-item`).some(e=>!e.hidden);let a=d(t,`combobox-separator`);for(let e of a)e.hidden=!G(e,`previous`)||!G(e,`next`);C&&(C.hidden=r>0),r===0?b.setAttribute(`data-empty`,``):b.removeAttribute(`data-empty`),W()},q=()=>{let t=f.ownerDocument.defaultView??window,n=f.getBoundingClientRect();b.style.minWidth=`${n.width}px`;let r=e({anchorRect:n,contentRect:b.getBoundingClientRect(),side:ue,align:de,sideOffset:fe,alignOffset:pe,avoidCollisions:me,collisionPadding:he,allowedSides:h});b.style.position=`absolute`,b.style.top=`${r.y+t.scrollY}px`,b.style.left=`${r.x+t.scrollX}px`,b.style.margin=`0`,b.setAttribute(`data-side`,r.side),b.setAttribute(`data-align`,r.align)},J=r({observedElements:[f,b],isActive:()=>D,ancestorScroll:!1,onUpdate:q,ignoreScrollTarget:e=>e instanceof Node&&b.contains(e)}),xe=e=>x&&x.contains(e)&&x.scrollHeight>x.clientHeight?x:b,Y=e=>{for(let t=0;t<P.length;t++){let n=P[t];t===e?(n.setAttribute(`data-highlighted`,``),y.setAttribute(`aria-activedescendant`,n.id),ee(n,xe(n))):n.removeAttribute(`data-highlighted`)}k=e},X=()=>{for(let e of M)e.removeAttribute(`data-highlighted`);k=-1,y.removeAttribute(`aria-activedescendant`)},Z=e=>{f.setAttribute(`data-state`,e),b.setAttribute(`data-state`,e),S&&S.setAttribute(`data-state`,e)},Q=(e,t=!1)=>{if(D!==e&&!(T&&e)){if(e){D=!0,m(y,`expanded`,!0),L.mount(),b.hidden=!1,Z(`open`),be(),A=!1,K(y.value);let e=P.findIndex(e=>B(e)===O);e>=0?Y(e):E&&P.length>0?Y(0):X(),J.start(),q(),J.update(),requestAnimationFrame(()=>{D&&J.update()})}else D=!1,m(y,`expanded`,!1),L.restore(),b.hidden=!0,Z(`closed`),X(),A=!1,J.stop(),y.value=V(O);i(f,`combobox:open-change`,{open:D}),ce?.(D)}},$=(e,t=!1)=>{if(O===e&&!t)return;let n=O;O=e,ve(),I&&(I.value=e??``),e===null?f.removeAttribute(`data-value`):f.setAttribute(`data-value`,e);let r=x??b,a=M.length>0?M:d(r,`combobox-item`);for(let t of a)B(t)===e?(m(t,`selected`,!0),t.setAttribute(`data-selected`,``)):(m(t,`selected`,!1),t.removeAttribute(`data-selected`));y.value=V(e),!t&&n!==e&&(i(f,`combobox:change`,{value:e}),se?.(e))},Se=e=>{if(R(e))return;let t=B(e);t!==void 0&&($(t),Q(!1))};return m(y,`expanded`,!1),b.hidden=!0,Z(`closed`),$(O,!0),j.push(p(y,`input`,()=>{let e=y.value;i(f,`combobox:input-change`,{inputValue:e}),le?.(e),D?(K(e),E&&P.length>0?Y(0):X(),J.update()):Q(!0)}),p(y,`keydown`,e=>{if(!T)switch(e.key){case`ArrowDown`:{if(e.preventDefault(),!D){Q(!0);return}A=!0;let t=P.length;if(t===0)return;Y(k===-1?0:(k+1)%t);break}case`ArrowUp`:{if(e.preventDefault(),!D){Q(!0);return}A=!0;let t=P.length;if(t===0)return;Y(k===-1?t-1:(k-1+t)%t);break}case`Home`:if(!D)return;e.preventDefault(),A=!0,P.length>0&&Y(0);break;case`End`:if(!D)return;e.preventDefault(),A=!0,P.length>0&&Y(P.length-1);break;case`Enter`:if(!D)return;e.preventDefault(),k>=0&&k<P.length&&Se(P[k]);break;case`Escape`:D?(e.preventDefault(),Q(!1)):O!==null&&(e.preventDefault(),$(null));break;case`Tab`:D&&Q(!1,!0);break}}),p(y,`focus`,()=>{T||(y.select(),ae&&!D&&Q(!0))})),S&&j.push(p(S,`click`,()=>{T||(D?Q(!1):(Q(!0),y.focus()))})),j.push(p(b,`click`,e=>{let t=e.target.closest?.(`[data-slot="combobox-item"]`);t&&!t.hidden&&Se(t)}),p(b,`pointermove`,e=>{let t=e.target.closest?.(`[data-slot="combobox-item"]`);if(!(A&&(A=!1,t&&F.get(t)===k)))if(t&&!R(t)&&!t.hidden){let e=F.get(t);e!==void 0&&e!==k&&Y(e)}else X()}),p(b,`pointerleave`,()=>{A||X()}),p(b,`mousedown`,e=>{e.preventDefault()})),j.push(t({root:f,isOpen:()=>D,onDismiss:()=>Q(!1),closeOnClickOutside:!0,closeOnEscape:!1})),j.push(p(f,`combobox:set`,e=>{let t=e.detail;t?.value!==void 0&&$(t.value),t?.open!==void 0&&Q(t.open),t?.inputValue!==void 0&&(y.value=t.inputValue)})),ne&&Q(!0),{get value(){return O},get inputValue(){return y.value},get isOpen(){return D},select:e=>$(e),clear:()=>$(null),open:()=>Q(!0),close:()=>Q(!1),destroy:()=>{J.stop(),L.cleanup(),j.forEach(e=>e()),j.length=0,I&&I.parentNode&&I.parentNode.removeChild(I),v.delete(f)}}}const v=new WeakSet;function y(e=document){let t=[];for(let n of f(e,`combobox`))v.has(n)||(v.add(n),t.push(_(n)));return t}export{y as create,_ as createCombobox};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-slot/combobox",
3
- "version": "0.2.31",
3
+ "version": "0.2.32",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.cjs",
File without changes
File without changes