stimeo-ui 0.18.1 → 0.19.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.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +54 -0
  3. data/dist/cable/index.js +6 -5
  4. data/dist/controllers/alert_dialog_controller.js +324 -76
  5. data/dist/controllers/announcer_controller.js +22 -9
  6. data/dist/controllers/auto_submit_controller.js +103 -16
  7. data/dist/controllers/avatar_controller.js +29 -9
  8. data/dist/controllers/breadcrumb_controller.js +205 -17
  9. data/dist/controllers/bulk_select_controller.js +31 -13
  10. data/dist/controllers/calendar_controller.js +242 -7
  11. data/dist/controllers/carousel_controller.js +70 -15
  12. data/dist/controllers/character_counter_controller.js +103 -16
  13. data/dist/controllers/clipboard_controller.js +6 -5
  14. data/dist/controllers/collapsible_controller.js +35 -8
  15. data/dist/controllers/combobox_controller.js +155 -26
  16. data/dist/controllers/command_palette_controller.js +445 -117
  17. data/dist/controllers/conditional_fields_controller.js +84 -44
  18. data/dist/controllers/confirm_controller.js +303 -68
  19. data/dist/controllers/context_menu_controller.js +59 -30
  20. data/dist/controllers/countdown_controller.js +6 -5
  21. data/dist/controllers/currency_input_controller.js +72 -7
  22. data/dist/controllers/data_grid_controller.js +273 -51
  23. data/dist/controllers/date_range_picker_controller.js +27 -5
  24. data/dist/controllers/dialog_controller.js +330 -79
  25. data/dist/controllers/direct_upload_controller.js +6 -5
  26. data/dist/controllers/dismissible_controller.js +68 -18
  27. data/dist/controllers/drawer_controller.js +362 -92
  28. data/dist/controllers/dropdown_controller.js +64 -52
  29. data/dist/controllers/editable_controller.js +140 -12
  30. data/dist/controllers/empty_state_controller.js +193 -9
  31. data/dist/controllers/file_dropzone_controller.js +313 -23
  32. data/dist/controllers/filter_controller.js +194 -5
  33. data/dist/controllers/flash_controller.js +205 -17
  34. data/dist/controllers/focus_controller.js +273 -57
  35. data/dist/controllers/form_field_controller.js +71 -14
  36. data/dist/controllers/form_validation_controller.js +11 -7
  37. data/dist/controllers/frame_loading_controller.js +75 -20
  38. data/dist/controllers/hover_card_controller.js +108 -39
  39. data/dist/controllers/input_mask_controller.js +95 -37
  40. data/dist/controllers/intersection_controller.js +61 -29
  41. data/dist/controllers/lazy_frame_controller.js +61 -29
  42. data/dist/controllers/listbox_controller.js +174 -83
  43. data/dist/controllers/local_time_controller.js +1 -7
  44. data/dist/controllers/menu_controller.js +45 -24
  45. data/dist/controllers/menubar_controller.js +232 -52
  46. data/dist/controllers/meter_controller.js +31 -9
  47. data/dist/controllers/multi_select_controller.js +195 -62
  48. data/dist/controllers/navigation_menu_controller.js +1 -1
  49. data/dist/controllers/nested_form_controller.js +275 -21
  50. data/dist/controllers/network_status_controller.js +31 -9
  51. data/dist/controllers/number_input_controller.js +180 -71
  52. data/dist/controllers/otp_controller.js +97 -11
  53. data/dist/controllers/overflow_menu_controller.js +190 -24
  54. data/dist/controllers/pagination_controller.js +186 -24
  55. data/dist/controllers/password_reveal_controller.js +25 -4
  56. data/dist/controllers/password_strength_controller.js +6 -5
  57. data/dist/controllers/popover_controller.js +466 -42
  58. data/dist/controllers/progress_controller.js +31 -9
  59. data/dist/controllers/radio_group_controller.js +279 -88
  60. data/dist/controllers/range_slider_controller.js +72 -54
  61. data/dist/controllers/rating_controller.js +58 -10
  62. data/dist/controllers/read_more_controller.js +191 -25
  63. data/dist/controllers/relative_time_controller.js +1 -7
  64. data/dist/controllers/roving_controller.js +204 -8
  65. data/dist/controllers/scroll_area_controller.js +61 -19
  66. data/dist/controllers/scroll_visibility_controller.js +25 -4
  67. data/dist/controllers/scrollspy_controller.js +240 -32
  68. data/dist/controllers/separator_controller.js +97 -58
  69. data/dist/controllers/sidebar_controller.js +368 -127
  70. data/dist/controllers/skeleton_controller.js +75 -20
  71. data/dist/controllers/slider_controller.js +72 -54
  72. data/dist/controllers/sortable_controller.js +6 -5
  73. data/dist/controllers/spinner_controller.js +75 -20
  74. data/dist/controllers/step_indicator_controller.js +181 -6
  75. data/dist/controllers/stepper_controller.js +188 -6
  76. data/dist/controllers/sticky_observer_controller.js +86 -33
  77. data/dist/controllers/submit_once_controller.js +31 -9
  78. data/dist/controllers/switch_controller.js +84 -44
  79. data/dist/controllers/tabs_controller.js +189 -3
  80. data/dist/controllers/tags_input_controller.js +293 -29
  81. data/dist/controllers/theme_controller.js +192 -5
  82. data/dist/controllers/time_picker_controller.js +77 -42
  83. data/dist/controllers/toast_controller.js +478 -16
  84. data/dist/controllers/toggle_group_controller.js +282 -79
  85. data/dist/controllers/toolbar_controller.js +276 -15
  86. data/dist/controllers/tooltip_controller.js +244 -46
  87. data/dist/controllers/tree_view_controller.js +287 -57
  88. data/dist/index.js +2258 -1016
  89. data/lib/stimeo/ui/version.rb +1 -1
  90. metadata +2 -2
@@ -22,6 +22,180 @@ function actionSource(source) {
22
22
  };
23
23
  }
24
24
 
25
+ function setAttributeIfChanged(element, name, value, beforeWrite) {
26
+ if (element.getAttribute(name) === value) return false;
27
+ beforeWrite?.();
28
+ if (value === null) element.removeAttribute(name);
29
+ else element.setAttribute(name, value);
30
+ return true;
31
+ }
32
+
33
+ function sharedRegistry(key, create) {
34
+ const symbol = Symbol.for(key);
35
+ const scope = globalThis;
36
+ const existing = scope[symbol];
37
+ if (existing !== void 0) return existing;
38
+ const registry2 = create();
39
+ Object.defineProperty(globalThis, symbol, { value: registry2 });
40
+ return registry2;
41
+ }
42
+
43
+ var registry = sharedRegistry(
44
+ "stimeo-ui.attribute-lease.registry.v1",
45
+ () => ({ stacks: /* @__PURE__ */ new WeakMap(), names: /* @__PURE__ */ new Map() })
46
+ );
47
+ function isAttributeValue(value) {
48
+ return value === null || typeof value === "string";
49
+ }
50
+ function readLeaseRecord(element, record) {
51
+ try {
52
+ const parsed = JSON.parse(element.getAttribute(record) ?? "null");
53
+ if (!Array.isArray(parsed)) return void 0;
54
+ const [original, written, ...rest] = parsed;
55
+ const linked = rest[0] !== false;
56
+ const beneath = linked ? rest : rest.slice(1);
57
+ if (!beneath.every((under) => typeof under === "string")) return void 0;
58
+ return isAttributeValue(original) && isAttributeValue(written) ? { original, written, linked, beneath } : void 0;
59
+ } catch {
60
+ return void 0;
61
+ }
62
+ }
63
+ function attributeStack(element, attribute) {
64
+ const byAttribute = registry.stacks.get(element) ?? /* @__PURE__ */ new Map();
65
+ registry.stacks.set(element, byAttribute);
66
+ const known = byAttribute.get(attribute);
67
+ if (known) return known;
68
+ const found = [];
69
+ for (const record of registry.names.get(attribute) ?? []) {
70
+ const read = readLeaseRecord(element, record);
71
+ if (read) found.push({ ...read, record });
72
+ }
73
+ const depth = ({ beneath }) => beneath.filter((name) => found.some((other) => other.record === name)).length;
74
+ const stack = found.sort((one, other) => depth(one) - depth(other)).map(({ record, written, original, linked }, index) => ({
75
+ lease: null,
76
+ record,
77
+ written,
78
+ original,
79
+ linked: linked && index > 0
80
+ }));
81
+ byAttribute.set(attribute, stack);
82
+ return stack;
83
+ }
84
+ function findHold(stack, lease, record) {
85
+ return stack.find((held) => held.lease === lease) ?? stack.find((held) => held.lease === null && held.record === record);
86
+ }
87
+ var AttributeLease = class {
88
+ #attribute;
89
+ #record;
90
+ #base;
91
+ #preserveAuthor;
92
+ #held = /* @__PURE__ */ new Set();
93
+ constructor(attribute, owner, options = {}) {
94
+ this.#attribute = attribute;
95
+ this.#record = `data-${owner}-${attribute}-lease`;
96
+ this.#base = options.base === true;
97
+ this.#preserveAuthor = options.preserveAuthor === true;
98
+ const names = registry.names;
99
+ names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
100
+ }
101
+ write(element, value, beforeWrite) {
102
+ const stack = attributeStack(element, this.#attribute);
103
+ const hold = this.#take(stack) ?? this.#acquire(element, stack);
104
+ this.#held.add(element);
105
+ const shows = !this.#covered(element, stack, hold);
106
+ hold.written = value;
107
+ this.#mark(element, stack);
108
+ if (shows) this.#reflect(element, value, beforeWrite);
109
+ }
110
+ adopt(element, options = {}) {
111
+ const known = registry.stacks.get(element)?.has(this.#attribute);
112
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
113
+ const stack = attributeStack(element, this.#attribute);
114
+ const hold = this.#take(stack);
115
+ if (!hold) return false;
116
+ const current = element.getAttribute(this.#attribute);
117
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
118
+ return false;
119
+ }
120
+ this.#held.add(element);
121
+ hold.original = current;
122
+ hold.linked = false;
123
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
124
+ if (!above.linked) break;
125
+ above.original = current;
126
+ }
127
+ this.#mark(element, stack);
128
+ return true;
129
+ }
130
+ return(element) {
131
+ const held = this.#held.delete(element);
132
+ const known = registry.stacks.get(element)?.has(this.#attribute);
133
+ const stack = held || known || element.hasAttribute(this.#record) ? attributeStack(element, this.#attribute) : [];
134
+ const hold = this.#take(stack);
135
+ if (!hold) return false;
136
+ const carries = element.getAttribute(this.#attribute) === hold.written;
137
+ const owns = carries && !this.#covered(element, stack, hold);
138
+ const index = stack.indexOf(hold);
139
+ stack.splice(index, 1);
140
+ const above = stack[index];
141
+ if (above) above.linked &&= hold.linked;
142
+ this.#mark(element, stack);
143
+ if (!owns) return true;
144
+ const below = hold.linked ? stack[index - 1] : void 0;
145
+ this.#reflect(element, below ? below.written : hold.original);
146
+ return true;
147
+ }
148
+ returnAll() {
149
+ for (const element of Array.from(this.#held)) this.return(element);
150
+ }
151
+ #take(stack) {
152
+ const hold = findHold(stack, this, this.#record);
153
+ if (hold) hold.lease = this;
154
+ return hold;
155
+ }
156
+ #acquire(element, stack) {
157
+ const current = element.getAttribute(this.#attribute);
158
+ const top = stack.at(-1);
159
+ const bottom = stack[0];
160
+ const over = top?.written === current ? top : void 0;
161
+ const from = this.#base ? this.#preserveAuthor && !stack.some((held) => held.written === current) ? void 0 : bottom : over;
162
+ const hold = {
163
+ lease: this,
164
+ record: this.#record,
165
+ written: current,
166
+ original: from ? from.original : current,
167
+ linked: !this.#base && over !== void 0
168
+ };
169
+ if (this.#base && bottom) {
170
+ bottom.linked = true;
171
+ stack.unshift(hold);
172
+ } else stack.push(hold);
173
+ return hold;
174
+ }
175
+ #covered(element, stack, hold) {
176
+ const current = element.getAttribute(this.#attribute);
177
+ return stack.slice(stack.indexOf(hold) + 1).some((above) => above.written === current);
178
+ }
179
+ #mark(element, stack) {
180
+ const records = /* @__PURE__ */ new Map([[this.#record, null]]);
181
+ stack.forEach((hold, index) => {
182
+ const names = stack.slice(0, index).map((under) => under.record);
183
+ const beneath = [...new Set(names.reverse())].filter((name) => name !== hold.record);
184
+ const marker = hold.linked || beneath.length === 0 ? [] : [false];
185
+ records.set(
186
+ hold.record,
187
+ hold.written === hold.original && beneath.length === 0 && index === stack.length - 1 ? null : JSON.stringify([hold.original, hold.written, ...marker, ...beneath])
188
+ );
189
+ });
190
+ for (const [name, recorded] of records) {
191
+ setAttributeIfChanged(element, name, recorded);
192
+ }
193
+ }
194
+ #reflect(element, value, beforeWrite) {
195
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
196
+ }
197
+ };
198
+
25
199
  function parseDeclared(raw, parse, fallback) {
26
200
  try {
27
201
  return parse(raw);
@@ -63,70 +237,102 @@ function queryRoot(selector) {
63
237
  }
64
238
  var IntersectionWatcher = class {
65
239
  #onEntries;
66
- #observer = null;
67
- #active = false;
68
- #usingPlatformDefaults = false;
240
+ #session = null;
69
241
  constructor(onEntries) {
70
242
  this.#onEntries = onEntries;
71
243
  }
72
244
  get active() {
73
- return this.#active;
245
+ return this.#session?.active ?? false;
74
246
  }
75
247
  get usingPlatformDefaults() {
76
- return this.#usingPlatformDefaults;
248
+ return this.#session?.usingPlatformDefaults ?? false;
77
249
  }
78
250
  start(targets, options = {}) {
79
- this.stop();
80
- if (typeof IntersectionObserver === "undefined") return false;
81
- const list = Array.isArray(targets) ? targets : [targets];
82
- if (list.length === 0) return false;
83
- const root = "root" in options ? options.root ?? null : queryRoot(options.rootSelector);
251
+ const previous = this.#session;
252
+ const session = {
253
+ observer: null,
254
+ active: false,
255
+ usingPlatformDefaults: false
256
+ };
257
+ this.#session = session;
84
258
  let observer = null;
85
259
  try {
260
+ if (previous) this.#release(previous);
261
+ if (this.#session !== session) return false;
262
+ if (typeof IntersectionObserver === "undefined") return false;
263
+ const list = Array.isArray(targets) ? targets : [targets];
264
+ const empty = list.length === 0;
265
+ if (this.#session !== session) return false;
266
+ if (empty) return false;
267
+ const root = "root" in options ? options.root ?? null : queryRoot(options.rootSelector);
268
+ if (this.#session !== session) return false;
86
269
  const onEntries = (entries) => {
87
- if (this.#active && this.#observer === observer) this.#onEntries(entries);
270
+ if (this.#session === session && session.active) this.#onEntries(entries);
88
271
  };
272
+ let usingPlatformDefaults = false;
89
273
  try {
90
- observer = new IntersectionObserver(onEntries, {
274
+ const configured = {
91
275
  root,
92
276
  rootMargin: options.rootMargin,
93
277
  threshold: options.threshold
94
- });
278
+ };
279
+ if (this.#session !== session) return false;
280
+ observer = new IntersectionObserver(onEntries, configured);
95
281
  } catch (error) {
282
+ if (this.#session !== session) return false;
96
283
  console.warn(
97
284
  "Stimeo UI: IntersectionObserver could not be constructed with the configured options; retrying with platform defaults.",
98
285
  error
99
286
  );
287
+ if (this.#session !== session) return false;
100
288
  observer = new IntersectionObserver(onEntries, { root });
101
- this.#usingPlatformDefaults = true;
289
+ usingPlatformDefaults = true;
290
+ }
291
+ if (this.#session !== session) {
292
+ this.#release(session, observer);
293
+ return false;
294
+ }
295
+ session.observer = observer;
296
+ for (const target of list) {
297
+ if (this.#session !== session) return false;
298
+ observer.observe(target);
299
+ if (this.#session !== session) {
300
+ this.#release(session, observer);
301
+ return false;
302
+ }
102
303
  }
103
- for (const target of list) observer.observe(target);
104
- this.#observer = observer;
105
- this.#active = true;
304
+ if (this.#session !== session) return false;
305
+ session.usingPlatformDefaults = usingPlatformDefaults;
306
+ session.active = true;
106
307
  return true;
107
308
  } catch (error) {
108
- observer?.disconnect();
109
- this.#observer = null;
110
- this.#active = false;
111
- this.#usingPlatformDefaults = false;
309
+ this.#release(session, observer);
112
310
  throw error;
113
311
  }
114
312
  }
115
313
  rearm(target) {
116
- if (!this.#observer) return;
314
+ const session = this.#session;
315
+ const observer = session?.observer;
316
+ if (!session?.active || !observer) return;
117
317
  try {
118
- this.#observer.unobserve(target);
119
- this.#observer.observe(target);
318
+ observer.unobserve(target);
319
+ if (this.#session !== session) return;
320
+ observer.observe(target);
321
+ if (this.#session !== session) this.#release(session, observer);
120
322
  } catch (error) {
121
- this.stop();
323
+ if (this.#session === session) this.#session = null;
324
+ this.#release(session, observer);
122
325
  throw error;
123
326
  }
124
327
  }
125
328
  stop() {
126
- this.#active = false;
127
- this.#observer?.disconnect();
128
- this.#observer = null;
129
- this.#usingPlatformDefaults = false;
329
+ const session = this.#session;
330
+ this.#session = null;
331
+ if (session) this.#release(session);
332
+ }
333
+ #release(session, observer = session.observer) {
334
+ session.observer = null;
335
+ observer?.disconnect();
130
336
  }
131
337
  };
132
338
 
@@ -279,6 +485,7 @@ var ScrollspyController = class _ScrollspyController extends Controller {
279
485
  return this.#safeOffset;
280
486
  }
281
487
  #watcher = new IntersectionWatcher((entries) => this.#onIntersection(entries));
488
+ #current = new AttributeLease("aria-current", this.identifier);
282
489
  #isConnected = false;
283
490
  #intersectionStates = /* @__PURE__ */ new Map();
284
491
  #activeSectionId = "";
@@ -328,7 +535,8 @@ var ScrollspyController = class _ScrollspyController extends Controller {
328
535
  linkTargetConnected() {
329
536
  if (this.#isConnected) this.#scheduleResync();
330
537
  }
331
- linkTargetDisconnected() {
538
+ linkTargetDisconnected(link) {
539
+ if (!this.linkTargets.includes(link)) this.#current.return(link);
332
540
  if (this.#isConnected) this.#scheduleResync();
333
541
  }
334
542
  #scheduleResync() {
@@ -477,9 +685,9 @@ var ScrollspyController = class _ScrollspyController extends Controller {
477
685
  );
478
686
  for (const link of this.linkTargets) {
479
687
  if (activeLinks.includes(link)) {
480
- link.setAttribute("aria-current", "location");
688
+ this.#current.write(link, "location");
481
689
  } else if (link.getAttribute("aria-current") === "location") {
482
- link.removeAttribute("aria-current");
690
+ this.#current.write(link, null);
483
691
  }
484
692
  }
485
693
  if (!announce) return;
@@ -10,6 +10,7 @@ function hasModifierChord(event) {
10
10
 
11
11
  function setAttributeIfChanged(element, name, value, beforeWrite) {
12
12
  if (element.getAttribute(name) === value) return false;
13
+ beforeWrite?.();
13
14
  if (value === null) element.removeAttribute(name);
14
15
  else element.setAttribute(name, value);
15
16
  return true;
@@ -83,14 +84,34 @@ var AttributeLease = class {
83
84
  const names = registry.names;
84
85
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
85
86
  }
86
- write(element, value) {
87
+ write(element, value, beforeWrite) {
87
88
  const stack = attributeStack(element, this.#attribute);
88
89
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
89
90
  this.#held.add(element);
90
91
  const shows = !this.#covered(element, stack, hold);
91
92
  hold.written = value;
92
93
  this.#mark(element, stack);
93
- if (shows) this.#reflect(element, value);
94
+ if (shows) this.#reflect(element, value, beforeWrite);
95
+ }
96
+ adopt(element, options = {}) {
97
+ const known = registry.stacks.get(element)?.has(this.#attribute);
98
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
99
+ const stack = attributeStack(element, this.#attribute);
100
+ const hold = this.#take(stack);
101
+ if (!hold) return false;
102
+ const current = element.getAttribute(this.#attribute);
103
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
104
+ return false;
105
+ }
106
+ this.#held.add(element);
107
+ hold.original = current;
108
+ hold.linked = false;
109
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
110
+ if (!above.linked) break;
111
+ above.original = current;
112
+ }
113
+ this.#mark(element, stack);
114
+ return true;
94
115
  }
95
116
  return(element) {
96
117
  const held = this.#held.delete(element);
@@ -156,8 +177,8 @@ var AttributeLease = class {
156
177
  setAttributeIfChanged(element, name, recorded);
157
178
  }
158
179
  }
159
- #reflect(element, value) {
160
- setAttributeIfChanged(element, this.#attribute, value);
180
+ #reflect(element, value, beforeWrite) {
181
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
161
182
  }
162
183
  };
163
184
 
@@ -283,25 +304,22 @@ function snapSteppedValue(raw, range) {
283
304
  if (!(range.min <= range.max)) return finiteFallback(range.min, range.max);
284
305
  const input = Number.isNaN(raw) ? finiteFallback(range.min, range.max) : raw;
285
306
  const clamped = Math.min(range.max, Math.max(range.min, input));
286
- const step = effectiveStep(range.step);
287
- const base = stepBase(range);
307
+ const grid = stepGrid(range);
288
308
  const candidates = [];
289
309
  if (Number.isFinite(range.min)) candidates.push(range.min);
290
310
  if (Number.isFinite(range.max)) candidates.push(range.max);
291
- const gridPosition = (clamped - base) / step;
292
- if (Number.isFinite(gridPosition)) {
293
- addGridCandidate(candidates, Math.floor(gridPosition), range, base, step);
294
- addGridCandidate(candidates, Math.ceil(gridPosition), range, base, step);
311
+ const position = gridPosition(clamped, grid);
312
+ if (Number.isFinite(position)) {
313
+ for (const index of [Math.floor(position), Math.ceil(position)]) {
314
+ const candidate = gridValue(index, grid);
315
+ if (inRange(candidate, range)) candidates.push(candidate);
316
+ }
295
317
  }
296
318
  if (candidates.length === 0) return clamped;
297
319
  let nearest = candidates[0];
298
- let nearestDistance = Math.abs(clamped - nearest);
299
320
  for (const candidate of candidates.slice(1)) {
300
- const distance = Math.abs(clamped - candidate);
301
- if (distance < nearestDistance || nearlyEqual(distance, nearestDistance) && candidate > nearest) {
302
- nearest = candidate;
303
- nearestDistance = distance;
304
- }
321
+ const nearer = isMidpoint(clamped, candidate, nearest, range, grid) ? candidate > nearest : Math.abs(clamped - candidate) < Math.abs(clamped - nearest);
322
+ if (nearer) nearest = candidate;
305
323
  }
306
324
  return nearest;
307
325
  }
@@ -310,46 +328,36 @@ function stepSteppedValue(current, count, range) {
310
328
  const distance = Math.abs(Math.trunc(count));
311
329
  if (!Number.isFinite(distance) || distance === 0) return value;
312
330
  const direction = Math.sign(count);
313
- const adjacent = adjacentSteppedValue(value, direction, range);
331
+ const adjacent = nextGridValue(value, direction, range);
332
+ if (adjacent === value) return value;
314
333
  const raw = adjacent + direction * (distance - 1) * effectiveStep(range.step);
315
- return snapSteppedValue(raw, range);
334
+ const endpoint = direction > 0 ? range.max : range.min;
335
+ return Number.isFinite(raw) || Number.isFinite(endpoint) ? snapSteppedValue(raw, range) : value;
336
+ }
337
+ function nextGridValue(current, direction, range) {
338
+ const grid = stepGrid(range);
339
+ const position = gridPosition(current, grid);
340
+ if (!Number.isFinite(position)) return current;
341
+ const index = direction > 0 ? Math.floor(position) + 1 : Math.ceil(position) - 1;
342
+ const first = gridValue(index, grid);
343
+ if (direction * (first - current) > 0 && !sameGridValue(first, current, grid.step)) {
344
+ return first;
345
+ }
346
+ const second = gridValue(index + direction, grid);
347
+ return direction * (second - current) > 0 ? second : current;
316
348
  }
317
- function adjacentSteppedValue(current, direction, range) {
349
+ function stepGrid(range) {
318
350
  const step = effectiveStep(range.step);
319
351
  const base = stepBase(range);
320
- const candidates = [];
321
- if (direction > 0) {
322
- const position2 = (current - base) / step;
323
- if (Number.isFinite(position2)) {
324
- let gridIndex = Math.floor(position2) + 1;
325
- let candidate = cleanGridValue(base + gridIndex * step, base, step);
326
- if (candidate < current || nearlyEqual(candidate, current)) {
327
- gridIndex += 1;
328
- candidate = cleanGridValue(base + gridIndex * step, base, step);
329
- }
330
- if (candidate > current && !nearlyEqual(candidate, current) && within(candidate, range.min, range.max)) {
331
- candidates.push(clamp(candidate, range));
332
- }
333
- }
334
- return clamp(Math.min(...candidates), range);
335
- }
336
- const position = (current - base) / step;
337
- if (Number.isFinite(position)) {
338
- let gridIndex = Math.ceil(position) - 1;
339
- let candidate = cleanGridValue(base + gridIndex * step, base, step);
340
- if (candidate > current || nearlyEqual(candidate, current)) {
341
- gridIndex -= 1;
342
- candidate = cleanGridValue(base + gridIndex * step, base, step);
343
- }
344
- if (candidate < current && !nearlyEqual(candidate, current) && within(candidate, range.min, range.max)) {
345
- candidates.push(clamp(candidate, range));
346
- }
347
- }
348
- return clamp(Math.max(...candidates), range);
352
+ const near = base - Math.trunc(base / step) * step;
353
+ const origin = cleanGridValue(Number.isFinite(near) ? near % step : base % step, base, step);
354
+ return { step, base, origin };
349
355
  }
350
- function addGridCandidate(candidates, index, range, base, step) {
351
- const candidate = cleanGridValue(base + index * step, base, step);
352
- if (within(candidate, range.min, range.max)) candidates.push(clamp(candidate, range));
356
+ function gridPosition(value, grid) {
357
+ return value / grid.step - grid.origin / grid.step;
358
+ }
359
+ function gridValue(index, grid) {
360
+ return cleanGridValue(grid.origin + index * grid.step, grid.base, grid.step);
353
361
  }
354
362
  function stepBase(range) {
355
363
  if (range.base !== void 0 && Number.isFinite(range.base)) return range.base;
@@ -365,15 +373,46 @@ function decimalPlaces(value) {
365
373
  const exponent = exponentText === void 0 ? 0 : Number(exponentText);
366
374
  return Math.max(0, fractionLength - exponent);
367
375
  }
368
- function within(value, min, max) {
369
- return (value > min || nearlyEqual(value, min)) && (value < max || nearlyEqual(value, max));
376
+ function inRange(value, range) {
377
+ return Number.isFinite(value) && value >= range.min && value <= range.max;
378
+ }
379
+ function sameGridValue(left, right, step) {
380
+ const rounding = Number.EPSILON * Math.max(Math.abs(left), Math.abs(right));
381
+ return Math.abs(left - right) <= Math.min(rounding, step / 4);
382
+ }
383
+ function isMidpoint(value, left, right, range, grid) {
384
+ const imbalance = Math.abs(Math.abs(value - left) - Math.abs(value - right));
385
+ const rounding = 4 * Number.EPSILON * Math.max(Math.abs(value), Math.abs(left), Math.abs(right));
386
+ if (!(imbalance <= rounding)) return false;
387
+ if (imbalance <= Math.abs(left - right) / 4) return true;
388
+ const lower = Math.min(left, right);
389
+ const upper = Math.max(left, right);
390
+ return value > lower && value < upper && isDecimalMidpoint(value, lower, upper, range, grid);
370
391
  }
371
- function clamp(value, range) {
372
- return Math.min(range.max, Math.max(range.min, value));
392
+ function isDecimalMidpoint(value, lower, upper, range, grid) {
393
+ const raw = decimalParts(value);
394
+ const base = decimalParts(grid.base);
395
+ const step = decimalParts(grid.step);
396
+ const min = decimalParts(lower);
397
+ const max = decimalParts(upper);
398
+ const place = Math.min(raw.place, base.place, step.place, min.place, max.place);
399
+ const units = (part) => part.units * 10n ** BigInt(part.place - place);
400
+ const origin = units(base);
401
+ const stride = units(step);
402
+ const rawUnits = units(raw);
403
+ const remainder = (rawUnits - origin) % stride;
404
+ const below = rawUnits - (remainder < 0n ? remainder + stride : remainder);
405
+ const above = below + stride;
406
+ const minUnits = units(min);
407
+ const maxUnits = units(max);
408
+ const lowerUnits = lower === range.min && minUnits > below ? minUnits : below;
409
+ const upperUnits = upper === range.max && maxUnits < above ? maxUnits : above;
410
+ return Number(`${lowerUnits}e${place}`) === lower && Number(`${upperUnits}e${place}`) === upper && Number(`${(lowerUnits + upperUnits) * 5n}e${place - 1}`) === value;
373
411
  }
374
- function nearlyEqual(left, right) {
375
- const scale = Math.max(Math.abs(left), Math.abs(right));
376
- return Number.isFinite(left) && Number.isFinite(right) && Math.abs(left - right) <= Number.EPSILON * scale;
412
+ function decimalParts(value) {
413
+ const [coefficient = "", exponent = "0"] = value.toString().split("e");
414
+ const [whole = "", fraction = ""] = coefficient.split(".");
415
+ return { units: BigInt(whole + fraction), place: Number(exponent) - fraction.length };
377
416
  }
378
417
  function finiteFallback(min, max) {
379
418
  if (Number.isFinite(min)) return min;