stimeo-ui 0.2.1 → 0.3.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 +104 -0
- package/dist/cable/index.d.ts +16 -16
- package/dist/cable/index.js.map +1 -1
- package/dist/controllers/accordion_controller.js +10 -0
- package/dist/controllers/accordion_controller.js.map +1 -1
- package/dist/controllers/alert_dialog_controller.js.map +1 -1
- package/dist/controllers/breadcrumb_controller.d.ts +97 -10
- package/dist/controllers/breadcrumb_controller.js +225 -13
- package/dist/controllers/breadcrumb_controller.js.map +1 -1
- package/dist/controllers/calendar_controller.d.ts +11 -1
- package/dist/controllers/calendar_controller.js +89 -22
- package/dist/controllers/calendar_controller.js.map +1 -1
- package/dist/controllers/carousel_controller.d.ts +16 -1
- package/dist/controllers/carousel_controller.js +47 -6
- package/dist/controllers/carousel_controller.js.map +1 -1
- package/dist/controllers/collapsible_controller.js +2 -2
- package/dist/controllers/collapsible_controller.js.map +1 -1
- package/dist/controllers/color_picker_controller.d.ts +12 -1
- package/dist/controllers/color_picker_controller.js +46 -7
- package/dist/controllers/color_picker_controller.js.map +1 -1
- package/dist/controllers/combobox_controller.d.ts +14 -1
- package/dist/controllers/combobox_controller.js +162 -23
- 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 +194 -17
- package/dist/controllers/command_palette_controller.js.map +1 -1
- package/dist/controllers/confirm_controller.js.map +1 -1
- package/dist/controllers/context_menu_controller.d.ts +7 -6
- package/dist/controllers/context_menu_controller.js +32 -10
- package/dist/controllers/context_menu_controller.js.map +1 -1
- package/dist/controllers/count_up_controller.js.map +1 -1
- package/dist/controllers/data_grid_controller.d.ts +19 -1
- package/dist/controllers/data_grid_controller.js +82 -4
- package/dist/controllers/data_grid_controller.js.map +1 -1
- package/dist/controllers/date_range_picker_controller.d.ts +1 -0
- package/dist/controllers/date_range_picker_controller.js +27 -3
- package/dist/controllers/date_range_picker_controller.js.map +1 -1
- package/dist/controllers/dialog_controller.js.map +1 -1
- package/dist/controllers/dismissible_controller.js.map +1 -1
- package/dist/controllers/drawer_controller.js.map +1 -1
- package/dist/controllers/dropdown_controller.d.ts +1 -2
- package/dist/controllers/dropdown_controller.js.map +1 -1
- package/dist/controllers/editable_controller.js +1 -0
- package/dist/controllers/editable_controller.js.map +1 -1
- package/dist/controllers/focus_controller.d.ts +1 -1
- package/dist/controllers/focus_controller.js.map +1 -1
- package/dist/controllers/form_field_controller.js.map +1 -1
- package/dist/controllers/form_validation_controller.js +1 -1
- package/dist/controllers/form_validation_controller.js.map +1 -1
- package/dist/controllers/highlight_controller.js.map +1 -1
- package/dist/controllers/hover_card_controller.d.ts +1 -1
- package/dist/controllers/hover_card_controller.js.map +1 -1
- package/dist/controllers/intersection_controller.js +36 -11
- package/dist/controllers/intersection_controller.js.map +1 -1
- package/dist/controllers/lazy_frame_controller.js +31 -10
- package/dist/controllers/lazy_frame_controller.js.map +1 -1
- package/dist/controllers/listbox_controller.d.ts +9 -2
- package/dist/controllers/listbox_controller.js +257 -53
- package/dist/controllers/listbox_controller.js.map +1 -1
- package/dist/controllers/local_time_controller.js +2 -2
- package/dist/controllers/local_time_controller.js.map +1 -1
- package/dist/controllers/menu_controller.d.ts +29 -11
- package/dist/controllers/menu_controller.js +104 -17
- package/dist/controllers/menu_controller.js.map +1 -1
- package/dist/controllers/menubar_controller.d.ts +91 -11
- package/dist/controllers/menubar_controller.js +415 -63
- package/dist/controllers/menubar_controller.js.map +1 -1
- package/dist/controllers/meter_controller.js.map +1 -1
- package/dist/controllers/multi_select_controller.d.ts +31 -5
- package/dist/controllers/multi_select_controller.js +312 -29
- package/dist/controllers/multi_select_controller.js.map +1 -1
- package/dist/controllers/navigation_menu_controller.d.ts +65 -18
- package/dist/controllers/navigation_menu_controller.js +154 -27
- package/dist/controllers/navigation_menu_controller.js.map +1 -1
- package/dist/controllers/nested_form_controller.d.ts +4 -5
- package/dist/controllers/nested_form_controller.js.map +1 -1
- package/dist/controllers/number_input_controller.d.ts +1 -1
- package/dist/controllers/number_input_controller.js +7 -0
- package/dist/controllers/number_input_controller.js.map +1 -1
- package/dist/controllers/otp_controller.js +18 -1
- package/dist/controllers/otp_controller.js.map +1 -1
- package/dist/controllers/overflow_indicator_controller.d.ts +3 -2
- package/dist/controllers/overflow_indicator_controller.js +81 -13
- package/dist/controllers/overflow_indicator_controller.js.map +1 -1
- package/dist/controllers/overflow_menu_controller.d.ts +78 -19
- package/dist/controllers/overflow_menu_controller.js +381 -57
- package/dist/controllers/overflow_menu_controller.js.map +1 -1
- package/dist/controllers/pagination_controller.d.ts +36 -5
- package/dist/controllers/pagination_controller.js +163 -32
- package/dist/controllers/pagination_controller.js.map +1 -1
- package/dist/controllers/persist_controller.js +6 -6
- package/dist/controllers/persist_controller.js.map +1 -1
- package/dist/controllers/pointer_drag_controller.d.ts +2 -3
- package/dist/controllers/pointer_drag_controller.js +9 -1
- package/dist/controllers/pointer_drag_controller.js.map +1 -1
- package/dist/controllers/popover_controller.d.ts +1 -2
- package/dist/controllers/popover_controller.js +2 -2
- package/dist/controllers/popover_controller.js.map +1 -1
- package/dist/controllers/portal_controller.d.ts +4 -4
- package/dist/controllers/portal_controller.js.map +1 -1
- package/dist/controllers/progress_controller.js.map +1 -1
- package/dist/controllers/radio_group_controller.js +22 -3
- package/dist/controllers/radio_group_controller.js.map +1 -1
- package/dist/controllers/range_slider_controller.d.ts +14 -4
- package/dist/controllers/range_slider_controller.js +32 -6
- package/dist/controllers/range_slider_controller.js.map +1 -1
- package/dist/controllers/rating_controller.js +16 -2
- package/dist/controllers/rating_controller.js.map +1 -1
- package/dist/controllers/read_more_controller.js +63 -19
- package/dist/controllers/read_more_controller.js.map +1 -1
- package/dist/controllers/reading_progress_controller.js.map +1 -1
- package/dist/controllers/resizable_controller.d.ts +3 -1
- package/dist/controllers/resizable_controller.js +65 -1
- package/dist/controllers/resizable_controller.js.map +1 -1
- package/dist/controllers/roving_controller.js +17 -2
- package/dist/controllers/roving_controller.js.map +1 -1
- package/dist/controllers/scroll_area_controller.js +86 -12
- package/dist/controllers/scroll_area_controller.js.map +1 -1
- package/dist/controllers/scroll_restore_controller.js +1 -1
- package/dist/controllers/scroll_restore_controller.js.map +1 -1
- package/dist/controllers/scroll_visibility_controller.d.ts +6 -3
- package/dist/controllers/scroll_visibility_controller.js +33 -3
- package/dist/controllers/scroll_visibility_controller.js.map +1 -1
- package/dist/controllers/scrollspy_controller.d.ts +64 -15
- package/dist/controllers/scrollspy_controller.js +346 -73
- package/dist/controllers/scrollspy_controller.js.map +1 -1
- package/dist/controllers/separator_controller.d.ts +20 -5
- package/dist/controllers/separator_controller.js +9 -0
- package/dist/controllers/separator_controller.js.map +1 -1
- package/dist/controllers/sidebar_controller.d.ts +1 -1
- package/dist/controllers/sidebar_controller.js.map +1 -1
- package/dist/controllers/skeleton_controller.js +1 -1
- package/dist/controllers/skeleton_controller.js.map +1 -1
- package/dist/controllers/slider_controller.d.ts +11 -1
- package/dist/controllers/slider_controller.js +32 -6
- package/dist/controllers/slider_controller.js.map +1 -1
- package/dist/controllers/sortable_controller.d.ts +3 -1
- package/dist/controllers/sortable_controller.js +34 -3
- package/dist/controllers/sortable_controller.js.map +1 -1
- package/dist/controllers/spinner_controller.js +1 -1
- package/dist/controllers/spinner_controller.js.map +1 -1
- package/dist/controllers/stick_to_bottom_controller.d.ts +5 -6
- package/dist/controllers/stick_to_bottom_controller.js +2 -1
- package/dist/controllers/stick_to_bottom_controller.js.map +1 -1
- package/dist/controllers/sticky_observer_controller.js +32 -11
- package/dist/controllers/sticky_observer_controller.js.map +1 -1
- package/dist/controllers/submit_once_controller.d.ts +2 -2
- package/dist/controllers/submit_once_controller.js.map +1 -1
- package/dist/controllers/switch_controller.js +1 -0
- package/dist/controllers/switch_controller.js.map +1 -1
- package/dist/controllers/tabs_controller.d.ts +11 -3
- package/dist/controllers/tabs_controller.js +26 -3
- package/dist/controllers/tabs_controller.js.map +1 -1
- package/dist/controllers/tags_input_controller.js +22 -2
- package/dist/controllers/tags_input_controller.js.map +1 -1
- package/dist/controllers/theme_controller.d.ts +1 -1
- package/dist/controllers/theme_controller.js +22 -3
- package/dist/controllers/theme_controller.js.map +1 -1
- package/dist/controllers/time_picker_controller.js +20 -1
- package/dist/controllers/time_picker_controller.js.map +1 -1
- package/dist/controllers/toast_controller.d.ts +4 -5
- package/dist/controllers/toast_controller.js +4 -5
- package/dist/controllers/toast_controller.js.map +1 -1
- package/dist/controllers/toggle_group_controller.js +23 -2
- package/dist/controllers/toggle_group_controller.js.map +1 -1
- package/dist/controllers/toolbar_controller.d.ts +53 -13
- package/dist/controllers/toolbar_controller.js +230 -31
- package/dist/controllers/toolbar_controller.js.map +1 -1
- package/dist/controllers/tooltip_controller.d.ts +1 -1
- package/dist/controllers/tooltip_controller.js.map +1 -1
- package/dist/controllers/transition_controller.js.map +1 -1
- package/dist/controllers/tree_view_controller.d.ts +87 -6
- package/dist/controllers/tree_view_controller.js +467 -51
- package/dist/controllers/tree_view_controller.js.map +1 -1
- package/dist/index.js +3514 -689
- package/dist/index.js.map +1 -1
- package/dist/inspector/cli.d.ts +375 -39
- package/dist/inspector/cli.js +228 -14
- package/dist/inspector/cli.js.map +1 -1
- package/dist/inspector/cli_bin.js +419 -48
- package/dist/inspector/cli_bin.js.map +1 -1
- package/dist/inspector/examples.json +24 -24
- package/dist/inspector/manifest.json +1040 -125
- package/package.json +5 -6
|
@@ -2,6 +2,17 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
2
2
|
|
|
3
3
|
// src/controllers/toggle_group_controller.ts
|
|
4
4
|
|
|
5
|
+
// src/utils/logical_scroll.ts
|
|
6
|
+
function isRtl(element) {
|
|
7
|
+
return window.getComputedStyle(element).direction === "rtl";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// src/utils/arrow_step.ts
|
|
11
|
+
function isReservedArrowChord(event, allow = []) {
|
|
12
|
+
if (!event.key.startsWith("Arrow")) return false;
|
|
13
|
+
return event.altKey && !allow.includes("alt") || event.ctrlKey && !allow.includes("ctrl") || event.metaKey && !allow.includes("meta") || event.shiftKey && !allow.includes("shift");
|
|
14
|
+
}
|
|
15
|
+
|
|
5
16
|
// src/utils/roving_tabindex.ts
|
|
6
17
|
var RovingTabindex = class {
|
|
7
18
|
/** Returns the current ordered item elements; called on every operation. */
|
|
@@ -60,17 +71,27 @@ var ToggleGroupController = class extends Controller {
|
|
|
60
71
|
}
|
|
61
72
|
/** Arrow/Home/End move focus only; Space/Enter toggle non-button hosts. */
|
|
62
73
|
onKeydown(event) {
|
|
74
|
+
if (event.defaultPrevented) return;
|
|
75
|
+
if (isReservedArrowChord(event)) return;
|
|
63
76
|
const current = this.itemTargets.indexOf(event.currentTarget);
|
|
64
77
|
if (current === -1) return;
|
|
65
78
|
let next = null;
|
|
79
|
+
const rtl = isRtl(this.element);
|
|
80
|
+
const horizontalStep = (key) => rtl ? key === "ArrowRight" ? -1 : 1 : key === "ArrowRight" ? 1 : -1;
|
|
66
81
|
switch (event.key) {
|
|
67
82
|
case "ArrowRight":
|
|
68
83
|
case "ArrowDown":
|
|
69
|
-
next = rovingMove(
|
|
84
|
+
next = rovingMove(
|
|
85
|
+
current,
|
|
86
|
+
this.itemTargets.length,
|
|
87
|
+
event.key === "ArrowDown" ? 1 : horizontalStep(event.key));
|
|
70
88
|
break;
|
|
71
89
|
case "ArrowLeft":
|
|
72
90
|
case "ArrowUp":
|
|
73
|
-
next = rovingMove(
|
|
91
|
+
next = rovingMove(
|
|
92
|
+
current,
|
|
93
|
+
this.itemTargets.length,
|
|
94
|
+
event.key === "ArrowUp" ? -1 : horizontalStep(event.key));
|
|
74
95
|
break;
|
|
75
96
|
case "Home":
|
|
76
97
|
next = 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/roving_tabindex.ts","../../src/controllers/toggle_group_controller.ts"],"names":[],"mappings":";;;;;AAqBO,IAAM,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c,CAAA,KAAM,MAAA;AAAA,EAC/C;AAAA;AAAA,EAGA,WAAA,CAAY,MAAmB,OAAA,EAAwB;AACrD,IAAA,IAAA,CAAK,YAAA,CAAa,cAAA,EAAgB,OAAA,GAAU,MAAA,GAAS,OAAO,CAAA;AAAA,EAC9D;AAAA;AAAA,EAGA,cAAA,GAA2B;AACzB,IAAA,OAAO,KAAK,WAAA,CACT,MAAA,CAAO,CAAC,IAAA,KAAS,KAAK,UAAA,CAAW,IAAI,CAAC,CAAA,CACtC,IAAI,CAAC,IAAA,KAAS,IAAA,CAAK,UAAA,CAAW,IAAI,CAAC,CAAA;AAAA,EACxC;AAAA;AAAA,EAGA,WAAW,IAAA,EAA2B;AACpC,IAAA,OAAO,IAAA,CAAK,YAAA,CAAa,YAAY,CAAA,IAAK,EAAA;AAAA,EAC5C;AACF","file":"toggle_group_controller.js","sourcesContent":["/**\n * Low-level roving-tabindex primitive shared by composite-widget controllers.\n *\n * The APG roving-tabindex pattern keeps a composite widget a single Tab stop:\n * exactly one item is in the Tab sequence (`tabindex=\"0\"`) while the rest are\n * removed from it (`tabindex=\"-1\"`), and the arrow keys move both DOM focus and\n * that single tabbable position together. {@link RovingTabindex} owns *only* that\n * mechanical bookkeeping — \"which one item is tabbable, and move focus there\".\n *\n * It is intentionally **policy-free**. Orientation, wrapping vs. clamping,\n * selection-follows-focus, typeahead, and `Home`/`End` semantics differ per APG\n * pattern (Radio Group, Toolbar, Rating, …); folding them into one helper would\n * flatten those widgets to a lowest common denominator and lose each pattern's\n * correctness. Those decisions therefore stay in each controller, which calls\n * {@link RovingTabindex.setActive} with an index it computed itself (optionally\n * via the pure {@link rovingMove} helper).\n *\n * @remarks\n * Items are read lazily through a getter so a controller can add or remove\n * targets (Stimulus re-scans the DOM) without re-wiring this helper.\n */\nexport class RovingTabindex {\n /** Returns the current ordered item elements; called on every operation. */\n readonly #getItems: () => HTMLElement[];\n\n /**\n * @param getItems - Returns the current ordered item elements. Called on every\n * operation so the live target list is always used.\n */\n constructor(getItems: () => HTMLElement[]) {\n this.#getItems = getItems;\n }\n\n /** Index of the currently tabbable item (`tabindex=\"0\"`), or `-1` if none. */\n get activeIndex(): number {\n return this.#getItems().findIndex((item) => item.tabIndex === 0);\n }\n\n /**\n * Makes exactly the item at `index` tabbable (`tabindex=\"0\"`) and removes every\n * other item from the Tab sequence (`tabindex=\"-1\"`). An out-of-range `index`\n * (e.g. `-1`) leaves all items at `-1`, which a controller can use to express\n * \"nothing is currently tabbable\".\n *\n * @param index - Position of the item to make tabbable.\n * @param options - Pass `{ focus: true }` to also move DOM focus to that item.\n */\n setActive(index: number, { focus = false }: { focus?: boolean } = {}): void {\n const items = this.#getItems();\n items.forEach((item, i) => {\n item.tabIndex = i === index ? 0 : -1;\n });\n if (focus) items[index]?.focus();\n }\n}\n\n/** Edge behavior for {@link rovingMove}: cycle past the ends, or stop at them. */\nexport type RovingWrap = \"wrap\" | \"clamp\";\n\n/**\n * Pure helper that resolves the target index for a one-step directional move.\n *\n * Keyboard/orientation mapping stays in the caller: it decides that a key means\n * `delta` `+1` (next) or `-1` (previous) and whether the widget should `\"wrap\"`\n * (Radio Group, Toolbar) or `\"clamp\"` at the ends.\n *\n * @param current - The index focus is moving from.\n * @param length - Number of items in the set.\n * @param delta - `+1` to move to the next item, `-1` for the previous.\n * @param wrap - `\"wrap\"` cycles around the ends; `\"clamp\"` stops at them.\n * @returns The resolved index, or `-1` when there are no items.\n */\nexport function rovingMove(\n current: number,\n length: number,\n delta: number,\n wrap: RovingWrap,\n): number {\n if (length === 0) return -1;\n const next = current + delta;\n if (wrap === \"wrap\") return (next + length) % length;\n return Math.min(length - 1, Math.max(0, next));\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { RovingTabindex, rovingMove } from \"../utils/roving_tabindex\";\n\n/**\n * Headless, accessible toggle-button group behavior.\n *\n * Markup contract (identifier: `stimeo--toggle-group`):\n * <div data-controller=\"stimeo--toggle-group\"\n * data-stimeo--toggle-group-mode-value=\"single\"\n * role=\"group\" aria-label=\"Text style\">\n * <button type=\"button\" aria-pressed=\"true\" tabindex=\"0\" data-value=\"bold\"\n * data-stimeo--toggle-group-target=\"item\"\n * data-action=\"click->stimeo--toggle-group#toggle\n * keydown->stimeo--toggle-group#onKeydown\">Bold</button>\n * <!-- more items; exactly one tabindex=0 -->\n * </div>\n *\n * Implements the WAI-ARIA APG **Button (toggle)** pattern with **Toolbar**-style\n * roving navigation. Each item's pressed state is `aria-pressed` (the accessible\n * name never changes); the group is a single Tab stop. For strict mutually\n * exclusive selection with radio semantics, use\n * {@link RadioGroupController | Radio Group} instead.\n *\n * @remarks\n * Behavior only — the consumer styles off `[aria-pressed=\"true\"]`. Per the\n * Toolbar model the arrow keys move **focus only**; activation is Space/Enter\n * (handled natively on `<button>` hosts, which synthesize a click).\n *\n * Behavior provided:\n * - Click (or Space/Enter) toggles an item. In `single` mode pressing one item\n * releases the others (0 or 1 pressed); `multiple` allows any number.\n * - `ArrowRight`/`ArrowDown` move focus to the next item, `ArrowLeft`/`ArrowUp`\n * to the previous (wrapping); `Home`/`End` to the first/last.\n * - `stimeo--toggle-group:change` is dispatched on every toggle.\n */\nexport class ToggleGroupController extends Controller<HTMLElement> {\n static override targets = [\"item\"];\n static override values = {\n mode: { type: String, default: \"multiple\" },\n };\n static actions = [\"onKeydown\", \"toggle\"] as const;\n static events = [\"change\"] as const;\n\n declare readonly itemTargets: HTMLElement[];\n declare modeValue: string;\n\n readonly #roving = new RovingTabindex(() => this.itemTargets);\n\n /** Establishes the roving entry point (first pressed item, else the first). */\n override connect(): void {\n const firstPressed = this.itemTargets.findIndex((item) => this.#isPressed(item));\n this.#roving.setActive(firstPressed === -1 ? 0 : firstPressed);\n }\n\n /** Toggles the activated item. Bound via `data-action` (click). */\n toggle(event: Event): void {\n this.#toggleIndex(this.itemTargets.indexOf(event.currentTarget as HTMLElement));\n }\n\n /** Arrow/Home/End move focus only; Space/Enter toggle non-button hosts. */\n onKeydown(event: KeyboardEvent): void {\n const current = this.itemTargets.indexOf(event.currentTarget as HTMLElement);\n if (current === -1) return;\n\n let next: number | null = null;\n switch (event.key) {\n case \"ArrowRight\":\n case \"ArrowDown\":\n next = rovingMove(current, this.itemTargets.length, 1, \"wrap\");\n break;\n case \"ArrowLeft\":\n case \"ArrowUp\":\n next = rovingMove(current, this.itemTargets.length, -1, \"wrap\");\n break;\n case \"Home\":\n next = 0;\n break;\n case \"End\":\n next = this.itemTargets.length - 1;\n break;\n case \" \":\n case \"Enter\":\n // Native <button> hosts synthesize a click that drives #toggle; handling\n // the key here too would toggle twice. Only non-button hosts need it.\n if (event.currentTarget instanceof HTMLButtonElement) return;\n event.preventDefault();\n this.#toggleIndex(current);\n return;\n default:\n return;\n }\n\n event.preventDefault();\n this.#roving.setActive(next, { focus: true });\n }\n\n /** Applies the toggle at `index` per the current mode and dispatches `change`. */\n #toggleIndex(index: number): void {\n const item = this.itemTargets[index];\n if (!item) return;\n const willPress = !this.#isPressed(item);\n if (this.modeValue === \"single\") {\n this.itemTargets.forEach((other, i) => {\n this.#setPressed(other, i === index && willPress);\n });\n } else {\n this.#setPressed(item, willPress);\n }\n this.#roving.setActive(index);\n this.dispatch(\"change\", {\n detail: { value: this.#itemValue(item), pressed: willPress, values: this.#pressedValues() },\n });\n }\n\n /** Whether an item is currently pressed. */\n #isPressed(item: HTMLElement): boolean {\n return item.getAttribute(\"aria-pressed\") === \"true\";\n }\n\n /** Reflects the pressed state on `aria-pressed`. */\n #setPressed(item: HTMLElement, pressed: boolean): void {\n item.setAttribute(\"aria-pressed\", pressed ? \"true\" : \"false\");\n }\n\n /** The `data-value` of every currently pressed item. */\n #pressedValues(): string[] {\n return this.itemTargets\n .filter((item) => this.#isPressed(item))\n .map((item) => this.#itemValue(item));\n }\n\n /** An item's value (`data-value`, defaulting to empty). */\n #itemValue(item: HTMLElement): string {\n return item.getAttribute(\"data-value\") ?? \"\";\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/logical_scroll.ts","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A,IAAA,CAAK,YAAA,CAAa,cAAA,EAAgB,OAAA,GAAU,MAAA,GAAS,OAAO,CAAA;AAAA,EAC9D;AAAA;AAAA,EAGA,cAAA,GAA2B;AACzB,IAAA,OAAO,KAAK,WAAA,CACT,MAAA,CAAO,CAAC,IAAA,KAAS,KAAK,UAAA,CAAW,IAAI,CAAC,CAAA,CACtC,IAAI,CAAC,IAAA,KAAS,IAAA,CAAK,UAAA,CAAW,IAAI,CAAC,CAAA;AAAA,EACxC;AAAA;AAAA,EAGA,WAAW,IAAA,EAA2B;AACpC,IAAA,OAAO,IAAA,CAAK,YAAA,CAAa,YAAY,CAAA,IAAK,EAAA;AAAA,EAC5C;AACF","file":"toggle_group_controller.js","sourcesContent":["/** Normalized scroll position and maximum distance on one logical axis. */\nexport interface LogicalScrollMetrics {\n position: number;\n max: number;\n}\n\n/**\n * Whether horizontal scrolling on `element` follows right-to-left inline flow.\n *\n * Resolved from the **computed** `direction`, so the authoring contract is the\n * usual `dir=\"rtl\"` (or a stylesheet) on the element or any ancestor.\n *\n * Scope: horizontal writing modes. A vertical writing mode (`writing-mode:\n * vertical-rl`) also inverts the horizontal axis, which this check does not\n * model — vertical writing modes are out of scope for the scroll utilities\n * (their consumers describe axes as horizontal/vertical, not inline/block).\n */\nexport function isRtl(element: Element): boolean {\n return window.getComputedStyle(element).direction === \"rtl\";\n}\n\n/**\n * Returns scroll distance from the logical start edge.\n *\n * CSSOM View exposes standards-mode RTL horizontal offsets as `0` at the inline\n * start (right) and increasingly negative values toward the inline end (left).\n * The normalized position is always clamped to `[0, max]`, which also absorbs\n * Safari's elastic overscroll values.\n */\nexport function logicalScrollMetrics(\n element: HTMLElement,\n horizontal: boolean,\n): LogicalScrollMetrics {\n const max = Math.max(\n 0,\n horizontal\n ? element.scrollWidth - element.clientWidth\n : element.scrollHeight - element.clientHeight,\n );\n const raw = horizontal ? element.scrollLeft : element.scrollTop;\n const position = horizontal && isRtl(element) ? -raw : raw;\n return { position: Math.min(max, Math.max(0, position)), max };\n}\n\n/**\n * Converts a logical start/end delta to the physical value accepted by\n * `Element.scrollBy`.\n */\nexport function physicalScrollDelta(\n element: HTMLElement,\n horizontal: boolean,\n logicalDelta: number,\n): number {\n return horizontal && isRtl(element) ? -logicalDelta : logicalDelta;\n}\n","import { isRtl } from \"./logical_scroll\";\n\n/**\n * Turns an arrow key into a **logical** step: `+1` for \"next\", `-1` for\n * \"previous\", `0` when the key names neither.\n *\n * APG defines the horizontal pair as *next / previous* and says a vertical\n * arrangement swaps in Down/Up for the same meaning — so the pair is one axis's\n * spelling of an order, and the order reverses with the writing direction. Only\n * the horizontal pair reverses. Down/Up name an axis the writing direction does\n * not mirror, and returning them unchanged is the point: many controllers fold\n * both pairs into one branch, where swapping the branches under RTL would flip\n * the vertical axis too — a bug that reads as \"the arrows work\" until someone\n * presses Down.\n *\n * **Direction is read from the element the caller passes, which should be the\n * container that lays the items out** — not the focused child. A child may carry\n * its own `dir` (an LTR input inside an RTL form is ordinary authoring), and\n * probing per handler makes two handlers disagree at the boundary between them.\n *\n * This decides direction only. Whether the axis is even active (an\n * `orientation=\"horizontal\"` widget ignoring Down/Up), how far the step lands,\n * and what wrapping does all stay with the caller.\n *\n * **It encodes the list-order convention: `ArrowDown` is *next*.** Widgets that\n * pair the arrows by *value* instead — `ArrowUp` meaning \"more\", as a rating or a\n * slider does — must not use this, or their vertical axis inverts. Reverse the\n * horizontal pair on its own there.\n *\n * @example\n * ```ts\n * const step = logicalArrowStep(event.key, this.element);\n * if (step === 0) return;\n * this.#roving.setActive(rovingMove(current, length, step, \"wrap\"), { focus: true });\n * ```\n */\nexport function logicalArrowStep(key: string, element: Element): 1 | -1 | 0 {\n if (key === \"ArrowDown\") return 1;\n if (key === \"ArrowUp\") return -1;\n if (key !== \"ArrowRight\" && key !== \"ArrowLeft\") return 0;\n const forward = isRtl(element) ? \"ArrowLeft\" : \"ArrowRight\";\n return key === forward ? 1 : -1;\n}\n\n/**\n * Rewrites `key` so an existing LTR-shaped branch keeps working under RTL:\n * `ArrowRight` and `ArrowLeft` trade places, everything else passes through.\n *\n * The alternative — negating a delta — silently breaks handlers whose two\n * horizontal branches are **not mirror images**. A grid that clamps one edge but\n * not the other, or a segmented field guarding `index > 0` on one side and\n * `index < length - 1` on the other, ends up applying the wrong guard to the\n * wrong direction. Swapping the key leaves each branch, guards and all, exactly\n * where its author put it.\n *\n * Same rule as {@link logicalArrowStep} about which element to read: pass the\n * container that lays the items out, not the focused child.\n *\n * @example\n * ```ts\n * switch (logicalArrowKey(event.key, this.element)) {\n * case \"ArrowLeft\": // \"previous\" — whatever direction that is on screen\n * ```\n */\nexport function logicalArrowKey(key: string, element: Element): string {\n if (key !== \"ArrowRight\" && key !== \"ArrowLeft\") return key;\n if (!isRtl(element)) return key;\n return key === \"ArrowRight\" ? \"ArrowLeft\" : \"ArrowRight\";\n}\n\n/** Modifiers a widget may claim on an arrow key, named for the `allow` list. */\nexport type ArrowModifier = \"alt\" | \"ctrl\" | \"meta\" | \"shift\";\n\n/**\n * True when an arrow key arrived carrying a modifier the widget must leave to\n * the browser: return without calling `preventDefault()` and without moving any\n * state.\n *\n * A bare arrow belongs to the widget; a chorded one usually does not.\n * `Alt`/`Meta` plus a horizontal arrow is history back/forward on every desktop\n * browser, and a widget that swallows it makes the shortcut work or not\n * depending on where focus happens to sit — a coin-flip the user cannot see.\n *\n * `allow` is for the combinations APG assigns to a pattern **and the widget\n * actually implements** — today only Combobox's optional `Alt+Down`/`Alt+Up`.\n * Listing one the widget does not implement defeats the point: the chord then\n * runs the plain-arrow branch, which is exactly what this guard exists to stop.\n * Non-arrow keys return `false`, so chorded letters and\n * `Control+Home`/`Control+End` are untouched.\n *\n * @example\n * ```ts\n * if (isReservedArrowChord(event)) return;\n * ```\n */\nexport function isReservedArrowChord(\n event: KeyboardEvent,\n allow: readonly ArrowModifier[] = [],\n): boolean {\n if (!event.key.startsWith(\"Arrow\")) return false;\n return (\n (event.altKey && !allow.includes(\"alt\")) ||\n (event.ctrlKey && !allow.includes(\"ctrl\")) ||\n (event.metaKey && !allow.includes(\"meta\")) ||\n (event.shiftKey && !allow.includes(\"shift\"))\n );\n}\n","/**\n * Low-level roving-tabindex primitive shared by composite-widget controllers.\n *\n * The APG roving-tabindex pattern keeps a composite widget a single Tab stop:\n * exactly one item is in the Tab sequence (`tabindex=\"0\"`) while the rest are\n * removed from it (`tabindex=\"-1\"`), and the arrow keys move both DOM focus and\n * that single tabbable position together. {@link RovingTabindex} owns *only* that\n * mechanical bookkeeping — \"which one item is tabbable, and move focus there\".\n *\n * It is intentionally **policy-free**. Orientation, wrapping vs. clamping,\n * selection-follows-focus, typeahead, and `Home`/`End` semantics differ per APG\n * pattern (Radio Group, Toolbar, Rating, …); folding them into one helper would\n * flatten those widgets to a lowest common denominator and lose each pattern's\n * correctness. Those decisions therefore stay in each controller, which calls\n * {@link RovingTabindex.setActive} with an index it computed itself (optionally\n * via the pure {@link rovingMove} helper).\n *\n * @remarks\n * Items are read lazily through a getter so a controller can add or remove\n * targets (Stimulus re-scans the DOM) without re-wiring this helper.\n */\nexport class RovingTabindex {\n /** Returns the current ordered item elements; called on every operation. */\n readonly #getItems: () => HTMLElement[];\n\n /**\n * @param getItems - Returns the current ordered item elements. Called on every\n * operation so the live target list is always used.\n */\n constructor(getItems: () => HTMLElement[]) {\n this.#getItems = getItems;\n }\n\n /** Index of the currently tabbable item (`tabindex=\"0\"`), or `-1` if none. */\n get activeIndex(): number {\n return this.#getItems().findIndex((item) => item.tabIndex === 0);\n }\n\n /**\n * Makes exactly the item at `index` tabbable (`tabindex=\"0\"`) and removes every\n * other item from the Tab sequence (`tabindex=\"-1\"`). An out-of-range `index`\n * (e.g. `-1`) leaves all items at `-1`, which a controller can use to express\n * \"nothing is currently tabbable\".\n *\n * @param index - Position of the item to make tabbable.\n * @param options - Pass `{ focus: true }` to also move DOM focus to that item.\n */\n setActive(index: number, { focus = false }: { focus?: boolean } = {}): void {\n const items = this.#getItems();\n items.forEach((item, i) => {\n item.tabIndex = i === index ? 0 : -1;\n });\n if (focus) items[index]?.focus();\n }\n}\n\n/** Edge behavior for {@link rovingMove}: cycle past the ends, or stop at them. */\nexport type RovingWrap = \"wrap\" | \"clamp\";\n\n/**\n * Pure helper that resolves the target index for a one-step directional move.\n *\n * Keyboard/orientation mapping stays in the caller: it decides that a key means\n * `delta` `+1` (next) or `-1` (previous) and whether the widget should `\"wrap\"`\n * (Radio Group, Toolbar) or `\"clamp\"` at the ends.\n *\n * @param current - The index focus is moving from.\n * @param length - Number of items in the set.\n * @param delta - `+1` to move to the next item, `-1` for the previous.\n * @param wrap - `\"wrap\"` cycles around the ends; `\"clamp\"` stops at them.\n * @returns The resolved index, or `-1` when there are no items.\n */\nexport function rovingMove(\n current: number,\n length: number,\n delta: number,\n wrap: RovingWrap,\n): number {\n if (length === 0) return -1;\n const next = current + delta;\n if (wrap === \"wrap\") return (next + length) % length;\n return Math.min(length - 1, Math.max(0, next));\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { isReservedArrowChord } from \"../utils/arrow_step\";\nimport { isRtl } from \"../utils/logical_scroll\";\nimport { RovingTabindex, rovingMove } from \"../utils/roving_tabindex\";\n\n/**\n * Headless, accessible toggle-button group behavior.\n *\n * Markup contract (identifier: `stimeo--toggle-group`):\n * <div data-controller=\"stimeo--toggle-group\"\n * data-stimeo--toggle-group-mode-value=\"single\"\n * role=\"group\" aria-label=\"Text style\">\n * <button type=\"button\" aria-pressed=\"true\" tabindex=\"0\" data-value=\"bold\"\n * data-stimeo--toggle-group-target=\"item\"\n * data-action=\"click->stimeo--toggle-group#toggle\n * keydown->stimeo--toggle-group#onKeydown\">Bold</button>\n * <!-- more items; exactly one tabindex=0 -->\n * </div>\n *\n * Implements the WAI-ARIA APG **Button (toggle)** pattern with **Toolbar**-style\n * roving navigation. Each item's pressed state is `aria-pressed` (the accessible\n * name never changes); the group is a single Tab stop. For strict mutually\n * exclusive selection with radio semantics, use\n * {@link RadioGroupController | Radio Group} instead.\n *\n * @remarks\n * Behavior only — the consumer styles off `[aria-pressed=\"true\"]`. Per the\n * Toolbar model the arrow keys move **focus only**; activation is Space/Enter\n * (handled natively on `<button>` hosts, which synthesize a click).\n *\n * Behavior provided:\n * - Click (or Space/Enter) toggles an item. In `single` mode pressing one item\n * releases the others (0 or 1 pressed); `multiple` allows any number.\n * - `ArrowRight`/`ArrowDown` move focus to the next item, `ArrowLeft`/`ArrowUp`\n * to the previous (wrapping); `Home`/`End` to the first/last.\n * - `stimeo--toggle-group:change` is dispatched on every toggle.\n */\nexport class ToggleGroupController extends Controller<HTMLElement> {\n static override targets = [\"item\"];\n static override values = {\n mode: { type: String, default: \"multiple\" },\n };\n static actions = [\"onKeydown\", \"toggle\"] as const;\n static events = [\"change\"] as const;\n\n declare readonly itemTargets: HTMLElement[];\n declare modeValue: string;\n\n readonly #roving = new RovingTabindex(() => this.itemTargets);\n\n /** Establishes the roving entry point (first pressed item, else the first). */\n override connect(): void {\n const firstPressed = this.itemTargets.findIndex((item) => this.#isPressed(item));\n this.#roving.setActive(firstPressed === -1 ? 0 : firstPressed);\n }\n\n /** Toggles the activated item. Bound via `data-action` (click). */\n toggle(event: Event): void {\n this.#toggleIndex(this.itemTargets.indexOf(event.currentTarget as HTMLElement));\n }\n\n /** Arrow/Home/End move focus only; Space/Enter toggle non-button hosts. */\n onKeydown(event: KeyboardEvent): void {\n // A descendant widget that already claimed the key must not ALSO move the roving focus —\n // composition depends on this yield.\n if (event.defaultPrevented) return;\n if (isReservedArrowChord(event)) return;\n const current = this.itemTargets.indexOf(event.currentTarget as HTMLElement);\n if (current === -1) return;\n\n let next: number | null = null;\n // Logical, not physical. APG defines these as \"next / previous control\", and\n // says a vertical arrangement swaps in Down/Up for the same meaning — so the\n // pair is one axis's spelling of an order, and the order reverses with the\n // writing direction. Read from the controller element: the container is what\n // lays the items out, and a child may carry its own `dir` (an LTR input\n // inside an RTL form is ordinary authoring).\n // The horizontal pair reverses; Down/Up must not come along. They share a case\n // body here, so the direction is decided per key rather than per branch —\n // swapping the case labels would flip the vertical axis too.\n const rtl = isRtl(this.element);\n const horizontalStep = (key: string) =>\n rtl ? (key === \"ArrowRight\" ? -1 : 1) : key === \"ArrowRight\" ? 1 : -1;\n switch (event.key) {\n case \"ArrowRight\":\n case \"ArrowDown\":\n next = rovingMove(\n current,\n this.itemTargets.length,\n event.key === \"ArrowDown\" ? 1 : horizontalStep(event.key),\n \"wrap\",\n );\n break;\n case \"ArrowLeft\":\n case \"ArrowUp\":\n next = rovingMove(\n current,\n this.itemTargets.length,\n event.key === \"ArrowUp\" ? -1 : horizontalStep(event.key),\n \"wrap\",\n );\n break;\n case \"Home\":\n next = 0;\n break;\n case \"End\":\n next = this.itemTargets.length - 1;\n break;\n case \" \":\n case \"Enter\":\n // Native <button> hosts synthesize a click that drives #toggle; handling\n // the key here too would toggle twice. Only non-button hosts need it.\n if (event.currentTarget instanceof HTMLButtonElement) return;\n event.preventDefault();\n this.#toggleIndex(current);\n return;\n default:\n return;\n }\n\n event.preventDefault();\n this.#roving.setActive(next, { focus: true });\n }\n\n /** Applies the toggle at `index` per the current mode and dispatches `change`. */\n #toggleIndex(index: number): void {\n const item = this.itemTargets[index];\n if (!item) return;\n const willPress = !this.#isPressed(item);\n if (this.modeValue === \"single\") {\n this.itemTargets.forEach((other, i) => {\n this.#setPressed(other, i === index && willPress);\n });\n } else {\n this.#setPressed(item, willPress);\n }\n this.#roving.setActive(index);\n this.dispatch(\"change\", {\n detail: { value: this.#itemValue(item), pressed: willPress, values: this.#pressedValues() },\n });\n }\n\n /** Whether an item is currently pressed. */\n #isPressed(item: HTMLElement): boolean {\n return item.getAttribute(\"aria-pressed\") === \"true\";\n }\n\n /** Reflects the pressed state on `aria-pressed`. */\n #setPressed(item: HTMLElement, pressed: boolean): void {\n item.setAttribute(\"aria-pressed\", pressed ? \"true\" : \"false\");\n }\n\n /** The `data-value` of every currently pressed item. */\n #pressedValues(): string[] {\n return this.itemTargets\n .filter((item) => this.#isPressed(item))\n .map((item) => this.#itemValue(item));\n }\n\n /** An item's value (`data-value`, defaulting to empty). */\n #itemValue(item: HTMLElement): string {\n return item.getAttribute(\"data-value\") ?? \"\";\n }\n}\n"]}
|
|
@@ -6,9 +6,8 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
6
6
|
* Markup contract (identifier: `stimeo--toolbar`):
|
|
7
7
|
* <div data-controller="stimeo--toolbar" role="toolbar" aria-label="Text formatting"
|
|
8
8
|
* data-stimeo--toolbar-orientation-value="horizontal">
|
|
9
|
-
* <button type="button" data-stimeo--toolbar-target="control"
|
|
10
|
-
*
|
|
11
|
-
* <!-- more controls; exactly one tabindex=0 -->
|
|
9
|
+
* <button type="button" data-stimeo--toolbar-target="control">Bold</button>
|
|
10
|
+
* <!-- more controls -->
|
|
12
11
|
* </div>
|
|
13
12
|
*
|
|
14
13
|
* Implements the WAI-ARIA APG **Toolbar** pattern: the group is a single Tab
|
|
@@ -19,14 +18,45 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
19
18
|
* @remarks
|
|
20
19
|
* Behavior only. The roving mechanics are delegated to {@link RovingTabindex};
|
|
21
20
|
* orientation and wrap policy stay here per the APG (they differ per widget).
|
|
21
|
+
* ARIA (`role="toolbar"`, the accessible name, and `aria-orientation="vertical"`
|
|
22
|
+
* when the axis is vertical) is the author's, exactly as with `role`.
|
|
22
23
|
*
|
|
23
24
|
* Behavior provided:
|
|
24
|
-
* - Exactly one control is tabbable (`tabindex="0"`); the rest are
|
|
25
|
+
* - Exactly one *navigable* control is tabbable (`tabindex="0"`); the rest are
|
|
26
|
+
* `-1`. The invariant is re-established on connect, on control add/remove,
|
|
27
|
+
* whenever `disabled` / `hidden` changes under the toolbar, and whenever an
|
|
28
|
+
* enclosing `<fieldset>` is disabled or re-enabled — disabling the control that
|
|
29
|
+
* held the Tab stop must never make the whole group unreachable by Tab, and
|
|
30
|
+
* unlocking the surrounding form must bring it back.
|
|
25
31
|
* - `ArrowRight`/`ArrowLeft` (horizontal) or `ArrowDown`/`ArrowUp` (vertical)
|
|
26
|
-
* move focus to the next/previous control; `Home`/`End` to the
|
|
32
|
+
* move focus to the next/previous navigable control; `Home`/`End` to the
|
|
33
|
+
* first/last. `aria-disabled="true"` controls remain in that roving order and
|
|
34
|
+
* can be both origins and destinations; only their activation is suppressed
|
|
35
|
+
* by the control itself or its owning behavior. Native `disabled` and `hidden`
|
|
36
|
+
* controls are excluded.
|
|
27
37
|
* - With `wrap=true` movement cycles past the ends; with `wrap=false` it stops.
|
|
28
|
-
* -
|
|
29
|
-
*
|
|
38
|
+
* - `orientation` accepts `horizontal` (default) and `vertical`; any other value
|
|
39
|
+
* degrades to `horizontal`. There is no `both` — for two-axis navigation use
|
|
40
|
+
* `stimeo--roving`, whose same-named Value does accept it.
|
|
41
|
+
* - Returning focus from outside lands on the most recently *active* control —
|
|
42
|
+
* the last one moved to by key, click, or programmatic `focus()`, because that
|
|
43
|
+
* is the one left tabbable (`focusin` keeps the Tab stop in sync).
|
|
44
|
+
*
|
|
45
|
+
* Keydown is **delegated on the container**, so controls added at runtime need
|
|
46
|
+
* no per-element `data-action` (Stimulus binds those asynchronously, which is
|
|
47
|
+
* unreliable for appended nodes). A `data-action="keydown->stimeo--toolbar#onKeydown"`
|
|
48
|
+
* on each control stays supported; it does not double-move, because the second
|
|
49
|
+
* pass sees `defaultPrevented`.
|
|
50
|
+
*
|
|
51
|
+
* Initial Tab stop: to choose it, write `tabindex` on **every** control (exactly
|
|
52
|
+
* one `0`, the rest `-1`). Natively focusable elements are `tabindex="0"`
|
|
53
|
+
* effectively when the attribute is absent, so annotating only the intended
|
|
54
|
+
* entry point makes the *first* control win instead.
|
|
55
|
+
*
|
|
56
|
+
* Known constraint: a toolbar that contains a text input competes with it for
|
|
57
|
+
* the arrow keys — the APG expects the toolbar to own them. Keep free-text
|
|
58
|
+
* fields outside the toolbar (IME composition is yielded to, but caret movement
|
|
59
|
+
* is not).
|
|
30
60
|
*/
|
|
31
61
|
declare class ToolbarController extends Controller<HTMLElement> {
|
|
32
62
|
#private;
|
|
@@ -45,14 +75,24 @@ declare class ToolbarController extends Controller<HTMLElement> {
|
|
|
45
75
|
readonly controlTargets: HTMLElement[];
|
|
46
76
|
orientationValue: string;
|
|
47
77
|
wrapValue: boolean;
|
|
78
|
+
connect(): void;
|
|
79
|
+
disconnect(): void;
|
|
48
80
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
|
|
81
|
+
* A control added at runtime is dropped out of the Tab sequence first — a
|
|
82
|
+
* fresh `<button>` is tabbable by default, which would make the group two Tab
|
|
83
|
+
* stops — and then the lone stop is re-established.
|
|
84
|
+
*/
|
|
85
|
+
controlTargetConnected(control: HTMLElement): void;
|
|
86
|
+
/** Removing the control that held the Tab stop must not orphan the group. */
|
|
87
|
+
controlTargetDisconnected(): void;
|
|
88
|
+
/**
|
|
89
|
+
* Arrow/Home/End move focus and the single tab stop.
|
|
90
|
+
*
|
|
91
|
+
* Binding this per control with `data-action` is **optional** — the same
|
|
92
|
+
* handling runs from the container's delegated listener. It stays a declared
|
|
93
|
+
* action so per-control wiring keeps working; wiring both does not
|
|
94
|
+
* double-move, because the delegated pass then yields on `defaultPrevented`.
|
|
53
95
|
*/
|
|
54
|
-
connect(): void;
|
|
55
|
-
/** Arrow/Home/End move focus and the single tab stop. Bound via `data-action`. */
|
|
56
96
|
onKeydown(event: KeyboardEvent): void;
|
|
57
97
|
}
|
|
58
98
|
|
|
@@ -2,6 +2,75 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
2
2
|
|
|
3
3
|
// src/controllers/toolbar_controller.ts
|
|
4
4
|
|
|
5
|
+
// src/utils/logical_scroll.ts
|
|
6
|
+
function isRtl(element) {
|
|
7
|
+
return window.getComputedStyle(element).direction === "rtl";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// src/utils/arrow_step.ts
|
|
11
|
+
function isReservedArrowChord(event, allow = []) {
|
|
12
|
+
if (!event.key.startsWith("Arrow")) return false;
|
|
13
|
+
return event.altKey && !allow.includes("alt") || event.ctrlKey && !allow.includes("ctrl") || event.metaKey && !allow.includes("meta") || event.shiftKey && !allow.includes("shift");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/utils/composition_tracker.ts
|
|
17
|
+
var CompositionTracker = class {
|
|
18
|
+
#observedTargets = /* @__PURE__ */ new Set();
|
|
19
|
+
#activeTargets = /* @__PURE__ */ new Set();
|
|
20
|
+
#onStart;
|
|
21
|
+
#onEnd;
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.#onStart = options.onStart;
|
|
24
|
+
this.#onEnd = options.onEnd;
|
|
25
|
+
}
|
|
26
|
+
/** Starts lifecycle tracking for `target`; repeated calls are idempotent. */
|
|
27
|
+
observe(target) {
|
|
28
|
+
if (this.#observedTargets.has(target)) return;
|
|
29
|
+
target.addEventListener("compositionstart", this.#handleStart);
|
|
30
|
+
target.addEventListener("compositionend", this.#handleEnd);
|
|
31
|
+
this.#observedTargets.add(target);
|
|
32
|
+
}
|
|
33
|
+
/** Stops tracking one target and clears any active composition it owned. */
|
|
34
|
+
unobserve(target) {
|
|
35
|
+
if (!this.#observedTargets.delete(target)) return;
|
|
36
|
+
target.removeEventListener("compositionstart", this.#handleStart);
|
|
37
|
+
target.removeEventListener("compositionend", this.#handleEnd);
|
|
38
|
+
this.#activeTargets.delete(target);
|
|
39
|
+
}
|
|
40
|
+
/** Releases every listener and clears state so reconnect starts cleanly. */
|
|
41
|
+
disconnect() {
|
|
42
|
+
for (const target of this.#observedTargets) {
|
|
43
|
+
target.removeEventListener("compositionstart", this.#handleStart);
|
|
44
|
+
target.removeEventListener("compositionend", this.#handleEnd);
|
|
45
|
+
}
|
|
46
|
+
this.#observedTargets.clear();
|
|
47
|
+
this.#activeTargets.clear();
|
|
48
|
+
}
|
|
49
|
+
/** True when lifecycle tracking or the current event reports composition. */
|
|
50
|
+
isComposing(event) {
|
|
51
|
+
return this.#activeTargets.size > 0 || event?.isComposing === true;
|
|
52
|
+
}
|
|
53
|
+
#handleStart = (event) => {
|
|
54
|
+
if (event.currentTarget) this.#activeTargets.add(event.currentTarget);
|
|
55
|
+
this.#onStart?.(event);
|
|
56
|
+
};
|
|
57
|
+
#handleEnd = (event) => {
|
|
58
|
+
if (event.currentTarget) this.#activeTargets.delete(event.currentTarget);
|
|
59
|
+
this.#onEnd?.(event);
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/utils/focus_candidate.ts
|
|
64
|
+
function inheritsFieldsetDisabled(control) {
|
|
65
|
+
let fieldset = control.closest("fieldset[disabled]");
|
|
66
|
+
while (fieldset) {
|
|
67
|
+
const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
|
|
68
|
+
if (!legend?.contains(control)) return true;
|
|
69
|
+
fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
5
74
|
// src/utils/roving_tabindex.ts
|
|
6
75
|
var RovingTabindex = class {
|
|
7
76
|
/** Returns the current ordered item elements; called on every operation. */
|
|
@@ -42,6 +111,7 @@ function rovingMove(current, length, delta, wrap) {
|
|
|
42
111
|
}
|
|
43
112
|
|
|
44
113
|
// src/controllers/toolbar_controller.ts
|
|
114
|
+
var STATE_ATTRIBUTES = ["disabled", "hidden"];
|
|
45
115
|
var ToolbarController = class extends Controller {
|
|
46
116
|
static targets = ["control"];
|
|
47
117
|
static values = {
|
|
@@ -50,61 +120,190 @@ var ToolbarController = class extends Controller {
|
|
|
50
120
|
};
|
|
51
121
|
static actions = ["onKeydown"];
|
|
52
122
|
#roving = new RovingTabindex(() => this.controlTargets);
|
|
123
|
+
#composition = new CompositionTracker();
|
|
124
|
+
#observer = null;
|
|
53
125
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
126
|
+
* Live between `connect()` and `disconnect()`. Stimulus reports the *initial*
|
|
127
|
+
* targets before `connect()` and re-reports them as disconnected after
|
|
128
|
+
* `disconnect()`; gating on this keeps the target callbacks from clobbering
|
|
129
|
+
* the authored Tab stop on mount and from resurrecting one after teardown.
|
|
58
130
|
*/
|
|
131
|
+
#connected = false;
|
|
59
132
|
connect() {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
133
|
+
this.#ensureTabStop();
|
|
134
|
+
this.element.addEventListener("keydown", this.#onKeydown);
|
|
135
|
+
this.element.addEventListener("focusin", this.#onFocusin);
|
|
136
|
+
this.#composition.observe(this.element);
|
|
137
|
+
if (typeof MutationObserver !== "undefined") {
|
|
138
|
+
const observer = new MutationObserver(() => this.#ensureTabStop());
|
|
139
|
+
observer.observe(this.element, {
|
|
140
|
+
subtree: true,
|
|
141
|
+
childList: true,
|
|
142
|
+
attributes: true,
|
|
143
|
+
attributeFilter: STATE_ATTRIBUTES
|
|
144
|
+
});
|
|
145
|
+
for (let fieldset = this.element.parentElement?.closest("fieldset") ?? null; fieldset; fieldset = fieldset.parentElement?.closest("fieldset") ?? null) {
|
|
146
|
+
observer.observe(fieldset, { attributes: true, attributeFilter: ["disabled"] });
|
|
147
|
+
}
|
|
148
|
+
this.#observer = observer;
|
|
65
149
|
}
|
|
66
|
-
|
|
67
|
-
|
|
150
|
+
this.#connected = true;
|
|
151
|
+
}
|
|
152
|
+
disconnect() {
|
|
153
|
+
this.#connected = false;
|
|
154
|
+
this.element.removeEventListener("keydown", this.#onKeydown);
|
|
155
|
+
this.element.removeEventListener("focusin", this.#onFocusin);
|
|
156
|
+
this.#composition.disconnect();
|
|
157
|
+
this.#observer?.disconnect();
|
|
158
|
+
this.#observer = null;
|
|
68
159
|
}
|
|
69
|
-
/**
|
|
160
|
+
/**
|
|
161
|
+
* A control added at runtime is dropped out of the Tab sequence first — a
|
|
162
|
+
* fresh `<button>` is tabbable by default, which would make the group two Tab
|
|
163
|
+
* stops — and then the lone stop is re-established.
|
|
164
|
+
*/
|
|
165
|
+
controlTargetConnected(control) {
|
|
166
|
+
if (!this.#connected) return;
|
|
167
|
+
control.tabIndex = -1;
|
|
168
|
+
this.#ensureTabStop();
|
|
169
|
+
}
|
|
170
|
+
/** Removing the control that held the Tab stop must not orphan the group. */
|
|
171
|
+
controlTargetDisconnected() {
|
|
172
|
+
if (!this.#connected) return;
|
|
173
|
+
this.#ensureTabStop();
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Arrow/Home/End move focus and the single tab stop.
|
|
177
|
+
*
|
|
178
|
+
* Binding this per control with `data-action` is **optional** — the same
|
|
179
|
+
* handling runs from the container's delegated listener. It stays a declared
|
|
180
|
+
* action so per-control wiring keeps working; wiring both does not
|
|
181
|
+
* double-move, because the delegated pass then yields on `defaultPrevented`.
|
|
182
|
+
*/
|
|
70
183
|
onKeydown(event) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
184
|
+
this.#handleKeydown(event);
|
|
185
|
+
}
|
|
186
|
+
/** Delegated counterpart of {@link onKeydown}; bound on the container. */
|
|
187
|
+
#onKeydown = (event) => {
|
|
188
|
+
this.#handleKeydown(event);
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
191
|
+
* Syncs the Tab stop to a control focused by other means (click, programmatic
|
|
192
|
+
* `focus()`) so Tab re-entry returns there. A non-navigable control is ignored:
|
|
193
|
+
* the lone Tab stop must never sit on something the user cannot operate.
|
|
194
|
+
*/
|
|
195
|
+
#onFocusin = (event) => {
|
|
196
|
+
const index = this.#indexOf(event.target);
|
|
197
|
+
const control = index === -1 ? void 0 : this.controlTargets[index];
|
|
198
|
+
if (!control || !this.#isNavigable(control)) return;
|
|
199
|
+
this.#roving.setActive(index);
|
|
200
|
+
};
|
|
201
|
+
#handleKeydown(event) {
|
|
202
|
+
if (event.defaultPrevented) return;
|
|
203
|
+
if (isReservedArrowChord(event)) return;
|
|
204
|
+
if (this.#composition.isComposing(event)) return;
|
|
205
|
+
this.#ensureTabStop();
|
|
206
|
+
const from = this.#indexOf(event.target);
|
|
207
|
+
if (from === -1) return;
|
|
76
208
|
const vertical = this.orientationValue === "vertical";
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
|
|
209
|
+
const rtl = !vertical && isRtl(this.element);
|
|
210
|
+
const forwardKey = vertical ? "ArrowDown" : rtl ? "ArrowLeft" : "ArrowRight";
|
|
211
|
+
const backwardKey = vertical ? "ArrowUp" : rtl ? "ArrowRight" : "ArrowLeft";
|
|
212
|
+
let target;
|
|
80
213
|
if (event.key === forwardKey) {
|
|
81
|
-
|
|
214
|
+
target = this.#nextNavigable(from, 1);
|
|
82
215
|
} else if (event.key === backwardKey) {
|
|
83
|
-
|
|
216
|
+
target = this.#nextNavigable(from, -1);
|
|
84
217
|
} else if (event.key === "Home") {
|
|
85
|
-
|
|
218
|
+
target = this.#navigableControls[0];
|
|
86
219
|
} else if (event.key === "End") {
|
|
87
|
-
|
|
220
|
+
const navigable = this.#navigableControls;
|
|
221
|
+
target = navigable[navigable.length - 1];
|
|
88
222
|
} else {
|
|
89
223
|
return;
|
|
90
224
|
}
|
|
91
225
|
event.preventDefault();
|
|
92
|
-
const target = navigable[next];
|
|
93
226
|
if (target) this.#roving.setActive(this.controlTargets.indexOf(target), { focus: true });
|
|
94
227
|
}
|
|
95
|
-
/**
|
|
228
|
+
/**
|
|
229
|
+
* Re-establishes the single Tab stop: keep the current one while it is still
|
|
230
|
+
* navigable, else hand it to the first navigable control. `-1` (no Tab stop at
|
|
231
|
+
* all) is reached only when every control is unavailable, and is recovered
|
|
232
|
+
* from as soon as one becomes navigable again. Idempotent by construction, so
|
|
233
|
+
* connect, the target callbacks, the observer, and keydown can all call it.
|
|
234
|
+
*/
|
|
235
|
+
#ensureTabStop() {
|
|
236
|
+
const active = this.#roving.activeIndex;
|
|
237
|
+
const activeEl = active === -1 ? null : this.controlTargets[active];
|
|
238
|
+
if (activeEl && this.#isNavigable(activeEl)) {
|
|
239
|
+
this.#roving.setActive(active);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const first = this.#navigableControls[0];
|
|
243
|
+
this.#roving.setActive(first ? this.controlTargets.indexOf(first) : -1);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* First navigable control strictly in the `delta` direction from `fromIndex`,
|
|
247
|
+
* scanning the **full** control list rather than the navigable subset so a
|
|
248
|
+
* non-navigable origin is never a dead end. When the direction yields nothing
|
|
249
|
+
* the origin keeps focus; when the origin itself is not navigable the first
|
|
250
|
+
* navigable control is used instead, so an arrow key always escapes.
|
|
251
|
+
*/
|
|
252
|
+
#nextNavigable(fromIndex, delta) {
|
|
253
|
+
const controls = this.controlTargets;
|
|
254
|
+
const length = controls.length;
|
|
255
|
+
const wrap = this.wrapValue ? "wrap" : "clamp";
|
|
256
|
+
let index = fromIndex;
|
|
257
|
+
for (let step = 0; step < length; step++) {
|
|
258
|
+
const next = rovingMove(index, length, delta, wrap);
|
|
259
|
+
if (next === index) break;
|
|
260
|
+
index = next;
|
|
261
|
+
const candidate = controls[index];
|
|
262
|
+
if (candidate && this.#isNavigable(candidate)) return candidate;
|
|
263
|
+
}
|
|
264
|
+
const origin = controls[fromIndex];
|
|
265
|
+
return origin && this.#isNavigable(origin) ? origin : this.#navigableControls[0];
|
|
266
|
+
}
|
|
267
|
+
/** Index in `controlTargets` of the control owning `target` (it or a descendant). */
|
|
268
|
+
#indexOf(target) {
|
|
269
|
+
const node = target;
|
|
270
|
+
if (!node) return -1;
|
|
271
|
+
return this.controlTargets.findIndex((control) => control === node || control.contains(node));
|
|
272
|
+
}
|
|
273
|
+
/** Controls eligible for the roving tab stop (excludes native disabled / hidden). */
|
|
96
274
|
get #navigableControls() {
|
|
97
275
|
return this.controlTargets.filter((control) => this.#isNavigable(control));
|
|
98
276
|
}
|
|
99
277
|
/**
|
|
100
|
-
* A control can hold the tab stop unless it is `hidden
|
|
101
|
-
*
|
|
278
|
+
* A control can hold the tab stop unless it is `hidden` or a disabled form
|
|
279
|
+
* control. `aria-disabled` remains navigable per the APG; activation suppression
|
|
280
|
+
* belongs to the control itself or its owning behavior. Native disabledness is
|
|
281
|
+
* read from the `disabled` property, narrowed by an `in` check rather than
|
|
282
|
+
* asserted since `<a>` and `<div role="button">` are legitimate controls that
|
|
283
|
+
* are unaffected by a disabled fieldset. CSS-only visibility cannot be detected
|
|
102
284
|
* headlessly and stays the consumer's responsibility.
|
|
103
285
|
*/
|
|
104
286
|
#isNavigable(control) {
|
|
105
|
-
if (control
|
|
106
|
-
if (
|
|
107
|
-
|
|
287
|
+
if (this.#isHidden(control)) return false;
|
|
288
|
+
if (!("disabled" in control)) return true;
|
|
289
|
+
if (control.disabled === true) return false;
|
|
290
|
+
return !inheritsFieldsetDisabled(control);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Whether `control`, or anything between it and the toolbar root, is `hidden`.
|
|
294
|
+
*
|
|
295
|
+
* Reading only the control's own attribute lets an invisible control hold the
|
|
296
|
+
* single Tab stop, which takes the *whole* toolbar out of the Tab sequence — an
|
|
297
|
+
* ordinary `hidden` wrapper is enough. The walk stops at the root: a toolbar
|
|
298
|
+
* inside a `hidden` region is already out of the page's Tab order.
|
|
299
|
+
*/
|
|
300
|
+
#isHidden(control) {
|
|
301
|
+
let node = control;
|
|
302
|
+
while (node && node !== this.element) {
|
|
303
|
+
if (node.hasAttribute("hidden")) return true;
|
|
304
|
+
node = node.parentElement;
|
|
305
|
+
}
|
|
306
|
+
return false;
|
|
108
307
|
}
|
|
109
308
|
};
|
|
110
309
|
|