@zag-js/combobox 0.53.0 → 0.55.0

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.js CHANGED
@@ -1,2 +1,1479 @@
1
- "use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var src_exports={};__export(src_exports,{anatomy:()=>anatomy,collection:()=>collection,connect:()=>connect,machine:()=>machine});module.exports=__toCommonJS(src_exports);var import_anatomy=require("@zag-js/anatomy");var anatomy=(0,import_anatomy.createAnatomy)("combobox").parts("root","label","input","positioner","control","trigger","content","clearTrigger","item","itemText","itemIndicator","itemGroup","itemGroupLabel");var parts=anatomy.build();var import_collection=require("@zag-js/collection");var import_core=require("@zag-js/core");var collection=options=>{return(0,import_core.ref)(new import_collection.Collection(options))};collection.empty=()=>{return(0,import_core.ref)(new import_collection.Collection({items:[]}))};var import_dom_event=require("@zag-js/dom-event");var import_dom_query2=require("@zag-js/dom-query");var import_popper=require("@zag-js/popper");var import_dom_query=require("@zag-js/dom-query");var dom=(0,import_dom_query.createScope)({getRootId:ctx=>ctx.ids?.root??`combobox:${ctx.id}`,getLabelId:ctx=>ctx.ids?.label??`combobox:${ctx.id}:label`,getControlId:ctx=>ctx.ids?.control??`combobox:${ctx.id}:control`,getInputId:ctx=>ctx.ids?.input??`combobox:${ctx.id}:input`,getContentId:ctx=>ctx.ids?.content??`combobox:${ctx.id}:content`,getPositionerId:ctx=>ctx.ids?.positioner??`combobox:${ctx.id}:popper`,getTriggerId:ctx=>ctx.ids?.trigger??`combobox:${ctx.id}:toggle-btn`,getClearTriggerId:ctx=>ctx.ids?.clearTrigger??`combobox:${ctx.id}:clear-btn`,getItemGroupId:(ctx,id)=>ctx.ids?.itemGroup?.(id)??`combobox:${ctx.id}:optgroup:${id}`,getItemGroupLabelId:(ctx,id)=>ctx.ids?.itemGroupLabel?.(id)??`combobox:${ctx.id}:optgroup-label:${id}`,getItemId:(ctx,id)=>`combobox:${ctx.id}:option:${id}`,getContentEl:ctx=>dom.getById(ctx,dom.getContentId(ctx)),getInputEl:ctx=>dom.getById(ctx,dom.getInputId(ctx)),getPositionerEl:ctx=>dom.getById(ctx,dom.getPositionerId(ctx)),getControlEl:ctx=>dom.getById(ctx,dom.getControlId(ctx)),getTriggerEl:ctx=>dom.getById(ctx,dom.getTriggerId(ctx)),getClearTriggerEl:ctx=>dom.getById(ctx,dom.getClearTriggerId(ctx)),getHighlightedItemEl:ctx=>{const value=ctx.highlightedValue;if(value==null)return;return(0,import_dom_query.query)(dom.getContentEl(ctx),`[role=option][data-value="${CSS.escape(value)}"`)},focusInputEl:ctx=>{const inputEl=dom.getInputEl(ctx);if(dom.getActiveElement(ctx)===inputEl)return;inputEl?.focus({preventScroll:true})},focusTriggerEl:ctx=>{const triggerEl=dom.getTriggerEl(ctx);if(dom.getActiveElement(ctx)===triggerEl)return;triggerEl?.focus({preventScroll:true})}});function connect(state,send,normalize){const translations=state.context.translations;const collection2=state.context.collection;const disabled=state.context.disabled;const interactive=state.context.isInteractive;const invalid=state.context.invalid;const readOnly=state.context.readOnly;const open=state.hasTag("open");const focused=state.hasTag("focused");const composite=state.context.composite;const highlightedValue=state.context.highlightedValue;const popperStyles=(0,import_popper.getPlacementStyles)({...state.context.positioning,placement:state.context.currentPlacement});function getItemState(props){const{item}=props;const disabled2=collection2.isItemDisabled(item);const value=collection2.itemToValue(item);return{value,disabled:Boolean(disabled2||disabled2),highlighted:highlightedValue===value,selected:state.context.value.includes(value)}}return{focused,open,inputValue:state.context.inputValue,highlightedValue,highlightedItem:state.context.highlightedItem,value:state.context.value,valueAsString:state.context.valueAsString,hasSelectedItems:state.context.hasSelectedItems,selectedItems:state.context.selectedItems,collection:state.context.collection,reposition(options={}){send({type:"POSITIONING.SET",options})},setCollection(collection3){send({type:"COLLECTION.SET",value:collection3})},setHighlightValue(value){send({type:"HIGHLIGHTED_VALUE.SET",value})},selectValue(value){send({type:"ITEM.SELECT",value})},setValue(value){send({type:"VALUE.SET",value})},setInputValue(value){send({type:"INPUT_VALUE.SET",value})},clearValue(value){if(value!=null){send({type:"ITEM.CLEAR",value})}else{send("VALUE.CLEAR")}},focus(){dom.getInputEl(state.context)?.focus()},setOpen(nextOpen){if(nextOpen===open)return;send(nextOpen?"OPEN":"CLOSE")},rootProps:normalize.element({...parts.root.attrs,dir:state.context.dir,id:dom.getRootId(state.context),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),"data-readonly":(0,import_dom_query2.dataAttr)(readOnly)}),labelProps:normalize.label({...parts.label.attrs,dir:state.context.dir,htmlFor:dom.getInputId(state.context),id:dom.getLabelId(state.context),"data-readonly":(0,import_dom_query2.dataAttr)(readOnly),"data-disabled":(0,import_dom_query2.dataAttr)(disabled),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),"data-focus":(0,import_dom_query2.dataAttr)(focused),onClick(event){if(composite)return;event.preventDefault();dom.getTriggerEl(state.context)?.focus({preventScroll:true})}}),controlProps:normalize.element({...parts.control.attrs,dir:state.context.dir,id:dom.getControlId(state.context),"data-state":open?"open":"closed","data-focus":(0,import_dom_query2.dataAttr)(focused),"data-disabled":(0,import_dom_query2.dataAttr)(disabled),"data-invalid":(0,import_dom_query2.dataAttr)(invalid)}),positionerProps:normalize.element({...parts.positioner.attrs,dir:state.context.dir,id:dom.getPositionerId(state.context),style:popperStyles.floating}),inputProps:normalize.input({...parts.input.attrs,dir:state.context.dir,"aria-invalid":(0,import_dom_query2.ariaAttr)(invalid),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),name:state.context.name,form:state.context.form,disabled,autoFocus:state.context.autoFocus,autoComplete:"off",autoCorrect:"off",autoCapitalize:"none",spellCheck:"false",readOnly,placeholder:state.context.placeholder,id:dom.getInputId(state.context),type:"text",role:"combobox",defaultValue:state.context.inputValue,"aria-autocomplete":state.context.autoComplete?"both":"list","aria-controls":dom.getContentId(state.context),"aria-expanded":open,"data-state":open?"open":"closed","aria-activedescendant":highlightedValue?dom.getItemId(state.context,highlightedValue):void 0,onClick(event){if(event.defaultPrevented)return;if(!state.context.openOnClick)return;if(!interactive)return;send("INPUT.CLICK")},onFocus(){if(disabled)return;send("INPUT.FOCUS")},onBlur(){if(disabled)return;send("INPUT.BLUR")},onChange(event){send({type:"INPUT.CHANGE",value:event.currentTarget.value})},onKeyDown(event){if(event.defaultPrevented)return;if(!interactive)return;if(event.ctrlKey||event.shiftKey||(0,import_dom_query2.isComposingEvent)(event))return;const openOnKeyPress=state.context.openOnKeyPress;const isModifierKey=event.ctrlKey||event.metaKey||event.shiftKey;const keypress=true;const keymap={ArrowDown(event2){if(!openOnKeyPress&&!open)return;send({type:event2.altKey?"OPEN":"INPUT.ARROW_DOWN",keypress});event2.preventDefault()},ArrowUp(){if(!openOnKeyPress&&!open)return;send({type:event.altKey?"CLOSE":"INPUT.ARROW_UP",keypress});event.preventDefault()},Home(event2){if(isModifierKey)return;send({type:"INPUT.HOME",keypress});if(open){event2.preventDefault()}},End(event2){if(isModifierKey)return;send({type:"INPUT.END",keypress});if(open){event2.preventDefault()}},Enter(event2){send({type:"INPUT.ENTER",keypress});if(open){event2.preventDefault()}const itemEl=dom.getHighlightedItemEl(state.context);(0,import_dom_event.clickIfLink)(itemEl)},Escape(){send({type:"INPUT.ESCAPE",keypress});event.preventDefault()}};const key=(0,import_dom_event.getEventKey)(event,state.context);const exec=keymap[key];exec?.(event)}}),getTriggerProps(props={}){return normalize.button({...parts.trigger.attrs,dir:state.context.dir,id:dom.getTriggerId(state.context),"aria-haspopup":composite?"listbox":"dialog",type:"button",tabIndex:props.focusable?void 0:-1,"aria-label":translations.triggerLabel,"aria-expanded":open,"data-state":open?"open":"closed","aria-controls":open?dom.getContentId(state.context):void 0,disabled,"data-focusable":(0,import_dom_query2.dataAttr)(props.focusable),"data-readonly":(0,import_dom_query2.dataAttr)(readOnly),"data-disabled":(0,import_dom_query2.dataAttr)(disabled),onFocus(){if(!props.focusable)return;send({type:"INPUT.FOCUS",src:"trigger"})},onClick(event){if(event.defaultPrevented)return;if(!interactive)return;if(!(0,import_dom_event.isLeftClick)(event))return;send("TRIGGER.CLICK")},onPointerDown(event){if(!interactive)return;if(event.pointerType==="touch")return;event.preventDefault();queueMicrotask(()=>{dom.getInputEl(state.context)?.focus({preventScroll:true})})},onKeyDown(event){if(event.defaultPrevented)return;if(composite)return;const keyMap={ArrowDown(){send({type:"INPUT.ARROW_DOWN",src:"trigger"})},ArrowUp(){send({type:"INPUT.ARROW_UP",src:"trigger"})}};const key=(0,import_dom_event.getEventKey)(event,state.context);const exec=keyMap[key];if(exec){exec(event);event.preventDefault()}}})},contentProps:normalize.element({...parts.content.attrs,dir:state.context.dir,id:dom.getContentId(state.context),role:!composite?"dialog":"listbox",tabIndex:-1,hidden:!open,"data-state":open?"open":"closed","aria-labelledby":dom.getLabelId(state.context),"aria-multiselectable":state.context.multiple&&composite?true:void 0,onPointerDown(event){event.preventDefault()}}),listProps:normalize.element({role:!composite?"listbox":void 0,"aria-labelledby":dom.getLabelId(state.context),"aria-multiselectable":state.context.multiple&&!composite?true:void 0}),clearTriggerProps:normalize.button({...parts.clearTrigger.attrs,dir:state.context.dir,id:dom.getClearTriggerId(state.context),type:"button",tabIndex:-1,disabled,"aria-label":translations.clearTriggerLabel,"aria-controls":dom.getInputId(state.context),hidden:!state.context.value.length,onPointerDown(event){event.preventDefault()},onClick(event){if(event.defaultPrevented)return;if(!interactive)return;send({type:"VALUE.CLEAR",src:"clear-trigger"})}}),getItemState,getItemProps(props){const itemState=getItemState(props);const value=itemState.value;return normalize.element({...parts.item.attrs,dir:state.context.dir,id:dom.getItemId(state.context,value),role:"option",tabIndex:-1,"data-highlighted":(0,import_dom_query2.dataAttr)(itemState.highlighted),"data-state":itemState.selected?"checked":"unchecked","aria-selected":itemState.highlighted,"aria-disabled":itemState.disabled,"data-disabled":(0,import_dom_query2.dataAttr)(itemState.disabled),"data-value":itemState.value,onPointerMove(){if(itemState.disabled)return;if(itemState.highlighted)return;send({type:"ITEM.POINTER_MOVE",value})},onPointerLeave(){if(props.persistFocus)return;if(itemState.disabled)return;const mouseMoved=state.previousEvent.type.includes("POINTER");if(!mouseMoved)return;send({type:"ITEM.POINTER_LEAVE",value})},onPointerUp(event){if((0,import_dom_query2.isDownloadingEvent)(event))return;if((0,import_dom_query2.isOpeningInNewTab)(event))return;if((0,import_dom_event.isContextMenuEvent)(event))return;if(itemState.disabled)return;send({type:"ITEM.CLICK",src:"pointerup",value})},onTouchEnd(event){event.preventDefault();event.stopPropagation()}})},getItemTextProps(props){const itemState=getItemState(props);return normalize.element({...parts.itemText.attrs,dir:state.context.dir,"data-disabled":(0,import_dom_query2.dataAttr)(itemState.disabled),"data-highlighted":(0,import_dom_query2.dataAttr)(itemState.highlighted)})},getItemIndicatorProps(props){const itemState=getItemState(props);return normalize.element({"aria-hidden":true,...parts.itemIndicator.attrs,dir:state.context.dir,"data-state":itemState.selected?"checked":"unchecked",hidden:!itemState.selected})},getItemGroupProps(props){const{id}=props;return normalize.element({...parts.itemGroup.attrs,dir:state.context.dir,id:dom.getItemGroupId(state.context,id),"aria-labelledby":dom.getItemGroupLabelId(state.context,id)})},getItemGroupLabelProps(props){const{htmlFor}=props;return normalize.element({...parts.itemGroupLabel.attrs,dir:state.context.dir,id:dom.getItemGroupLabelId(state.context,htmlFor),role:"group"})}}}var import_aria_hidden=require("@zag-js/aria-hidden");var import_core2=require("@zag-js/core");var import_dismissable=require("@zag-js/dismissable");var import_dom_query3=require("@zag-js/dom-query");var import_popper2=require("@zag-js/popper");var import_utils=require("@zag-js/utils");var{and,not}=import_core2.guards;function machine(userContext){const ctx=(0,import_utils.compact)(userContext);return(0,import_core2.createMachine)({id:"combobox",initial:ctx.open?"suggesting":"idle",context:{loopFocus:true,openOnClick:false,value:[],highlightedValue:null,inputValue:"",allowCustomValue:false,closeOnSelect:!ctx.multiple,inputBehavior:"none",selectionBehavior:"replace",openOnKeyPress:true,openOnChange:true,composite:true,...ctx,highlightedItem:null,selectedItems:[],valueAsString:"",collection:ctx.collection??collection.empty(),positioning:{placement:"bottom",flip:false,sameWidth:true,...ctx.positioning},translations:{triggerLabel:"Toggle suggestions",clearTriggerLabel:"Clear value",...ctx.translations}},created:["syncInitialValues","syncSelectionBehavior"],computed:{isInputValueEmpty:ctx2=>ctx2.inputValue.length===0,isInteractive:ctx2=>!(ctx2.readOnly||ctx2.disabled),autoComplete:ctx2=>ctx2.inputBehavior==="autocomplete",autoHighlight:ctx2=>ctx2.inputBehavior==="autohighlight",hasSelectedItems:ctx2=>ctx2.value.length>0},watch:{value:["syncSelectedItems"],inputValue:["syncInputValue"],highlightedValue:["syncHighlightedItem","autofillInputValue"],multiple:["syncSelectionBehavior"],open:["toggleVisibility"]},on:{"HIGHLIGHTED_VALUE.SET":{actions:["setHighlightedItem"]},"ITEM.SELECT":{actions:["selectItem"]},"ITEM.CLEAR":{actions:["clearItem"]},"VALUE.SET":{actions:["setSelectedItems"]},"INPUT_VALUE.SET":{actions:"setInputValue"},"COLLECTION.SET":{actions:["setCollection"]},"POSITIONING.SET":{actions:["reposition"]}},states:{idle:{tags:["idle","closed"],entry:["scrollContentToTop","clearHighlightedItem"],on:{"CONTROLLED.OPEN":{target:"interacting"},"TRIGGER.CLICK":[{guard:"isOpenControlled",actions:["setInitialFocus","highlightFirstSelectedItem","invokeOnOpen"]},{target:"interacting",actions:["setInitialFocus","highlightFirstSelectedItem","invokeOnOpen"]}],"INPUT.CLICK":[{guard:"isOpenControlled",actions:["highlightFirstSelectedItem","invokeOnOpen"]},{target:"interacting",actions:["highlightFirstSelectedItem","invokeOnOpen"]}],"INPUT.FOCUS":{target:"focused"},OPEN:[{guard:"isOpenControlled",actions:["invokeOnOpen"]},{target:"interacting",actions:["invokeOnOpen"]}],"VALUE.CLEAR":{target:"focused",actions:["clearInputValue","clearSelectedItems","setInitialFocus"]}}},focused:{tags:["focused","closed"],entry:["scrollContentToTop","clearHighlightedItem"],on:{"CONTROLLED.OPEN":[{guard:"isChangeEvent",target:"suggesting"},{target:"interacting"}],"INPUT.CHANGE":[{guard:and("isOpenControlled","openOnChange"),actions:["setInputValue","invokeOnOpen","highlightFirstItemIfNeeded"]},{guard:"openOnChange",target:"suggesting",actions:["setInputValue","invokeOnOpen","highlightFirstItemIfNeeded"]},{actions:"setInputValue"}],"LAYER.INTERACT_OUTSIDE":{target:"idle"},"INPUT.ESCAPE":{guard:and("isCustomValue",not("allowCustomValue")),actions:"revertInputValue"},"INPUT.BLUR":{target:"idle"},"INPUT.CLICK":[{guard:"isOpenControlled",actions:["highlightFirstSelectedItem","invokeOnOpen"]},{target:"interacting",actions:["highlightFirstSelectedItem","invokeOnOpen"]}],"TRIGGER.CLICK":[{guard:"isOpenControlled",actions:["setInitialFocus","highlightFirstSelectedItem","invokeOnOpen"]},{target:"interacting",actions:["setInitialFocus","highlightFirstSelectedItem","invokeOnOpen"]}],"INPUT.ARROW_DOWN":[{guard:and("isOpenControlled","autoComplete"),actions:["invokeOnOpen"]},{guard:"autoComplete",target:"interacting",actions:["invokeOnOpen"]},{guard:"isOpenControlled",actions:["highlightFirstOrSelectedItem","invokeOnOpen"]},{target:"interacting",actions:["highlightFirstOrSelectedItem","invokeOnOpen"]}],"INPUT.ARROW_UP":[{guard:"autoComplete",target:"interacting",actions:"invokeOnOpen"},{guard:"autoComplete",target:"interacting",actions:"invokeOnOpen"},{target:"interacting",actions:["highlightLastOrSelectedItem","invokeOnOpen"]},{target:"interacting",actions:["highlightLastOrSelectedItem","invokeOnOpen"]}],OPEN:[{guard:"isOpenControlled",actions:["invokeOnOpen"]},{target:"interacting",actions:["invokeOnOpen"]}],"VALUE.CLEAR":{actions:["clearInputValue","clearSelectedItems"]}}},interacting:{tags:["open","focused"],entry:["setInitialFocus"],activities:["scrollToHighlightedItem","trackDismissableLayer","computePlacement","hideOtherElements"],on:{"CONTROLLED.CLOSE":[{guard:"restoreFocus",target:"focused",actions:["setFinalFocus"]},{target:"idle"}],"INPUT.HOME":{actions:["highlightFirstItem"]},"INPUT.END":{actions:["highlightLastItem"]},"INPUT.ARROW_DOWN":[{guard:and("autoComplete","isLastItemHighlighted"),actions:["clearHighlightedItem","scrollContentToTop"]},{actions:["highlightNextItem"]}],"INPUT.ARROW_UP":[{guard:and("autoComplete","isFirstItemHighlighted"),actions:"clearHighlightedItem"},{actions:"highlightPrevItem"}],"INPUT.ENTER":[{guard:and("isOpenControlled","closeOnSelect"),actions:["selectHighlightedItem","invokeOnClose"]},{guard:"closeOnSelect",target:"focused",actions:["selectHighlightedItem","invokeOnClose","setFinalFocus"]},{actions:["selectHighlightedItem"]}],"INPUT.CHANGE":[{guard:"autoComplete",target:"suggesting",actions:["setInputValue","invokeOnOpen"]},{target:"suggesting",actions:["clearHighlightedItem","setInputValue","invokeOnOpen"]}],"ITEM.POINTER_MOVE":{actions:["setHighlightedItem"]},"ITEM.POINTER_LEAVE":{actions:["clearHighlightedItem"]},"ITEM.CLICK":[{guard:and("isOpenControlled","closeOnSelect"),actions:["selectItem","invokeOnClose"]},{guard:"closeOnSelect",target:"focused",actions:["selectItem","invokeOnClose","setFinalFocus"]},{actions:["selectItem"]}],"LAYER.ESCAPE":[{guard:and("isOpenControlled","autoComplete"),actions:["syncInputValue","invokeOnClose"]},{guard:"autoComplete",target:"focused",actions:["syncInputValue","invokeOnClose"]},{guard:"isOpenControlled",actions:"invokeOnClose"},{target:"focused",actions:["invokeOnClose","setFinalFocus"]}],"TRIGGER.CLICK":[{guard:"isOpenControlled",actions:"invokeOnClose"},{target:"focused",actions:"invokeOnClose"}],"LAYER.INTERACT_OUTSIDE":[{guard:and("isOpenControlled","isCustomValue",not("allowCustomValue")),actions:["revertInputValue","invokeOnClose"]},{guard:and("isCustomValue",not("allowCustomValue")),target:"idle",actions:["revertInputValue","invokeOnClose"]},{guard:"isOpenControlled",actions:"invokeOnClose"},{target:"idle",actions:"invokeOnClose"}],CLOSE:[{guard:"isOpenControlled",actions:["invokeOnClose"]},{target:"focused",actions:["invokeOnClose","setFinalFocus"]}],"VALUE.CLEAR":[{guard:"isOpenControlled",actions:["clearInputValue","clearSelectedItems","invokeOnClose"]},{target:"focused",actions:["clearInputValue","clearSelectedItems","invokeOnClose","setFinalFocus"]}]}},suggesting:{tags:["open","focused"],activities:["trackDismissableLayer","scrollToHighlightedItem","computePlacement","trackChildNodes","hideOtherElements"],entry:["setInitialFocus"],on:{"CONTROLLED.CLOSE":[{guard:"restoreFocus",target:"focused",actions:["setFinalFocus"]},{target:"idle"}],CHILDREN_CHANGE:{actions:["highlightFirstItem"]},"INPUT.ARROW_DOWN":{target:"interacting",actions:["highlightNextItem"]},"INPUT.ARROW_UP":{target:"interacting",actions:["highlightPrevItem"]},"INPUT.HOME":{target:"interacting",actions:["highlightFirstItem"]},"INPUT.END":{target:"interacting",actions:["highlightLastItem"]},"INPUT.ENTER":[{guard:and("isOpenControlled","closeOnSelect"),actions:["selectHighlightedItem","invokeOnClose"]},{guard:"closeOnSelect",target:"focused",actions:["selectHighlightedItem","invokeOnClose","setFinalFocus"]},{actions:["selectHighlightedItem"]}],"INPUT.CHANGE":[{guard:"autoHighlight",actions:["setInputValue"]},{actions:["setInputValue"]}],"LAYER.ESCAPE":[{guard:"isOpenControlled",actions:["invokeOnClose"]},{target:"focused",actions:["invokeOnClose"]}],"ITEM.POINTER_MOVE":{target:"interacting",actions:["setHighlightedItem"]},"ITEM.POINTER_LEAVE":{actions:["clearHighlightedItem"]},"LAYER.INTERACT_OUTSIDE":[{guard:and("isOpenControlled","isCustomValue",not("allowCustomValue")),actions:["revertInputValue","invokeOnClose"]},{guard:and("isCustomValue",not("allowCustomValue")),target:"idle",actions:["revertInputValue","invokeOnClose"]},{guard:"isOpenControlled",actions:["invokeOnClose"]},{target:"idle",actions:["invokeOnClose"]}],"TRIGGER.CLICK":[{guard:"isOpenControlled",actions:["invokeOnClose"]},{target:"focused",actions:["invokeOnClose"]}],"ITEM.CLICK":[{guard:and("isOpenControlled","closeOnSelect"),actions:["selectItem","invokeOnClose"]},{guard:"closeOnSelect",target:"focused",actions:["selectItem","invokeOnClose","setFinalFocus"]},{actions:["selectItem"]}],CLOSE:[{guard:"isOpenControlled",actions:["invokeOnClose"]},{target:"focused",actions:["invokeOnClose","setFinalFocus"]}],"VALUE.CLEAR":[{guard:"isOpenControlled",actions:["clearInputValue","clearSelectedItems","invokeOnClose"]},{target:"focused",actions:["clearInputValue","clearSelectedItems","invokeOnClose","setFinalFocus"]}]}}}},{guards:{isInputValueEmpty:ctx2=>ctx2.isInputValueEmpty,autoComplete:ctx2=>ctx2.autoComplete&&!ctx2.multiple,autoHighlight:ctx2=>ctx2.autoHighlight,isFirstItemHighlighted:ctx2=>ctx2.collection.first()===ctx2.highlightedValue,isLastItemHighlighted:ctx2=>ctx2.collection.last()===ctx2.highlightedValue,isCustomValue:ctx2=>ctx2.inputValue!==ctx2.valueAsString,allowCustomValue:ctx2=>!!ctx2.allowCustomValue,hasHighlightedItem:ctx2=>ctx2.highlightedValue!=null,closeOnSelect:ctx2=>!!ctx2.closeOnSelect,isOpenControlled:ctx2=>!!ctx2["open.controlled"],openOnChange:(ctx2,evt)=>{if((0,import_utils.isBoolean)(ctx2.openOnChange))return ctx2.openOnChange;return!!ctx2.openOnChange?.({inputValue:evt.value})},restoreFocus:(_ctx,evt)=>evt.restoreFocus==null?true:!!evt.restoreFocus,isChangeEvent:(_ctx,evt)=>evt.previousEvent?.type==="INPUT.CHANGE"},activities:{trackDismissableLayer(ctx2,_evt,{send}){if(ctx2.disableLayer)return;const contentEl=()=>dom.getContentEl(ctx2);return(0,import_dismissable.trackDismissableElement)(contentEl,{defer:true,exclude:()=>[dom.getInputEl(ctx2),dom.getTriggerEl(ctx2),dom.getClearTriggerEl(ctx2)],onFocusOutside:ctx2.onFocusOutside,onPointerDownOutside:ctx2.onPointerDownOutside,onInteractOutside:ctx2.onInteractOutside,onEscapeKeyDown(event){event.preventDefault();event.stopPropagation();send("LAYER.ESCAPE")},onDismiss(){send({type:"LAYER.INTERACT_OUTSIDE",restoreFocus:false})}})},hideOtherElements(ctx2){return(0,import_aria_hidden.ariaHidden)([dom.getInputEl(ctx2),dom.getContentEl(ctx2),dom.getTriggerEl(ctx2)])},computePlacement(ctx2){const controlEl=()=>dom.getControlEl(ctx2);const positionerEl=()=>dom.getPositionerEl(ctx2);ctx2.currentPlacement=ctx2.positioning.placement;return(0,import_popper2.getPlacement)(controlEl,positionerEl,{...ctx2.positioning,defer:true,onComplete(data){ctx2.currentPlacement=data.placement}})},trackChildNodes(ctx2,_evt,{send}){if(!ctx2.autoHighlight)return;const exec=()=>send("CHILDREN_CHANGE");const contentEl=()=>dom.getContentEl(ctx2);return(0,import_dom_query3.observeChildren)(contentEl,{callback:exec,defer:true})},scrollToHighlightedItem(ctx2,_evt,{getState}){const inputEl=dom.getInputEl(ctx2);let cleanups=[];const exec=immediate=>{const state=getState();const pointer=state.event.type.includes("POINTER");if(pointer||!ctx2.highlightedValue)return;const itemEl=dom.getHighlightedItemEl(ctx2);const contentEl=dom.getContentEl(ctx2);if(ctx2.scrollToIndexFn){const highlightedIndex=ctx2.collection.indexOf(ctx2.highlightedValue);ctx2.scrollToIndexFn({index:highlightedIndex,immediate});return}const rafCleanup2=(0,import_dom_query3.raf)(()=>{(0,import_dom_query3.scrollIntoView)(itemEl,{rootEl:contentEl,block:"nearest"})});cleanups.push(rafCleanup2)};const rafCleanup=(0,import_dom_query3.raf)(()=>exec(true));cleanups.push(rafCleanup);const observerCleanup=(0,import_dom_query3.observeAttributes)(inputEl,{attributes:["aria-activedescendant"],callback:()=>exec(false)});cleanups.push(observerCleanup);return()=>{cleanups.forEach(cleanup=>cleanup())}}},actions:{reposition(ctx2,evt){const controlEl=()=>dom.getControlEl(ctx2);const positionerEl=()=>dom.getPositionerEl(ctx2);(0,import_popper2.getPlacement)(controlEl,positionerEl,{...ctx2.positioning,...evt.options,defer:true,listeners:false,onComplete(data){ctx2.currentPlacement=data.placement}})},setHighlightedItem(ctx2,evt){if(evt.value==null)return;set.highlightedValue(ctx2,evt.value)},clearHighlightedItem(ctx2){set.highlightedValue(ctx2,null,true)},selectHighlightedItem(ctx2){set.value(ctx2,ctx2.highlightedValue)},selectItem(ctx2,evt){if(evt.value==null)return;set.value(ctx2,evt.value)},clearItem(ctx2,evt){if(evt.value==null)return;const value=ctx2.value.filter(v=>v!==evt.value);set.value(ctx2,value)},setInitialFocus(ctx2){(0,import_dom_query3.raf)(()=>{dom.focusInputEl(ctx2)})},setFinalFocus(ctx2){(0,import_dom_query3.raf)(()=>{const triggerEl=dom.getTriggerEl(ctx2);if(triggerEl?.dataset.focusable==null){dom.focusInputEl(ctx2)}else{dom.focusTriggerEl(ctx2)}})},syncInputValue(ctx2){const inputEl=dom.getInputEl(ctx2);if(!inputEl)return;inputEl.value=ctx2.inputValue;queueMicrotask(()=>{const{selectionStart,selectionEnd}=inputEl;if(Math.abs((selectionEnd??0)-(selectionStart??0))!==0)return;if(selectionStart!==0)return;inputEl.setSelectionRange(inputEl.value.length,inputEl.value.length)})},setInputValue(ctx2,evt){set.inputValue(ctx2,evt.value)},clearInputValue(ctx2){set.inputValue(ctx2,"")},revertInputValue(ctx2){const inputValue=(0,import_utils.match)(ctx2.selectionBehavior,{replace:ctx2.hasSelectedItems?ctx2.valueAsString:"",preserve:ctx2.inputValue,clear:""});set.inputValue(ctx2,inputValue)},syncInitialValues(ctx2){const selectedItems=ctx2.collection.items(ctx2.value);const valueAsString=ctx2.collection.itemsToString(selectedItems);ctx2.highlightedItem=ctx2.collection.item(ctx2.highlightedValue);ctx2.selectedItems=selectedItems;ctx2.valueAsString=valueAsString;ctx2.inputValue=(0,import_utils.match)(ctx2.selectionBehavior,{preserve:ctx2.inputValue||valueAsString,replace:valueAsString,clear:""})},syncSelectionBehavior(ctx2){if(ctx2.multiple){ctx2.selectionBehavior="clear"}},setSelectedItems(ctx2,evt){if(!(0,import_utils.isArray)(evt.value))return;set.value(ctx2,evt.value)},clearSelectedItems(ctx2){set.value(ctx2,[])},scrollContentToTop(ctx2){if(ctx2.scrollToIndexFn){ctx2.scrollToIndexFn({index:0,immediate:true})}else{const contentEl=dom.getContentEl(ctx2);if(!contentEl)return;contentEl.scrollTop=0}},invokeOnOpen(ctx2){ctx2.onOpenChange?.({open:true})},invokeOnClose(ctx2){ctx2.onOpenChange?.({open:false})},highlightFirstItem(ctx2){(0,import_dom_query3.raf)(()=>{const value=ctx2.collection.first();set.highlightedValue(ctx2,value)})},highlightFirstItemIfNeeded(ctx2){if(!ctx2.autoHighlight)return;(0,import_dom_query3.raf)(()=>{const value=ctx2.collection.first();set.highlightedValue(ctx2,value)})},highlightLastItem(ctx2){(0,import_dom_query3.raf)(()=>{const value=ctx2.collection.last();set.highlightedValue(ctx2,value)})},highlightNextItem(ctx2){let value=null;if(ctx2.highlightedValue){value=ctx2.collection.next(ctx2.highlightedValue);if(!value&&ctx2.loopFocus)value=ctx2.collection.first()}else{value=ctx2.collection.first()}set.highlightedValue(ctx2,value)},highlightPrevItem(ctx2){let value=null;if(ctx2.highlightedValue){value=ctx2.collection.prev(ctx2.highlightedValue);if(!value&&ctx2.loopFocus)value=ctx2.collection.last()}else{value=ctx2.collection.last()}set.highlightedValue(ctx2,value)},highlightFirstSelectedItem(ctx2){(0,import_dom_query3.raf)(()=>{const[value]=ctx2.collection.sort(ctx2.value);set.highlightedValue(ctx2,value)})},highlightFirstOrSelectedItem(ctx2){(0,import_dom_query3.raf)(()=>{let value=null;if(ctx2.hasSelectedItems){value=ctx2.collection.sort(ctx2.value)[0]}else{value=ctx2.collection.first()}set.highlightedValue(ctx2,value)})},highlightLastOrSelectedItem(ctx2){(0,import_dom_query3.raf)(()=>{let value=null;if(ctx2.hasSelectedItems){value=ctx2.collection.sort(ctx2.value)[0]}else{value=ctx2.collection.last()}set.highlightedValue(ctx2,value)})},autofillInputValue(ctx2,evt){const inputEl=dom.getInputEl(ctx2);if(!ctx2.autoComplete||!inputEl||!evt.keypress)return;const valueText=ctx2.collection.valueToString(ctx2.highlightedValue);(0,import_dom_query3.raf)(()=>{inputEl.value=valueText||ctx2.inputValue})},setCollection(ctx2,evt){ctx2.collection=evt.value},syncSelectedItems(ctx2){sync.valueChange(ctx2)},syncHighlightedItem(ctx2){sync.highlightChange(ctx2)},toggleVisibility(ctx2,evt,{send}){send({type:ctx2.open?"CONTROLLED.OPEN":"CONTROLLED.CLOSE",previousEvent:evt})}}})}var sync={valueChange:ctx=>{const prevSelectedItems=ctx.selectedItems;ctx.selectedItems=ctx.value.map(v=>{const foundItem=prevSelectedItems.find(item=>ctx.collection.itemToValue(item)===v);if(foundItem)return foundItem;return ctx.collection.item(v)});const valueAsString=ctx.collection.itemsToString(ctx.selectedItems);ctx.valueAsString=valueAsString;let inputValue;if(ctx.getSelectionValue){inputValue=ctx.getSelectionValue({inputValue:ctx.inputValue,selectedItems:Array.from(ctx.selectedItems),valueAsString})}else{inputValue=(0,import_utils.match)(ctx.selectionBehavior,{replace:ctx.valueAsString,preserve:ctx.inputValue,clear:""})}set.inputValue(ctx,inputValue)},highlightChange:ctx=>{ctx.highlightedItem=ctx.collection.item(ctx.highlightedValue)}};var invoke={valueChange:ctx=>{sync.valueChange(ctx);ctx.onValueChange?.({value:Array.from(ctx.value),items:Array.from(ctx.selectedItems)})},highlightChange:ctx=>{sync.highlightChange(ctx);ctx.onHighlightChange?.({highlightedValue:ctx.highlightedValue,highlightedItem:ctx.highlightedItem})},inputChange:ctx=>{ctx.onInputValueChange?.({inputValue:ctx.inputValue})}};var set={value:(ctx,value,force=false)=>{if((0,import_utils.isEqual)(ctx.value,value))return;if(value==null&&!force)return;if(value==null&&force){ctx.value=[];invoke.valueChange(ctx);return}if((0,import_utils.isArray)(value)){ctx.value=value}else if(value!=null){ctx.value=ctx.multiple?(0,import_utils.addOrRemove)(ctx.value,value):[value]}invoke.valueChange(ctx)},highlightedValue:(ctx,value,force=false)=>{if((0,import_utils.isEqual)(ctx.highlightedValue,value))return;if(!value&&!force)return;ctx.highlightedValue=value||null;invoke.highlightChange(ctx)},inputValue:(ctx,value)=>{if((0,import_utils.isEqual)(ctx.inputValue,value))return;ctx.inputValue=value;invoke.inputChange(ctx)}};0&&(module.exports={anatomy,collection,connect,machine});
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ anatomy: () => anatomy,
24
+ collection: () => collection,
25
+ connect: () => connect,
26
+ machine: () => machine
27
+ });
28
+ module.exports = __toCommonJS(src_exports);
29
+
30
+ // src/combobox.anatomy.ts
31
+ var import_anatomy = require("@zag-js/anatomy");
32
+ var anatomy = (0, import_anatomy.createAnatomy)("combobox").parts(
33
+ "root",
34
+ "clearTrigger",
35
+ "content",
36
+ "control",
37
+ "input",
38
+ "item",
39
+ "itemGroup",
40
+ "itemGroupLabel",
41
+ "itemIndicator",
42
+ "itemText",
43
+ "label",
44
+ "list",
45
+ "positioner",
46
+ "trigger"
47
+ );
48
+ var parts = anatomy.build();
49
+
50
+ // src/combobox.collection.ts
51
+ var import_collection = require("@zag-js/collection");
52
+ var import_core = require("@zag-js/core");
53
+ var collection = (options) => {
54
+ return (0, import_core.ref)(new import_collection.Collection(options));
55
+ };
56
+ collection.empty = () => {
57
+ return (0, import_core.ref)(new import_collection.Collection({ items: [] }));
58
+ };
59
+
60
+ // src/combobox.connect.ts
61
+ var import_dom_event = require("@zag-js/dom-event");
62
+ var import_dom_query2 = require("@zag-js/dom-query");
63
+ var import_popper = require("@zag-js/popper");
64
+
65
+ // src/combobox.dom.ts
66
+ var import_dom_query = require("@zag-js/dom-query");
67
+ var dom = (0, import_dom_query.createScope)({
68
+ getRootId: (ctx) => ctx.ids?.root ?? `combobox:${ctx.id}`,
69
+ getLabelId: (ctx) => ctx.ids?.label ?? `combobox:${ctx.id}:label`,
70
+ getControlId: (ctx) => ctx.ids?.control ?? `combobox:${ctx.id}:control`,
71
+ getInputId: (ctx) => ctx.ids?.input ?? `combobox:${ctx.id}:input`,
72
+ getContentId: (ctx) => ctx.ids?.content ?? `combobox:${ctx.id}:content`,
73
+ getPositionerId: (ctx) => ctx.ids?.positioner ?? `combobox:${ctx.id}:popper`,
74
+ getTriggerId: (ctx) => ctx.ids?.trigger ?? `combobox:${ctx.id}:toggle-btn`,
75
+ getClearTriggerId: (ctx) => ctx.ids?.clearTrigger ?? `combobox:${ctx.id}:clear-btn`,
76
+ getItemGroupId: (ctx, id) => ctx.ids?.itemGroup?.(id) ?? `combobox:${ctx.id}:optgroup:${id}`,
77
+ getItemGroupLabelId: (ctx, id) => ctx.ids?.itemGroupLabel?.(id) ?? `combobox:${ctx.id}:optgroup-label:${id}`,
78
+ getItemId: (ctx, id) => `combobox:${ctx.id}:option:${id}`,
79
+ getContentEl: (ctx) => dom.getById(ctx, dom.getContentId(ctx)),
80
+ getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
81
+ getPositionerEl: (ctx) => dom.getById(ctx, dom.getPositionerId(ctx)),
82
+ getControlEl: (ctx) => dom.getById(ctx, dom.getControlId(ctx)),
83
+ getTriggerEl: (ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
84
+ getClearTriggerEl: (ctx) => dom.getById(ctx, dom.getClearTriggerId(ctx)),
85
+ getHighlightedItemEl: (ctx) => {
86
+ const value = ctx.highlightedValue;
87
+ if (value == null)
88
+ return;
89
+ return (0, import_dom_query.query)(dom.getContentEl(ctx), `[role=option][data-value="${CSS.escape(value)}"`);
90
+ },
91
+ focusInputEl: (ctx) => {
92
+ const inputEl = dom.getInputEl(ctx);
93
+ if (dom.getActiveElement(ctx) === inputEl)
94
+ return;
95
+ inputEl?.focus({ preventScroll: true });
96
+ },
97
+ focusTriggerEl: (ctx) => {
98
+ const triggerEl = dom.getTriggerEl(ctx);
99
+ if (dom.getActiveElement(ctx) === triggerEl)
100
+ return;
101
+ triggerEl?.focus({ preventScroll: true });
102
+ }
103
+ });
104
+
105
+ // src/combobox.connect.ts
106
+ function connect(state, send, normalize) {
107
+ const translations = state.context.translations;
108
+ const collection2 = state.context.collection;
109
+ const disabled = state.context.disabled;
110
+ const interactive = state.context.isInteractive;
111
+ const invalid = state.context.invalid;
112
+ const readOnly = state.context.readOnly;
113
+ const open = state.hasTag("open");
114
+ const focused = state.hasTag("focused");
115
+ const composite = state.context.composite;
116
+ const highlightedValue = state.context.highlightedValue;
117
+ const popperStyles = (0, import_popper.getPlacementStyles)({
118
+ ...state.context.positioning,
119
+ placement: state.context.currentPlacement
120
+ });
121
+ function getItemState(props) {
122
+ const { item } = props;
123
+ const disabled2 = collection2.isItemDisabled(item);
124
+ const value = collection2.itemToValue(item);
125
+ return {
126
+ value,
127
+ disabled: Boolean(disabled2 || disabled2),
128
+ highlighted: highlightedValue === value,
129
+ selected: state.context.value.includes(value)
130
+ };
131
+ }
132
+ return {
133
+ focused,
134
+ open,
135
+ inputValue: state.context.inputValue,
136
+ highlightedValue,
137
+ highlightedItem: state.context.highlightedItem,
138
+ value: state.context.value,
139
+ valueAsString: state.context.valueAsString,
140
+ hasSelectedItems: state.context.hasSelectedItems,
141
+ selectedItems: state.context.selectedItems,
142
+ collection: state.context.collection,
143
+ reposition(options = {}) {
144
+ send({ type: "POSITIONING.SET", options });
145
+ },
146
+ setCollection(collection3) {
147
+ send({ type: "COLLECTION.SET", value: collection3 });
148
+ },
149
+ setHighlightValue(value) {
150
+ send({ type: "HIGHLIGHTED_VALUE.SET", value });
151
+ },
152
+ selectValue(value) {
153
+ send({ type: "ITEM.SELECT", value });
154
+ },
155
+ setValue(value) {
156
+ send({ type: "VALUE.SET", value });
157
+ },
158
+ setInputValue(value) {
159
+ send({ type: "INPUT_VALUE.SET", value });
160
+ },
161
+ clearValue(value) {
162
+ if (value != null) {
163
+ send({ type: "ITEM.CLEAR", value });
164
+ } else {
165
+ send("VALUE.CLEAR");
166
+ }
167
+ },
168
+ focus() {
169
+ dom.getInputEl(state.context)?.focus();
170
+ },
171
+ setOpen(nextOpen) {
172
+ if (nextOpen === open)
173
+ return;
174
+ send(nextOpen ? "OPEN" : "CLOSE");
175
+ },
176
+ getRootProps() {
177
+ return normalize.element({
178
+ ...parts.root.attrs,
179
+ dir: state.context.dir,
180
+ id: dom.getRootId(state.context),
181
+ "data-invalid": (0, import_dom_query2.dataAttr)(invalid),
182
+ "data-readonly": (0, import_dom_query2.dataAttr)(readOnly)
183
+ });
184
+ },
185
+ getLabelProps() {
186
+ return normalize.label({
187
+ ...parts.label.attrs,
188
+ dir: state.context.dir,
189
+ htmlFor: dom.getInputId(state.context),
190
+ id: dom.getLabelId(state.context),
191
+ "data-readonly": (0, import_dom_query2.dataAttr)(readOnly),
192
+ "data-disabled": (0, import_dom_query2.dataAttr)(disabled),
193
+ "data-invalid": (0, import_dom_query2.dataAttr)(invalid),
194
+ "data-focus": (0, import_dom_query2.dataAttr)(focused),
195
+ onClick(event) {
196
+ if (composite)
197
+ return;
198
+ event.preventDefault();
199
+ dom.getTriggerEl(state.context)?.focus({ preventScroll: true });
200
+ }
201
+ });
202
+ },
203
+ getControlProps() {
204
+ return normalize.element({
205
+ ...parts.control.attrs,
206
+ dir: state.context.dir,
207
+ id: dom.getControlId(state.context),
208
+ "data-state": open ? "open" : "closed",
209
+ "data-focus": (0, import_dom_query2.dataAttr)(focused),
210
+ "data-disabled": (0, import_dom_query2.dataAttr)(disabled),
211
+ "data-invalid": (0, import_dom_query2.dataAttr)(invalid)
212
+ });
213
+ },
214
+ getPositionerProps() {
215
+ return normalize.element({
216
+ ...parts.positioner.attrs,
217
+ dir: state.context.dir,
218
+ id: dom.getPositionerId(state.context),
219
+ style: popperStyles.floating
220
+ });
221
+ },
222
+ getInputProps() {
223
+ return normalize.input({
224
+ ...parts.input.attrs,
225
+ dir: state.context.dir,
226
+ "aria-invalid": (0, import_dom_query2.ariaAttr)(invalid),
227
+ "data-invalid": (0, import_dom_query2.dataAttr)(invalid),
228
+ name: state.context.name,
229
+ form: state.context.form,
230
+ disabled,
231
+ autoFocus: state.context.autoFocus,
232
+ autoComplete: "off",
233
+ autoCorrect: "off",
234
+ autoCapitalize: "none",
235
+ spellCheck: "false",
236
+ readOnly,
237
+ placeholder: state.context.placeholder,
238
+ id: dom.getInputId(state.context),
239
+ type: "text",
240
+ role: "combobox",
241
+ defaultValue: state.context.inputValue,
242
+ "aria-autocomplete": state.context.autoComplete ? "both" : "list",
243
+ "aria-controls": dom.getContentId(state.context),
244
+ "aria-expanded": open,
245
+ "data-state": open ? "open" : "closed",
246
+ "aria-activedescendant": highlightedValue ? dom.getItemId(state.context, highlightedValue) : void 0,
247
+ onClick(event) {
248
+ if (event.defaultPrevented)
249
+ return;
250
+ if (!state.context.openOnClick)
251
+ return;
252
+ if (!interactive)
253
+ return;
254
+ send("INPUT.CLICK");
255
+ },
256
+ onFocus() {
257
+ if (disabled)
258
+ return;
259
+ send("INPUT.FOCUS");
260
+ },
261
+ onBlur() {
262
+ if (disabled)
263
+ return;
264
+ send("INPUT.BLUR");
265
+ },
266
+ onChange(event) {
267
+ send({ type: "INPUT.CHANGE", value: event.currentTarget.value });
268
+ },
269
+ onKeyDown(event) {
270
+ if (event.defaultPrevented)
271
+ return;
272
+ if (!interactive)
273
+ return;
274
+ if (event.ctrlKey || event.shiftKey || (0, import_dom_query2.isComposingEvent)(event))
275
+ return;
276
+ const openOnKeyPress = state.context.openOnKeyPress;
277
+ const isModifierKey = event.ctrlKey || event.metaKey || event.shiftKey;
278
+ const keypress = true;
279
+ const keymap = {
280
+ ArrowDown(event2) {
281
+ if (!openOnKeyPress && !open)
282
+ return;
283
+ send({ type: event2.altKey ? "OPEN" : "INPUT.ARROW_DOWN", keypress });
284
+ event2.preventDefault();
285
+ },
286
+ ArrowUp() {
287
+ if (!openOnKeyPress && !open)
288
+ return;
289
+ send({ type: event.altKey ? "CLOSE" : "INPUT.ARROW_UP", keypress });
290
+ event.preventDefault();
291
+ },
292
+ Home(event2) {
293
+ if (isModifierKey)
294
+ return;
295
+ send({ type: "INPUT.HOME", keypress });
296
+ if (open) {
297
+ event2.preventDefault();
298
+ }
299
+ },
300
+ End(event2) {
301
+ if (isModifierKey)
302
+ return;
303
+ send({ type: "INPUT.END", keypress });
304
+ if (open) {
305
+ event2.preventDefault();
306
+ }
307
+ },
308
+ Enter(event2) {
309
+ send({ type: "INPUT.ENTER", keypress });
310
+ if (open) {
311
+ event2.preventDefault();
312
+ }
313
+ const itemEl = dom.getHighlightedItemEl(state.context);
314
+ (0, import_dom_event.clickIfLink)(itemEl);
315
+ },
316
+ Escape() {
317
+ send({ type: "INPUT.ESCAPE", keypress });
318
+ event.preventDefault();
319
+ }
320
+ };
321
+ const key = (0, import_dom_event.getEventKey)(event, state.context);
322
+ const exec = keymap[key];
323
+ exec?.(event);
324
+ }
325
+ });
326
+ },
327
+ getTriggerProps(props = {}) {
328
+ return normalize.button({
329
+ ...parts.trigger.attrs,
330
+ dir: state.context.dir,
331
+ id: dom.getTriggerId(state.context),
332
+ "aria-haspopup": composite ? "listbox" : "dialog",
333
+ type: "button",
334
+ tabIndex: props.focusable ? void 0 : -1,
335
+ "aria-label": translations.triggerLabel,
336
+ "aria-expanded": open,
337
+ "data-state": open ? "open" : "closed",
338
+ "aria-controls": open ? dom.getContentId(state.context) : void 0,
339
+ disabled,
340
+ "data-focusable": (0, import_dom_query2.dataAttr)(props.focusable),
341
+ "data-readonly": (0, import_dom_query2.dataAttr)(readOnly),
342
+ "data-disabled": (0, import_dom_query2.dataAttr)(disabled),
343
+ onFocus() {
344
+ if (!props.focusable)
345
+ return;
346
+ send({ type: "INPUT.FOCUS", src: "trigger" });
347
+ },
348
+ onClick(event) {
349
+ if (event.defaultPrevented)
350
+ return;
351
+ if (!interactive)
352
+ return;
353
+ if (!(0, import_dom_event.isLeftClick)(event))
354
+ return;
355
+ send("TRIGGER.CLICK");
356
+ },
357
+ onPointerDown(event) {
358
+ if (!interactive)
359
+ return;
360
+ if (event.pointerType === "touch")
361
+ return;
362
+ event.preventDefault();
363
+ queueMicrotask(() => {
364
+ dom.getInputEl(state.context)?.focus({ preventScroll: true });
365
+ });
366
+ },
367
+ onKeyDown(event) {
368
+ if (event.defaultPrevented)
369
+ return;
370
+ if (composite)
371
+ return;
372
+ const keyMap = {
373
+ ArrowDown() {
374
+ send({ type: "INPUT.ARROW_DOWN", src: "trigger" });
375
+ },
376
+ ArrowUp() {
377
+ send({ type: "INPUT.ARROW_UP", src: "trigger" });
378
+ }
379
+ };
380
+ const key = (0, import_dom_event.getEventKey)(event, state.context);
381
+ const exec = keyMap[key];
382
+ if (exec) {
383
+ exec(event);
384
+ event.preventDefault();
385
+ }
386
+ }
387
+ });
388
+ },
389
+ getContentProps() {
390
+ return normalize.element({
391
+ ...parts.content.attrs,
392
+ dir: state.context.dir,
393
+ id: dom.getContentId(state.context),
394
+ role: !composite ? "dialog" : "listbox",
395
+ tabIndex: -1,
396
+ hidden: !open,
397
+ "data-state": open ? "open" : "closed",
398
+ "aria-labelledby": dom.getLabelId(state.context),
399
+ "aria-multiselectable": state.context.multiple && composite ? true : void 0,
400
+ onPointerDown(event) {
401
+ event.preventDefault();
402
+ }
403
+ });
404
+ },
405
+ getListProps() {
406
+ return normalize.element({
407
+ ...parts.list.attrs,
408
+ role: !composite ? "listbox" : void 0,
409
+ "aria-labelledby": dom.getLabelId(state.context),
410
+ "aria-multiselectable": state.context.multiple && !composite ? true : void 0
411
+ });
412
+ },
413
+ getClearTriggerProps() {
414
+ return normalize.button({
415
+ ...parts.clearTrigger.attrs,
416
+ dir: state.context.dir,
417
+ id: dom.getClearTriggerId(state.context),
418
+ type: "button",
419
+ tabIndex: -1,
420
+ disabled,
421
+ "aria-label": translations.clearTriggerLabel,
422
+ "aria-controls": dom.getInputId(state.context),
423
+ hidden: !state.context.value.length,
424
+ onPointerDown(event) {
425
+ event.preventDefault();
426
+ },
427
+ onClick(event) {
428
+ if (event.defaultPrevented)
429
+ return;
430
+ if (!interactive)
431
+ return;
432
+ send({ type: "VALUE.CLEAR", src: "clear-trigger" });
433
+ }
434
+ });
435
+ },
436
+ getItemState,
437
+ getItemProps(props) {
438
+ const itemState = getItemState(props);
439
+ const value = itemState.value;
440
+ return normalize.element({
441
+ ...parts.item.attrs,
442
+ dir: state.context.dir,
443
+ id: dom.getItemId(state.context, value),
444
+ role: "option",
445
+ tabIndex: -1,
446
+ "data-highlighted": (0, import_dom_query2.dataAttr)(itemState.highlighted),
447
+ "data-state": itemState.selected ? "checked" : "unchecked",
448
+ "aria-selected": itemState.highlighted,
449
+ "aria-disabled": itemState.disabled,
450
+ "data-disabled": (0, import_dom_query2.dataAttr)(itemState.disabled),
451
+ "data-value": itemState.value,
452
+ onPointerMove() {
453
+ if (itemState.disabled)
454
+ return;
455
+ if (itemState.highlighted)
456
+ return;
457
+ send({ type: "ITEM.POINTER_MOVE", value });
458
+ },
459
+ onPointerLeave() {
460
+ if (props.persistFocus)
461
+ return;
462
+ if (itemState.disabled)
463
+ return;
464
+ const mouseMoved = state.previousEvent.type.includes("POINTER");
465
+ if (!mouseMoved)
466
+ return;
467
+ send({ type: "ITEM.POINTER_LEAVE", value });
468
+ },
469
+ onPointerUp(event) {
470
+ if ((0, import_dom_query2.isDownloadingEvent)(event))
471
+ return;
472
+ if ((0, import_dom_query2.isOpeningInNewTab)(event))
473
+ return;
474
+ if ((0, import_dom_event.isContextMenuEvent)(event))
475
+ return;
476
+ if (itemState.disabled)
477
+ return;
478
+ send({ type: "ITEM.CLICK", src: "pointerup", value });
479
+ },
480
+ onTouchEnd(event) {
481
+ event.preventDefault();
482
+ event.stopPropagation();
483
+ }
484
+ });
485
+ },
486
+ getItemTextProps(props) {
487
+ const itemState = getItemState(props);
488
+ return normalize.element({
489
+ ...parts.itemText.attrs,
490
+ dir: state.context.dir,
491
+ "data-disabled": (0, import_dom_query2.dataAttr)(itemState.disabled),
492
+ "data-highlighted": (0, import_dom_query2.dataAttr)(itemState.highlighted)
493
+ });
494
+ },
495
+ getItemIndicatorProps(props) {
496
+ const itemState = getItemState(props);
497
+ return normalize.element({
498
+ "aria-hidden": true,
499
+ ...parts.itemIndicator.attrs,
500
+ dir: state.context.dir,
501
+ "data-state": itemState.selected ? "checked" : "unchecked",
502
+ hidden: !itemState.selected
503
+ });
504
+ },
505
+ getItemGroupProps(props) {
506
+ const { id } = props;
507
+ return normalize.element({
508
+ ...parts.itemGroup.attrs,
509
+ dir: state.context.dir,
510
+ id: dom.getItemGroupId(state.context, id),
511
+ "aria-labelledby": dom.getItemGroupLabelId(state.context, id)
512
+ });
513
+ },
514
+ getItemGroupLabelProps(props) {
515
+ const { htmlFor } = props;
516
+ return normalize.element({
517
+ ...parts.itemGroupLabel.attrs,
518
+ dir: state.context.dir,
519
+ id: dom.getItemGroupLabelId(state.context, htmlFor),
520
+ role: "group"
521
+ });
522
+ }
523
+ };
524
+ }
525
+
526
+ // src/combobox.machine.ts
527
+ var import_aria_hidden = require("@zag-js/aria-hidden");
528
+ var import_core2 = require("@zag-js/core");
529
+ var import_dismissable = require("@zag-js/dismissable");
530
+ var import_dom_query3 = require("@zag-js/dom-query");
531
+ var import_popper2 = require("@zag-js/popper");
532
+ var import_utils = require("@zag-js/utils");
533
+ var { and, not } = import_core2.guards;
534
+ function machine(userContext) {
535
+ const ctx = (0, import_utils.compact)(userContext);
536
+ return (0, import_core2.createMachine)(
537
+ {
538
+ id: "combobox",
539
+ initial: ctx.open ? "suggesting" : "idle",
540
+ context: {
541
+ loopFocus: true,
542
+ openOnClick: false,
543
+ value: [],
544
+ highlightedValue: null,
545
+ inputValue: "",
546
+ allowCustomValue: false,
547
+ closeOnSelect: !ctx.multiple,
548
+ inputBehavior: "none",
549
+ selectionBehavior: "replace",
550
+ openOnKeyPress: true,
551
+ openOnChange: true,
552
+ composite: true,
553
+ ...ctx,
554
+ highlightedItem: null,
555
+ selectedItems: [],
556
+ valueAsString: "",
557
+ collection: ctx.collection ?? collection.empty(),
558
+ positioning: {
559
+ placement: "bottom",
560
+ flip: false,
561
+ sameWidth: true,
562
+ ...ctx.positioning
563
+ },
564
+ translations: {
565
+ triggerLabel: "Toggle suggestions",
566
+ clearTriggerLabel: "Clear value",
567
+ ...ctx.translations
568
+ }
569
+ },
570
+ created: ["syncInitialValues", "syncSelectionBehavior"],
571
+ computed: {
572
+ isInputValueEmpty: (ctx2) => ctx2.inputValue.length === 0,
573
+ isInteractive: (ctx2) => !(ctx2.readOnly || ctx2.disabled),
574
+ autoComplete: (ctx2) => ctx2.inputBehavior === "autocomplete",
575
+ autoHighlight: (ctx2) => ctx2.inputBehavior === "autohighlight",
576
+ hasSelectedItems: (ctx2) => ctx2.value.length > 0
577
+ },
578
+ watch: {
579
+ value: ["syncSelectedItems"],
580
+ inputValue: ["syncInputValue"],
581
+ highlightedValue: ["syncHighlightedItem", "autofillInputValue"],
582
+ multiple: ["syncSelectionBehavior"],
583
+ open: ["toggleVisibility"]
584
+ },
585
+ on: {
586
+ "HIGHLIGHTED_VALUE.SET": {
587
+ actions: ["setHighlightedItem"]
588
+ },
589
+ "ITEM.SELECT": {
590
+ actions: ["selectItem"]
591
+ },
592
+ "ITEM.CLEAR": {
593
+ actions: ["clearItem"]
594
+ },
595
+ "VALUE.SET": {
596
+ actions: ["setSelectedItems"]
597
+ },
598
+ "INPUT_VALUE.SET": {
599
+ actions: "setInputValue"
600
+ },
601
+ "COLLECTION.SET": {
602
+ actions: ["setCollection"]
603
+ },
604
+ "POSITIONING.SET": {
605
+ actions: ["reposition"]
606
+ }
607
+ },
608
+ states: {
609
+ idle: {
610
+ tags: ["idle", "closed"],
611
+ entry: ["scrollContentToTop", "clearHighlightedItem"],
612
+ on: {
613
+ "CONTROLLED.OPEN": {
614
+ target: "interacting"
615
+ },
616
+ "TRIGGER.CLICK": [
617
+ {
618
+ guard: "isOpenControlled",
619
+ actions: ["setInitialFocus", "highlightFirstSelectedItem", "invokeOnOpen"]
620
+ },
621
+ {
622
+ target: "interacting",
623
+ actions: ["setInitialFocus", "highlightFirstSelectedItem", "invokeOnOpen"]
624
+ }
625
+ ],
626
+ "INPUT.CLICK": [
627
+ {
628
+ guard: "isOpenControlled",
629
+ actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
630
+ },
631
+ {
632
+ target: "interacting",
633
+ actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
634
+ }
635
+ ],
636
+ "INPUT.FOCUS": {
637
+ target: "focused"
638
+ },
639
+ OPEN: [
640
+ {
641
+ guard: "isOpenControlled",
642
+ actions: ["invokeOnOpen"]
643
+ },
644
+ {
645
+ target: "interacting",
646
+ actions: ["invokeOnOpen"]
647
+ }
648
+ ],
649
+ "VALUE.CLEAR": {
650
+ target: "focused",
651
+ actions: ["clearInputValue", "clearSelectedItems", "setInitialFocus"]
652
+ }
653
+ }
654
+ },
655
+ focused: {
656
+ tags: ["focused", "closed"],
657
+ entry: ["scrollContentToTop", "clearHighlightedItem"],
658
+ on: {
659
+ "CONTROLLED.OPEN": [
660
+ {
661
+ guard: "isChangeEvent",
662
+ target: "suggesting"
663
+ },
664
+ {
665
+ target: "interacting"
666
+ }
667
+ ],
668
+ "INPUT.CHANGE": [
669
+ {
670
+ guard: and("isOpenControlled", "openOnChange"),
671
+ actions: ["setInputValue", "invokeOnOpen", "highlightFirstItemIfNeeded"]
672
+ },
673
+ {
674
+ guard: "openOnChange",
675
+ target: "suggesting",
676
+ actions: ["setInputValue", "invokeOnOpen", "highlightFirstItemIfNeeded"]
677
+ },
678
+ {
679
+ actions: "setInputValue"
680
+ }
681
+ ],
682
+ "LAYER.INTERACT_OUTSIDE": {
683
+ target: "idle"
684
+ },
685
+ "INPUT.ESCAPE": {
686
+ guard: and("isCustomValue", not("allowCustomValue")),
687
+ actions: "revertInputValue"
688
+ },
689
+ "INPUT.BLUR": {
690
+ target: "idle"
691
+ },
692
+ "INPUT.CLICK": [
693
+ {
694
+ guard: "isOpenControlled",
695
+ actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
696
+ },
697
+ {
698
+ target: "interacting",
699
+ actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
700
+ }
701
+ ],
702
+ "TRIGGER.CLICK": [
703
+ {
704
+ guard: "isOpenControlled",
705
+ actions: ["setInitialFocus", "highlightFirstSelectedItem", "invokeOnOpen"]
706
+ },
707
+ {
708
+ target: "interacting",
709
+ actions: ["setInitialFocus", "highlightFirstSelectedItem", "invokeOnOpen"]
710
+ }
711
+ ],
712
+ "INPUT.ARROW_DOWN": [
713
+ // == group 1 ==
714
+ {
715
+ guard: and("isOpenControlled", "autoComplete"),
716
+ actions: ["invokeOnOpen"]
717
+ },
718
+ {
719
+ guard: "autoComplete",
720
+ target: "interacting",
721
+ actions: ["invokeOnOpen"]
722
+ },
723
+ // == group 2 ==
724
+ {
725
+ guard: "isOpenControlled",
726
+ actions: ["highlightFirstOrSelectedItem", "invokeOnOpen"]
727
+ },
728
+ {
729
+ target: "interacting",
730
+ actions: ["highlightFirstOrSelectedItem", "invokeOnOpen"]
731
+ }
732
+ ],
733
+ "INPUT.ARROW_UP": [
734
+ // == group 1 ==
735
+ {
736
+ guard: "autoComplete",
737
+ target: "interacting",
738
+ actions: "invokeOnOpen"
739
+ },
740
+ {
741
+ guard: "autoComplete",
742
+ target: "interacting",
743
+ actions: "invokeOnOpen"
744
+ },
745
+ // == group 2 ==
746
+ {
747
+ target: "interacting",
748
+ actions: ["highlightLastOrSelectedItem", "invokeOnOpen"]
749
+ },
750
+ {
751
+ target: "interacting",
752
+ actions: ["highlightLastOrSelectedItem", "invokeOnOpen"]
753
+ }
754
+ ],
755
+ OPEN: [
756
+ {
757
+ guard: "isOpenControlled",
758
+ actions: ["invokeOnOpen"]
759
+ },
760
+ {
761
+ target: "interacting",
762
+ actions: ["invokeOnOpen"]
763
+ }
764
+ ],
765
+ "VALUE.CLEAR": {
766
+ actions: ["clearInputValue", "clearSelectedItems"]
767
+ }
768
+ }
769
+ },
770
+ interacting: {
771
+ tags: ["open", "focused"],
772
+ entry: ["setInitialFocus"],
773
+ activities: ["scrollToHighlightedItem", "trackDismissableLayer", "computePlacement", "hideOtherElements"],
774
+ on: {
775
+ "CONTROLLED.CLOSE": [
776
+ {
777
+ guard: "restoreFocus",
778
+ target: "focused",
779
+ actions: ["setFinalFocus"]
780
+ },
781
+ {
782
+ target: "idle"
783
+ }
784
+ ],
785
+ "INPUT.HOME": {
786
+ actions: ["highlightFirstItem"]
787
+ },
788
+ "INPUT.END": {
789
+ actions: ["highlightLastItem"]
790
+ },
791
+ "INPUT.ARROW_DOWN": [
792
+ {
793
+ guard: and("autoComplete", "isLastItemHighlighted"),
794
+ actions: ["clearHighlightedItem", "scrollContentToTop"]
795
+ },
796
+ {
797
+ actions: ["highlightNextItem"]
798
+ }
799
+ ],
800
+ "INPUT.ARROW_UP": [
801
+ {
802
+ guard: and("autoComplete", "isFirstItemHighlighted"),
803
+ actions: "clearHighlightedItem"
804
+ },
805
+ {
806
+ actions: "highlightPrevItem"
807
+ }
808
+ ],
809
+ "INPUT.ENTER": [
810
+ {
811
+ guard: and("isOpenControlled", "closeOnSelect"),
812
+ actions: ["selectHighlightedItem", "invokeOnClose"]
813
+ },
814
+ {
815
+ guard: "closeOnSelect",
816
+ target: "focused",
817
+ actions: ["selectHighlightedItem", "invokeOnClose", "setFinalFocus"]
818
+ },
819
+ {
820
+ actions: ["selectHighlightedItem"]
821
+ }
822
+ ],
823
+ "INPUT.CHANGE": [
824
+ {
825
+ guard: "autoComplete",
826
+ target: "suggesting",
827
+ actions: ["setInputValue", "invokeOnOpen"]
828
+ },
829
+ {
830
+ target: "suggesting",
831
+ actions: ["clearHighlightedItem", "setInputValue", "invokeOnOpen"]
832
+ }
833
+ ],
834
+ "ITEM.POINTER_MOVE": {
835
+ actions: ["setHighlightedItem"]
836
+ },
837
+ "ITEM.POINTER_LEAVE": {
838
+ actions: ["clearHighlightedItem"]
839
+ },
840
+ "ITEM.CLICK": [
841
+ {
842
+ guard: and("isOpenControlled", "closeOnSelect"),
843
+ actions: ["selectItem", "invokeOnClose"]
844
+ },
845
+ {
846
+ guard: "closeOnSelect",
847
+ target: "focused",
848
+ actions: ["selectItem", "invokeOnClose", "setFinalFocus"]
849
+ },
850
+ {
851
+ actions: ["selectItem"]
852
+ }
853
+ ],
854
+ "LAYER.ESCAPE": [
855
+ {
856
+ guard: and("isOpenControlled", "autoComplete"),
857
+ actions: ["syncInputValue", "invokeOnClose"]
858
+ },
859
+ {
860
+ guard: "autoComplete",
861
+ target: "focused",
862
+ actions: ["syncInputValue", "invokeOnClose"]
863
+ },
864
+ {
865
+ guard: "isOpenControlled",
866
+ actions: "invokeOnClose"
867
+ },
868
+ {
869
+ target: "focused",
870
+ actions: ["invokeOnClose", "setFinalFocus"]
871
+ }
872
+ ],
873
+ "TRIGGER.CLICK": [
874
+ {
875
+ guard: "isOpenControlled",
876
+ actions: "invokeOnClose"
877
+ },
878
+ {
879
+ target: "focused",
880
+ actions: "invokeOnClose"
881
+ }
882
+ ],
883
+ "LAYER.INTERACT_OUTSIDE": [
884
+ // == group 1 ==
885
+ {
886
+ guard: and("isOpenControlled", "isCustomValue", not("allowCustomValue")),
887
+ actions: ["revertInputValue", "invokeOnClose"]
888
+ },
889
+ {
890
+ guard: and("isCustomValue", not("allowCustomValue")),
891
+ target: "idle",
892
+ actions: ["revertInputValue", "invokeOnClose"]
893
+ },
894
+ // == group 2 ==
895
+ {
896
+ guard: "isOpenControlled",
897
+ actions: "invokeOnClose"
898
+ },
899
+ {
900
+ target: "idle",
901
+ actions: "invokeOnClose"
902
+ }
903
+ ],
904
+ CLOSE: [
905
+ {
906
+ guard: "isOpenControlled",
907
+ actions: ["invokeOnClose"]
908
+ },
909
+ {
910
+ target: "focused",
911
+ actions: ["invokeOnClose", "setFinalFocus"]
912
+ }
913
+ ],
914
+ "VALUE.CLEAR": [
915
+ {
916
+ guard: "isOpenControlled",
917
+ actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose"]
918
+ },
919
+ {
920
+ target: "focused",
921
+ actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose", "setFinalFocus"]
922
+ }
923
+ ]
924
+ }
925
+ },
926
+ suggesting: {
927
+ tags: ["open", "focused"],
928
+ activities: [
929
+ "trackDismissableLayer",
930
+ "scrollToHighlightedItem",
931
+ "computePlacement",
932
+ "trackChildNodes",
933
+ "hideOtherElements"
934
+ ],
935
+ entry: ["setInitialFocus"],
936
+ on: {
937
+ "CONTROLLED.CLOSE": [
938
+ {
939
+ guard: "restoreFocus",
940
+ target: "focused",
941
+ actions: ["setFinalFocus"]
942
+ },
943
+ {
944
+ target: "idle"
945
+ }
946
+ ],
947
+ CHILDREN_CHANGE: {
948
+ actions: ["highlightFirstItem"]
949
+ },
950
+ "INPUT.ARROW_DOWN": {
951
+ target: "interacting",
952
+ actions: ["highlightNextItem"]
953
+ },
954
+ "INPUT.ARROW_UP": {
955
+ target: "interacting",
956
+ actions: ["highlightPrevItem"]
957
+ },
958
+ "INPUT.HOME": {
959
+ target: "interacting",
960
+ actions: ["highlightFirstItem"]
961
+ },
962
+ "INPUT.END": {
963
+ target: "interacting",
964
+ actions: ["highlightLastItem"]
965
+ },
966
+ "INPUT.ENTER": [
967
+ {
968
+ guard: and("isOpenControlled", "closeOnSelect"),
969
+ actions: ["selectHighlightedItem", "invokeOnClose"]
970
+ },
971
+ {
972
+ guard: "closeOnSelect",
973
+ target: "focused",
974
+ actions: ["selectHighlightedItem", "invokeOnClose", "setFinalFocus"]
975
+ },
976
+ {
977
+ actions: ["selectHighlightedItem"]
978
+ }
979
+ ],
980
+ "INPUT.CHANGE": [
981
+ {
982
+ guard: "autoHighlight",
983
+ actions: ["setInputValue"]
984
+ },
985
+ {
986
+ actions: ["setInputValue"]
987
+ }
988
+ ],
989
+ "LAYER.ESCAPE": [
990
+ {
991
+ guard: "isOpenControlled",
992
+ actions: ["invokeOnClose"]
993
+ },
994
+ {
995
+ target: "focused",
996
+ actions: ["invokeOnClose"]
997
+ }
998
+ ],
999
+ "ITEM.POINTER_MOVE": {
1000
+ target: "interacting",
1001
+ actions: ["setHighlightedItem"]
1002
+ },
1003
+ "ITEM.POINTER_LEAVE": {
1004
+ actions: ["clearHighlightedItem"]
1005
+ },
1006
+ "LAYER.INTERACT_OUTSIDE": [
1007
+ // == group 1 ==
1008
+ {
1009
+ guard: and("isOpenControlled", "isCustomValue", not("allowCustomValue")),
1010
+ actions: ["revertInputValue", "invokeOnClose"]
1011
+ },
1012
+ {
1013
+ guard: and("isCustomValue", not("allowCustomValue")),
1014
+ target: "idle",
1015
+ actions: ["revertInputValue", "invokeOnClose"]
1016
+ },
1017
+ // == group 2 ==
1018
+ {
1019
+ guard: "isOpenControlled",
1020
+ actions: ["invokeOnClose"]
1021
+ },
1022
+ {
1023
+ target: "idle",
1024
+ actions: ["invokeOnClose"]
1025
+ }
1026
+ ],
1027
+ "TRIGGER.CLICK": [
1028
+ {
1029
+ guard: "isOpenControlled",
1030
+ actions: ["invokeOnClose"]
1031
+ },
1032
+ {
1033
+ target: "focused",
1034
+ actions: ["invokeOnClose"]
1035
+ }
1036
+ ],
1037
+ "ITEM.CLICK": [
1038
+ {
1039
+ guard: and("isOpenControlled", "closeOnSelect"),
1040
+ actions: ["selectItem", "invokeOnClose"]
1041
+ },
1042
+ {
1043
+ guard: "closeOnSelect",
1044
+ target: "focused",
1045
+ actions: ["selectItem", "invokeOnClose", "setFinalFocus"]
1046
+ },
1047
+ {
1048
+ actions: ["selectItem"]
1049
+ }
1050
+ ],
1051
+ CLOSE: [
1052
+ {
1053
+ guard: "isOpenControlled",
1054
+ actions: ["invokeOnClose"]
1055
+ },
1056
+ {
1057
+ target: "focused",
1058
+ actions: ["invokeOnClose", "setFinalFocus"]
1059
+ }
1060
+ ],
1061
+ "VALUE.CLEAR": [
1062
+ {
1063
+ guard: "isOpenControlled",
1064
+ actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose"]
1065
+ },
1066
+ {
1067
+ target: "focused",
1068
+ actions: ["clearInputValue", "clearSelectedItems", "invokeOnClose", "setFinalFocus"]
1069
+ }
1070
+ ]
1071
+ }
1072
+ }
1073
+ }
1074
+ },
1075
+ {
1076
+ guards: {
1077
+ isInputValueEmpty: (ctx2) => ctx2.isInputValueEmpty,
1078
+ autoComplete: (ctx2) => ctx2.autoComplete && !ctx2.multiple,
1079
+ autoHighlight: (ctx2) => ctx2.autoHighlight,
1080
+ isFirstItemHighlighted: (ctx2) => ctx2.collection.first() === ctx2.highlightedValue,
1081
+ isLastItemHighlighted: (ctx2) => ctx2.collection.last() === ctx2.highlightedValue,
1082
+ isCustomValue: (ctx2) => ctx2.inputValue !== ctx2.valueAsString,
1083
+ allowCustomValue: (ctx2) => !!ctx2.allowCustomValue,
1084
+ hasHighlightedItem: (ctx2) => ctx2.highlightedValue != null,
1085
+ closeOnSelect: (ctx2) => !!ctx2.closeOnSelect,
1086
+ isOpenControlled: (ctx2) => !!ctx2["open.controlled"],
1087
+ openOnChange: (ctx2, evt) => {
1088
+ if ((0, import_utils.isBoolean)(ctx2.openOnChange))
1089
+ return ctx2.openOnChange;
1090
+ return !!ctx2.openOnChange?.({ inputValue: evt.value });
1091
+ },
1092
+ restoreFocus: (_ctx, evt) => evt.restoreFocus == null ? true : !!evt.restoreFocus,
1093
+ isChangeEvent: (_ctx, evt) => evt.previousEvent?.type === "INPUT.CHANGE"
1094
+ },
1095
+ activities: {
1096
+ trackDismissableLayer(ctx2, _evt, { send }) {
1097
+ if (ctx2.disableLayer)
1098
+ return;
1099
+ const contentEl = () => dom.getContentEl(ctx2);
1100
+ return (0, import_dismissable.trackDismissableElement)(contentEl, {
1101
+ defer: true,
1102
+ exclude: () => [dom.getInputEl(ctx2), dom.getTriggerEl(ctx2), dom.getClearTriggerEl(ctx2)],
1103
+ onFocusOutside: ctx2.onFocusOutside,
1104
+ onPointerDownOutside: ctx2.onPointerDownOutside,
1105
+ onInteractOutside: ctx2.onInteractOutside,
1106
+ onEscapeKeyDown(event) {
1107
+ event.preventDefault();
1108
+ event.stopPropagation();
1109
+ send("LAYER.ESCAPE");
1110
+ },
1111
+ onDismiss() {
1112
+ send({ type: "LAYER.INTERACT_OUTSIDE", restoreFocus: false });
1113
+ }
1114
+ });
1115
+ },
1116
+ hideOtherElements(ctx2) {
1117
+ return (0, import_aria_hidden.ariaHidden)([dom.getInputEl(ctx2), dom.getContentEl(ctx2), dom.getTriggerEl(ctx2)]);
1118
+ },
1119
+ computePlacement(ctx2) {
1120
+ const controlEl = () => dom.getControlEl(ctx2);
1121
+ const positionerEl = () => dom.getPositionerEl(ctx2);
1122
+ ctx2.currentPlacement = ctx2.positioning.placement;
1123
+ return (0, import_popper2.getPlacement)(controlEl, positionerEl, {
1124
+ ...ctx2.positioning,
1125
+ defer: true,
1126
+ onComplete(data) {
1127
+ ctx2.currentPlacement = data.placement;
1128
+ }
1129
+ });
1130
+ },
1131
+ // in event the options are fetched (async), we still want to auto-highlight the first option
1132
+ trackChildNodes(ctx2, _evt, { send }) {
1133
+ if (!ctx2.autoHighlight)
1134
+ return;
1135
+ const exec = () => send("CHILDREN_CHANGE");
1136
+ const contentEl = () => dom.getContentEl(ctx2);
1137
+ return (0, import_dom_query3.observeChildren)(contentEl, {
1138
+ callback: exec,
1139
+ defer: true
1140
+ });
1141
+ },
1142
+ scrollToHighlightedItem(ctx2, _evt, { getState }) {
1143
+ const inputEl = dom.getInputEl(ctx2);
1144
+ let cleanups = [];
1145
+ const exec = (immediate) => {
1146
+ const state = getState();
1147
+ const pointer = state.event.type.includes("POINTER");
1148
+ if (pointer || !ctx2.highlightedValue)
1149
+ return;
1150
+ const itemEl = dom.getHighlightedItemEl(ctx2);
1151
+ const contentEl = dom.getContentEl(ctx2);
1152
+ if (ctx2.scrollToIndexFn) {
1153
+ const highlightedIndex = ctx2.collection.indexOf(ctx2.highlightedValue);
1154
+ ctx2.scrollToIndexFn({ index: highlightedIndex, immediate });
1155
+ return;
1156
+ }
1157
+ const rafCleanup2 = (0, import_dom_query3.raf)(() => {
1158
+ (0, import_dom_query3.scrollIntoView)(itemEl, { rootEl: contentEl, block: "nearest" });
1159
+ });
1160
+ cleanups.push(rafCleanup2);
1161
+ };
1162
+ const rafCleanup = (0, import_dom_query3.raf)(() => exec(true));
1163
+ cleanups.push(rafCleanup);
1164
+ const observerCleanup = (0, import_dom_query3.observeAttributes)(inputEl, {
1165
+ attributes: ["aria-activedescendant"],
1166
+ callback: () => exec(false)
1167
+ });
1168
+ cleanups.push(observerCleanup);
1169
+ return () => {
1170
+ cleanups.forEach((cleanup) => cleanup());
1171
+ };
1172
+ }
1173
+ },
1174
+ actions: {
1175
+ reposition(ctx2, evt) {
1176
+ const controlEl = () => dom.getControlEl(ctx2);
1177
+ const positionerEl = () => dom.getPositionerEl(ctx2);
1178
+ (0, import_popper2.getPlacement)(controlEl, positionerEl, {
1179
+ ...ctx2.positioning,
1180
+ ...evt.options,
1181
+ defer: true,
1182
+ listeners: false,
1183
+ onComplete(data) {
1184
+ ctx2.currentPlacement = data.placement;
1185
+ }
1186
+ });
1187
+ },
1188
+ setHighlightedItem(ctx2, evt) {
1189
+ if (evt.value == null)
1190
+ return;
1191
+ set.highlightedValue(ctx2, evt.value);
1192
+ },
1193
+ clearHighlightedItem(ctx2) {
1194
+ set.highlightedValue(ctx2, null, true);
1195
+ },
1196
+ selectHighlightedItem(ctx2) {
1197
+ set.value(ctx2, ctx2.highlightedValue);
1198
+ },
1199
+ selectItem(ctx2, evt) {
1200
+ if (evt.value == null)
1201
+ return;
1202
+ set.value(ctx2, evt.value);
1203
+ },
1204
+ clearItem(ctx2, evt) {
1205
+ if (evt.value == null)
1206
+ return;
1207
+ const value = ctx2.value.filter((v) => v !== evt.value);
1208
+ set.value(ctx2, value);
1209
+ },
1210
+ setInitialFocus(ctx2) {
1211
+ (0, import_dom_query3.raf)(() => {
1212
+ dom.focusInputEl(ctx2);
1213
+ });
1214
+ },
1215
+ setFinalFocus(ctx2) {
1216
+ (0, import_dom_query3.raf)(() => {
1217
+ const triggerEl = dom.getTriggerEl(ctx2);
1218
+ if (triggerEl?.dataset.focusable == null) {
1219
+ dom.focusInputEl(ctx2);
1220
+ } else {
1221
+ dom.focusTriggerEl(ctx2);
1222
+ }
1223
+ });
1224
+ },
1225
+ syncInputValue(ctx2) {
1226
+ const inputEl = dom.getInputEl(ctx2);
1227
+ if (!inputEl)
1228
+ return;
1229
+ inputEl.value = ctx2.inputValue;
1230
+ queueMicrotask(() => {
1231
+ const { selectionStart, selectionEnd } = inputEl;
1232
+ if (Math.abs((selectionEnd ?? 0) - (selectionStart ?? 0)) !== 0)
1233
+ return;
1234
+ if (selectionStart !== 0)
1235
+ return;
1236
+ inputEl.setSelectionRange(inputEl.value.length, inputEl.value.length);
1237
+ });
1238
+ },
1239
+ setInputValue(ctx2, evt) {
1240
+ set.inputValue(ctx2, evt.value);
1241
+ },
1242
+ clearInputValue(ctx2) {
1243
+ set.inputValue(ctx2, "");
1244
+ },
1245
+ revertInputValue(ctx2) {
1246
+ const inputValue = (0, import_utils.match)(ctx2.selectionBehavior, {
1247
+ replace: ctx2.hasSelectedItems ? ctx2.valueAsString : "",
1248
+ preserve: ctx2.inputValue,
1249
+ clear: ""
1250
+ });
1251
+ set.inputValue(ctx2, inputValue);
1252
+ },
1253
+ syncInitialValues(ctx2) {
1254
+ const selectedItems = ctx2.collection.items(ctx2.value);
1255
+ const valueAsString = ctx2.collection.itemsToString(selectedItems);
1256
+ ctx2.highlightedItem = ctx2.collection.item(ctx2.highlightedValue);
1257
+ ctx2.selectedItems = selectedItems;
1258
+ ctx2.valueAsString = valueAsString;
1259
+ ctx2.inputValue = (0, import_utils.match)(ctx2.selectionBehavior, {
1260
+ preserve: ctx2.inputValue || valueAsString,
1261
+ replace: valueAsString,
1262
+ clear: ""
1263
+ });
1264
+ },
1265
+ syncSelectionBehavior(ctx2) {
1266
+ if (ctx2.multiple) {
1267
+ ctx2.selectionBehavior = "clear";
1268
+ }
1269
+ },
1270
+ setSelectedItems(ctx2, evt) {
1271
+ if (!(0, import_utils.isArray)(evt.value))
1272
+ return;
1273
+ set.value(ctx2, evt.value);
1274
+ },
1275
+ clearSelectedItems(ctx2) {
1276
+ set.value(ctx2, []);
1277
+ },
1278
+ scrollContentToTop(ctx2) {
1279
+ if (ctx2.scrollToIndexFn) {
1280
+ ctx2.scrollToIndexFn({ index: 0, immediate: true });
1281
+ } else {
1282
+ const contentEl = dom.getContentEl(ctx2);
1283
+ if (!contentEl)
1284
+ return;
1285
+ contentEl.scrollTop = 0;
1286
+ }
1287
+ },
1288
+ invokeOnOpen(ctx2) {
1289
+ ctx2.onOpenChange?.({ open: true });
1290
+ },
1291
+ invokeOnClose(ctx2) {
1292
+ ctx2.onOpenChange?.({ open: false });
1293
+ },
1294
+ highlightFirstItem(ctx2) {
1295
+ (0, import_dom_query3.raf)(() => {
1296
+ const value = ctx2.collection.first();
1297
+ set.highlightedValue(ctx2, value);
1298
+ });
1299
+ },
1300
+ highlightFirstItemIfNeeded(ctx2) {
1301
+ if (!ctx2.autoHighlight)
1302
+ return;
1303
+ (0, import_dom_query3.raf)(() => {
1304
+ const value = ctx2.collection.first();
1305
+ set.highlightedValue(ctx2, value);
1306
+ });
1307
+ },
1308
+ highlightLastItem(ctx2) {
1309
+ (0, import_dom_query3.raf)(() => {
1310
+ const value = ctx2.collection.last();
1311
+ set.highlightedValue(ctx2, value);
1312
+ });
1313
+ },
1314
+ highlightNextItem(ctx2) {
1315
+ let value = null;
1316
+ if (ctx2.highlightedValue) {
1317
+ value = ctx2.collection.next(ctx2.highlightedValue);
1318
+ if (!value && ctx2.loopFocus)
1319
+ value = ctx2.collection.first();
1320
+ } else {
1321
+ value = ctx2.collection.first();
1322
+ }
1323
+ set.highlightedValue(ctx2, value);
1324
+ },
1325
+ highlightPrevItem(ctx2) {
1326
+ let value = null;
1327
+ if (ctx2.highlightedValue) {
1328
+ value = ctx2.collection.prev(ctx2.highlightedValue);
1329
+ if (!value && ctx2.loopFocus)
1330
+ value = ctx2.collection.last();
1331
+ } else {
1332
+ value = ctx2.collection.last();
1333
+ }
1334
+ set.highlightedValue(ctx2, value);
1335
+ },
1336
+ highlightFirstSelectedItem(ctx2) {
1337
+ (0, import_dom_query3.raf)(() => {
1338
+ const [value] = ctx2.collection.sort(ctx2.value);
1339
+ set.highlightedValue(ctx2, value);
1340
+ });
1341
+ },
1342
+ highlightFirstOrSelectedItem(ctx2) {
1343
+ (0, import_dom_query3.raf)(() => {
1344
+ let value = null;
1345
+ if (ctx2.hasSelectedItems) {
1346
+ value = ctx2.collection.sort(ctx2.value)[0];
1347
+ } else {
1348
+ value = ctx2.collection.first();
1349
+ }
1350
+ set.highlightedValue(ctx2, value);
1351
+ });
1352
+ },
1353
+ highlightLastOrSelectedItem(ctx2) {
1354
+ (0, import_dom_query3.raf)(() => {
1355
+ let value = null;
1356
+ if (ctx2.hasSelectedItems) {
1357
+ value = ctx2.collection.sort(ctx2.value)[0];
1358
+ } else {
1359
+ value = ctx2.collection.last();
1360
+ }
1361
+ set.highlightedValue(ctx2, value);
1362
+ });
1363
+ },
1364
+ autofillInputValue(ctx2, evt) {
1365
+ const inputEl = dom.getInputEl(ctx2);
1366
+ if (!ctx2.autoComplete || !inputEl || !evt.keypress)
1367
+ return;
1368
+ const valueText = ctx2.collection.valueToString(ctx2.highlightedValue);
1369
+ (0, import_dom_query3.raf)(() => {
1370
+ inputEl.value = valueText || ctx2.inputValue;
1371
+ });
1372
+ },
1373
+ setCollection(ctx2, evt) {
1374
+ ctx2.collection = evt.value;
1375
+ },
1376
+ syncSelectedItems(ctx2) {
1377
+ sync.valueChange(ctx2);
1378
+ },
1379
+ syncHighlightedItem(ctx2) {
1380
+ sync.highlightChange(ctx2);
1381
+ },
1382
+ toggleVisibility(ctx2, evt, { send }) {
1383
+ send({ type: ctx2.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE", previousEvent: evt });
1384
+ }
1385
+ }
1386
+ }
1387
+ );
1388
+ }
1389
+ var sync = {
1390
+ valueChange: (ctx) => {
1391
+ const prevSelectedItems = ctx.selectedItems;
1392
+ ctx.selectedItems = ctx.value.map((v) => {
1393
+ const foundItem = prevSelectedItems.find((item) => ctx.collection.itemToValue(item) === v);
1394
+ if (foundItem)
1395
+ return foundItem;
1396
+ return ctx.collection.item(v);
1397
+ });
1398
+ const valueAsString = ctx.collection.itemsToString(ctx.selectedItems);
1399
+ ctx.valueAsString = valueAsString;
1400
+ let inputValue;
1401
+ if (ctx.getSelectionValue) {
1402
+ inputValue = ctx.getSelectionValue({
1403
+ inputValue: ctx.inputValue,
1404
+ selectedItems: Array.from(ctx.selectedItems),
1405
+ valueAsString
1406
+ });
1407
+ } else {
1408
+ inputValue = (0, import_utils.match)(ctx.selectionBehavior, {
1409
+ replace: ctx.valueAsString,
1410
+ preserve: ctx.inputValue,
1411
+ clear: ""
1412
+ });
1413
+ }
1414
+ set.inputValue(ctx, inputValue);
1415
+ },
1416
+ highlightChange: (ctx) => {
1417
+ ctx.highlightedItem = ctx.collection.item(ctx.highlightedValue);
1418
+ }
1419
+ };
1420
+ var invoke = {
1421
+ valueChange: (ctx) => {
1422
+ sync.valueChange(ctx);
1423
+ ctx.onValueChange?.({
1424
+ value: Array.from(ctx.value),
1425
+ items: Array.from(ctx.selectedItems)
1426
+ });
1427
+ },
1428
+ highlightChange: (ctx) => {
1429
+ sync.highlightChange(ctx);
1430
+ ctx.onHighlightChange?.({
1431
+ highlightedValue: ctx.highlightedValue,
1432
+ highlightedItem: ctx.highlightedItem
1433
+ });
1434
+ },
1435
+ inputChange: (ctx) => {
1436
+ ctx.onInputValueChange?.({ inputValue: ctx.inputValue });
1437
+ }
1438
+ };
1439
+ var set = {
1440
+ value: (ctx, value, force = false) => {
1441
+ if ((0, import_utils.isEqual)(ctx.value, value))
1442
+ return;
1443
+ if (value == null && !force)
1444
+ return;
1445
+ if (value == null && force) {
1446
+ ctx.value = [];
1447
+ invoke.valueChange(ctx);
1448
+ return;
1449
+ }
1450
+ if ((0, import_utils.isArray)(value)) {
1451
+ ctx.value = value;
1452
+ } else if (value != null) {
1453
+ ctx.value = ctx.multiple ? (0, import_utils.addOrRemove)(ctx.value, value) : [value];
1454
+ }
1455
+ invoke.valueChange(ctx);
1456
+ },
1457
+ highlightedValue: (ctx, value, force = false) => {
1458
+ if ((0, import_utils.isEqual)(ctx.highlightedValue, value))
1459
+ return;
1460
+ if (!value && !force)
1461
+ return;
1462
+ ctx.highlightedValue = value || null;
1463
+ invoke.highlightChange(ctx);
1464
+ },
1465
+ inputValue: (ctx, value) => {
1466
+ if ((0, import_utils.isEqual)(ctx.inputValue, value))
1467
+ return;
1468
+ ctx.inputValue = value;
1469
+ invoke.inputChange(ctx);
1470
+ }
1471
+ };
1472
+ // Annotate the CommonJS export names for ESM import in node:
1473
+ 0 && (module.exports = {
1474
+ anatomy,
1475
+ collection,
1476
+ connect,
1477
+ machine
1478
+ });
2
1479
  //# sourceMappingURL=index.js.map