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