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
@@ -1,13 +1,14 @@
1
1
  import { Controller } from '@hotwired/stimulus';
2
2
 
3
+ var afterFocus = /* @__PURE__ */ Symbol.for("stimeo--announcer:after-focus");
3
4
  function announce(message, options = {}) {
4
5
  const text = message.trim();
5
6
  if (text.length === 0) return;
6
- window.dispatchEvent(
7
- new CustomEvent("stimeo--announcer:announce", {
8
- detail: { message: text, assertive: options.assertive === true }
9
- })
10
- );
7
+ const event = new CustomEvent("stimeo--announcer:announce", {
8
+ detail: { message: text, assertive: options.assertive === true }
9
+ });
10
+ if (options.afterFocus) event[afterFocus] = true;
11
+ window.dispatchEvent(event);
11
12
  }
12
13
 
13
14
  function sharedRegistry(key, create) {
@@ -38,6 +39,7 @@ function ensureId(element, prefix = "stimeo") {
38
39
 
39
40
  function setAttributeIfChanged(element, name, value, beforeWrite) {
40
41
  if (element.getAttribute(name) === value) return false;
42
+ beforeWrite?.();
41
43
  if (value === null) element.removeAttribute(name);
42
44
  else element.setAttribute(name, value);
43
45
  return true;
@@ -101,14 +103,34 @@ var AttributeLease = class {
101
103
  const names = registry.names;
102
104
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
103
105
  }
104
- write(element, value) {
106
+ write(element, value, beforeWrite) {
105
107
  const stack = attributeStack(element, this.#attribute);
106
108
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
107
109
  this.#held.add(element);
108
110
  const shows = !this.#covered(element, stack, hold);
109
111
  hold.written = value;
110
112
  this.#mark(element, stack);
111
- if (shows) this.#reflect(element, value);
113
+ if (shows) this.#reflect(element, value, beforeWrite);
114
+ }
115
+ adopt(element, options = {}) {
116
+ const known = registry.stacks.get(element)?.has(this.#attribute);
117
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
118
+ const stack = attributeStack(element, this.#attribute);
119
+ const hold = this.#take(stack);
120
+ if (!hold) return false;
121
+ const current = element.getAttribute(this.#attribute);
122
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
123
+ return false;
124
+ }
125
+ this.#held.add(element);
126
+ hold.original = current;
127
+ hold.linked = false;
128
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
129
+ if (!above.linked) break;
130
+ above.original = current;
131
+ }
132
+ this.#mark(element, stack);
133
+ return true;
112
134
  }
113
135
  return(element) {
114
136
  const held = this.#held.delete(element);
@@ -174,8 +196,8 @@ var AttributeLease = class {
174
196
  setAttributeIfChanged(element, name, recorded);
175
197
  }
176
198
  }
177
- #reflect(element, value) {
178
- setAttributeIfChanged(element, this.#attribute, value);
199
+ #reflect(element, value, beforeWrite) {
200
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
179
201
  }
180
202
  };
181
203
 
@@ -238,7 +260,10 @@ var FormFieldController = class extends Controller {
238
260
  #ariaInvalid = new AttributeLease("aria-invalid", this.identifier, {
239
261
  base: true
240
262
  });
263
+ #errorVisibility = new AttributeLease("hidden", this.identifier);
264
+ #knownErrors = /* @__PURE__ */ new Set();
241
265
  #observer = new MutationObserver((records) => {
266
+ this.#returnDepartedErrors();
242
267
  if (records.some((record) => this.#isRelevantMutation(record))) {
243
268
  this.#reconcile.schedule();
244
269
  }
@@ -249,6 +274,15 @@ var FormFieldController = class extends Controller {
249
274
  #initialized = false;
250
275
  connect() {
251
276
  this.#reconcile.activate();
277
+ this.#returnDepartedErrors();
278
+ const retained = this.#activeControl;
279
+ if (retained) {
280
+ if (this.#ariaDescribedBy.adopt(retained, { ifChanged: true })) {
281
+ this.#baseDescribedBy = this.#externalDescribedByTokens(retained);
282
+ }
283
+ this.#ariaErrorMessage.adopt(retained, { ifChanged: true });
284
+ this.#ariaInvalid.adopt(retained, { ifChanged: true });
285
+ }
252
286
  if (!this.#initialized) {
253
287
  this.#explicitInvalid = this.element.hasAttribute(this.#invalidAttribute) && this.#shownErrors().length === 0;
254
288
  this.#initialized = true;
@@ -265,7 +299,11 @@ var FormFieldController = class extends Controller {
265
299
  disconnect() {
266
300
  this.#reconcile.cancel();
267
301
  this.#observer.disconnect();
268
- this.#activeControl && this.#releaseControl(this.#activeControl);
302
+ const retained = this.scope.containsElement(this.element);
303
+ if (!retained) this.element.removeAttribute(this.#invalidAttribute);
304
+ if (this.#activeControl && (this.element.isConnected || !retained)) {
305
+ this.#releaseControl(this.#activeControl);
306
+ }
269
307
  }
270
308
  controlTargetConnected() {
271
309
  this.#reconcile.schedule();
@@ -279,10 +317,14 @@ var FormFieldController = class extends Controller {
279
317
  descriptionTargetDisconnected() {
280
318
  this.#reconcile.schedule();
281
319
  }
282
- errorTargetConnected() {
320
+ errorTargetConnected(error) {
321
+ this.#knownErrors.add(error);
283
322
  this.#reconcile.schedule();
284
323
  }
285
- errorTargetDisconnected() {
324
+ errorTargetDisconnected(error) {
325
+ if (!this.scope.containsElement(error) || !this.errorTargets.includes(error)) {
326
+ this.#returnError(error);
327
+ }
286
328
  this.#reconcile.schedule();
287
329
  }
288
330
  setError(arg, options = {}) {
@@ -291,7 +333,7 @@ var FormFieldController = class extends Controller {
291
333
  this.errorTargets[0]?.replaceChildren(document.createTextNode(message));
292
334
  }
293
335
  for (const error of this.errorTargets) {
294
- error.hidden = (error.textContent ?? "").trim() === "";
336
+ this.#writeErrorVisibility(error, (error.textContent ?? "").trim() === "");
295
337
  }
296
338
  this.#explicitInvalid = true;
297
339
  this.#reconcileDom();
@@ -304,7 +346,7 @@ var FormFieldController = class extends Controller {
304
346
  clearError() {
305
347
  for (const error of this.errorTargets) {
306
348
  error.replaceChildren();
307
- error.hidden = true;
349
+ this.#writeErrorVisibility(error, true);
308
350
  }
309
351
  this.#explicitInvalid = false;
310
352
  this.#reconcileDom();
@@ -328,6 +370,21 @@ var FormFieldController = class extends Controller {
328
370
  const describedBy = this.#uniqueTokens([...this.#baseDescribedBy, ...associationIds]);
329
371
  this.#ariaDescribedBy.write(control, describedBy.length > 0 ? describedBy.join(" ") : null);
330
372
  }
373
+ #writeErrorVisibility(error, hidden) {
374
+ this.#knownErrors.add(error);
375
+ this.#errorVisibility.adopt(error, { ifChanged: true });
376
+ this.#errorVisibility.write(error, hidden ? "" : null);
377
+ }
378
+ #returnError(error) {
379
+ this.#knownErrors.delete(error);
380
+ this.#errorVisibility.return(error);
381
+ }
382
+ #returnDepartedErrors() {
383
+ const errors = new Set(this.errorTargets);
384
+ for (const error of this.#knownErrors) {
385
+ if (!errors.has(error)) this.#returnError(error);
386
+ }
387
+ }
331
388
  #ensureAssociationIds() {
332
389
  for (const description of this.descriptionTargets) {
333
390
  ensureId(description, "stimeo--form-field-desc");
@@ -6,11 +6,16 @@ function setDefaultAttribute(element, name, value) {
6
6
  return true;
7
7
  }
8
8
 
9
+ var HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
9
10
  function inheritsFieldsetDisabled(control) {
10
11
  let fieldset = control.closest("fieldset[disabled]");
11
12
  while (fieldset) {
12
- const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
13
- if (!legend?.contains(control)) return true;
13
+ if (fieldset.namespaceURI === HTML_NAMESPACE && fieldset.localName === "fieldset") {
14
+ const legend = Array.from(fieldset.children).find(
15
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "legend"
16
+ );
17
+ if (!legend?.contains(control)) return true;
18
+ }
14
19
  fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
15
20
  }
16
21
  return false;
@@ -68,11 +73,10 @@ function hasNativeTabStop(element) {
68
73
  if (element.tagName === "AUDIO" || element.tagName === "VIDEO") {
69
74
  return element.hasAttribute("controls");
70
75
  }
71
- if (element instanceof HTMLElement && element.tagName === "SUMMARY") {
72
- const details = element.parentElement;
73
- return details instanceof HTMLDetailsElement && Array.from(details.children).find((child) => child.tagName === "SUMMARY") === element;
74
- }
75
- return false;
76
+ const details = element.parentElement;
77
+ return details instanceof HTMLDetailsElement && Array.from(details.children).find(
78
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "summary"
79
+ ) === element;
76
80
  }
77
81
  function hasEditableTabStop(element) {
78
82
  const value = element.getAttribute("contenteditable")?.toLowerCase();
@@ -7,18 +7,20 @@ 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) {
21
22
  if (element.getAttribute(name) === value) return false;
23
+ beforeWrite?.();
22
24
  if (value === null) element.removeAttribute(name);
23
25
  else element.setAttribute(name, value);
24
26
  return true;
@@ -92,14 +94,34 @@ var AttributeLease = class {
92
94
  const names = registry.names;
93
95
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
94
96
  }
95
- write(element, value) {
97
+ write(element, value, beforeWrite) {
96
98
  const stack = attributeStack(element, this.#attribute);
97
99
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
98
100
  this.#held.add(element);
99
101
  const shows = !this.#covered(element, stack, hold);
100
102
  hold.written = value;
101
103
  this.#mark(element, stack);
102
- if (shows) this.#reflect(element, value);
104
+ if (shows) this.#reflect(element, value, beforeWrite);
105
+ }
106
+ adopt(element, options = {}) {
107
+ const known = registry.stacks.get(element)?.has(this.#attribute);
108
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
109
+ const stack = attributeStack(element, this.#attribute);
110
+ const hold = this.#take(stack);
111
+ if (!hold) return false;
112
+ const current = element.getAttribute(this.#attribute);
113
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
114
+ return false;
115
+ }
116
+ this.#held.add(element);
117
+ hold.original = current;
118
+ hold.linked = false;
119
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
120
+ if (!above.linked) break;
121
+ above.original = current;
122
+ }
123
+ this.#mark(element, stack);
124
+ return true;
103
125
  }
104
126
  return(element) {
105
127
  const held = this.#held.delete(element);
@@ -165,8 +187,8 @@ var AttributeLease = class {
165
187
  setAttributeIfChanged(element, name, recorded);
166
188
  }
167
189
  }
168
- #reflect(element, value) {
169
- setAttributeIfChanged(element, this.#attribute, value);
190
+ #reflect(element, value, beforeWrite) {
191
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
170
192
  }
171
193
  };
172
194
 
@@ -220,7 +242,7 @@ var ListenerSet = class {
220
242
 
221
243
  var MinDurationFloor = class {
222
244
  #timers;
223
- #timerId = null;
245
+ #task = null;
224
246
  #since = 0;
225
247
  constructor(timers) {
226
248
  this.#timers = timers;
@@ -229,25 +251,19 @@ var MinDurationFloor = class {
229
251
  this.#since = Date.now();
230
252
  }
231
253
  get pending() {
232
- return this.#timerId !== null;
254
+ return this.#task?.pending ?? false;
233
255
  }
234
256
  schedule(minDuration, finish) {
235
257
  this.cancel();
236
258
  const remaining = minDuration - (Date.now() - this.#since);
237
259
  if (remaining > 0) {
238
- this.#timerId = this.#timers.set(() => {
239
- this.#timerId = null;
240
- finish();
241
- }, remaining);
260
+ this.#task = this.#timers.setTask(finish, remaining);
242
261
  } else {
243
262
  finish();
244
263
  }
245
264
  }
246
265
  cancel() {
247
- if (this.#timerId !== null) {
248
- this.#timers.clear(this.#timerId);
249
- this.#timerId = null;
250
- }
266
+ this.#task?.cancel();
251
267
  }
252
268
  };
253
269
 
@@ -314,6 +330,22 @@ var TimerRegistry = class {
314
330
  return this.ids.size;
315
331
  }
316
332
  };
333
+ var TimeoutRegistration = class {
334
+ #registration;
335
+ constructor(owner, id) {
336
+ this.#registration = { owner, id };
337
+ }
338
+ get pending() {
339
+ return this.#registration !== null;
340
+ }
341
+ cancel() {
342
+ const registration = this.#registration;
343
+ if (registration) registration.owner.clear(registration.id);
344
+ }
345
+ expire() {
346
+ this.#registration = null;
347
+ }
348
+ };
317
349
  var SafeTimeout = class extends TimerRegistry {
318
350
  set(callback, delay) {
319
351
  const id = this.schedule(() => {
@@ -330,6 +362,29 @@ var SafeTimeout = class extends TimerRegistry {
330
362
  window.clearTimeout(id);
331
363
  }
332
364
  };
365
+ var TaskTimeout = class extends SafeTimeout {
366
+ #tasks = /* @__PURE__ */ new Map();
367
+ setTask(callback, delay) {
368
+ const id = this.set(() => {
369
+ this.#tasks.delete(id);
370
+ registration.expire();
371
+ callback();
372
+ }, delay);
373
+ const registration = new TimeoutRegistration(this, id);
374
+ this.#tasks.set(id, registration);
375
+ return registration;
376
+ }
377
+ clear(id) {
378
+ this.#tasks.get(id)?.expire();
379
+ this.#tasks.delete(id);
380
+ super.clear(id);
381
+ }
382
+ clearAll() {
383
+ for (const registration of this.#tasks.values()) registration.expire();
384
+ this.#tasks.clear();
385
+ super.clearAll();
386
+ }
387
+ };
333
388
 
334
389
  var FrameLoadingController = class _FrameLoadingController extends Controller {
335
390
  #numbers = new NumberValueReader();
@@ -344,7 +399,7 @@ var FrameLoadingController = class _FrameLoadingController extends Controller {
344
399
  minDuration: NUMBER_BOUNDS.timer
345
400
  };
346
401
  static events = ["start", "end", "reconcile"];
347
- #timeouts = new SafeTimeout();
402
+ #timeouts = new TaskTimeout();
348
403
  #floor = new MinDurationFloor(this.#timeouts);
349
404
  #gate = new DetachGate();
350
405
  #listeners = new ListenerSet();
@@ -2,6 +2,7 @@ import { Controller } from '@hotwired/stimulus';
2
2
 
3
3
  function setAttributeIfChanged(element, name, value, beforeWrite) {
4
4
  if (element.getAttribute(name) === value) return false;
5
+ beforeWrite?.();
5
6
  if (value === null) element.removeAttribute(name);
6
7
  else element.setAttribute(name, value);
7
8
  return true;
@@ -75,14 +76,34 @@ var AttributeLease = class {
75
76
  const names = registry.names;
76
77
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
77
78
  }
78
- write(element, value) {
79
+ write(element, value, beforeWrite) {
79
80
  const stack = attributeStack(element, this.#attribute);
80
81
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
81
82
  this.#held.add(element);
82
83
  const shows = !this.#covered(element, stack, hold);
83
84
  hold.written = value;
84
85
  this.#mark(element, stack);
85
- if (shows) this.#reflect(element, value);
86
+ if (shows) this.#reflect(element, value, beforeWrite);
87
+ }
88
+ adopt(element, options = {}) {
89
+ const known = registry.stacks.get(element)?.has(this.#attribute);
90
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
91
+ const stack = attributeStack(element, this.#attribute);
92
+ const hold = this.#take(stack);
93
+ if (!hold) return false;
94
+ const current = element.getAttribute(this.#attribute);
95
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
96
+ return false;
97
+ }
98
+ this.#held.add(element);
99
+ hold.original = current;
100
+ hold.linked = false;
101
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
102
+ if (!above.linked) break;
103
+ above.original = current;
104
+ }
105
+ this.#mark(element, stack);
106
+ return true;
86
107
  }
87
108
  return(element) {
88
109
  const held = this.#held.delete(element);
@@ -148,8 +169,8 @@ var AttributeLease = class {
148
169
  setAttributeIfChanged(element, name, recorded);
149
170
  }
150
171
  }
151
- #reflect(element, value) {
152
- setAttributeIfChanged(element, this.#attribute, value);
172
+ #reflect(element, value, beforeWrite) {
173
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
153
174
  }
154
175
  };
155
176
 
@@ -160,6 +181,7 @@ var EscapeLayer = class _EscapeLayer {
160
181
  );
161
182
  #active = null;
162
183
  activate(ownerDocument = document, options) {
184
+ const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
163
185
  this.deactivate();
164
186
  let registry2 = _EscapeLayer.#registries.get(ownerDocument);
165
187
  if (!registry2) {
@@ -167,7 +189,6 @@ var EscapeLayer = class _EscapeLayer {
167
189
  _EscapeLayer.#registries.set(ownerDocument, registry2);
168
190
  ownerDocument.addEventListener("keydown", registry2.onKeydown);
169
191
  }
170
- const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
171
192
  registry2.stack.push(entry);
172
193
  this.#active = { document: ownerDocument, entry };
173
194
  }
@@ -349,11 +370,18 @@ var HoverCardController = class _HoverCardController extends Controller {
349
370
  #pendingOpen = null;
350
371
  #pendingClose = null;
351
372
  #stopScrollDismiss = null;
352
- #shownCard = null;
373
+ #open = false;
374
+ #revealToken = null;
375
+ #currentCard = null;
376
+ #hidden = new AttributeLease("hidden", this.identifier);
377
+ #state = new AttributeLease("data-state", this.identifier);
353
378
  #connected = false;
354
379
  #reporting = false;
355
380
  connect() {
381
+ this.#reporting = false;
356
382
  this.#cancelTimers();
383
+ this.#open = false;
384
+ this.#currentCard = null;
357
385
  this.#conceal("api");
358
386
  this.#connected = true;
359
387
  this.#reporting = true;
@@ -363,18 +391,20 @@ var HoverCardController = class _HoverCardController extends Controller {
363
391
  this.#reporting = false;
364
392
  this.#cancelTimers();
365
393
  this.#release();
394
+ this.#open = false;
395
+ this.#currentCard = null;
366
396
  }
367
397
  closeOnScrollValueChanged() {
368
- if (this.#shownCard) this.#syncScrollDismiss();
398
+ if (this.#open) this.#syncScrollDismiss();
399
+ }
400
+ cardTargetConnected() {
401
+ if (this.#connected) this.#syncCard();
369
402
  }
370
403
  cardTargetDisconnected(card) {
371
- if (card !== this.#shownCard) return;
372
- if (this.cardTargets.includes(card)) return;
373
- this.#cancelTimers();
374
- this.#release();
375
- card.hidden = true;
376
- card.setAttribute("data-state", "closed");
377
- this.#reflectClosed();
404
+ if (this.#connected) this.#syncCard();
405
+ if (!this.scope.containsElement(card) || !this.cardTargets.includes(card)) {
406
+ this.#returnCard(card);
407
+ }
378
408
  }
379
409
  triggerTargetConnected() {
380
410
  if (this.#connected) this.#reflectTrigger();
@@ -384,6 +414,7 @@ var HoverCardController = class _HoverCardController extends Controller {
384
414
  if (this.#connected) this.#reflectTrigger();
385
415
  }
386
416
  open(event) {
417
+ this.#syncCard();
387
418
  const reason = stateReasonFor(event);
388
419
  this.#cancelClose();
389
420
  if (this.#isOpen || this.#pendingOpen !== null) return;
@@ -397,53 +428,91 @@ var HoverCardController = class _HoverCardController extends Controller {
397
428
  }, this.#safeOpenDelay);
398
429
  }
399
430
  close(event) {
431
+ this.#syncCard();
400
432
  const reason = stateReasonFor(event);
401
433
  this.#cancelOpen();
402
- if (!this.#closable || this.#pendingClose !== null) return;
403
- this.#pendingClose = this.#timers.set(() => {
404
- this.#pendingClose = null;
405
- if (this.element.contains(document.activeElement)) return;
406
- this.#conceal(reason);
407
- }, this.#safeCloseDelay);
434
+ if (!this.#closable) return;
435
+ const keepHovered = reason === "focus" || reason === "pointer";
436
+ if (this.#pendingClose) {
437
+ this.#pendingClose.keepHovered &&= keepHovered;
438
+ return;
439
+ }
440
+ const pending = {
441
+ keepHovered,
442
+ timer: this.#timers.set(() => {
443
+ this.#pendingClose = null;
444
+ if (this.element.contains(document.activeElement)) return;
445
+ const [card] = this.cardTargets;
446
+ if (pending.keepHovered && card && !card.hidden && (card.matches(":hover") || this.hasTriggerTarget && this.triggerTarget.matches(":hover"))) {
447
+ return;
448
+ }
449
+ this.#conceal(reason);
450
+ }, this.#safeCloseDelay)
451
+ };
452
+ this.#pendingClose = pending;
408
453
  }
409
454
  #reveal(reason) {
410
- if (!this.hasCardTarget) return;
411
- const card = this.cardTarget;
412
455
  const was = this.#isOpen;
413
- card.hidden = false;
414
- card.setAttribute("data-state", "open");
415
- if (this.hasTriggerTarget) this.#expanded.write(this.triggerTarget, "true");
416
- this.#shownCard = card;
456
+ this.#open = true;
457
+ const token = {};
458
+ this.#revealToken = token;
459
+ this.#reflectCard(this.#currentCard, true);
460
+ this.#reflectTrigger();
417
461
  if (!was && this.#reporting) this.dispatch("open", { detail: { reason }, cancelable: false });
418
- if (this.#shownCard !== card) return;
462
+ if (!this.#open || !this.#reporting) return;
463
+ this.#syncCard();
464
+ if (this.#revealToken !== token) return;
419
465
  this.#escapeLayer.activate(document, { onDismiss: () => this.#dismiss("escape") });
420
466
  this.#syncScrollDismiss();
421
467
  }
422
468
  #conceal(reason) {
469
+ this.#syncCard();
423
470
  const was = this.#isOpen;
471
+ this.#open = false;
424
472
  this.#release();
425
- this.#reflectClosed();
473
+ this.#reflectCard(this.#currentCard, false);
474
+ this.#reflectTrigger();
426
475
  if (was && this.#reporting) this.dispatch("close", { detail: { reason }, cancelable: false });
427
476
  }
428
- #reflectClosed() {
429
- if (this.hasCardTarget) {
430
- this.cardTarget.hidden = true;
431
- this.cardTarget.setAttribute("data-state", "closed");
477
+ #syncCard() {
478
+ const next = this.hasCardTarget ? this.cardTarget : null;
479
+ const current = this.#currentCard;
480
+ if (current === next) return;
481
+ if (current) {
482
+ this.#open = !current.hidden;
483
+ if (!this.scope.containsElement(current) || !this.cardTargets.includes(current)) {
484
+ this.#returnCard(current);
485
+ } else this.#reflectCard(current, false);
432
486
  }
433
- if (this.hasTriggerTarget) this.#expanded.write(this.triggerTarget, "false");
487
+ this.#currentCard = next;
488
+ this.#reflectCard(next, this.#open);
489
+ this.#reflectTrigger();
490
+ if (!this.#open) this.#release();
491
+ else this.#syncScrollDismiss();
492
+ }
493
+ #reflectCard(card, open) {
494
+ if (!card) return;
495
+ this.#hidden.write(card, open ? null : "");
496
+ this.#state.write(card, open ? "open" : "closed");
497
+ }
498
+ #returnCard(card) {
499
+ this.#hidden.return(card);
500
+ this.#state.return(card);
434
501
  }
435
502
  #reflectTrigger() {
436
503
  if (this.hasTriggerTarget) this.#expanded.write(this.triggerTarget, String(this.#isOpen));
437
504
  }
438
505
  #release() {
439
- this.#shownCard = null;
506
+ this.#revealToken = null;
440
507
  this.#escapeLayer.deactivate();
441
508
  this.#stopScrollDismiss?.();
442
509
  this.#stopScrollDismiss = null;
443
510
  }
444
511
  #syncScrollDismiss() {
445
- if (this.closeOnScrollValue) {
446
- this.#stopScrollDismiss ??= observeScrollDismiss(this.element, () => this.#dismiss("scroll"));
512
+ if (this.closeOnScrollValue && this.#currentCard && this.#open) {
513
+ this.#stopScrollDismiss ??= observeScrollDismiss(this.element, () => {
514
+ if (this.hasCardTarget) this.#dismiss("scroll");
515
+ });
447
516
  return;
448
517
  }
449
518
  this.#stopScrollDismiss?.();
@@ -465,15 +534,15 @@ var HoverCardController = class _HoverCardController extends Controller {
465
534
  }
466
535
  #cancelClose() {
467
536
  if (this.#pendingClose !== null) {
468
- this.#timers.clear(this.#pendingClose);
537
+ this.#timers.clear(this.#pendingClose.timer);
469
538
  this.#pendingClose = null;
470
539
  }
471
540
  }
472
541
  get #isOpen() {
473
- return this.hasCardTarget && !this.cardTarget.hidden;
542
+ return this.#currentCard ? !this.#currentCard.hidden : this.#open;
474
543
  }
475
544
  get #closable() {
476
- return this.#isOpen || this.#shownCard !== null;
545
+ return this.#isOpen || this.#open;
477
546
  }
478
547
  get #safeOpenDelay() {
479
548
  return this.#numbers.read(