stimeo-ui 0.18.1 → 0.18.2

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.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/dist/cable/index.js +6 -5
  4. data/dist/controllers/alert_dialog_controller.js +89 -45
  5. data/dist/controllers/announcer_controller.js +22 -9
  6. data/dist/controllers/auto_submit_controller.js +77 -11
  7. data/dist/controllers/bulk_select_controller.js +6 -9
  8. data/dist/controllers/character_counter_controller.js +77 -11
  9. data/dist/controllers/clipboard_controller.js +6 -5
  10. data/dist/controllers/combobox_controller.js +71 -6
  11. data/dist/controllers/command_palette_controller.js +160 -51
  12. data/dist/controllers/conditional_fields_controller.js +84 -44
  13. data/dist/controllers/confirm_controller.js +89 -45
  14. data/dist/controllers/context_menu_controller.js +1 -1
  15. data/dist/controllers/countdown_controller.js +6 -5
  16. data/dist/controllers/currency_input_controller.js +71 -6
  17. data/dist/controllers/data_grid_controller.js +77 -42
  18. data/dist/controllers/date_range_picker_controller.js +2 -1
  19. data/dist/controllers/dialog_controller.js +89 -45
  20. data/dist/controllers/direct_upload_controller.js +6 -5
  21. data/dist/controllers/dismissible_controller.js +11 -7
  22. data/dist/controllers/drawer_controller.js +89 -45
  23. data/dist/controllers/dropdown_controller.js +1 -1
  24. data/dist/controllers/editable_controller.js +71 -6
  25. data/dist/controllers/empty_state_controller.js +6 -5
  26. data/dist/controllers/file_dropzone_controller.js +13 -11
  27. data/dist/controllers/flash_controller.js +6 -5
  28. data/dist/controllers/focus_controller.js +89 -45
  29. data/dist/controllers/form_field_controller.js +6 -5
  30. data/dist/controllers/form_validation_controller.js +11 -7
  31. data/dist/controllers/frame_loading_controller.js +50 -16
  32. data/dist/controllers/hover_card_controller.js +1 -1
  33. data/dist/controllers/input_mask_controller.js +94 -36
  34. data/dist/controllers/intersection_controller.js +61 -29
  35. data/dist/controllers/lazy_frame_controller.js +61 -29
  36. data/dist/controllers/listbox_controller.js +80 -47
  37. data/dist/controllers/local_time_controller.js +1 -7
  38. data/dist/controllers/menu_controller.js +1 -1
  39. data/dist/controllers/menubar_controller.js +4 -6
  40. data/dist/controllers/meter_controller.js +6 -5
  41. data/dist/controllers/multi_select_controller.js +86 -21
  42. data/dist/controllers/navigation_menu_controller.js +1 -1
  43. data/dist/controllers/nested_form_controller.js +17 -12
  44. data/dist/controllers/network_status_controller.js +6 -5
  45. data/dist/controllers/number_input_controller.js +154 -66
  46. data/dist/controllers/otp_controller.js +71 -6
  47. data/dist/controllers/overflow_menu_controller.js +7 -2
  48. data/dist/controllers/pagination_controller.js +7 -2
  49. data/dist/controllers/password_strength_controller.js +6 -5
  50. data/dist/controllers/popover_controller.js +12 -8
  51. data/dist/controllers/progress_controller.js +6 -5
  52. data/dist/controllers/radio_group_controller.js +85 -49
  53. data/dist/controllers/range_slider_controller.js +72 -54
  54. data/dist/controllers/relative_time_controller.js +1 -7
  55. data/dist/controllers/roving_controller.js +7 -2
  56. data/dist/controllers/scroll_area_controller.js +11 -7
  57. data/dist/controllers/scrollspy_controller.js +61 -29
  58. data/dist/controllers/separator_controller.js +72 -54
  59. data/dist/controllers/sidebar_controller.js +89 -45
  60. data/dist/controllers/skeleton_controller.js +50 -16
  61. data/dist/controllers/slider_controller.js +72 -54
  62. data/dist/controllers/sortable_controller.js +6 -5
  63. data/dist/controllers/spinner_controller.js +50 -16
  64. data/dist/controllers/sticky_observer_controller.js +61 -29
  65. data/dist/controllers/submit_once_controller.js +6 -5
  66. data/dist/controllers/switch_controller.js +84 -44
  67. data/dist/controllers/tags_input_controller.js +83 -20
  68. data/dist/controllers/time_picker_controller.js +77 -42
  69. data/dist/controllers/toggle_group_controller.js +84 -44
  70. data/dist/controllers/toolbar_controller.js +78 -8
  71. data/dist/controllers/tooltip_controller.js +1 -1
  72. data/dist/controllers/tree_view_controller.js +87 -49
  73. data/dist/index.js +503 -286
  74. data/lib/stimeo/ui/version.rb +1 -1
  75. metadata +2 -2
@@ -29,14 +29,15 @@ function fillTemplate(template, values) {
29
29
  });
30
30
  }
31
31
 
32
+ var afterFocus = /* @__PURE__ */ Symbol.for("stimeo--announcer:after-focus");
32
33
  function announce(message, options = {}) {
33
34
  const text = message.trim();
34
35
  if (text.length === 0) return;
35
- window.dispatchEvent(
36
- new CustomEvent("stimeo--announcer:announce", {
37
- detail: { message: text, assertive: options.assertive === true }
38
- })
39
- );
36
+ const event = new CustomEvent("stimeo--announcer:announce", {
37
+ detail: { message: text, assertive: options.assertive === true }
38
+ });
39
+ if (options.afterFocus) event[afterFocus] = true;
40
+ window.dispatchEvent(event);
40
41
  }
41
42
 
42
43
  function sharedRegistry(key, create) {
@@ -229,10 +230,6 @@ function matchingPart(root, selector) {
229
230
  return root.matches(selector) ? root : root.querySelector(selector);
230
231
  }
231
232
  function writeLabel(slot, text) {
232
- if (slot.childElementCount === 0) {
233
- slot.textContent = text;
234
- return;
235
- }
236
233
  let first = null;
237
234
  for (const node of Array.from(slot.childNodes)) {
238
235
  if (node.nodeType !== Node.TEXT_NODE) continue;
@@ -393,7 +390,50 @@ function isCompositionInputType(inputType) {
393
390
  return inputType !== void 0 && COMPOSITION_INPUT_TYPES.has(inputType);
394
391
  }
395
392
  var BEFORE_MORPH_ATTRIBUTE = "turbo:before-morph-attribute";
396
- var textOf = (element) => element.value;
393
+ var textOf = (element) => {
394
+ if ("value" in element) return element.value;
395
+ return element instanceof Element ? element.textContent : void 0;
396
+ };
397
+ var nodesOf = (element) => {
398
+ const nodes = [];
399
+ if ("value" in element || !(element instanceof Element)) return nodes;
400
+ const walker = element.ownerDocument.createTreeWalker(element);
401
+ for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) nodes.push(node);
402
+ return nodes;
403
+ };
404
+ var folded = (text) => text.replaceAll("\xA0", " ");
405
+ var startOf = (node, data, replaced, caret) => {
406
+ const text = folded(node.data);
407
+ const composed = folded(data);
408
+ if (replaced?.startContainer === node && text.startsWith(composed, replaced.startOffset)) {
409
+ return replaced.startOffset;
410
+ }
411
+ const start = text.lastIndexOf(composed, caret);
412
+ return start !== -1 && start + composed.length >= caret ? start : -1;
413
+ };
414
+ var spotOf = (element, replaced, data) => {
415
+ if ("value" in element || !(element instanceof Element)) return null;
416
+ const selection = element.ownerDocument.getSelection();
417
+ const node = selection?.focusNode;
418
+ if (!(node instanceof Text) || node.data === "" || !element.contains(node)) return null;
419
+ const range = element.ownerDocument.createRange();
420
+ const start = typeof data === "string" && data !== "" ? startOf(node, data, replaced, selection?.focusOffset ?? 0) : -1;
421
+ if (start === -1) {
422
+ range.selectNodeContents(node);
423
+ } else {
424
+ range.setStart(node, start);
425
+ range.setEnd(node, start + data.length);
426
+ }
427
+ return range;
428
+ };
429
+ var replacedBy = (event) => typeof event.getTargetRanges === "function" ? event.getTargetRanges()[0] ?? null : null;
430
+ var rewritten = ({ element, text, nodes, spot }) => {
431
+ if (spot !== null) {
432
+ return spot.collapsed || !(element instanceof Node && element.contains(spot.startContainer));
433
+ }
434
+ const current = nodesOf(element);
435
+ return textOf(element) !== text || current.length !== nodes.length || current.some((node, index) => node !== nodes[index]);
436
+ };
397
437
  var holdsFocus = (element) => element instanceof Node && element.getRootNode().activeElement === element;
398
438
  var CompositionTracker = class {
399
439
  #observedTargets = /* @__PURE__ */ new Set();
@@ -484,11 +524,11 @@ var CompositionTracker = class {
484
524
  }
485
525
  #settle() {
486
526
  const composition = this.#composition;
487
- if (composition === null) return;
488
- const { target, element, text } = composition;
527
+ if (composition === null || !this.#observedTargets.has(composition.target)) return;
528
+ const { target, element } = composition;
489
529
  if (target instanceof Node && element instanceof Node && !target.contains(element)) {
490
530
  this.#composition = null;
491
- } else if (textOf(element) !== text) {
531
+ } else if (rewritten(composition)) {
492
532
  this.#abandon(false);
493
533
  }
494
534
  }
@@ -513,7 +553,14 @@ var CompositionTracker = class {
513
553
  this.#owe(true);
514
554
  const target = event.currentTarget;
515
555
  const element = event.target;
516
- this.#composition = { target, element, text: textOf(element) };
556
+ this.#composition = {
557
+ target,
558
+ element,
559
+ text: textOf(element),
560
+ nodes: nodesOf(element),
561
+ spot: spotOf(element, null, void 0),
562
+ replaced: null
563
+ };
517
564
  this.#runAbandon();
518
565
  this.#onStart?.(event);
519
566
  };
@@ -537,6 +584,10 @@ var CompositionTracker = class {
537
584
  if (this.#composition !== null && !keyComposing && !isComposing && inputType && !isCompositionInputType(inputType)) {
538
585
  this.#abandon(true);
539
586
  }
587
+ const composition = this.#composition;
588
+ if (composition !== null && event.target === composition.element) {
589
+ composition.replaced = replacedBy(event);
590
+ }
540
591
  this.#runAbandon();
541
592
  };
542
593
  #handleInput = (event) => {
@@ -544,6 +595,15 @@ var CompositionTracker = class {
544
595
  const { isTrusted, isComposing, inputType } = event;
545
596
  if (composition !== null && event.target === composition.element && (isTrusted || isComposing || isCompositionInputType(inputType))) {
546
597
  composition.text = textOf(composition.element);
598
+ composition.nodes = nodesOf(composition.element);
599
+ if (isCompositionInputType(inputType)) {
600
+ composition.spot = spotOf(
601
+ composition.element,
602
+ composition.replaced,
603
+ event.data
604
+ );
605
+ }
606
+ composition.replaced = null;
547
607
  } else {
548
608
  this.#settle();
549
609
  }
@@ -564,7 +624,9 @@ var CompositionTracker = class {
564
624
  const composition = this.#composition;
565
625
  if (composition?.target !== event.currentTarget || event.target !== composition.element) return;
566
626
  const detail = event.detail;
567
- if (detail?.attributeName === "value") event.preventDefault();
627
+ if (detail?.attributeName === "value" && "value" in composition.element) {
628
+ event.preventDefault();
629
+ }
568
630
  };
569
631
  };
570
632
 
@@ -1533,14 +1595,17 @@ var MultiSelectController = class _MultiSelectController extends Controller {
1533
1595
  tag?.remove();
1534
1596
  }
1535
1597
  #removeTagAt(index, focus = "neighbor") {
1536
- const tag = this.tagTargets[index];
1598
+ const tags = this.tagTargets;
1599
+ const tag = tags[index];
1537
1600
  if (!tag) return;
1601
+ const afterFocus2 = focus === "neighbor" && tags.length === 1 && this.hasInputTarget;
1538
1602
  const value = tag.dataset.value ?? "";
1539
1603
  const option = this.optionTargets.find((candidate) => this.#optionValue(candidate) === value);
1540
1604
  if (option) option.setAttribute("aria-selected", "false");
1541
1605
  tag.remove();
1542
1606
  this.#refreshRoving();
1543
- if (!this.#publishSelection(false, option ? this.#optionLabel(option) : value, value)) return;
1607
+ const label = option ? this.#optionLabel(option) : value;
1608
+ if (!this.#publishSelection(false, label, value, "user", afterFocus2)) return;
1544
1609
  if (focus === "input") {
1545
1610
  this.#focusInput();
1546
1611
  return;
@@ -1577,7 +1642,7 @@ var MultiSelectController = class _MultiSelectController extends Controller {
1577
1642
  const options = { name: this.nameValue, form: this.formValue };
1578
1643
  return writeFields(this.fieldsTarget, this.#values, options);
1579
1644
  }
1580
- #publishSelection(selected, label, value, reason = "user") {
1645
+ #publishSelection(selected, label, value, reason = "user", afterFocus2 = false) {
1581
1646
  const values = this.#values;
1582
1647
  const changed = !this.#sameSelection(this.#selectionValues, values);
1583
1648
  const fieldChanged = this.#syncFields();
@@ -1586,7 +1651,7 @@ var MultiSelectController = class _MultiSelectController extends Controller {
1586
1651
  const token = this.#moves.record();
1587
1652
  if (fieldChanged) commitField(this.fieldsTarget);
1588
1653
  if (!this.#moves.isLatest(token)) return false;
1589
- this.#announceTransition(selected, label, value, values.length);
1654
+ this.#announceTransition(selected, label, value, values.length, afterFocus2);
1590
1655
  if (!this.#moves.isLatest(token)) return false;
1591
1656
  this.dispatch("change", { detail: { values, reason } });
1592
1657
  return this.#moves.isLatest(token);
@@ -1594,9 +1659,9 @@ var MultiSelectController = class _MultiSelectController extends Controller {
1594
1659
  #refreshRoving() {
1595
1660
  this.#chipRow.ensureTabStop();
1596
1661
  }
1597
- #announceTransition(selected, label, value, count) {
1662
+ #announceTransition(selected, label, value, count, afterFocus2 = false) {
1598
1663
  const template = selected ? this.announceTextValue : this.announceRemovedTextValue;
1599
- announce(fillTemplate(template, { label, value, count }));
1664
+ announce(fillTemplate(template, { label, value, count }), { afterFocus: afterFocus2 });
1600
1665
  }
1601
1666
  #sameSelection(left, right) {
1602
1667
  if (left.length !== right.length) return false;
@@ -73,6 +73,7 @@ var EscapeLayer = class _EscapeLayer {
73
73
  );
74
74
  #active = null;
75
75
  activate(ownerDocument = document, options) {
76
+ const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
76
77
  this.deactivate();
77
78
  let registry = _EscapeLayer.#registries.get(ownerDocument);
78
79
  if (!registry) {
@@ -80,7 +81,6 @@ var EscapeLayer = class _EscapeLayer {
80
81
  _EscapeLayer.#registries.set(ownerDocument, registry);
81
82
  ownerDocument.addEventListener("keydown", registry.onKeydown);
82
83
  }
83
- const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
84
84
  registry.stack.push(entry);
85
85
  this.#active = { document: ownerDocument, entry };
86
86
  }
@@ -7,14 +7,15 @@ function fillTemplate(template, values) {
7
7
  });
8
8
  }
9
9
 
10
+ var afterFocus = /* @__PURE__ */ Symbol.for("stimeo--announcer:after-focus");
10
11
  function announce(message, options = {}) {
11
12
  const text = message.trim();
12
13
  if (text.length === 0) return;
13
- window.dispatchEvent(
14
- new CustomEvent("stimeo--announcer:announce", {
15
- detail: { message: text, assertive: options.assertive === true }
16
- })
17
- );
14
+ const event = new CustomEvent("stimeo--announcer:announce", {
15
+ detail: { message: text, assertive: options.assertive === true }
16
+ });
17
+ if (options.afterFocus) event[afterFocus] = true;
18
+ window.dispatchEvent(event);
18
19
  }
19
20
 
20
21
  function setAttributeIfChanged(element, name, value, beforeWrite) {
@@ -170,11 +171,16 @@ var AttributeLease = class {
170
171
  }
171
172
  };
172
173
 
174
+ var HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
173
175
  function inheritsFieldsetDisabled(control) {
174
176
  let fieldset = control.closest("fieldset[disabled]");
175
177
  while (fieldset) {
176
- const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
177
- if (!legend?.contains(control)) return true;
178
+ if (fieldset.namespaceURI === HTML_NAMESPACE && fieldset.localName === "fieldset") {
179
+ const legend = Array.from(fieldset.children).find(
180
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "legend"
181
+ );
182
+ if (!legend?.contains(control)) return true;
183
+ }
178
184
  fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
179
185
  }
180
186
  return false;
@@ -232,11 +238,10 @@ function hasNativeTabStop(element) {
232
238
  if (element.tagName === "AUDIO" || element.tagName === "VIDEO") {
233
239
  return element.hasAttribute("controls");
234
240
  }
235
- if (element instanceof HTMLElement && element.tagName === "SUMMARY") {
236
- const details = element.parentElement;
237
- return details instanceof HTMLDetailsElement && Array.from(details.children).find((child) => child.tagName === "SUMMARY") === element;
238
- }
239
- return false;
241
+ const details = element.parentElement;
242
+ return details instanceof HTMLDetailsElement && Array.from(details.children).find(
243
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "summary"
244
+ ) === element;
240
245
  }
241
246
  function hasEditableTabStop(element) {
242
247
  const value = element.getAttribute("contenteditable")?.toLowerCase();
@@ -7,14 +7,15 @@ function fillTemplate(template, values) {
7
7
  });
8
8
  }
9
9
 
10
+ var afterFocus = /* @__PURE__ */ Symbol.for("stimeo--announcer:after-focus");
10
11
  function announce(message, options = {}) {
11
12
  const text = message.trim();
12
13
  if (text.length === 0) return;
13
- window.dispatchEvent(
14
- new CustomEvent("stimeo--announcer:announce", {
15
- detail: { message: text, assertive: options.assertive === true }
16
- })
17
- );
14
+ const event = new CustomEvent("stimeo--announcer:announce", {
15
+ detail: { message: text, assertive: options.assertive === true }
16
+ });
17
+ if (options.afterFocus) event[afterFocus] = true;
18
+ window.dispatchEvent(event);
18
19
  }
19
20
 
20
21
  function setAttributeIfChanged(element, name, value, beforeWrite) {
@@ -214,7 +214,50 @@ function isCompositionInputType(inputType) {
214
214
  return inputType !== void 0 && COMPOSITION_INPUT_TYPES.has(inputType);
215
215
  }
216
216
  var BEFORE_MORPH_ATTRIBUTE = "turbo:before-morph-attribute";
217
- var textOf = (element) => element.value;
217
+ var textOf = (element) => {
218
+ if ("value" in element) return element.value;
219
+ return element instanceof Element ? element.textContent : void 0;
220
+ };
221
+ var nodesOf = (element) => {
222
+ const nodes = [];
223
+ if ("value" in element || !(element instanceof Element)) return nodes;
224
+ const walker = element.ownerDocument.createTreeWalker(element);
225
+ for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) nodes.push(node);
226
+ return nodes;
227
+ };
228
+ var folded = (text) => text.replaceAll("\xA0", " ");
229
+ var startOf = (node, data, replaced, caret) => {
230
+ const text = folded(node.data);
231
+ const composed = folded(data);
232
+ if (replaced?.startContainer === node && text.startsWith(composed, replaced.startOffset)) {
233
+ return replaced.startOffset;
234
+ }
235
+ const start = text.lastIndexOf(composed, caret);
236
+ return start !== -1 && start + composed.length >= caret ? start : -1;
237
+ };
238
+ var spotOf = (element, replaced, data) => {
239
+ if ("value" in element || !(element instanceof Element)) return null;
240
+ const selection = element.ownerDocument.getSelection();
241
+ const node = selection?.focusNode;
242
+ if (!(node instanceof Text) || node.data === "" || !element.contains(node)) return null;
243
+ const range = element.ownerDocument.createRange();
244
+ const start = typeof data === "string" && data !== "" ? startOf(node, data, replaced, selection?.focusOffset ?? 0) : -1;
245
+ if (start === -1) {
246
+ range.selectNodeContents(node);
247
+ } else {
248
+ range.setStart(node, start);
249
+ range.setEnd(node, start + data.length);
250
+ }
251
+ return range;
252
+ };
253
+ var replacedBy = (event) => typeof event.getTargetRanges === "function" ? event.getTargetRanges()[0] ?? null : null;
254
+ var rewritten = ({ element, text, nodes, spot }) => {
255
+ if (spot !== null) {
256
+ return spot.collapsed || !(element instanceof Node && element.contains(spot.startContainer));
257
+ }
258
+ const current = nodesOf(element);
259
+ return textOf(element) !== text || current.length !== nodes.length || current.some((node, index) => node !== nodes[index]);
260
+ };
218
261
  var holdsFocus = (element) => element instanceof Node && element.getRootNode().activeElement === element;
219
262
  var CompositionTracker = class {
220
263
  #observedTargets = /* @__PURE__ */ new Set();
@@ -305,11 +348,11 @@ var CompositionTracker = class {
305
348
  }
306
349
  #settle() {
307
350
  const composition = this.#composition;
308
- if (composition === null) return;
309
- const { target, element, text } = composition;
351
+ if (composition === null || !this.#observedTargets.has(composition.target)) return;
352
+ const { target, element } = composition;
310
353
  if (target instanceof Node && element instanceof Node && !target.contains(element)) {
311
354
  this.#composition = null;
312
- } else if (textOf(element) !== text) {
355
+ } else if (rewritten(composition)) {
313
356
  this.#abandon(false);
314
357
  }
315
358
  }
@@ -334,7 +377,14 @@ var CompositionTracker = class {
334
377
  this.#owe(true);
335
378
  const target = event.currentTarget;
336
379
  const element = event.target;
337
- this.#composition = { target, element, text: textOf(element) };
380
+ this.#composition = {
381
+ target,
382
+ element,
383
+ text: textOf(element),
384
+ nodes: nodesOf(element),
385
+ spot: spotOf(element, null, void 0),
386
+ replaced: null
387
+ };
338
388
  this.#runAbandon();
339
389
  this.#onStart?.(event);
340
390
  };
@@ -358,6 +408,10 @@ var CompositionTracker = class {
358
408
  if (this.#composition !== null && !keyComposing && !isComposing && inputType && !isCompositionInputType(inputType)) {
359
409
  this.#abandon(true);
360
410
  }
411
+ const composition = this.#composition;
412
+ if (composition !== null && event.target === composition.element) {
413
+ composition.replaced = replacedBy(event);
414
+ }
361
415
  this.#runAbandon();
362
416
  };
363
417
  #handleInput = (event) => {
@@ -365,6 +419,15 @@ var CompositionTracker = class {
365
419
  const { isTrusted, isComposing, inputType } = event;
366
420
  if (composition !== null && event.target === composition.element && (isTrusted || isComposing || isCompositionInputType(inputType))) {
367
421
  composition.text = textOf(composition.element);
422
+ composition.nodes = nodesOf(composition.element);
423
+ if (isCompositionInputType(inputType)) {
424
+ composition.spot = spotOf(
425
+ composition.element,
426
+ composition.replaced,
427
+ event.data
428
+ );
429
+ }
430
+ composition.replaced = null;
368
431
  } else {
369
432
  this.#settle();
370
433
  }
@@ -385,7 +448,9 @@ var CompositionTracker = class {
385
448
  const composition = this.#composition;
386
449
  if (composition?.target !== event.currentTarget || event.target !== composition.element) return;
387
450
  const detail = event.detail;
388
- if (detail?.attributeName === "value") event.preventDefault();
451
+ if (detail?.attributeName === "value" && "value" in composition.element) {
452
+ event.preventDefault();
453
+ }
389
454
  };
390
455
  };
391
456
 
@@ -588,25 +653,22 @@ function snapSteppedValue(raw, range) {
588
653
  if (!(range.min <= range.max)) return finiteFallback(range.min, range.max);
589
654
  const input = Number.isNaN(raw) ? finiteFallback(range.min, range.max) : raw;
590
655
  const clamped = Math.min(range.max, Math.max(range.min, input));
591
- const step = effectiveStep(range.step);
592
- const base = stepBase(range);
656
+ const grid = stepGrid(range);
593
657
  const candidates = [];
594
658
  if (Number.isFinite(range.min)) candidates.push(range.min);
595
659
  if (Number.isFinite(range.max)) candidates.push(range.max);
596
- const gridPosition = (clamped - base) / step;
597
- if (Number.isFinite(gridPosition)) {
598
- addGridCandidate(candidates, Math.floor(gridPosition), range, base, step);
599
- addGridCandidate(candidates, Math.ceil(gridPosition), range, base, step);
660
+ const position = gridPosition(clamped, grid);
661
+ if (Number.isFinite(position)) {
662
+ for (const index of [Math.floor(position), Math.ceil(position)]) {
663
+ const candidate = gridValue(index, grid);
664
+ if (inRange(candidate, range)) candidates.push(candidate);
665
+ }
600
666
  }
601
667
  if (candidates.length === 0) return clamped;
602
668
  let nearest = candidates[0];
603
- let nearestDistance = Math.abs(clamped - nearest);
604
669
  for (const candidate of candidates.slice(1)) {
605
- const distance = Math.abs(clamped - candidate);
606
- if (distance < nearestDistance || nearlyEqual(distance, nearestDistance) && candidate > nearest) {
607
- nearest = candidate;
608
- nearestDistance = distance;
609
- }
670
+ const nearer = isMidpoint(clamped, candidate, nearest, range, grid) ? candidate > nearest : Math.abs(clamped - candidate) < Math.abs(clamped - nearest);
671
+ if (nearer) nearest = candidate;
610
672
  }
611
673
  return nearest;
612
674
  }
@@ -615,46 +677,36 @@ function stepSteppedValue(current, count, range) {
615
677
  const distance = Math.abs(Math.trunc(count));
616
678
  if (!Number.isFinite(distance) || distance === 0) return value;
617
679
  const direction = Math.sign(count);
618
- const adjacent = adjacentSteppedValue(value, direction, range);
680
+ const adjacent = nextGridValue(value, direction, range);
681
+ if (adjacent === value) return value;
619
682
  const raw = adjacent + direction * (distance - 1) * effectiveStep(range.step);
620
- return snapSteppedValue(raw, range);
683
+ const endpoint = direction > 0 ? range.max : range.min;
684
+ return Number.isFinite(raw) || Number.isFinite(endpoint) ? snapSteppedValue(raw, range) : value;
621
685
  }
622
- function adjacentSteppedValue(current, direction, range) {
686
+ function nextGridValue(current, direction, range) {
687
+ const grid = stepGrid(range);
688
+ const position = gridPosition(current, grid);
689
+ if (!Number.isFinite(position)) return current;
690
+ const index = direction > 0 ? Math.floor(position) + 1 : Math.ceil(position) - 1;
691
+ const first = gridValue(index, grid);
692
+ if (direction * (first - current) > 0 && !sameGridValue(first, current, grid.step)) {
693
+ return first;
694
+ }
695
+ const second = gridValue(index + direction, grid);
696
+ return direction * (second - current) > 0 ? second : current;
697
+ }
698
+ function stepGrid(range) {
623
699
  const step = effectiveStep(range.step);
624
700
  const base = stepBase(range);
625
- const candidates = [];
626
- if (direction > 0) {
627
- const position2 = (current - base) / step;
628
- if (Number.isFinite(position2)) {
629
- let gridIndex = Math.floor(position2) + 1;
630
- let candidate = cleanGridValue(base + gridIndex * step, base, step);
631
- if (candidate < current || nearlyEqual(candidate, current)) {
632
- gridIndex += 1;
633
- candidate = cleanGridValue(base + gridIndex * step, base, step);
634
- }
635
- if (candidate > current && !nearlyEqual(candidate, current) && within(candidate, range.min, range.max)) {
636
- candidates.push(clamp(candidate, range));
637
- }
638
- }
639
- return clamp(Math.min(...candidates), range);
640
- }
641
- const position = (current - base) / step;
642
- if (Number.isFinite(position)) {
643
- let gridIndex = Math.ceil(position) - 1;
644
- let candidate = cleanGridValue(base + gridIndex * step, base, step);
645
- if (candidate > current || nearlyEqual(candidate, current)) {
646
- gridIndex -= 1;
647
- candidate = cleanGridValue(base + gridIndex * step, base, step);
648
- }
649
- if (candidate < current && !nearlyEqual(candidate, current) && within(candidate, range.min, range.max)) {
650
- candidates.push(clamp(candidate, range));
651
- }
652
- }
653
- return clamp(Math.max(...candidates), range);
701
+ const near = base - Math.trunc(base / step) * step;
702
+ const origin = cleanGridValue(Number.isFinite(near) ? near % step : base % step, base, step);
703
+ return { step, base, origin };
654
704
  }
655
- function addGridCandidate(candidates, index, range, base, step) {
656
- const candidate = cleanGridValue(base + index * step, base, step);
657
- if (within(candidate, range.min, range.max)) candidates.push(clamp(candidate, range));
705
+ function gridPosition(value, grid) {
706
+ return value / grid.step - grid.origin / grid.step;
707
+ }
708
+ function gridValue(index, grid) {
709
+ return cleanGridValue(grid.origin + index * grid.step, grid.base, grid.step);
658
710
  }
659
711
  function stepBase(range) {
660
712
  if (range.base !== void 0 && Number.isFinite(range.base)) return range.base;
@@ -670,15 +722,46 @@ function decimalPlaces(value) {
670
722
  const exponent = exponentText === void 0 ? 0 : Number(exponentText);
671
723
  return Math.max(0, fractionLength - exponent);
672
724
  }
673
- function within(value, min, max) {
674
- return (value > min || nearlyEqual(value, min)) && (value < max || nearlyEqual(value, max));
725
+ function inRange(value, range) {
726
+ return Number.isFinite(value) && value >= range.min && value <= range.max;
727
+ }
728
+ function sameGridValue(left, right, step) {
729
+ const rounding = Number.EPSILON * Math.max(Math.abs(left), Math.abs(right));
730
+ return Math.abs(left - right) <= Math.min(rounding, step / 4);
731
+ }
732
+ function isMidpoint(value, left, right, range, grid) {
733
+ const imbalance = Math.abs(Math.abs(value - left) - Math.abs(value - right));
734
+ const rounding = 4 * Number.EPSILON * Math.max(Math.abs(value), Math.abs(left), Math.abs(right));
735
+ if (!(imbalance <= rounding)) return false;
736
+ if (imbalance <= Math.abs(left - right) / 4) return true;
737
+ const lower = Math.min(left, right);
738
+ const upper = Math.max(left, right);
739
+ return value > lower && value < upper && isDecimalMidpoint(value, lower, upper, range, grid);
675
740
  }
676
- function clamp(value, range) {
677
- return Math.min(range.max, Math.max(range.min, value));
741
+ function isDecimalMidpoint(value, lower, upper, range, grid) {
742
+ const raw = decimalParts(value);
743
+ const base = decimalParts(grid.base);
744
+ const step = decimalParts(grid.step);
745
+ const min = decimalParts(lower);
746
+ const max = decimalParts(upper);
747
+ const place = Math.min(raw.place, base.place, step.place, min.place, max.place);
748
+ const units = (part) => part.units * 10n ** BigInt(part.place - place);
749
+ const origin = units(base);
750
+ const stride = units(step);
751
+ const rawUnits = units(raw);
752
+ const remainder = (rawUnits - origin) % stride;
753
+ const below = rawUnits - (remainder < 0n ? remainder + stride : remainder);
754
+ const above = below + stride;
755
+ const minUnits = units(min);
756
+ const maxUnits = units(max);
757
+ const lowerUnits = lower === range.min && minUnits > below ? minUnits : below;
758
+ const upperUnits = upper === range.max && maxUnits < above ? maxUnits : above;
759
+ return Number(`${lowerUnits}e${place}`) === lower && Number(`${upperUnits}e${place}`) === upper && Number(`${(lowerUnits + upperUnits) * 5n}e${place - 1}`) === value;
678
760
  }
679
- function nearlyEqual(left, right) {
680
- const scale = Math.max(Math.abs(left), Math.abs(right));
681
- return Number.isFinite(left) && Number.isFinite(right) && Math.abs(left - right) <= Number.EPSILON * scale;
761
+ function decimalParts(value) {
762
+ const [coefficient = "", exponent = "0"] = value.toString().split("e");
763
+ const [whole = "", fraction = ""] = coefficient.split(".");
764
+ return { units: BigInt(whole + fraction), place: Number(exponent) - fraction.length };
682
765
  }
683
766
  function finiteFallback(min, max) {
684
767
  if (Number.isFinite(min)) return min;
@@ -877,16 +960,17 @@ var NumberInputController = class _NumberInputController extends Controller {
877
960
  next = this.#safePageStep > 0 ? this.#currentValue() - this.#safePageStep : stepSteppedValue(this.#currentValue(), -10, this.#steppedRange);
878
961
  break;
879
962
  case "Home":
880
- if (!Number.isFinite(this.#safeMin)) return;
881
963
  next = this.#safeMin;
882
964
  break;
883
965
  case "End":
884
- if (!Number.isFinite(this.#safeMax)) return;
885
966
  next = this.#safeMax;
886
967
  break;
887
968
  default:
888
969
  return;
889
970
  }
971
+ if (next === Number.POSITIVE_INFINITY) next = this.#safeMax;
972
+ if (next === Number.NEGATIVE_INFINITY) next = this.#safeMin;
973
+ if (!Number.isFinite(next)) return;
890
974
  event.preventDefault();
891
975
  this.#commit(next, true);
892
976
  }
@@ -1074,12 +1158,16 @@ var NumberInputController = class _NumberInputController extends Controller {
1074
1158
  }
1075
1159
  #updateButtons(value, range) {
1076
1160
  const move = this.#move;
1077
- for (const button of this.incrementTargets) {
1078
- this.#toggleButton(button, value < range.max, move);
1161
+ const increments = this.incrementTargets;
1162
+ const canIncrement = increments.length > 0 && value < range.max && stepSteppedValue(value, 1, range) > value;
1163
+ for (const button of increments) {
1164
+ this.#toggleButton(button, canIncrement, move);
1079
1165
  if (!this.#moves.isLatest(move)) return;
1080
1166
  }
1081
- for (const button of this.decrementTargets) {
1082
- this.#toggleButton(button, value > range.min, move);
1167
+ const decrements = this.decrementTargets;
1168
+ const canDecrement = decrements.length > 0 && value > range.min && stepSteppedValue(value, -1, range) < value;
1169
+ for (const button of decrements) {
1170
+ this.#toggleButton(button, canDecrement, move);
1083
1171
  if (!this.#moves.isLatest(move)) return;
1084
1172
  }
1085
1173
  }