stimeo-ui 0.5.0 → 0.7.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.
- package/CHANGELOG.md +178 -0
- package/dist/controllers/alert_dialog_controller.d.ts +2 -0
- package/dist/controllers/alert_dialog_controller.js +75 -4
- package/dist/controllers/alert_dialog_controller.js.map +1 -1
- package/dist/controllers/aspect_ratio_controller.d.ts +1 -3
- package/dist/controllers/aspect_ratio_controller.js +19 -11
- package/dist/controllers/aspect_ratio_controller.js.map +1 -1
- package/dist/controllers/auto_submit_controller.d.ts +2 -0
- package/dist/controllers/auto_submit_controller.js.map +1 -1
- package/dist/controllers/avatar_controller.d.ts +36 -15
- package/dist/controllers/avatar_controller.js +237 -40
- package/dist/controllers/avatar_controller.js.map +1 -1
- package/dist/controllers/breadcrumb_controller.d.ts +2 -0
- package/dist/controllers/breadcrumb_controller.js.map +1 -1
- package/dist/controllers/bulk_select_controller.d.ts +2 -0
- package/dist/controllers/bulk_select_controller.js.map +1 -1
- package/dist/controllers/calendar_controller.d.ts +2 -0
- package/dist/controllers/calendar_controller.js.map +1 -1
- package/dist/controllers/carousel_controller.d.ts +12 -3
- package/dist/controllers/carousel_controller.js +85 -9
- package/dist/controllers/carousel_controller.js.map +1 -1
- package/dist/controllers/character_counter_controller.d.ts +52 -20
- package/dist/controllers/character_counter_controller.js +338 -63
- package/dist/controllers/character_counter_controller.js.map +1 -1
- package/dist/controllers/checkbox_controller.d.ts +34 -5
- package/dist/controllers/checkbox_controller.js +136 -25
- package/dist/controllers/checkbox_controller.js.map +1 -1
- package/dist/controllers/color_picker_controller.d.ts +10 -1
- package/dist/controllers/color_picker_controller.js +35 -9
- package/dist/controllers/color_picker_controller.js.map +1 -1
- package/dist/controllers/combobox_controller.d.ts +2 -0
- package/dist/controllers/combobox_controller.js.map +1 -1
- package/dist/controllers/command_palette_controller.d.ts +2 -0
- package/dist/controllers/command_palette_controller.js +75 -4
- package/dist/controllers/command_palette_controller.js.map +1 -1
- package/dist/controllers/conditional_fields_controller.d.ts +42 -14
- package/dist/controllers/conditional_fields_controller.js +345 -51
- package/dist/controllers/conditional_fields_controller.js.map +1 -1
- package/dist/controllers/confirm_controller.d.ts +2 -0
- package/dist/controllers/confirm_controller.js +75 -4
- package/dist/controllers/confirm_controller.js.map +1 -1
- package/dist/controllers/count_up_controller.d.ts +2 -0
- package/dist/controllers/count_up_controller.js.map +1 -1
- package/dist/controllers/countdown_controller.d.ts +4 -0
- package/dist/controllers/countdown_controller.js.map +1 -1
- package/dist/controllers/currency_input_controller.d.ts +2 -0
- package/dist/controllers/currency_input_controller.js.map +1 -1
- package/dist/controllers/data_grid_controller.d.ts +3 -0
- package/dist/controllers/data_grid_controller.js.map +1 -1
- package/dist/controllers/date_range_picker_controller.d.ts +23 -4
- package/dist/controllers/date_range_picker_controller.js +157 -30
- package/dist/controllers/date_range_picker_controller.js.map +1 -1
- package/dist/controllers/dialog_controller.js +75 -4
- package/dist/controllers/dialog_controller.js.map +1 -1
- package/dist/controllers/direct_upload_controller.d.ts +56 -24
- package/dist/controllers/direct_upload_controller.js +201 -45
- package/dist/controllers/direct_upload_controller.js.map +1 -1
- package/dist/controllers/dirty_form_controller.d.ts +14 -6
- package/dist/controllers/dirty_form_controller.js +192 -29
- package/dist/controllers/dirty_form_controller.js.map +1 -1
- package/dist/controllers/dismissible_controller.d.ts +2 -0
- package/dist/controllers/dismissible_controller.js +83 -18
- package/dist/controllers/dismissible_controller.js.map +1 -1
- package/dist/controllers/drawer_controller.js +75 -4
- package/dist/controllers/drawer_controller.js.map +1 -1
- package/dist/controllers/empty_state_controller.d.ts +2 -0
- package/dist/controllers/empty_state_controller.js.map +1 -1
- package/dist/controllers/file_dropzone_controller.d.ts +9 -1
- package/dist/controllers/file_dropzone_controller.js +26 -3
- package/dist/controllers/file_dropzone_controller.js.map +1 -1
- package/dist/controllers/filter_controller.d.ts +2 -0
- package/dist/controllers/filter_controller.js.map +1 -1
- package/dist/controllers/flash_controller.d.ts +4 -0
- package/dist/controllers/flash_controller.js.map +1 -1
- package/dist/controllers/focus_controller.d.ts +4 -3
- package/dist/controllers/focus_controller.js +75 -4
- package/dist/controllers/focus_controller.js.map +1 -1
- package/dist/controllers/form_field_controller.d.ts +50 -10
- package/dist/controllers/form_field_controller.js +280 -62
- package/dist/controllers/form_field_controller.js.map +1 -1
- package/dist/controllers/form_validation_controller.d.ts +10 -8
- package/dist/controllers/form_validation_controller.js +208 -83
- package/dist/controllers/form_validation_controller.js.map +1 -1
- package/dist/controllers/frame_loading_controller.d.ts +2 -0
- package/dist/controllers/frame_loading_controller.js.map +1 -1
- package/dist/controllers/highlight_controller.d.ts +2 -0
- package/dist/controllers/highlight_controller.js.map +1 -1
- package/dist/controllers/hover_card_controller.d.ts +2 -2
- package/dist/controllers/hover_card_controller.js.map +1 -1
- package/dist/controllers/idle_controller.d.ts +5 -3
- package/dist/controllers/idle_controller.js +27 -5
- package/dist/controllers/idle_controller.js.map +1 -1
- package/dist/controllers/input_mask_controller.d.ts +2 -0
- package/dist/controllers/input_mask_controller.js.map +1 -1
- package/dist/controllers/lazy_frame_controller.d.ts +2 -0
- package/dist/controllers/lazy_frame_controller.js.map +1 -1
- package/dist/controllers/listbox_controller.d.ts +2 -0
- package/dist/controllers/listbox_controller.js.map +1 -1
- package/dist/controllers/local_time_controller.d.ts +2 -0
- package/dist/controllers/local_time_controller.js.map +1 -1
- package/dist/controllers/masonry_controller.d.ts +2 -0
- package/dist/controllers/masonry_controller.js.map +1 -1
- package/dist/controllers/menubar_controller.js +5 -3
- package/dist/controllers/menubar_controller.js.map +1 -1
- package/dist/controllers/meter_controller.d.ts +2 -0
- package/dist/controllers/meter_controller.js.map +1 -1
- package/dist/controllers/multi_select_controller.d.ts +48 -12
- package/dist/controllers/multi_select_controller.js +460 -151
- package/dist/controllers/multi_select_controller.js.map +1 -1
- package/dist/controllers/nested_form_controller.d.ts +2 -0
- package/dist/controllers/nested_form_controller.js.map +1 -1
- package/dist/controllers/network_status_controller.d.ts +2 -0
- package/dist/controllers/network_status_controller.js.map +1 -1
- package/dist/controllers/number_input_controller.d.ts +26 -6
- package/dist/controllers/number_input_controller.js +317 -51
- package/dist/controllers/number_input_controller.js.map +1 -1
- package/dist/controllers/otp_controller.d.ts +3 -0
- package/dist/controllers/otp_controller.js.map +1 -1
- package/dist/controllers/overflow_indicator_controller.d.ts +2 -0
- package/dist/controllers/overflow_indicator_controller.js.map +1 -1
- package/dist/controllers/overflow_menu_controller.d.ts +2 -0
- package/dist/controllers/overflow_menu_controller.js +6 -1
- package/dist/controllers/overflow_menu_controller.js.map +1 -1
- package/dist/controllers/pagination_controller.d.ts +2 -0
- package/dist/controllers/pagination_controller.js +35 -1
- package/dist/controllers/pagination_controller.js.map +1 -1
- package/dist/controllers/password_reveal_controller.d.ts +2 -0
- package/dist/controllers/password_reveal_controller.js.map +1 -1
- package/dist/controllers/password_strength_controller.d.ts +5 -3
- package/dist/controllers/password_strength_controller.js +20 -2
- package/dist/controllers/password_strength_controller.js.map +1 -1
- package/dist/controllers/persist_controller.d.ts +33 -19
- package/dist/controllers/persist_controller.js +449 -120
- package/dist/controllers/persist_controller.js.map +1 -1
- package/dist/controllers/pointer_drag_controller.d.ts +3 -0
- package/dist/controllers/pointer_drag_controller.js.map +1 -1
- package/dist/controllers/popover_controller.d.ts +2 -2
- package/dist/controllers/popover_controller.js +77 -4
- package/dist/controllers/popover_controller.js.map +1 -1
- package/dist/controllers/portal_controller.d.ts +3 -2
- package/dist/controllers/portal_controller.js.map +1 -1
- package/dist/controllers/preview_guard_controller.d.ts +2 -0
- package/dist/controllers/preview_guard_controller.js.map +1 -1
- package/dist/controllers/progress_controller.d.ts +3 -0
- package/dist/controllers/progress_controller.js.map +1 -1
- package/dist/controllers/radio_group_controller.d.ts +44 -15
- package/dist/controllers/radio_group_controller.js +540 -56
- package/dist/controllers/radio_group_controller.js.map +1 -1
- package/dist/controllers/rating_controller.d.ts +38 -31
- package/dist/controllers/rating_controller.js +276 -89
- package/dist/controllers/rating_controller.js.map +1 -1
- package/dist/controllers/reading_progress_controller.d.ts +2 -0
- package/dist/controllers/reading_progress_controller.js.map +1 -1
- package/dist/controllers/resizable_controller.d.ts +2 -0
- package/dist/controllers/resizable_controller.js +33 -0
- package/dist/controllers/resizable_controller.js.map +1 -1
- package/dist/controllers/roving_controller.d.ts +6 -0
- package/dist/controllers/roving_controller.js +60 -5
- package/dist/controllers/roving_controller.js.map +1 -1
- package/dist/controllers/scroll_area_controller.d.ts +25 -11
- package/dist/controllers/scroll_area_controller.js +557 -125
- package/dist/controllers/scroll_area_controller.js.map +1 -1
- package/dist/controllers/scroll_visibility_controller.d.ts +2 -0
- package/dist/controllers/scroll_visibility_controller.js +33 -0
- package/dist/controllers/scroll_visibility_controller.js.map +1 -1
- package/dist/controllers/scrollspy_controller.d.ts +2 -0
- package/dist/controllers/scrollspy_controller.js.map +1 -1
- package/dist/controllers/separator_controller.d.ts +54 -11
- package/dist/controllers/separator_controller.js +354 -38
- package/dist/controllers/separator_controller.js.map +1 -1
- package/dist/controllers/sidebar_controller.js +83 -10
- package/dist/controllers/sidebar_controller.js.map +1 -1
- package/dist/controllers/skeleton_controller.d.ts +2 -0
- package/dist/controllers/skeleton_controller.js.map +1 -1
- package/dist/controllers/slider_controller.d.ts +2 -0
- package/dist/controllers/slider_controller.js.map +1 -1
- package/dist/controllers/smart_sticky_header_controller.d.ts +2 -0
- package/dist/controllers/smart_sticky_header_controller.js.map +1 -1
- package/dist/controllers/spinner_controller.d.ts +2 -0
- package/dist/controllers/spinner_controller.js.map +1 -1
- package/dist/controllers/step_indicator_controller.d.ts +2 -0
- package/dist/controllers/step_indicator_controller.js.map +1 -1
- package/dist/controllers/stepper_controller.d.ts +2 -0
- package/dist/controllers/stepper_controller.js.map +1 -1
- package/dist/controllers/stick_to_bottom_controller.d.ts +2 -0
- package/dist/controllers/stick_to_bottom_controller.js.map +1 -1
- package/dist/controllers/sticky_observer_controller.d.ts +2 -0
- package/dist/controllers/sticky_observer_controller.js.map +1 -1
- package/dist/controllers/submit_once_controller.d.ts +94 -38
- package/dist/controllers/submit_once_controller.js +399 -121
- package/dist/controllers/submit_once_controller.js.map +1 -1
- package/dist/controllers/switch_controller.d.ts +2 -0
- package/dist/controllers/switch_controller.js.map +1 -1
- package/dist/controllers/tags_input_controller.d.ts +43 -11
- package/dist/controllers/tags_input_controller.js +356 -120
- package/dist/controllers/tags_input_controller.js.map +1 -1
- package/dist/controllers/textarea_autosize_controller.d.ts +2 -0
- package/dist/controllers/textarea_autosize_controller.js.map +1 -1
- package/dist/controllers/theme_controller.d.ts +2 -0
- package/dist/controllers/theme_controller.js +8 -6
- package/dist/controllers/theme_controller.js.map +1 -1
- package/dist/controllers/time_picker_controller.d.ts +44 -13
- package/dist/controllers/time_picker_controller.js +296 -107
- package/dist/controllers/time_picker_controller.js.map +1 -1
- package/dist/controllers/toast_controller.d.ts +4 -0
- package/dist/controllers/toast_controller.js.map +1 -1
- package/dist/controllers/toggle_group_controller.d.ts +41 -13
- package/dist/controllers/toggle_group_controller.js +378 -55
- package/dist/controllers/toggle_group_controller.js.map +1 -1
- package/dist/controllers/toolbar_controller.js +5 -3
- package/dist/controllers/toolbar_controller.js.map +1 -1
- package/dist/controllers/tooltip_controller.d.ts +2 -2
- package/dist/controllers/tooltip_controller.js.map +1 -1
- package/dist/controllers/transition_controller.d.ts +4 -3
- package/dist/controllers/transition_controller.js.map +1 -1
- package/dist/controllers/tree_view_controller.d.ts +2 -0
- package/dist/controllers/tree_view_controller.js +7 -4
- package/dist/controllers/tree_view_controller.js.map +1 -1
- package/dist/index.js +4963 -1581
- package/dist/index.js.map +1 -1
- package/dist/inspector/cli.d.ts +123 -6
- package/dist/inspector/cli.js +212 -18
- package/dist/inspector/cli.js.map +1 -1
- package/dist/inspector/cli_bin.js +274 -50
- package/dist/inspector/cli_bin.js.map +1 -1
- package/dist/inspector/examples.json +15 -15
- package/dist/inspector/manifest.json +602 -80
- package/dist/positioning/index.d.ts +4 -2
- package/dist/positioning/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,134 @@
|
|
|
1
1
|
import { Controller } from '@hotwired/stimulus';
|
|
2
2
|
|
|
3
|
+
// src/controllers/conditional_fields_controller.ts
|
|
4
|
+
|
|
5
|
+
// src/utils/focus_candidate.ts
|
|
6
|
+
function inheritsFieldsetDisabled(control) {
|
|
7
|
+
let fieldset = control.closest("fieldset[disabled]");
|
|
8
|
+
while (fieldset) {
|
|
9
|
+
const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
|
|
10
|
+
if (!legend?.contains(control)) return true;
|
|
11
|
+
fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
function canTakeFocus(element) {
|
|
16
|
+
if (element.closest("[hidden], [inert]")) return false;
|
|
17
|
+
if (element instanceof HTMLInputElement && element.type === "hidden") return false;
|
|
18
|
+
if (!("disabled" in element)) return true;
|
|
19
|
+
if (element.disabled) return false;
|
|
20
|
+
return !inheritsFieldsetDisabled(element);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/utils/microtask_coalescer.ts
|
|
24
|
+
var MicrotaskCoalescer = class {
|
|
25
|
+
#run;
|
|
26
|
+
#queued = false;
|
|
27
|
+
#active = false;
|
|
28
|
+
#generation = 0;
|
|
29
|
+
/** @param run - the single reconciliation pass, invoked at most once per batch. */
|
|
30
|
+
constructor(run) {
|
|
31
|
+
this.#run = run;
|
|
32
|
+
}
|
|
33
|
+
/** Opens the window in which {@link schedule} is honoured; call from `connect()`. */
|
|
34
|
+
activate() {
|
|
35
|
+
this.#active = true;
|
|
36
|
+
}
|
|
37
|
+
/** Closes the window and drops any pending pass; call from `disconnect()`. */
|
|
38
|
+
cancel() {
|
|
39
|
+
this.#active = false;
|
|
40
|
+
this.#queued = false;
|
|
41
|
+
this.#generation += 1;
|
|
42
|
+
}
|
|
43
|
+
/** Requests one pass after the batch settles. Idempotent; inert outside the window. */
|
|
44
|
+
schedule() {
|
|
45
|
+
if (!this.#active || this.#queued) return;
|
|
46
|
+
this.#queued = true;
|
|
47
|
+
const generation = this.#generation;
|
|
48
|
+
queueMicrotask(() => {
|
|
49
|
+
if (generation !== this.#generation || !this.#queued || !this.#active) return;
|
|
50
|
+
this.#queued = false;
|
|
51
|
+
this.#run();
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/utils/before_cache_reset.ts
|
|
57
|
+
var BeforeCacheReset = class _BeforeCacheReset {
|
|
58
|
+
/** Every subscribed instance, iterated by the one shared document listener. */
|
|
59
|
+
static #subscribers = /* @__PURE__ */ new Set();
|
|
60
|
+
/** The shared listener; installed while at least one instance is subscribed. */
|
|
61
|
+
static #onBeforeCache = () => {
|
|
62
|
+
for (const subscriber of _BeforeCacheReset.#subscribers) subscriber.#rewind();
|
|
63
|
+
};
|
|
64
|
+
#rewind;
|
|
65
|
+
/** @param rewind - the pass that returns this controller's state to its initial form. */
|
|
66
|
+
constructor(rewind) {
|
|
67
|
+
this.#rewind = rewind;
|
|
68
|
+
}
|
|
69
|
+
/** Subscribes to `turbo:before-cache`; call from `connect()`. Idempotent. */
|
|
70
|
+
activate() {
|
|
71
|
+
const first = _BeforeCacheReset.#subscribers.size === 0;
|
|
72
|
+
_BeforeCacheReset.#subscribers.add(this);
|
|
73
|
+
if (first) {
|
|
74
|
+
document.addEventListener("turbo:before-cache", _BeforeCacheReset.#onBeforeCache);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** Unsubscribes; call from `disconnect()`. Safe when never subscribed. */
|
|
78
|
+
deactivate() {
|
|
79
|
+
_BeforeCacheReset.#subscribers.delete(this);
|
|
80
|
+
if (_BeforeCacheReset.#subscribers.size > 0) return;
|
|
81
|
+
document.removeEventListener("turbo:before-cache", _BeforeCacheReset.#onBeforeCache);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// src/utils/tabindex_loan.ts
|
|
86
|
+
var TabindexLoan = class {
|
|
87
|
+
#value;
|
|
88
|
+
#lent = /* @__PURE__ */ new Set();
|
|
89
|
+
/** Returns live loans before Turbo can copy them into its page snapshot. */
|
|
90
|
+
#beforeCache = new BeforeCacheReset(() => this.returnAll());
|
|
91
|
+
/**
|
|
92
|
+
* @param value - the `tabindex` to lend. `"-1"` (the default) is
|
|
93
|
+
* programmatically focusable but not a Tab stop; `"0"` is a real Tab stop,
|
|
94
|
+
* which a scroll region with no focusable content of its own needs.
|
|
95
|
+
*/
|
|
96
|
+
constructor(value = "-1") {
|
|
97
|
+
this.#value = value;
|
|
98
|
+
}
|
|
99
|
+
/** Lends `element` the value; no-ops when it already carries a `tabindex`. */
|
|
100
|
+
lend(element) {
|
|
101
|
+
if (element.hasAttribute("tabindex")) return;
|
|
102
|
+
element.setAttribute("tabindex", this.#value);
|
|
103
|
+
this.#lent.add(element);
|
|
104
|
+
this.#beforeCache.activate();
|
|
105
|
+
}
|
|
106
|
+
/** Takes back every loan whose value is still the one that was lent. */
|
|
107
|
+
returnAll() {
|
|
108
|
+
for (const element of this.#lent) {
|
|
109
|
+
if (element.getAttribute("tabindex") === this.#value) element.removeAttribute("tabindex");
|
|
110
|
+
}
|
|
111
|
+
this.#lent.clear();
|
|
112
|
+
this.#beforeCache.deactivate();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
3
116
|
// src/controllers/conditional_fields_controller.ts
|
|
4
117
|
var DISABLED_MARKER = "data-conditional-disabled";
|
|
118
|
+
var OBSERVED_ATTRIBUTES = [
|
|
119
|
+
"hidden",
|
|
120
|
+
"aria-hidden",
|
|
121
|
+
"data-visible",
|
|
122
|
+
DISABLED_MARKER,
|
|
123
|
+
"disabled",
|
|
124
|
+
"data-when-checked",
|
|
125
|
+
"data-when-unchecked",
|
|
126
|
+
"data-when-value",
|
|
127
|
+
"checked",
|
|
128
|
+
"selected",
|
|
129
|
+
"value",
|
|
130
|
+
"type"
|
|
131
|
+
];
|
|
5
132
|
var ConditionalFieldsController = class extends Controller {
|
|
6
133
|
static targets = ["trigger", "region"];
|
|
7
134
|
static values = {
|
|
@@ -9,56 +136,171 @@ var ConditionalFieldsController = class extends Controller {
|
|
|
9
136
|
match: { type: String, default: "any" }
|
|
10
137
|
};
|
|
11
138
|
static actions = ["evaluate"];
|
|
12
|
-
static events = ["change"];
|
|
139
|
+
static events = ["change", "reconcile"];
|
|
140
|
+
/** Logical state last committed for each current region; rebuilt on every connect. */
|
|
13
141
|
#lastVisible = /* @__PURE__ */ new WeakMap();
|
|
14
|
-
|
|
15
|
-
|
|
142
|
+
/** Controller writes awaiting an observer callback, separated from authored morphs. */
|
|
143
|
+
#internalWrites = /* @__PURE__ */ new WeakMap();
|
|
144
|
+
#observing = false;
|
|
145
|
+
/** Coalesces one target/morph/mutation batch into one full DOM reconciliation. */
|
|
146
|
+
#reconcile = new MicrotaskCoalescer(() => this.#reconcileDom());
|
|
147
|
+
/** Provides the last-resort focus landmark without claiming an authored tabindex. */
|
|
148
|
+
#rootTabindex = new TabindexLoan();
|
|
149
|
+
/** Watches retained targets and controls whose declarative or reflected state changed. */
|
|
150
|
+
#observer = new MutationObserver((records) => this.#onMutations(records));
|
|
151
|
+
/** Evaluates only events whose source is one of this instance's live trigger targets. */
|
|
152
|
+
#onTriggerInput = (event) => {
|
|
153
|
+
const target = event.target;
|
|
154
|
+
const triggers = this.triggerTargets;
|
|
155
|
+
if (target instanceof Element && triggers.some((trigger) => trigger === target)) {
|
|
156
|
+
this.#commitChange(triggers);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
/** Reconciles property-only retained-element changes after Turbo morphs. */
|
|
160
|
+
#onMorph = () => {
|
|
161
|
+
this.#reconcile.schedule();
|
|
162
|
+
};
|
|
163
|
+
/** Reconciles the settled defaults after a non-cancelled owning form reset. */
|
|
164
|
+
#onReset = (event) => {
|
|
165
|
+
const form = event.target;
|
|
166
|
+
if (form instanceof HTMLFormElement && this.#hasTriggerOwnedBy(form)) {
|
|
167
|
+
queueMicrotask(() => {
|
|
168
|
+
if (!event.defaultPrevented) this.#reconcile.schedule();
|
|
169
|
+
});
|
|
170
|
+
}
|
|
16
171
|
};
|
|
172
|
+
/** Reflects initial live state and opens every retained-DOM reconciliation path. */
|
|
17
173
|
connect() {
|
|
18
|
-
this
|
|
19
|
-
this
|
|
20
|
-
this.
|
|
174
|
+
this.#lastVisible = /* @__PURE__ */ new WeakMap();
|
|
175
|
+
this.#internalWrites = /* @__PURE__ */ new WeakMap();
|
|
176
|
+
this.#reconcile.activate();
|
|
177
|
+
this.#settle();
|
|
178
|
+
this.#observer.observe(this.element, {
|
|
179
|
+
attributes: true,
|
|
180
|
+
attributeFilter: OBSERVED_ATTRIBUTES,
|
|
181
|
+
childList: true,
|
|
182
|
+
subtree: true
|
|
183
|
+
});
|
|
184
|
+
this.#observing = true;
|
|
185
|
+
this.element.addEventListener("change", this.#onTriggerInput);
|
|
186
|
+
this.element.addEventListener("input", this.#onTriggerInput);
|
|
187
|
+
this.element.addEventListener("turbo:morph-element", this.#onMorph);
|
|
188
|
+
document.addEventListener("reset", this.#onReset, true);
|
|
21
189
|
}
|
|
190
|
+
/** Releases all external resources and returns the temporary focus landmark loan. */
|
|
22
191
|
disconnect() {
|
|
23
|
-
this.
|
|
24
|
-
this
|
|
192
|
+
this.#reconcile.cancel();
|
|
193
|
+
this.#observing = false;
|
|
194
|
+
this.#observer.disconnect();
|
|
195
|
+
this.element.removeEventListener("change", this.#onTriggerInput);
|
|
196
|
+
this.element.removeEventListener("input", this.#onTriggerInput);
|
|
197
|
+
this.element.removeEventListener("turbo:morph-element", this.#onMorph);
|
|
198
|
+
document.removeEventListener("reset", this.#onReset, true);
|
|
199
|
+
this.#rootTabindex.returnAll();
|
|
200
|
+
}
|
|
201
|
+
/** Reconciles after a trigger target is inserted or restored. */
|
|
202
|
+
triggerTargetConnected() {
|
|
203
|
+
this.#reconcile.schedule();
|
|
204
|
+
}
|
|
205
|
+
/** Reconciles after a trigger target is removed or replaced. */
|
|
206
|
+
triggerTargetDisconnected() {
|
|
207
|
+
this.#reconcile.schedule();
|
|
208
|
+
}
|
|
209
|
+
/** Initializes a region target inserted or restored at runtime. */
|
|
210
|
+
regionTargetConnected() {
|
|
211
|
+
this.#reconcile.schedule();
|
|
212
|
+
}
|
|
213
|
+
/** Reconciles the remaining region set after a target leaves. */
|
|
214
|
+
regionTargetDisconnected() {
|
|
215
|
+
this.#reconcile.schedule();
|
|
25
216
|
}
|
|
26
|
-
/**
|
|
217
|
+
/** Reconciles when application code or a morph changes the match policy. */
|
|
218
|
+
matchValueChanged() {
|
|
219
|
+
this.#reconcile.schedule();
|
|
220
|
+
}
|
|
221
|
+
/** Reconciles control ownership when hidden controls become enabled or excluded. */
|
|
222
|
+
disableHiddenValueChanged() {
|
|
223
|
+
this.#reconcile.schedule();
|
|
224
|
+
}
|
|
225
|
+
/** Re-evaluates every region and repairs all reflected DOM state. */
|
|
27
226
|
evaluate() {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
227
|
+
this.#commitChange();
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Computes one settled plan, shows destinations before hiding sources, and then
|
|
231
|
+
* returns logical transitions in DOM order after every region is internally coherent.
|
|
232
|
+
*/
|
|
233
|
+
#settle(triggers = this.triggerTargets) {
|
|
234
|
+
const regions = this.regionTargets;
|
|
235
|
+
const plan = /* @__PURE__ */ new Map();
|
|
236
|
+
for (const region of regions) plan.set(region, this.#isVisible(region, triggers));
|
|
237
|
+
const changed = /* @__PURE__ */ new Set();
|
|
238
|
+
const applicationOrder = [
|
|
239
|
+
...regions.filter((region) => plan.get(region) === true),
|
|
240
|
+
...regions.filter((region) => plan.get(region) === false)
|
|
241
|
+
];
|
|
242
|
+
for (const region of applicationOrder) {
|
|
243
|
+
const visible = plan.get(region);
|
|
244
|
+
if (visible !== void 0 && this.#applyRegion(region, visible, plan)) {
|
|
245
|
+
changed.add(region);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return regions.filter((region) => changed.has(region)).map((region) => ({ region, visible: plan.get(region) === true }));
|
|
249
|
+
}
|
|
250
|
+
/** Reports logical transitions caused by user input or the public action. */
|
|
251
|
+
#commitChange(triggers = this.triggerTargets) {
|
|
252
|
+
for (const detail of this.#settle(triggers)) {
|
|
253
|
+
this.dispatch("change", { detail });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** Reports logical transitions derived from DOM, lifecycle, reset, or Value state. */
|
|
257
|
+
#reconcileDom() {
|
|
258
|
+
for (const detail of this.#settle()) {
|
|
259
|
+
this.dispatch("reconcile", { detail });
|
|
31
260
|
}
|
|
32
261
|
}
|
|
33
|
-
/** Applies
|
|
34
|
-
#applyRegion(region, visible) {
|
|
262
|
+
/** Applies one planned state while keeping event history separate from DOM repair. */
|
|
263
|
+
#applyRegion(region, visible, plan) {
|
|
35
264
|
const previous = this.#lastVisible.get(region) ?? !region.hidden;
|
|
36
|
-
|
|
265
|
+
const changed = visible !== previous;
|
|
37
266
|
this.#lastVisible.set(region, visible);
|
|
38
|
-
if (visible)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
267
|
+
if (!visible) this.#retreatFocus(region, plan);
|
|
268
|
+
this.#syncRegionState(region, visible);
|
|
269
|
+
this.#syncRegionControls(region, visible);
|
|
270
|
+
return changed;
|
|
271
|
+
}
|
|
272
|
+
/** Reflects visibility idempotently, recording writes so the observer ignores its own work. */
|
|
273
|
+
#syncRegionState(region, visible) {
|
|
274
|
+
this.#writeAttribute(region, "hidden", visible ? null : "");
|
|
275
|
+
this.#writeAttribute(region, "aria-hidden", visible ? null : "true");
|
|
276
|
+
this.#writeAttribute(region, "data-visible", visible ? "true" : null);
|
|
277
|
+
}
|
|
278
|
+
/** Enables/disables descendant controls, touching only state carrying our marker. */
|
|
279
|
+
#syncRegionControls(region, visible) {
|
|
280
|
+
const shouldDisable = !visible && this.disableHiddenValue;
|
|
281
|
+
const controls = region.querySelectorAll("input, textarea, select, button");
|
|
282
|
+
for (const control of controls) {
|
|
283
|
+
const marked = control.hasAttribute(DISABLED_MARKER);
|
|
284
|
+
if (shouldDisable) {
|
|
285
|
+
if (!control.disabled) {
|
|
286
|
+
this.#writeAttribute(control, "disabled", "");
|
|
287
|
+
this.#writeAttribute(control, DISABLED_MARKER, "true");
|
|
288
|
+
} else if (marked && control.getAttribute(DISABLED_MARKER) !== "true") {
|
|
289
|
+
this.#writeAttribute(control, DISABLED_MARKER, "true");
|
|
290
|
+
}
|
|
291
|
+
} else if (marked) {
|
|
292
|
+
this.#writeAttribute(control, "disabled", null);
|
|
293
|
+
this.#writeAttribute(control, DISABLED_MARKER, null);
|
|
294
|
+
}
|
|
52
295
|
}
|
|
53
296
|
}
|
|
54
297
|
/** Whether a region's declared condition holds across `triggers` per `match`. */
|
|
55
298
|
#isVisible(region, triggers) {
|
|
56
299
|
const predicate = this.#predicateFor(region);
|
|
57
300
|
if (predicate === null) return !region.hidden;
|
|
58
|
-
|
|
59
|
-
return this.matchValue === "all" ? triggers.every(predicate) : triggers.some(predicate);
|
|
301
|
+
return triggers.length > 0 && (this.matchValue === "all" ? triggers.every(predicate) : triggers.some(predicate));
|
|
60
302
|
}
|
|
61
|
-
/** Builds the per-trigger predicate from the region's
|
|
303
|
+
/** Builds the deterministic per-trigger predicate from the region's condition. */
|
|
62
304
|
#predicateFor(region) {
|
|
63
305
|
if (region.hasAttribute("data-when-checked")) {
|
|
64
306
|
return (trigger) => this.#isChecked(trigger);
|
|
@@ -75,35 +317,87 @@ var ConditionalFieldsController = class extends Controller {
|
|
|
75
317
|
#isChecked(trigger) {
|
|
76
318
|
return trigger instanceof HTMLInputElement && trigger.checked;
|
|
77
319
|
}
|
|
78
|
-
/** A trigger
|
|
320
|
+
/** A trigger has a value when a radio/checkbox is selected, or its control value matches. */
|
|
79
321
|
#matchesValue(trigger, wanted) {
|
|
80
322
|
if (trigger instanceof HTMLInputElement && (trigger.type === "checkbox" || trigger.type === "radio")) {
|
|
81
323
|
return trigger.checked && trigger.value === wanted;
|
|
82
324
|
}
|
|
83
325
|
return trigger.value === wanted;
|
|
84
326
|
}
|
|
85
|
-
/**
|
|
86
|
-
#
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
327
|
+
/** Moves focus to the first safe trigger, or to the controller landmark as a fallback. */
|
|
328
|
+
#retreatFocus(region, plan) {
|
|
329
|
+
const active = document.activeElement;
|
|
330
|
+
if (active && region.contains(active)) this.#moveFocus(plan);
|
|
331
|
+
}
|
|
332
|
+
/** Moves focus after the caller establishes that the active node is being hidden. */
|
|
333
|
+
#moveFocus(plan) {
|
|
334
|
+
const destination = this.triggerTargets.find(
|
|
335
|
+
(trigger) => canTakeFocus(trigger) && this.#survivesPlan(trigger, plan)
|
|
336
|
+
);
|
|
337
|
+
if (destination) {
|
|
338
|
+
this.#rootTabindex.returnAll();
|
|
339
|
+
destination.focus();
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
this.#rootTabindex.lend(this.element);
|
|
343
|
+
this.element.focus();
|
|
344
|
+
}
|
|
345
|
+
/** Whether a focus candidate sits outside every region this pass will hide. */
|
|
346
|
+
#survivesPlan(candidate, plan) {
|
|
347
|
+
return [...plan].every(([region, visible]) => visible || !region.contains(candidate));
|
|
348
|
+
}
|
|
349
|
+
/** Records one idempotent attribute write before MutationObserver sees it. */
|
|
350
|
+
#writeAttribute(element, name, value) {
|
|
351
|
+
if (element.getAttribute(name) !== value) {
|
|
352
|
+
if (this.#observing) {
|
|
353
|
+
let writes = this.#internalWrites.get(element);
|
|
354
|
+
if (!writes) {
|
|
355
|
+
writes = /* @__PURE__ */ new Map();
|
|
356
|
+
this.#internalWrites.set(element, writes);
|
|
94
357
|
}
|
|
95
|
-
|
|
96
|
-
control.disabled = false;
|
|
97
|
-
control.removeAttribute(DISABLED_MARKER);
|
|
358
|
+
writes.set(name, value);
|
|
98
359
|
}
|
|
360
|
+
if (value === null) element.removeAttribute(name);
|
|
361
|
+
else element.setAttribute(name, value);
|
|
99
362
|
}
|
|
100
363
|
}
|
|
101
|
-
/**
|
|
102
|
-
#
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
364
|
+
/** Separates authored records from controller writes and queues one relevant repair. */
|
|
365
|
+
#onMutations(records) {
|
|
366
|
+
const triggers = this.triggerTargets;
|
|
367
|
+
const regions = this.regionTargets;
|
|
368
|
+
const touchedWrites = /* @__PURE__ */ new Map();
|
|
369
|
+
let needsReconcile = false;
|
|
370
|
+
for (const record of records) {
|
|
371
|
+
if (record.type === "attributes" && record.target instanceof Element) {
|
|
372
|
+
const name = record.attributeName;
|
|
373
|
+
const writes = this.#internalWrites.get(record.target);
|
|
374
|
+
if (name && writes?.has(name)) {
|
|
375
|
+
let names = touchedWrites.get(record.target);
|
|
376
|
+
if (!names) {
|
|
377
|
+
names = /* @__PURE__ */ new Set();
|
|
378
|
+
touchedWrites.set(record.target, names);
|
|
379
|
+
}
|
|
380
|
+
names.add(name);
|
|
381
|
+
if (record.target.getAttribute(name) === writes.get(name)) continue;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (record.target instanceof Element && this.#elementAffectsState(record.target, triggers, regions)) {
|
|
385
|
+
needsReconcile = true;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
for (const [element, names] of touchedWrites) {
|
|
389
|
+
const writes = this.#internalWrites.get(element);
|
|
390
|
+
for (const name of names) writes?.delete(name);
|
|
391
|
+
}
|
|
392
|
+
if (needsReconcile) this.#reconcile.schedule();
|
|
393
|
+
}
|
|
394
|
+
/** Whether an authored mutation target belongs to a trigger, region, or managed control. */
|
|
395
|
+
#elementAffectsState(target, triggers, regions) {
|
|
396
|
+
return triggers.some((trigger) => trigger === target || trigger.contains(target)) || regions.some((region) => region === target || region.contains(target));
|
|
397
|
+
}
|
|
398
|
+
/** Whether a form owns at least one current trigger, including `form=` associations. */
|
|
399
|
+
#hasTriggerOwnedBy(form) {
|
|
400
|
+
return this.triggerTargets.some((trigger) => trigger.form === form);
|
|
107
401
|
}
|
|
108
402
|
};
|
|
109
403
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/controllers/conditional_fields_controller.ts"],"names":[],"mappings":";;;AAQA,IAAM,eAAA,GAAkB,2BAAA;AA2BjB,IAAM,2BAAA,GAAN,cAA0C,UAAA,CAAwB;AAAA,EACvE,OAAgB,OAAA,GAAU,CAAC,SAAA,EAAW,QAAQ,CAAA;AAAA,EAC9C,OAAgB,MAAA,GAAS;AAAA,IACvB,aAAA,EAAe,EAAE,IAAA,EAAM,OAAA,EAAS,SAAS,IAAA,EAAK;AAAA,IAC9C,KAAA,EAAO,EAAE,IAAA,EAAM,MAAA,EAAQ,SAAS,KAAA;AAAM,GACxC;AAAA,EACA,OAAO,OAAA,GAAU,CAAC,UAAU,CAAA;AAAA,EAC5B,OAAO,MAAA,GAAS,CAAC,QAAQ,CAAA;AAAA,EAShB,YAAA,uBAAmB,OAAA,EAA8B;AAAA,EAEjD,YAAY,MAAY;AAC/B,IAAA,IAAA,CAAK,QAAA,EAAS;AAAA,EAChB,CAAA;AAAA,EAES,OAAA,GAAgB;AACvB,IAAA,IAAA,CAAK,QAAA,EAAS;AACd,IAAA,IAAA,CAAK,OAAA,CAAQ,gBAAA,CAAiB,QAAA,EAAU,IAAA,CAAK,SAAS,CAAA;AACtD,IAAA,IAAA,CAAK,OAAA,CAAQ,gBAAA,CAAiB,OAAA,EAAS,IAAA,CAAK,SAAS,CAAA;AAAA,EACvD;AAAA,EAES,UAAA,GAAmB;AAC1B,IAAA,IAAA,CAAK,OAAA,CAAQ,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.js","sourcesContent":["import { Controller } from \"@hotwired/stimulus\";\n\n/** Trigger controls whose state drives region visibility. */\ntype Trigger = HTMLInputElement | HTMLSelectElement;\n/** Form controls inside a region that get disabled while hidden. */\ntype Disableable = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | HTMLButtonElement;\n\n/** Marker on inputs this controller disabled (so we never re-enable authored-disabled ones). */\nconst DISABLED_MARKER = \"data-conditional-disabled\";\n\n/**\n * Headless dependent-fields behavior: shows/hides (and enables/disables) regions\n * based on trigger control values, keeping `hidden` / `aria-hidden` / `disabled` in\n * sync (no dedicated APG pattern).\n *\n * Markup contract (identifier: `stimeo--conditional-fields`):\n * <form data-controller=\"stimeo--conditional-fields\">\n * <input type=\"checkbox\" data-stimeo--conditional-fields-target=\"trigger\">\n * <fieldset data-stimeo--conditional-fields-target=\"region\" data-when-checked>\n * …address…\n * </fieldset>\n * </form>\n *\n * Each region declares its condition with a data attribute: `data-when-checked`,\n * `data-when-unchecked`, or `data-when-value=\"x\"`. When the condition (combined\n * across triggers per `match`) holds the region is shown and its inputs enabled;\n * otherwise it is hidden, `aria-hidden`, and — with `disableHidden` — its inputs are\n * disabled so they drop out of the submission. Hiding a region that holds focus\n * first retreats focus to a trigger.\n *\n * @remarks\n * Behavior only. Visibility/enabled state lives entirely on the elements' `hidden` /\n * `disabled` / `aria-hidden`, re-derived from triggers on `connect()` (Morph-safe).\n * The change/input listener is removed on `disconnect()` (Turbo navigation included).\n */\nexport class ConditionalFieldsController extends Controller<HTMLElement> {\n static override targets = [\"trigger\", \"region\"];\n static override values = {\n disableHidden: { type: Boolean, default: true },\n match: { type: String, default: \"any\" },\n };\n static actions = [\"evaluate\"] as const;\n static events = [\"change\"] as const;\n\n declare readonly triggerTargets: Trigger[];\n declare readonly regionTargets: HTMLElement[];\n declare readonly hasTriggerTarget: boolean;\n\n declare disableHiddenValue: boolean;\n declare matchValue: string;\n\n readonly #lastVisible = new WeakMap<HTMLElement, boolean>();\n\n readonly #onChange = (): void => {\n this.evaluate();\n };\n\n override connect(): void {\n this.evaluate();\n this.element.addEventListener(\"change\", this.#onChange);\n this.element.addEventListener(\"input\", this.#onChange);\n }\n\n override disconnect(): void {\n this.element.removeEventListener(\"change\", this.#onChange);\n this.element.removeEventListener(\"input\", this.#onChange);\n }\n\n /** Re-evaluates every region against the current trigger state. */\n evaluate(): void {\n // Resolved once per pass: this runs per keystroke (input/change bubble up from\n // the whole form) and every Stimulus target access re-queries the scope.\n const triggers = this.triggerTargets;\n for (const region of this.regionTargets) {\n this.#applyRegion(region, this.#isVisible(region, triggers));\n }\n }\n\n /** Applies a region's visibility, syncing hidden/aria/disabled and emitting change. */\n #applyRegion(region: HTMLElement, visible: boolean): void {\n const previous = this.#lastVisible.get(region) ?? !region.hidden;\n if (visible === previous && this.#lastVisible.has(region)) return;\n this.#lastVisible.set(region, visible);\n\n if (visible) {\n region.hidden = false;\n region.removeAttribute(\"aria-hidden\");\n region.setAttribute(\"data-visible\", \"true\");\n this.#setRegionDisabled(region, false);\n } else {\n this.#retreatFocus(region);\n region.hidden = true;\n region.setAttribute(\"aria-hidden\", \"true\");\n region.removeAttribute(\"data-visible\");\n this.#setRegionDisabled(region, true);\n }\n\n if (visible !== previous) {\n this.dispatch(\"change\", { detail: { region, visible } });\n }\n }\n\n /** Whether a region's declared condition holds across `triggers` per `match`. */\n #isVisible(region: HTMLElement, triggers: Trigger[]): boolean {\n const predicate = this.#predicateFor(region);\n if (predicate === null) return !region.hidden; // no condition declared → leave as-is\n if (triggers.length === 0) return false;\n return this.matchValue === \"all\" ? triggers.every(predicate) : triggers.some(predicate);\n }\n\n /** Builds the per-trigger predicate from the region's `data-when-*` attribute. */\n #predicateFor(region: HTMLElement): ((trigger: Trigger) => boolean) | null {\n if (region.hasAttribute(\"data-when-checked\")) {\n return (trigger) => this.#isChecked(trigger);\n }\n if (region.hasAttribute(\"data-when-unchecked\")) {\n return (trigger) => !this.#isChecked(trigger);\n }\n const wanted = region.dataset.whenValue;\n if (wanted !== undefined) {\n return (trigger) => this.#matchesValue(trigger, wanted);\n }\n return null;\n }\n\n #isChecked(trigger: Trigger): boolean {\n return trigger instanceof HTMLInputElement && trigger.checked;\n }\n\n /** A trigger \"has\" a value: selected radio/checkbox value, or the control value. */\n #matchesValue(trigger: Trigger, wanted: string): boolean {\n if (\n trigger instanceof HTMLInputElement &&\n (trigger.type === \"checkbox\" || trigger.type === \"radio\")\n ) {\n return trigger.checked && trigger.value === wanted;\n }\n return trigger.value === wanted;\n }\n\n /** Enables/disables a region's inputs, tracking only the ones we disabled. */\n #setRegionDisabled(region: HTMLElement, disabled: boolean): void {\n if (!this.disableHiddenValue) return;\n const controls = region.querySelectorAll<Disableable>(\"input, textarea, select, button\");\n for (const control of Array.from(controls)) {\n if (disabled) {\n if (!control.disabled) {\n control.disabled = true;\n control.setAttribute(DISABLED_MARKER, \"true\");\n }\n } else if (control.hasAttribute(DISABLED_MARKER)) {\n control.disabled = false;\n control.removeAttribute(DISABLED_MARKER);\n }\n }\n }\n\n /** Moves focus out of a region about to be hidden, to a trigger when possible. */\n #retreatFocus(region: HTMLElement): void {\n const active = document.activeElement;\n if (!(active instanceof HTMLElement) || !region.contains(active)) return;\n active.blur();\n if (this.hasTriggerTarget) this.triggerTargets[0]?.focus();\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/focus_candidate.ts","../../src/utils/microtask_coalescer.ts","../../src/utils/before_cache_reset.ts","../../src/utils/tabindex_loan.ts","../../src/controllers/conditional_fields_controller.ts"],"names":[],"mappings":";;;;;AAWO,SAAS,yBAAyB,OAAA,EAA+B;AACtE,EAAA,IAAI,QAAA,GAA2B,OAAA,CAAQ,OAAA,CAAQ,oBAAoB,CAAA;AACnE,EAAA,OAAO,QAAA,EAAU;AACf,IAAA,MAAM,MAAA,GAAS,KAAA,CAAM,IAAA,CAAK,QAAA,CAAS,QAAQ,CAAA,CAAE,IAAA,CAAK,CAAC,KAAA,KAAU,KAAA,CAAM,OAAA,KAAY,Q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A,CAAS,KAAK,CAAC,OAAA,KAAY,YAAY,MAAA,IAAU,OAAA,CAAQ,SAAS,MAAM,CAAC,KACzE,OAAA,CAAQ,IAAA,CAAK,CAAC,MAAA,KAAW,MAAA,KAAW,UAAU,MAAA,CAAO,QAAA,CAAS,MAAM,CAAC,CAAA;AAAA,EAEzE;AAAA;AAAA,EAGA,mBAAmB,IAAA,EAAgC;AACjD,IAAA,OAAO,KAAK,cAAA,CAAe,IAAA,CAAK,CAAC,OAAA,KAAY,OAAA,CAAQ,SAAS,IAAI,CAAA;AAAA,EACpE;AACF","file":"conditional_fields_controller.js","sourcesContent":["/**\n * Whether a disabled `<fieldset>` ancestor actually reaches `control`.\n *\n * HTML exempts the contents of a fieldset's **first direct-child `<legend>`**, and\n * the exemption is per fieldset — a control legal in one legend can still be\n * disabled by a second, outer fieldset, so the walk continues upward.\n *\n * Exported for the consumer that needs this rule *without* the rest of\n * {@link canTakeFocus}: `toolbar` bounds its own `hidden` walk at the toolbar\n * root, which is a different rule, so it composes the two itself.\n */\nexport function inheritsFieldsetDisabled(control: HTMLElement): boolean {\n let fieldset: Element | null = control.closest(\"fieldset[disabled]\");\n while (fieldset) {\n const legend = Array.from(fieldset.children).find((child) => child.tagName === \"LEGEND\");\n if (!legend?.contains(control)) return true;\n fieldset = fieldset.parentElement?.closest(\"fieldset[disabled]\") ?? null;\n }\n return false;\n}\n\n/**\n * Whether an element can actually take focus, checked **before** `focus()` runs.\n *\n * A controller that must move focus off a control it is about to disable or hide\n * picks a destination and calls `focus()` on it. If that destination cannot take\n * focus, the call **fails silently**: `hidden` and natively `disabled` elements\n * swallow it, so the caret stays in the subtree that is disappearing and lands on\n * `<body>` a frame later — the exact outcome the rescue exists to prevent, minus\n * any signal that it happened.\n *\n * Testing after the fact is the obvious alternative and is deliberately not the\n * rule. Reading `document.activeElement` back only works in a real browser, and\n * looping over candidates that way performs a real focus move per failure —\n * observable to assistive technology. Checking first costs nothing and catches\n * the cases that actually occur.\n *\n * **`aria-disabled` is not disqualifying.** It is the attribute an author uses\n * for a control that must stay *discoverable*, and the roving contract keeps\n * such items reachable. Only the three conditions that make the platform refuse\n * focus are checked: `hidden` / `inert` (an ancestor's counts),\n * `input[type=\"hidden\"]`, the native `disabled` property, and `disabled`\n * inherited from an ancestor `fieldset`. CSS-only invisibility is handled by\n * {@link isRenderedForFocus} when a consumer needs sequential-focus semantics.\n *\n * Reading `:disabled` instead of walking the fieldset chain would be shorter, but\n * that pseudo-class is not evaluated consistently outside real browsers and this\n * has to be right headlessly too. happy-dom in particular focuses a `<button>`\n * inside a disabled fieldset where a real engine refuses, so the inheritance is\n * spelled out rather than delegated.\n *\n * What a consumer does when nothing survives is its own call: some fall back to\n * their landmark, while a widget whose caret already sits somewhere legitimate\n * refuses the move outright rather than relocating it.\n *\n * @example\n * ```ts\n * const target = candidates.find(canTakeFocus);\n * if (target) target.focus();\n * else {\n * this.#tabindex.lend(this.element); // nothing left: fall back to the landmark\n * this.element.focus();\n * }\n * ```\n *\n * @param element - the candidate destination\n */\nexport function canTakeFocus(element: HTMLElement): boolean {\n if (element.closest(\"[hidden], [inert]\")) return false;\n if (element instanceof HTMLInputElement && element.type === \"hidden\") return false;\n if (!(\"disabled\" in element)) return true;\n if ((element as HTMLElement & { disabled: boolean }).disabled) return false;\n return !inheritsFieldsetDisabled(element);\n}\n\n/** Elements whose semantics or authored attributes can place them in sequential focus order. */\nexport const TAB_STOP_CANDIDATE_SELECTOR = [\n \"a[href]\",\n \"area[href]\",\n \"button\",\n \"input\",\n \"select\",\n \"textarea\",\n \"summary\",\n \"iframe\",\n \"audio[controls]\",\n \"video[controls]\",\n \"[tabindex]\",\n \"[contenteditable]\",\n].join(\",\");\n\n/** Optional browser visibility API used to exclude CSS-hidden candidates. */\ninterface VisibilityCheckable {\n checkVisibility?: (options?: { visibilityProperty?: boolean }) => boolean;\n}\n\n/** Whether CSS visibility allows an otherwise eligible element to participate in focus order. */\nexport function isRenderedForFocus(element: HTMLElement): boolean {\n const check = (element as HTMLElement & VisibilityCheckable).checkVisibility;\n return typeof check === \"function\" ? check.call(element, { visibilityProperty: true }) : true;\n}\n\n/** Parses an authored `tabindex`; invalid syntax has no explicit focus-order meaning. */\nfunction authoredTabindex(element: HTMLElement): number | null {\n const value = element.getAttribute(\"tabindex\");\n if (value === null || !/^[+-]?\\d+$/.test(value.trim())) return null;\n return Number(value);\n}\n\n/** Whether the element's native semantics place it in sequential focus order. */\nfunction hasNativeTabStop(element: HTMLElement): boolean {\n if (element instanceof HTMLAnchorElement || element instanceof HTMLAreaElement) {\n return element.hasAttribute(\"href\");\n }\n if (\n element instanceof HTMLButtonElement ||\n element instanceof HTMLSelectElement ||\n element instanceof HTMLTextAreaElement\n ) {\n return true;\n }\n if (element instanceof HTMLInputElement) return element.type !== \"hidden\";\n if (element instanceof HTMLIFrameElement) return true;\n if (element.tagName === \"AUDIO\" || element.tagName === \"VIDEO\") {\n return element.hasAttribute(\"controls\");\n }\n if (element instanceof HTMLElement && element.tagName === \"SUMMARY\") {\n const details = element.parentElement;\n return (\n details instanceof HTMLDetailsElement &&\n Array.from(details.children).find((child) => child.tagName === \"SUMMARY\") === element\n );\n }\n return false;\n}\n\n/** Whether an explicit `contenteditable` value creates an editable tab stop. */\nfunction hasEditableTabStop(element: HTMLElement): boolean {\n const value = element.getAttribute(\"contenteditable\")?.toLowerCase();\n return value === \"\" || value === \"true\" || value === \"plaintext-only\";\n}\n\n/**\n * Whether an element is a usable sequential Tab stop right now.\n *\n * Native semantics, authored `tabindex`, editable hosts, inherited disabled state,\n * HTML `hidden`/`inert`, and CSS visibility are evaluated together. `aria-disabled`\n * remains focusable because it communicates unavailability without removing the\n * control from discovery order.\n */\nexport function isTabStop(element: HTMLElement): boolean {\n if (!canTakeFocus(element) || !isRenderedForFocus(element)) return false;\n\n const tabindex = authoredTabindex(element);\n if (tabindex !== null) return tabindex >= 0;\n return hasNativeTabStop(element) || hasEditableTabStop(element);\n}\n\n/** Returns every usable sequential Tab stop below `root` in document order. */\nexport function tabStopsWithin(root: ParentNode): HTMLElement[] {\n return Array.from(root.querySelectorAll<HTMLElement>(TAB_STOP_CANDIDATE_SELECTOR)).filter(\n isTabStop,\n );\n}\n\n/** Returns the first usable sequential Tab stop below `root`, if one exists. */\nexport function firstTabStop(root: ParentNode): HTMLElement | null {\n for (const candidate of root.querySelectorAll<HTMLElement>(TAB_STOP_CANDIDATE_SELECTOR)) {\n if (isTabStop(candidate)) return candidate;\n }\n return null;\n}\n\n/** Whether `root` contains at least one usable sequential Tab stop. */\nexport function hasTabStop(root: ParentNode): boolean {\n return firstTabStop(root) !== null;\n}\n","/**\n * Collapses many Stimulus lifecycle callbacks from one DOM mutation into a\n * single pass.\n *\n * Stimulus fires `<name>TargetConnected` / `Disconnected` once per element and\n * `<name>ValueChanged` once per changed attribute. Replacing a list of N options\n * or morphing several render Values therefore delivers N callbacks — but the\n * useful unit of work is \"reconcile against the resulting declarative input\",\n * once, after the batch has settled. Every controller with reconcilable targets\n * or render Values needs the same shape: a `queued` flag plus `queueMicrotask`.\n *\n * **A microtask is the right horizon, and the reason is specific.** Stimulus\n * drives these callbacks from a `MutationObserver`, whose own callback already\n * runs as a microtask with the whole batch in hand; scheduling one more lands\n * after the last sibling callback of that batch and still before paint or any\n * event handler. A timer would be later than it needs to be, and reconciling\n * synchronously would run once per element against a half-applied DOM.\n *\n * **The two guards are not the same guard.** Scheduling is refused before the\n * controller connects, and running is refused after it disconnects:\n *\n * - **Before `connect()`** — Stimulus delivers initial target and Value callbacks\n * ahead of `connect()`. Reconciling there would compute output against a\n * controller whose own state has not been initialised, and `connect()` is\n * about to do a full pass anyway.\n * - **After `disconnect()`** — Stimulus fires a callback for **every** target\n * during teardown, and a microtask queued just before it would otherwise run\n * against a detached tree. {@link MicrotaskCoalescer.cancel} exists for the\n * teardown path to drop the pending pass outright.\n *\n * Both guards are part of one contract here rather than something each consumer\n * has to remember separately.\n *\n * Scope is the scheduling only. *What* to reconcile — keep the surviving active\n * option, fall back to the next / previous / first visible one, rebuild derived\n * chips or hidden fields — stays in the controller, because no two consumers\n * answer it the same way.\n *\n * This file's own doc block is dropped from `dist`, but every member comment is\n * inlined into each consumer entry (`tsup` builds with `splitting: false`), so\n * rationale belongs here and only the contract belongs on the members.\n *\n * @example\n * ```ts\n * readonly #reconcile = new MicrotaskCoalescer(() => this.#reconcileOptions());\n *\n * connect() { this.#reconcile.activate(); }\n * disconnect() { this.#reconcile.cancel(); }\n *\n * optionTargetConnected() { this.#reconcile.schedule(); }\n * optionTargetDisconnected() { this.#reconcile.schedule(); }\n * ```\n */\nexport class MicrotaskCoalescer {\n readonly #run: () => void;\n #queued = false;\n #active = false;\n #generation = 0;\n\n /** @param run - the single reconciliation pass, invoked at most once per batch. */\n constructor(run: () => void) {\n this.#run = run;\n }\n\n /** Opens the window in which {@link schedule} is honoured; call from `connect()`. */\n activate(): void {\n this.#active = true;\n }\n\n /** Closes the window and drops any pending pass; call from `disconnect()`. */\n cancel(): void {\n this.#active = false;\n this.#queued = false;\n this.#generation += 1;\n }\n\n /** Requests one pass after the batch settles. Idempotent; inert outside the window. */\n schedule(): void {\n if (!this.#active || this.#queued) return;\n this.#queued = true;\n const generation = this.#generation;\n queueMicrotask(() => {\n // A cancelled callback must not consume a pass queued after reconnect.\n if (generation !== this.#generation || !this.#queued || !this.#active) return;\n this.#queued = false;\n this.#run();\n });\n }\n}\n","/**\n * Runs a controller's \"return to the initial state\" pass just before Turbo\n * caches the page.\n *\n * **`disconnect()` cannot do this job, for two independent reasons.** Turbo\n * queues the clone from this event rather than taking it here, and the body swap\n * that runs the controller's `disconnect()` is queued separately — so which of\n * the two lands first is not something a controller can rely on, and a rewind\n * written in `disconnect()` may reach only the DOM being thrown away. In the\n * other direction, `disconnect()` also fires on an in-page move (Stimulus tears\n * down and reconnects the same element), where rewinding would wipe a\n * legitimately in-progress interaction — a spinner mid-load would vanish. One\n * timing is unreliable, the other is too eager; `turbo:before-cache` is the only\n * point that is exactly \"the page is about to be frozen\".\n *\n * Scope is the subscription only: registering on `activate()`, unregistering on\n * `deactivate()`, and one shared document listener no matter how many instances\n * are live. *What* to return to its initial state — which `data-state`, which\n * `hidden`, which `aria-busy` — stays in the controller, because no two\n * consumers answer it the same way (the `MicrotaskCoalescer` split).\n *\n * **Rewind state, not appearance.** The pass writes attributes the controller\n * itself owns; the visual result of those attributes is the consumer's CSS, and\n * a library that reached for style or class names would be guessing at markup\n * it does not own.\n *\n * Both entry points are idempotent, so the lifecycle hooks can call them\n * unconditionally: a second `activate()` does not double-subscribe and does not\n * make the callback run twice, and `deactivate()` on an instance that never\n * subscribed is a no-op.\n *\n * This file's own doc block is dropped from `dist`, but every member comment is\n * inlined into each consumer entry (`tsup` builds with `splitting: false`), so\n * rationale belongs here and only the contract belongs on the members.\n *\n * @example\n * ```ts\n * readonly #beforeCache = new BeforeCacheReset(() => this.#rewind());\n *\n * connect() { this.#beforeCache.activate(); }\n * disconnect() { this.#beforeCache.deactivate(); }\n * ```\n */\nexport class BeforeCacheReset {\n /** Every subscribed instance, iterated by the one shared document listener. */\n static readonly #subscribers = new Set<BeforeCacheReset>();\n\n /** The shared listener; installed while at least one instance is subscribed. */\n static readonly #onBeforeCache = (): void => {\n for (const subscriber of BeforeCacheReset.#subscribers) subscriber.#rewind();\n };\n\n readonly #rewind: () => void;\n\n /** @param rewind - the pass that returns this controller's state to its initial form. */\n constructor(rewind: () => void) {\n this.#rewind = rewind;\n }\n\n /** Subscribes to `turbo:before-cache`; call from `connect()`. Idempotent. */\n activate(): void {\n const first = BeforeCacheReset.#subscribers.size === 0;\n BeforeCacheReset.#subscribers.add(this);\n if (first) {\n document.addEventListener(\"turbo:before-cache\", BeforeCacheReset.#onBeforeCache);\n }\n }\n\n /** Unsubscribes; call from `disconnect()`. Safe when never subscribed. */\n deactivate(): void {\n BeforeCacheReset.#subscribers.delete(this);\n if (BeforeCacheReset.#subscribers.size > 0) return;\n document.removeEventListener(\"turbo:before-cache\", BeforeCacheReset.#onBeforeCache);\n }\n}\n","import { BeforeCacheReset } from \"./before_cache_reset\";\n\n/**\n * Shared bookkeeping for a `tabindex` a controller lends an element temporarily.\n *\n * A controller that must move focus somewhere the author never made focusable\n * (a landmark root, a scroll destination) reaches for the same trick: add a\n * `tabindex` just-in-time and hand it back once it is no longer needed. The\n * borrow is the easy half; the return is what the two conditions below are for.\n *\n * **Returning needs two conditions, not one.** Owning the borrow is not enough:\n * the attribute must also still hold the value this instance wrote. A consumer\n * that changed it afterwards — `tabindex=\"0\"` to make the root its own Tab stop\n * — owns it now, and removing it there silently discards authored markup. The\n * bookkeeping is dropped either way, since the loan is over regardless of who\n * ends up owning the value.\n *\n * **Never borrow over an existing value.** An element that already carries a\n * `tabindex` is the author's to control, so there is nothing to lend and nothing\n * to return.\n *\n * Every live loan also owns a shared `turbo:before-cache` subscription. The loan\n * is returned before Turbo can copy it into a snapshot, so consumers get cache\n * safety without duplicating a lifecycle hook; `returnAll()` removes the\n * subscription again as soon as no loan remains.\n *\n * The registry is keyed by element, so a controller borrowing on a single\n * element (`this.element`) and one borrowing across a changing set of targets\n * use the same API — the single-element case is a set of one. It holds no\n * opinion about *when* to borrow or where focus goes next; that stays in the\n * controller.\n *\n * **The API is deliberately two methods.** This file's own doc block is dropped\n * from `dist`, but every member comment is inlined into **each** consumer entry\n * (`tsup` builds with `splitting: false`), so rationale belongs here, only the\n * contract belongs on the members, and every method no consumer calls is still\n * paid for once per consumer entry.\n *\n * @example\n * ```ts\n * readonly #tabindex = new TabindexLoan();\n *\n * #rescueFocus() {\n * this.#tabindex.lend(this.element);\n * this.element.focus();\n * }\n *\n * disconnect() {\n * this.#tabindex.returnAll();\n * }\n * ```\n */\nexport class TabindexLoan<T extends HTMLElement = HTMLElement> {\n readonly #value: string;\n readonly #lent = new Set<T>();\n /** Returns live loans before Turbo can copy them into its page snapshot. */\n readonly #beforeCache = new BeforeCacheReset(() => this.returnAll());\n\n /**\n * @param value - the `tabindex` to lend. `\"-1\"` (the default) is\n * programmatically focusable but not a Tab stop; `\"0\"` is a real Tab stop,\n * which a scroll region with no focusable content of its own needs.\n */\n constructor(value: string = \"-1\") {\n this.#value = value;\n }\n\n /** Lends `element` the value; no-ops when it already carries a `tabindex`. */\n lend(element: T): void {\n if (element.hasAttribute(\"tabindex\")) return;\n element.setAttribute(\"tabindex\", this.#value);\n this.#lent.add(element);\n // Subscribe only while a real loan exists. Keeping this guarantee here means\n // every consumer is Turbo-safe without another lifecycle hook to remember.\n this.#beforeCache.activate();\n }\n\n /** Takes back every loan whose value is still the one that was lent. */\n returnAll(): void {\n for (const element of this.#lent) {\n if (element.getAttribute(\"tabindex\") === this.#value) element.removeAttribute(\"tabindex\");\n }\n this.#lent.clear();\n this.#beforeCache.deactivate();\n }\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { canTakeFocus } from \"../utils/focus_candidate\";\nimport { MicrotaskCoalescer } from \"../utils/microtask_coalescer\";\nimport { TabindexLoan } from \"../utils/tabindex_loan\";\n\n/** Trigger controls whose live state drives region visibility. */\ntype Trigger = HTMLInputElement | HTMLSelectElement;\n/** Form controls inside a region that can be excluded from submission. */\ntype Disableable = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | HTMLButtonElement;\n/** One logical visibility transition settled during an evaluation pass. */\ntype RegionTransition = { region: HTMLElement; visible: boolean };\n\n/** Marker on controls this component disabled, so authored disabled state remains untouched. */\nconst DISABLED_MARKER = \"data-conditional-disabled\";\n\n/** Retained-DOM inputs and controller outputs that can require reconciliation. */\nconst OBSERVED_ATTRIBUTES = [\n \"hidden\",\n \"aria-hidden\",\n \"data-visible\",\n DISABLED_MARKER,\n \"disabled\",\n \"data-when-checked\",\n \"data-when-unchecked\",\n \"data-when-value\",\n \"checked\",\n \"selected\",\n \"value\",\n \"type\",\n];\n\n/** Attribute value expected after one controller-owned DOM write. */\ntype ExpectedAttribute = string | null;\n\n/**\n * Headless dependent-fields behavior: shows/hides (and enables/disables) regions\n * from trigger control values while keeping submission and accessibility state in\n * sync (no dedicated APG pattern).\n *\n * Markup contract (identifier: `stimeo--conditional-fields`):\n * <form data-controller=\"stimeo--conditional-fields\">\n * <input type=\"checkbox\" data-stimeo--conditional-fields-target=\"trigger\">\n * <fieldset data-stimeo--conditional-fields-target=\"region\" data-when-checked>\n * …address…\n * </fieldset>\n * </form>\n *\n * A region declares one condition: `data-when-checked`, `data-when-unchecked`,\n * or `data-when-value=\"x\"`. The condition is combined across all triggers using\n * `match` (`\"any\"` by default, or `\"all\"`). A region with no condition preserves\n * its authored `hidden` state; a declared condition with no triggers is false.\n *\n * Hidden regions receive `hidden` and `aria-hidden=\"true\"`; visible regions expose\n * `data-visible=\"true\"`. With `disableHidden`, descendant controls are disabled and\n * marked with `data-conditional-disabled=\"true\"`, and only marked controls are\n * re-enabled. Before a focused region is hidden, focus moves to the first trigger\n * that can actually receive it. If every trigger is unsafe — including one inside\n * another region being hidden in the same pass — the controller root temporarily\n * receives `tabindex=\"-1\"` and focus.\n *\n * `change` and `reconcile` dispatch `{ region, visible }`.\n *\n * @remarks\n * Behavior only. Trigger `input` / `change`, dynamic targets and descendants,\n * condition attributes, Values, native form reset, and Turbo retained-element\n * morphs all reconcile against the live DOM. Direct writes to an input's live\n * `checked` / `value` property remain observable only when the caller also emits\n * the corresponding native event. DOM repair is silent. User input or the public\n * action dispatches `stimeo--conditional-fields:change`; a lifecycle, Value,\n * reset, mutation, or morph reconciliation dispatches\n * `stimeo--conditional-fields:reconcile` instead. Initial connection is silent.\n * Every listener, observer, queued pass, and borrowed root `tabindex` is released\n * on `disconnect()`.\n */\nexport class ConditionalFieldsController extends Controller<HTMLElement> {\n static override targets = [\"trigger\", \"region\"];\n static override values = {\n disableHidden: { type: Boolean, default: true },\n match: { type: String, default: \"any\" },\n };\n static actions = [\"evaluate\"] as const;\n static events = [\"change\", \"reconcile\"] as const;\n\n declare readonly triggerTargets: Trigger[];\n declare readonly regionTargets: HTMLElement[];\n\n declare disableHiddenValue: boolean;\n declare matchValue: string;\n\n /** Logical state last committed for each current region; rebuilt on every connect. */\n #lastVisible = new WeakMap<HTMLElement, boolean>();\n /** Controller writes awaiting an observer callback, separated from authored morphs. */\n #internalWrites = new WeakMap<Element, Map<string, ExpectedAttribute>>();\n #observing = false;\n\n /** Coalesces one target/morph/mutation batch into one full DOM reconciliation. */\n readonly #reconcile = new MicrotaskCoalescer(() => this.#reconcileDom());\n /** Provides the last-resort focus landmark without claiming an authored tabindex. */\n readonly #rootTabindex = new TabindexLoan<HTMLElement>();\n /** Watches retained targets and controls whose declarative or reflected state changed. */\n readonly #observer = new MutationObserver((records) => this.#onMutations(records));\n\n /** Evaluates only events whose source is one of this instance's live trigger targets. */\n readonly #onTriggerInput = (event: Event): void => {\n const target = event.target;\n const triggers = this.triggerTargets;\n if (target instanceof Element && triggers.some((trigger) => trigger === target)) {\n this.#commitChange(triggers);\n }\n };\n\n /** Reconciles property-only retained-element changes after Turbo morphs. */\n readonly #onMorph = (): void => {\n this.#reconcile.schedule();\n };\n\n /** Reconciles the settled defaults after a non-cancelled owning form reset. */\n readonly #onReset = (event: Event): void => {\n const form = event.target;\n if (form instanceof HTMLFormElement && this.#hasTriggerOwnedBy(form)) {\n queueMicrotask(() => {\n if (!event.defaultPrevented) this.#reconcile.schedule();\n });\n }\n };\n\n /** Reflects initial live state and opens every retained-DOM reconciliation path. */\n override connect(): void {\n this.#lastVisible = new WeakMap();\n this.#internalWrites = new WeakMap();\n this.#reconcile.activate();\n this.#settle();\n this.#observer.observe(this.element, {\n attributes: true,\n attributeFilter: OBSERVED_ATTRIBUTES,\n childList: true,\n subtree: true,\n });\n this.#observing = true;\n this.element.addEventListener(\"change\", this.#onTriggerInput);\n this.element.addEventListener(\"input\", this.#onTriggerInput);\n this.element.addEventListener(\"turbo:morph-element\", this.#onMorph);\n document.addEventListener(\"reset\", this.#onReset, true);\n }\n\n /** Releases all external resources and returns the temporary focus landmark loan. */\n override disconnect(): void {\n this.#reconcile.cancel();\n this.#observing = false;\n this.#observer.disconnect();\n this.element.removeEventListener(\"change\", this.#onTriggerInput);\n this.element.removeEventListener(\"input\", this.#onTriggerInput);\n this.element.removeEventListener(\"turbo:morph-element\", this.#onMorph);\n document.removeEventListener(\"reset\", this.#onReset, true);\n this.#rootTabindex.returnAll();\n }\n\n /** Reconciles after a trigger target is inserted or restored. */\n triggerTargetConnected(): void {\n this.#reconcile.schedule();\n }\n\n /** Reconciles after a trigger target is removed or replaced. */\n triggerTargetDisconnected(): void {\n this.#reconcile.schedule();\n }\n\n /** Initializes a region target inserted or restored at runtime. */\n regionTargetConnected(): void {\n this.#reconcile.schedule();\n }\n\n /** Reconciles the remaining region set after a target leaves. */\n regionTargetDisconnected(): void {\n this.#reconcile.schedule();\n }\n\n /** Reconciles when application code or a morph changes the match policy. */\n matchValueChanged(): void {\n this.#reconcile.schedule();\n }\n\n /** Reconciles control ownership when hidden controls become enabled or excluded. */\n disableHiddenValueChanged(): void {\n this.#reconcile.schedule();\n }\n\n /** Re-evaluates every region and repairs all reflected DOM state. */\n evaluate(): void {\n this.#commitChange();\n }\n\n /**\n * Computes one settled plan, shows destinations before hiding sources, and then\n * returns logical transitions in DOM order after every region is internally coherent.\n */\n #settle(triggers: Trigger[] = this.triggerTargets): RegionTransition[] {\n const regions = this.regionTargets;\n const plan = new Map<HTMLElement, boolean>();\n for (const region of regions) plan.set(region, this.#isVisible(region, triggers));\n\n const changed = new Set<HTMLElement>();\n const applicationOrder = [\n ...regions.filter((region) => plan.get(region) === true),\n ...regions.filter((region) => plan.get(region) === false),\n ];\n for (const region of applicationOrder) {\n const visible = plan.get(region);\n if (visible !== undefined && this.#applyRegion(region, visible, plan)) {\n changed.add(region);\n }\n }\n\n return regions\n .filter((region) => changed.has(region))\n .map((region) => ({ region, visible: plan.get(region) === true }));\n }\n\n /** Reports logical transitions caused by user input or the public action. */\n #commitChange(triggers: Trigger[] = this.triggerTargets): void {\n for (const detail of this.#settle(triggers)) {\n this.dispatch(\"change\", { detail });\n }\n }\n\n /** Reports logical transitions derived from DOM, lifecycle, reset, or Value state. */\n #reconcileDom(): void {\n for (const detail of this.#settle()) {\n this.dispatch(\"reconcile\", { detail });\n }\n }\n\n /** Applies one planned state while keeping event history separate from DOM repair. */\n #applyRegion(\n region: HTMLElement,\n visible: boolean,\n plan: ReadonlyMap<HTMLElement, boolean>,\n ): boolean {\n const previous = this.#lastVisible.get(region) ?? !region.hidden;\n const changed = visible !== previous;\n this.#lastVisible.set(region, visible);\n\n if (!visible) this.#retreatFocus(region, plan);\n this.#syncRegionState(region, visible);\n this.#syncRegionControls(region, visible);\n return changed;\n }\n\n /** Reflects visibility idempotently, recording writes so the observer ignores its own work. */\n #syncRegionState(region: HTMLElement, visible: boolean): void {\n this.#writeAttribute(region, \"hidden\", visible ? null : \"\");\n this.#writeAttribute(region, \"aria-hidden\", visible ? null : \"true\");\n this.#writeAttribute(region, \"data-visible\", visible ? \"true\" : null);\n }\n\n /** Enables/disables descendant controls, touching only state carrying our marker. */\n #syncRegionControls(region: HTMLElement, visible: boolean): void {\n const shouldDisable = !visible && this.disableHiddenValue;\n const controls = region.querySelectorAll<Disableable>(\"input, textarea, select, button\");\n for (const control of controls) {\n const marked = control.hasAttribute(DISABLED_MARKER);\n if (shouldDisable) {\n if (!control.disabled) {\n this.#writeAttribute(control, \"disabled\", \"\");\n this.#writeAttribute(control, DISABLED_MARKER, \"true\");\n } else if (marked && control.getAttribute(DISABLED_MARKER) !== \"true\") {\n this.#writeAttribute(control, DISABLED_MARKER, \"true\");\n }\n } else if (marked) {\n this.#writeAttribute(control, \"disabled\", null);\n this.#writeAttribute(control, DISABLED_MARKER, null);\n }\n }\n }\n\n /** Whether a region's declared condition holds across `triggers` per `match`. */\n #isVisible(region: HTMLElement, triggers: Trigger[]): boolean {\n const predicate = this.#predicateFor(region);\n if (predicate === null) return !region.hidden;\n return (\n triggers.length > 0 &&\n (this.matchValue === \"all\" ? triggers.every(predicate) : triggers.some(predicate))\n );\n }\n\n /** Builds the deterministic per-trigger predicate from the region's condition. */\n #predicateFor(region: HTMLElement): ((trigger: Trigger) => boolean) | null {\n if (region.hasAttribute(\"data-when-checked\")) {\n return (trigger) => this.#isChecked(trigger);\n }\n if (region.hasAttribute(\"data-when-unchecked\")) {\n return (trigger) => !this.#isChecked(trigger);\n }\n const wanted = region.dataset.whenValue;\n if (wanted !== undefined) {\n return (trigger) => this.#matchesValue(trigger, wanted);\n }\n return null;\n }\n\n #isChecked(trigger: Trigger): boolean {\n return trigger instanceof HTMLInputElement && trigger.checked;\n }\n\n /** A trigger has a value when a radio/checkbox is selected, or its control value matches. */\n #matchesValue(trigger: Trigger, wanted: string): boolean {\n if (\n trigger instanceof HTMLInputElement &&\n (trigger.type === \"checkbox\" || trigger.type === \"radio\")\n ) {\n return trigger.checked && trigger.value === wanted;\n }\n return trigger.value === wanted;\n }\n\n /** Moves focus to the first safe trigger, or to the controller landmark as a fallback. */\n #retreatFocus(region: HTMLElement, plan: ReadonlyMap<HTMLElement, boolean>): void {\n const active = document.activeElement;\n if (active && region.contains(active)) this.#moveFocus(plan);\n }\n\n /** Moves focus after the caller establishes that the active node is being hidden. */\n #moveFocus(plan: ReadonlyMap<HTMLElement, boolean>): void {\n const destination = this.triggerTargets.find(\n (trigger) => canTakeFocus(trigger) && this.#survivesPlan(trigger, plan),\n );\n if (destination) {\n this.#rootTabindex.returnAll();\n destination.focus();\n return;\n }\n\n this.#rootTabindex.lend(this.element);\n this.element.focus();\n }\n\n /** Whether a focus candidate sits outside every region this pass will hide. */\n #survivesPlan(candidate: HTMLElement, plan: ReadonlyMap<HTMLElement, boolean>): boolean {\n return [...plan].every(([region, visible]) => visible || !region.contains(candidate));\n }\n\n /** Records one idempotent attribute write before MutationObserver sees it. */\n #writeAttribute(element: Element, name: string, value: ExpectedAttribute): void {\n if (element.getAttribute(name) !== value) {\n if (this.#observing) {\n let writes = this.#internalWrites.get(element);\n if (!writes) {\n writes = new Map();\n this.#internalWrites.set(element, writes);\n }\n writes.set(name, value);\n }\n if (value === null) element.removeAttribute(name);\n else element.setAttribute(name, value);\n }\n }\n\n /** Separates authored records from controller writes and queues one relevant repair. */\n #onMutations(records: MutationRecord[]): void {\n const triggers = this.triggerTargets;\n const regions = this.regionTargets;\n const touchedWrites = new Map<Element, Set<string>>();\n let needsReconcile = false;\n\n for (const record of records) {\n if (record.type === \"attributes\" && record.target instanceof Element) {\n const name = record.attributeName;\n const writes = this.#internalWrites.get(record.target);\n if (name && writes?.has(name)) {\n let names = touchedWrites.get(record.target);\n if (!names) {\n names = new Set();\n touchedWrites.set(record.target, names);\n }\n names.add(name);\n if (record.target.getAttribute(name) === writes.get(name)) continue;\n }\n }\n if (\n record.target instanceof Element &&\n this.#elementAffectsState(record.target, triggers, regions)\n ) {\n needsReconcile = true;\n }\n }\n\n for (const [element, names] of touchedWrites) {\n const writes = this.#internalWrites.get(element);\n for (const name of names) writes?.delete(name);\n }\n if (needsReconcile) this.#reconcile.schedule();\n }\n\n /** Whether an authored mutation target belongs to a trigger, region, or managed control. */\n #elementAffectsState(target: Element, triggers: Trigger[], regions: HTMLElement[]): boolean {\n return (\n triggers.some((trigger) => trigger === target || trigger.contains(target)) ||\n regions.some((region) => region === target || region.contains(target))\n );\n }\n\n /** Whether a form owns at least one current trigger, including `form=` associations. */\n #hasTriggerOwnedBy(form: HTMLFormElement): boolean {\n return this.triggerTargets.some((trigger) => trigger.form === form);\n }\n}\n"]}
|
|
@@ -25,6 +25,8 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
25
25
|
* <a href="/items/1" data-action="click->stimeo--confirm#request"
|
|
26
26
|
* data-stimeo--confirm-message-param="Delete this item?">Delete</a>
|
|
27
27
|
*
|
|
28
|
+
* `open` dispatches `{ message }`; `resolve` dispatches `{ confirmed }`.
|
|
29
|
+
*
|
|
28
30
|
* @remarks
|
|
29
31
|
* Behavior only — the dialog's a11y (focus trap, restore, roles) is delegated to
|
|
30
32
|
* the shared {@link FocusTrap}; this controller adds the Turbo bridge and the
|