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