@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.mjs CHANGED
@@ -55,20 +55,17 @@ var dom = createScope({
55
55
  getClearTriggerEl: (ctx) => dom.getById(ctx, dom.getClearTriggerId(ctx)),
56
56
  getHighlightedItemEl: (ctx) => {
57
57
  const value = ctx.highlightedValue;
58
- if (value == null)
59
- return;
58
+ if (value == null) return;
60
59
  return query(dom.getContentEl(ctx), `[role=option][data-value="${CSS.escape(value)}"`);
61
60
  },
62
61
  focusInputEl: (ctx) => {
63
62
  const inputEl = dom.getInputEl(ctx);
64
- if (dom.getActiveElement(ctx) === inputEl)
65
- return;
63
+ if (dom.getActiveElement(ctx) === inputEl) return;
66
64
  inputEl?.focus({ preventScroll: true });
67
65
  },
68
66
  focusTriggerEl: (ctx) => {
69
67
  const triggerEl = dom.getTriggerEl(ctx);
70
- if (dom.getActiveElement(ctx) === triggerEl)
71
- return;
68
+ if (dom.getActiveElement(ctx) === triggerEl) return;
72
69
  triggerEl?.focus({ preventScroll: true });
73
70
  }
74
71
  });
@@ -140,8 +137,7 @@ function connect(state, send, normalize) {
140
137
  dom.getInputEl(state.context)?.focus();
141
138
  },
142
139
  setOpen(nextOpen) {
143
- if (nextOpen === open)
144
- return;
140
+ if (nextOpen === open) return;
145
141
  send(nextOpen ? "OPEN" : "CLOSE");
146
142
  },
147
143
  getRootProps() {
@@ -164,8 +160,7 @@ function connect(state, send, normalize) {
164
160
  "data-invalid": dataAttr(invalid),
165
161
  "data-focus": dataAttr(focused),
166
162
  onClick(event) {
167
- if (composite)
168
- return;
163
+ if (composite) return;
169
164
  event.preventDefault();
170
165
  dom.getTriggerEl(state.context)?.focus({ preventScroll: true });
171
166
  }
@@ -216,61 +211,49 @@ function connect(state, send, normalize) {
216
211
  "data-state": open ? "open" : "closed",
217
212
  "aria-activedescendant": highlightedValue ? dom.getItemId(state.context, highlightedValue) : void 0,
218
213
  onClick(event) {
219
- if (event.defaultPrevented)
220
- return;
221
- if (!state.context.openOnClick)
222
- return;
223
- if (!interactive)
224
- return;
214
+ if (event.defaultPrevented) return;
215
+ if (!state.context.openOnClick) return;
216
+ if (!interactive) return;
225
217
  send("INPUT.CLICK");
226
218
  },
227
219
  onFocus() {
228
- if (disabled)
229
- return;
220
+ if (disabled) return;
230
221
  send("INPUT.FOCUS");
231
222
  },
232
223
  onBlur() {
233
- if (disabled)
234
- return;
224
+ if (disabled) return;
235
225
  send("INPUT.BLUR");
236
226
  },
237
227
  onChange(event) {
238
228
  send({ type: "INPUT.CHANGE", value: event.currentTarget.value });
239
229
  },
240
230
  onKeyDown(event) {
241
- if (event.defaultPrevented)
242
- return;
243
- if (!interactive)
244
- return;
245
- if (event.ctrlKey || event.shiftKey || isComposingEvent(event))
246
- return;
231
+ if (event.defaultPrevented) return;
232
+ if (!interactive) return;
233
+ if (event.ctrlKey || event.shiftKey || isComposingEvent(event)) return;
247
234
  const openOnKeyPress = state.context.openOnKeyPress;
248
235
  const isModifierKey = event.ctrlKey || event.metaKey || event.shiftKey;
249
236
  const keypress = true;
250
237
  const keymap = {
251
238
  ArrowDown(event2) {
252
- if (!openOnKeyPress && !open)
253
- return;
239
+ if (!openOnKeyPress && !open) return;
254
240
  send({ type: event2.altKey ? "OPEN" : "INPUT.ARROW_DOWN", keypress });
255
241
  event2.preventDefault();
256
242
  },
257
243
  ArrowUp() {
258
- if (!openOnKeyPress && !open)
259
- return;
244
+ if (!openOnKeyPress && !open) return;
260
245
  send({ type: event.altKey ? "CLOSE" : "INPUT.ARROW_UP", keypress });
261
246
  event.preventDefault();
262
247
  },
263
248
  Home(event2) {
264
- if (isModifierKey)
265
- return;
249
+ if (isModifierKey) return;
266
250
  send({ type: "INPUT.HOME", keypress });
267
251
  if (open) {
268
252
  event2.preventDefault();
269
253
  }
270
254
  },
271
255
  End(event2) {
272
- if (isModifierKey)
273
- return;
256
+ if (isModifierKey) return;
274
257
  send({ type: "INPUT.END", keypress });
275
258
  if (open) {
276
259
  event2.preventDefault();
@@ -312,34 +295,26 @@ function connect(state, send, normalize) {
312
295
  "data-readonly": dataAttr(readOnly),
313
296
  "data-disabled": dataAttr(disabled),
314
297
  onFocus() {
315
- if (!props.focusable)
316
- return;
298
+ if (!props.focusable) return;
317
299
  send({ type: "INPUT.FOCUS", src: "trigger" });
318
300
  },
319
301
  onClick(event) {
320
- if (event.defaultPrevented)
321
- return;
322
- if (!interactive)
323
- return;
324
- if (!isLeftClick(event))
325
- return;
302
+ if (event.defaultPrevented) return;
303
+ if (!interactive) return;
304
+ if (!isLeftClick(event)) return;
326
305
  send("TRIGGER.CLICK");
327
306
  },
328
307
  onPointerDown(event) {
329
- if (!interactive)
330
- return;
331
- if (event.pointerType === "touch")
332
- return;
308
+ if (!interactive) return;
309
+ if (event.pointerType === "touch") return;
333
310
  event.preventDefault();
334
311
  queueMicrotask(() => {
335
312
  dom.getInputEl(state.context)?.focus({ preventScroll: true });
336
313
  });
337
314
  },
338
315
  onKeyDown(event) {
339
- if (event.defaultPrevented)
340
- return;
341
- if (composite)
342
- return;
316
+ if (event.defaultPrevented) return;
317
+ if (composite) return;
343
318
  const keyMap = {
344
319
  ArrowDown() {
345
320
  send({ type: "INPUT.ARROW_DOWN", src: "trigger" });
@@ -396,10 +371,8 @@ function connect(state, send, normalize) {
396
371
  event.preventDefault();
397
372
  },
398
373
  onClick(event) {
399
- if (event.defaultPrevented)
400
- return;
401
- if (!interactive)
402
- return;
374
+ if (event.defaultPrevented) return;
375
+ if (!interactive) return;
403
376
  send({ type: "VALUE.CLEAR", src: "clear-trigger" });
404
377
  }
405
378
  });
@@ -421,31 +394,22 @@ function connect(state, send, normalize) {
421
394
  "data-disabled": dataAttr(itemState.disabled),
422
395
  "data-value": itemState.value,
423
396
  onPointerMove() {
424
- if (itemState.disabled)
425
- return;
426
- if (itemState.highlighted)
427
- return;
397
+ if (itemState.disabled) return;
398
+ if (itemState.highlighted) return;
428
399
  send({ type: "ITEM.POINTER_MOVE", value });
429
400
  },
430
401
  onPointerLeave() {
431
- if (props.persistFocus)
432
- return;
433
- if (itemState.disabled)
434
- return;
402
+ if (props.persistFocus) return;
403
+ if (itemState.disabled) return;
435
404
  const mouseMoved = state.previousEvent.type.includes("POINTER");
436
- if (!mouseMoved)
437
- return;
405
+ if (!mouseMoved) return;
438
406
  send({ type: "ITEM.POINTER_LEAVE", value });
439
407
  },
440
408
  onPointerUp(event) {
441
- if (isDownloadingEvent(event))
442
- return;
443
- if (isOpeningInNewTab(event))
444
- return;
445
- if (isContextMenuEvent(event))
446
- return;
447
- if (itemState.disabled)
448
- return;
409
+ if (isDownloadingEvent(event)) return;
410
+ if (isOpeningInNewTab(event)) return;
411
+ if (isContextMenuEvent(event)) return;
412
+ if (itemState.disabled) return;
449
413
  send({ type: "ITEM.CLICK", src: "pointerup", value });
450
414
  },
451
415
  onTouchEnd(event) {
@@ -521,6 +485,8 @@ function machine(userContext) {
521
485
  openOnKeyPress: true,
522
486
  openOnChange: true,
523
487
  composite: true,
488
+ readOnly: false,
489
+ disabled: false,
524
490
  ...ctx,
525
491
  highlightedItem: null,
526
492
  selectedItems: [],
@@ -1056,8 +1022,7 @@ function machine(userContext) {
1056
1022
  closeOnSelect: (ctx2) => !!ctx2.closeOnSelect,
1057
1023
  isOpenControlled: (ctx2) => !!ctx2["open.controlled"],
1058
1024
  openOnChange: (ctx2, evt) => {
1059
- if (isBoolean(ctx2.openOnChange))
1060
- return ctx2.openOnChange;
1025
+ if (isBoolean(ctx2.openOnChange)) return ctx2.openOnChange;
1061
1026
  return !!ctx2.openOnChange?.({ inputValue: evt.value });
1062
1027
  },
1063
1028
  restoreFocus: (_ctx, evt) => evt.restoreFocus == null ? true : !!evt.restoreFocus,
@@ -1065,8 +1030,7 @@ function machine(userContext) {
1065
1030
  },
1066
1031
  activities: {
1067
1032
  trackDismissableLayer(ctx2, _evt, { send }) {
1068
- if (ctx2.disableLayer)
1069
- return;
1033
+ if (ctx2.disableLayer) return;
1070
1034
  const contentEl = () => dom.getContentEl(ctx2);
1071
1035
  return trackDismissableElement(contentEl, {
1072
1036
  defer: true,
@@ -1101,8 +1065,7 @@ function machine(userContext) {
1101
1065
  },
1102
1066
  // in event the options are fetched (async), we still want to auto-highlight the first option
1103
1067
  trackChildNodes(ctx2, _evt, { send }) {
1104
- if (!ctx2.autoHighlight)
1105
- return;
1068
+ if (!ctx2.autoHighlight) return;
1106
1069
  const exec = () => send("CHILDREN_CHANGE");
1107
1070
  const contentEl = () => dom.getContentEl(ctx2);
1108
1071
  return observeChildren(contentEl, {
@@ -1116,8 +1079,7 @@ function machine(userContext) {
1116
1079
  const exec = (immediate) => {
1117
1080
  const state = getState();
1118
1081
  const pointer = state.event.type.includes("POINTER");
1119
- if (pointer || !ctx2.highlightedValue)
1120
- return;
1082
+ if (pointer || !ctx2.highlightedValue) return;
1121
1083
  const itemEl = dom.getHighlightedItemEl(ctx2);
1122
1084
  const contentEl = dom.getContentEl(ctx2);
1123
1085
  if (ctx2.scrollToIndexFn) {
@@ -1157,8 +1119,7 @@ function machine(userContext) {
1157
1119
  });
1158
1120
  },
1159
1121
  setHighlightedItem(ctx2, evt) {
1160
- if (evt.value == null)
1161
- return;
1122
+ if (evt.value == null) return;
1162
1123
  set.highlightedValue(ctx2, evt.value);
1163
1124
  },
1164
1125
  clearHighlightedItem(ctx2) {
@@ -1168,13 +1129,11 @@ function machine(userContext) {
1168
1129
  set.value(ctx2, ctx2.highlightedValue);
1169
1130
  },
1170
1131
  selectItem(ctx2, evt) {
1171
- if (evt.value == null)
1172
- return;
1132
+ if (evt.value == null) return;
1173
1133
  set.value(ctx2, evt.value);
1174
1134
  },
1175
1135
  clearItem(ctx2, evt) {
1176
- if (evt.value == null)
1177
- return;
1136
+ if (evt.value == null) return;
1178
1137
  const value = ctx2.value.filter((v) => v !== evt.value);
1179
1138
  set.value(ctx2, value);
1180
1139
  },
@@ -1195,15 +1154,12 @@ function machine(userContext) {
1195
1154
  },
1196
1155
  syncInputValue(ctx2) {
1197
1156
  const inputEl = dom.getInputEl(ctx2);
1198
- if (!inputEl)
1199
- return;
1157
+ if (!inputEl) return;
1200
1158
  inputEl.value = ctx2.inputValue;
1201
1159
  queueMicrotask(() => {
1202
1160
  const { selectionStart, selectionEnd } = inputEl;
1203
- if (Math.abs((selectionEnd ?? 0) - (selectionStart ?? 0)) !== 0)
1204
- return;
1205
- if (selectionStart !== 0)
1206
- return;
1161
+ if (Math.abs((selectionEnd ?? 0) - (selectionStart ?? 0)) !== 0) return;
1162
+ if (selectionStart !== 0) return;
1207
1163
  inputEl.setSelectionRange(inputEl.value.length, inputEl.value.length);
1208
1164
  });
1209
1165
  },
@@ -1239,8 +1195,7 @@ function machine(userContext) {
1239
1195
  }
1240
1196
  },
1241
1197
  setSelectedItems(ctx2, evt) {
1242
- if (!isArray(evt.value))
1243
- return;
1198
+ if (!isArray(evt.value)) return;
1244
1199
  set.value(ctx2, evt.value);
1245
1200
  },
1246
1201
  clearSelectedItems(ctx2) {
@@ -1251,8 +1206,7 @@ function machine(userContext) {
1251
1206
  ctx2.scrollToIndexFn({ index: 0, immediate: true });
1252
1207
  } else {
1253
1208
  const contentEl = dom.getContentEl(ctx2);
1254
- if (!contentEl)
1255
- return;
1209
+ if (!contentEl) return;
1256
1210
  contentEl.scrollTop = 0;
1257
1211
  }
1258
1212
  },
@@ -1269,8 +1223,7 @@ function machine(userContext) {
1269
1223
  });
1270
1224
  },
1271
1225
  highlightFirstItemIfNeeded(ctx2) {
1272
- if (!ctx2.autoHighlight)
1273
- return;
1226
+ if (!ctx2.autoHighlight) return;
1274
1227
  raf(() => {
1275
1228
  const value = ctx2.collection.first();
1276
1229
  set.highlightedValue(ctx2, value);
@@ -1286,8 +1239,7 @@ function machine(userContext) {
1286
1239
  let value = null;
1287
1240
  if (ctx2.highlightedValue) {
1288
1241
  value = ctx2.collection.next(ctx2.highlightedValue);
1289
- if (!value && ctx2.loopFocus)
1290
- value = ctx2.collection.first();
1242
+ if (!value && ctx2.loopFocus) value = ctx2.collection.first();
1291
1243
  } else {
1292
1244
  value = ctx2.collection.first();
1293
1245
  }
@@ -1297,8 +1249,7 @@ function machine(userContext) {
1297
1249
  let value = null;
1298
1250
  if (ctx2.highlightedValue) {
1299
1251
  value = ctx2.collection.prev(ctx2.highlightedValue);
1300
- if (!value && ctx2.loopFocus)
1301
- value = ctx2.collection.last();
1252
+ if (!value && ctx2.loopFocus) value = ctx2.collection.last();
1302
1253
  } else {
1303
1254
  value = ctx2.collection.last();
1304
1255
  }
@@ -1334,8 +1285,7 @@ function machine(userContext) {
1334
1285
  },
1335
1286
  autofillInputValue(ctx2, evt) {
1336
1287
  const inputEl = dom.getInputEl(ctx2);
1337
- if (!ctx2.autoComplete || !inputEl || !evt.keypress)
1338
- return;
1288
+ if (!ctx2.autoComplete || !inputEl || !evt.keypress) return;
1339
1289
  const valueText = ctx2.collection.valueToString(ctx2.highlightedValue);
1340
1290
  raf(() => {
1341
1291
  inputEl.value = valueText || ctx2.inputValue;
@@ -1362,8 +1312,7 @@ var sync = {
1362
1312
  const prevSelectedItems = ctx.selectedItems;
1363
1313
  ctx.selectedItems = ctx.value.map((v) => {
1364
1314
  const foundItem = prevSelectedItems.find((item) => ctx.collection.itemToValue(item) === v);
1365
- if (foundItem)
1366
- return foundItem;
1315
+ if (foundItem) return foundItem;
1367
1316
  return ctx.collection.item(v);
1368
1317
  });
1369
1318
  const valueAsString = ctx.collection.itemsToString(ctx.selectedItems);
@@ -1409,10 +1358,8 @@ var invoke = {
1409
1358
  };
1410
1359
  var set = {
1411
1360
  value: (ctx, value, force = false) => {
1412
- if (isEqual(ctx.value, value))
1413
- return;
1414
- if (value == null && !force)
1415
- return;
1361
+ if (isEqual(ctx.value, value)) return;
1362
+ if (value == null && !force) return;
1416
1363
  if (value == null && force) {
1417
1364
  ctx.value = [];
1418
1365
  invoke.valueChange(ctx);
@@ -1426,16 +1373,13 @@ var set = {
1426
1373
  invoke.valueChange(ctx);
1427
1374
  },
1428
1375
  highlightedValue: (ctx, value, force = false) => {
1429
- if (isEqual(ctx.highlightedValue, value))
1430
- return;
1431
- if (!value && !force)
1432
- return;
1376
+ if (isEqual(ctx.highlightedValue, value)) return;
1377
+ if (!value && !force) return;
1433
1378
  ctx.highlightedValue = value || null;
1434
1379
  invoke.highlightChange(ctx);
1435
1380
  },
1436
1381
  inputValue: (ctx, value) => {
1437
- if (isEqual(ctx.inputValue, value))
1438
- return;
1382
+ if (isEqual(ctx.inputValue, value)) return;
1439
1383
  ctx.inputValue = value;
1440
1384
  invoke.inputChange(ctx);
1441
1385
  }