@zag-js/combobox 0.55.0 → 0.56.1

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.d.mts CHANGED
@@ -178,7 +178,10 @@ interface PublicContext<T extends CollectionItem = CollectionItem> extends Direc
178
178
  */
179
179
  collection: Collection<any>;
180
180
  /**
181
- * Whether to allow multiple selection
181
+ * Whether to allow multiple selection.
182
+ *
183
+ * **Good to know:** When `multiple` is `true`, the `selectionBehavior` is automatically set to `clear`.
184
+ * It is recommended to render the selected items in a separate container.
182
185
  */
183
186
  multiple?: boolean;
184
187
  /**
package/dist/index.d.ts CHANGED
@@ -178,7 +178,10 @@ interface PublicContext<T extends CollectionItem = CollectionItem> extends Direc
178
178
  */
179
179
  collection: Collection<any>;
180
180
  /**
181
- * Whether to allow multiple selection
181
+ * Whether to allow multiple selection.
182
+ *
183
+ * **Good to know:** When `multiple` is `true`, the `selectionBehavior` is automatically set to `clear`.
184
+ * It is recommended to render the selected items in a separate container.
182
185
  */
183
186
  multiple?: boolean;
184
187
  /**
package/dist/index.js CHANGED
@@ -84,20 +84,17 @@ var dom = (0, import_dom_query.createScope)({
84
84
  getClearTriggerEl: (ctx) => dom.getById(ctx, dom.getClearTriggerId(ctx)),
85
85
  getHighlightedItemEl: (ctx) => {
86
86
  const value = ctx.highlightedValue;
87
- if (value == null)
88
- return;
87
+ if (value == null) return;
89
88
  return (0, import_dom_query.query)(dom.getContentEl(ctx), `[role=option][data-value="${CSS.escape(value)}"`);
90
89
  },
91
90
  focusInputEl: (ctx) => {
92
91
  const inputEl = dom.getInputEl(ctx);
93
- if (dom.getActiveElement(ctx) === inputEl)
94
- return;
92
+ if (dom.getActiveElement(ctx) === inputEl) return;
95
93
  inputEl?.focus({ preventScroll: true });
96
94
  },
97
95
  focusTriggerEl: (ctx) => {
98
96
  const triggerEl = dom.getTriggerEl(ctx);
99
- if (dom.getActiveElement(ctx) === triggerEl)
100
- return;
97
+ if (dom.getActiveElement(ctx) === triggerEl) return;
101
98
  triggerEl?.focus({ preventScroll: true });
102
99
  }
103
100
  });
@@ -169,8 +166,7 @@ function connect(state, send, normalize) {
169
166
  dom.getInputEl(state.context)?.focus();
170
167
  },
171
168
  setOpen(nextOpen) {
172
- if (nextOpen === open)
173
- return;
169
+ if (nextOpen === open) return;
174
170
  send(nextOpen ? "OPEN" : "CLOSE");
175
171
  },
176
172
  getRootProps() {
@@ -193,8 +189,7 @@ function connect(state, send, normalize) {
193
189
  "data-invalid": (0, import_dom_query2.dataAttr)(invalid),
194
190
  "data-focus": (0, import_dom_query2.dataAttr)(focused),
195
191
  onClick(event) {
196
- if (composite)
197
- return;
192
+ if (composite) return;
198
193
  event.preventDefault();
199
194
  dom.getTriggerEl(state.context)?.focus({ preventScroll: true });
200
195
  }
@@ -245,61 +240,49 @@ function connect(state, send, normalize) {
245
240
  "data-state": open ? "open" : "closed",
246
241
  "aria-activedescendant": highlightedValue ? dom.getItemId(state.context, highlightedValue) : void 0,
247
242
  onClick(event) {
248
- if (event.defaultPrevented)
249
- return;
250
- if (!state.context.openOnClick)
251
- return;
252
- if (!interactive)
253
- return;
243
+ if (event.defaultPrevented) return;
244
+ if (!state.context.openOnClick) return;
245
+ if (!interactive) return;
254
246
  send("INPUT.CLICK");
255
247
  },
256
248
  onFocus() {
257
- if (disabled)
258
- return;
249
+ if (disabled) return;
259
250
  send("INPUT.FOCUS");
260
251
  },
261
252
  onBlur() {
262
- if (disabled)
263
- return;
253
+ if (disabled) return;
264
254
  send("INPUT.BLUR");
265
255
  },
266
256
  onChange(event) {
267
257
  send({ type: "INPUT.CHANGE", value: event.currentTarget.value });
268
258
  },
269
259
  onKeyDown(event) {
270
- if (event.defaultPrevented)
271
- return;
272
- if (!interactive)
273
- return;
274
- if (event.ctrlKey || event.shiftKey || (0, import_dom_query2.isComposingEvent)(event))
275
- return;
260
+ if (event.defaultPrevented) return;
261
+ if (!interactive) return;
262
+ if (event.ctrlKey || event.shiftKey || (0, import_dom_query2.isComposingEvent)(event)) return;
276
263
  const openOnKeyPress = state.context.openOnKeyPress;
277
264
  const isModifierKey = event.ctrlKey || event.metaKey || event.shiftKey;
278
265
  const keypress = true;
279
266
  const keymap = {
280
267
  ArrowDown(event2) {
281
- if (!openOnKeyPress && !open)
282
- return;
268
+ if (!openOnKeyPress && !open) return;
283
269
  send({ type: event2.altKey ? "OPEN" : "INPUT.ARROW_DOWN", keypress });
284
270
  event2.preventDefault();
285
271
  },
286
272
  ArrowUp() {
287
- if (!openOnKeyPress && !open)
288
- return;
273
+ if (!openOnKeyPress && !open) return;
289
274
  send({ type: event.altKey ? "CLOSE" : "INPUT.ARROW_UP", keypress });
290
275
  event.preventDefault();
291
276
  },
292
277
  Home(event2) {
293
- if (isModifierKey)
294
- return;
278
+ if (isModifierKey) return;
295
279
  send({ type: "INPUT.HOME", keypress });
296
280
  if (open) {
297
281
  event2.preventDefault();
298
282
  }
299
283
  },
300
284
  End(event2) {
301
- if (isModifierKey)
302
- return;
285
+ if (isModifierKey) return;
303
286
  send({ type: "INPUT.END", keypress });
304
287
  if (open) {
305
288
  event2.preventDefault();
@@ -341,34 +324,26 @@ function connect(state, send, normalize) {
341
324
  "data-readonly": (0, import_dom_query2.dataAttr)(readOnly),
342
325
  "data-disabled": (0, import_dom_query2.dataAttr)(disabled),
343
326
  onFocus() {
344
- if (!props.focusable)
345
- return;
327
+ if (!props.focusable) return;
346
328
  send({ type: "INPUT.FOCUS", src: "trigger" });
347
329
  },
348
330
  onClick(event) {
349
- if (event.defaultPrevented)
350
- return;
351
- if (!interactive)
352
- return;
353
- if (!(0, import_dom_event.isLeftClick)(event))
354
- return;
331
+ if (event.defaultPrevented) return;
332
+ if (!interactive) return;
333
+ if (!(0, import_dom_event.isLeftClick)(event)) return;
355
334
  send("TRIGGER.CLICK");
356
335
  },
357
336
  onPointerDown(event) {
358
- if (!interactive)
359
- return;
360
- if (event.pointerType === "touch")
361
- return;
337
+ if (!interactive) return;
338
+ if (event.pointerType === "touch") return;
362
339
  event.preventDefault();
363
340
  queueMicrotask(() => {
364
341
  dom.getInputEl(state.context)?.focus({ preventScroll: true });
365
342
  });
366
343
  },
367
344
  onKeyDown(event) {
368
- if (event.defaultPrevented)
369
- return;
370
- if (composite)
371
- return;
345
+ if (event.defaultPrevented) return;
346
+ if (composite) return;
372
347
  const keyMap = {
373
348
  ArrowDown() {
374
349
  send({ type: "INPUT.ARROW_DOWN", src: "trigger" });
@@ -425,10 +400,8 @@ function connect(state, send, normalize) {
425
400
  event.preventDefault();
426
401
  },
427
402
  onClick(event) {
428
- if (event.defaultPrevented)
429
- return;
430
- if (!interactive)
431
- return;
403
+ if (event.defaultPrevented) return;
404
+ if (!interactive) return;
432
405
  send({ type: "VALUE.CLEAR", src: "clear-trigger" });
433
406
  }
434
407
  });
@@ -450,31 +423,22 @@ function connect(state, send, normalize) {
450
423
  "data-disabled": (0, import_dom_query2.dataAttr)(itemState.disabled),
451
424
  "data-value": itemState.value,
452
425
  onPointerMove() {
453
- if (itemState.disabled)
454
- return;
455
- if (itemState.highlighted)
456
- return;
426
+ if (itemState.disabled) return;
427
+ if (itemState.highlighted) return;
457
428
  send({ type: "ITEM.POINTER_MOVE", value });
458
429
  },
459
430
  onPointerLeave() {
460
- if (props.persistFocus)
461
- return;
462
- if (itemState.disabled)
463
- return;
431
+ if (props.persistFocus) return;
432
+ if (itemState.disabled) return;
464
433
  const mouseMoved = state.previousEvent.type.includes("POINTER");
465
- if (!mouseMoved)
466
- return;
434
+ if (!mouseMoved) return;
467
435
  send({ type: "ITEM.POINTER_LEAVE", value });
468
436
  },
469
437
  onPointerUp(event) {
470
- if ((0, import_dom_query2.isDownloadingEvent)(event))
471
- return;
472
- if ((0, import_dom_query2.isOpeningInNewTab)(event))
473
- return;
474
- if ((0, import_dom_event.isContextMenuEvent)(event))
475
- return;
476
- if (itemState.disabled)
477
- return;
438
+ if ((0, import_dom_query2.isDownloadingEvent)(event)) return;
439
+ if ((0, import_dom_query2.isOpeningInNewTab)(event)) return;
440
+ if ((0, import_dom_event.isContextMenuEvent)(event)) return;
441
+ if (itemState.disabled) return;
478
442
  send({ type: "ITEM.CLICK", src: "pointerup", value });
479
443
  },
480
444
  onTouchEnd(event) {
@@ -550,6 +514,8 @@ function machine(userContext) {
550
514
  openOnKeyPress: true,
551
515
  openOnChange: true,
552
516
  composite: true,
517
+ readOnly: false,
518
+ disabled: false,
553
519
  ...ctx,
554
520
  highlightedItem: null,
555
521
  selectedItems: [],
@@ -1085,8 +1051,7 @@ function machine(userContext) {
1085
1051
  closeOnSelect: (ctx2) => !!ctx2.closeOnSelect,
1086
1052
  isOpenControlled: (ctx2) => !!ctx2["open.controlled"],
1087
1053
  openOnChange: (ctx2, evt) => {
1088
- if ((0, import_utils.isBoolean)(ctx2.openOnChange))
1089
- return ctx2.openOnChange;
1054
+ if ((0, import_utils.isBoolean)(ctx2.openOnChange)) return ctx2.openOnChange;
1090
1055
  return !!ctx2.openOnChange?.({ inputValue: evt.value });
1091
1056
  },
1092
1057
  restoreFocus: (_ctx, evt) => evt.restoreFocus == null ? true : !!evt.restoreFocus,
@@ -1094,8 +1059,7 @@ function machine(userContext) {
1094
1059
  },
1095
1060
  activities: {
1096
1061
  trackDismissableLayer(ctx2, _evt, { send }) {
1097
- if (ctx2.disableLayer)
1098
- return;
1062
+ if (ctx2.disableLayer) return;
1099
1063
  const contentEl = () => dom.getContentEl(ctx2);
1100
1064
  return (0, import_dismissable.trackDismissableElement)(contentEl, {
1101
1065
  defer: true,
@@ -1130,8 +1094,7 @@ function machine(userContext) {
1130
1094
  },
1131
1095
  // in event the options are fetched (async), we still want to auto-highlight the first option
1132
1096
  trackChildNodes(ctx2, _evt, { send }) {
1133
- if (!ctx2.autoHighlight)
1134
- return;
1097
+ if (!ctx2.autoHighlight) return;
1135
1098
  const exec = () => send("CHILDREN_CHANGE");
1136
1099
  const contentEl = () => dom.getContentEl(ctx2);
1137
1100
  return (0, import_dom_query3.observeChildren)(contentEl, {
@@ -1145,8 +1108,7 @@ function machine(userContext) {
1145
1108
  const exec = (immediate) => {
1146
1109
  const state = getState();
1147
1110
  const pointer = state.event.type.includes("POINTER");
1148
- if (pointer || !ctx2.highlightedValue)
1149
- return;
1111
+ if (pointer || !ctx2.highlightedValue) return;
1150
1112
  const itemEl = dom.getHighlightedItemEl(ctx2);
1151
1113
  const contentEl = dom.getContentEl(ctx2);
1152
1114
  if (ctx2.scrollToIndexFn) {
@@ -1186,8 +1148,7 @@ function machine(userContext) {
1186
1148
  });
1187
1149
  },
1188
1150
  setHighlightedItem(ctx2, evt) {
1189
- if (evt.value == null)
1190
- return;
1151
+ if (evt.value == null) return;
1191
1152
  set.highlightedValue(ctx2, evt.value);
1192
1153
  },
1193
1154
  clearHighlightedItem(ctx2) {
@@ -1197,13 +1158,11 @@ function machine(userContext) {
1197
1158
  set.value(ctx2, ctx2.highlightedValue);
1198
1159
  },
1199
1160
  selectItem(ctx2, evt) {
1200
- if (evt.value == null)
1201
- return;
1161
+ if (evt.value == null) return;
1202
1162
  set.value(ctx2, evt.value);
1203
1163
  },
1204
1164
  clearItem(ctx2, evt) {
1205
- if (evt.value == null)
1206
- return;
1165
+ if (evt.value == null) return;
1207
1166
  const value = ctx2.value.filter((v) => v !== evt.value);
1208
1167
  set.value(ctx2, value);
1209
1168
  },
@@ -1224,15 +1183,12 @@ function machine(userContext) {
1224
1183
  },
1225
1184
  syncInputValue(ctx2) {
1226
1185
  const inputEl = dom.getInputEl(ctx2);
1227
- if (!inputEl)
1228
- return;
1186
+ if (!inputEl) return;
1229
1187
  inputEl.value = ctx2.inputValue;
1230
1188
  queueMicrotask(() => {
1231
1189
  const { selectionStart, selectionEnd } = inputEl;
1232
- if (Math.abs((selectionEnd ?? 0) - (selectionStart ?? 0)) !== 0)
1233
- return;
1234
- if (selectionStart !== 0)
1235
- return;
1190
+ if (Math.abs((selectionEnd ?? 0) - (selectionStart ?? 0)) !== 0) return;
1191
+ if (selectionStart !== 0) return;
1236
1192
  inputEl.setSelectionRange(inputEl.value.length, inputEl.value.length);
1237
1193
  });
1238
1194
  },
@@ -1268,8 +1224,7 @@ function machine(userContext) {
1268
1224
  }
1269
1225
  },
1270
1226
  setSelectedItems(ctx2, evt) {
1271
- if (!(0, import_utils.isArray)(evt.value))
1272
- return;
1227
+ if (!(0, import_utils.isArray)(evt.value)) return;
1273
1228
  set.value(ctx2, evt.value);
1274
1229
  },
1275
1230
  clearSelectedItems(ctx2) {
@@ -1280,8 +1235,7 @@ function machine(userContext) {
1280
1235
  ctx2.scrollToIndexFn({ index: 0, immediate: true });
1281
1236
  } else {
1282
1237
  const contentEl = dom.getContentEl(ctx2);
1283
- if (!contentEl)
1284
- return;
1238
+ if (!contentEl) return;
1285
1239
  contentEl.scrollTop = 0;
1286
1240
  }
1287
1241
  },
@@ -1298,8 +1252,7 @@ function machine(userContext) {
1298
1252
  });
1299
1253
  },
1300
1254
  highlightFirstItemIfNeeded(ctx2) {
1301
- if (!ctx2.autoHighlight)
1302
- return;
1255
+ if (!ctx2.autoHighlight) return;
1303
1256
  (0, import_dom_query3.raf)(() => {
1304
1257
  const value = ctx2.collection.first();
1305
1258
  set.highlightedValue(ctx2, value);
@@ -1315,8 +1268,7 @@ function machine(userContext) {
1315
1268
  let value = null;
1316
1269
  if (ctx2.highlightedValue) {
1317
1270
  value = ctx2.collection.next(ctx2.highlightedValue);
1318
- if (!value && ctx2.loopFocus)
1319
- value = ctx2.collection.first();
1271
+ if (!value && ctx2.loopFocus) value = ctx2.collection.first();
1320
1272
  } else {
1321
1273
  value = ctx2.collection.first();
1322
1274
  }
@@ -1326,8 +1278,7 @@ function machine(userContext) {
1326
1278
  let value = null;
1327
1279
  if (ctx2.highlightedValue) {
1328
1280
  value = ctx2.collection.prev(ctx2.highlightedValue);
1329
- if (!value && ctx2.loopFocus)
1330
- value = ctx2.collection.last();
1281
+ if (!value && ctx2.loopFocus) value = ctx2.collection.last();
1331
1282
  } else {
1332
1283
  value = ctx2.collection.last();
1333
1284
  }
@@ -1363,8 +1314,7 @@ function machine(userContext) {
1363
1314
  },
1364
1315
  autofillInputValue(ctx2, evt) {
1365
1316
  const inputEl = dom.getInputEl(ctx2);
1366
- if (!ctx2.autoComplete || !inputEl || !evt.keypress)
1367
- return;
1317
+ if (!ctx2.autoComplete || !inputEl || !evt.keypress) return;
1368
1318
  const valueText = ctx2.collection.valueToString(ctx2.highlightedValue);
1369
1319
  (0, import_dom_query3.raf)(() => {
1370
1320
  inputEl.value = valueText || ctx2.inputValue;
@@ -1391,8 +1341,7 @@ var sync = {
1391
1341
  const prevSelectedItems = ctx.selectedItems;
1392
1342
  ctx.selectedItems = ctx.value.map((v) => {
1393
1343
  const foundItem = prevSelectedItems.find((item) => ctx.collection.itemToValue(item) === v);
1394
- if (foundItem)
1395
- return foundItem;
1344
+ if (foundItem) return foundItem;
1396
1345
  return ctx.collection.item(v);
1397
1346
  });
1398
1347
  const valueAsString = ctx.collection.itemsToString(ctx.selectedItems);
@@ -1438,10 +1387,8 @@ var invoke = {
1438
1387
  };
1439
1388
  var set = {
1440
1389
  value: (ctx, value, force = false) => {
1441
- if ((0, import_utils.isEqual)(ctx.value, value))
1442
- return;
1443
- if (value == null && !force)
1444
- return;
1390
+ if ((0, import_utils.isEqual)(ctx.value, value)) return;
1391
+ if (value == null && !force) return;
1445
1392
  if (value == null && force) {
1446
1393
  ctx.value = [];
1447
1394
  invoke.valueChange(ctx);
@@ -1455,16 +1402,13 @@ var set = {
1455
1402
  invoke.valueChange(ctx);
1456
1403
  },
1457
1404
  highlightedValue: (ctx, value, force = false) => {
1458
- if ((0, import_utils.isEqual)(ctx.highlightedValue, value))
1459
- return;
1460
- if (!value && !force)
1461
- return;
1405
+ if ((0, import_utils.isEqual)(ctx.highlightedValue, value)) return;
1406
+ if (!value && !force) return;
1462
1407
  ctx.highlightedValue = value || null;
1463
1408
  invoke.highlightChange(ctx);
1464
1409
  },
1465
1410
  inputValue: (ctx, value) => {
1466
- if ((0, import_utils.isEqual)(ctx.inputValue, value))
1467
- return;
1411
+ if ((0, import_utils.isEqual)(ctx.inputValue, value)) return;
1468
1412
  ctx.inputValue = value;
1469
1413
  invoke.inputChange(ctx);
1470
1414
  }