stimeo-ui 0.2.0 → 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 +163 -0
- package/dist/cable/index.d.ts +16 -16
- package/dist/cable/index.js.map +1 -1
- package/dist/controllers/accordion_controller.d.ts +3 -2
- package/dist/controllers/accordion_controller.js +10 -0
- package/dist/controllers/accordion_controller.js.map +1 -1
- package/dist/controllers/alert_dialog_controller.d.ts +64 -0
- package/dist/controllers/alert_dialog_controller.js +318 -0
- package/dist/controllers/alert_dialog_controller.js.map +1 -0
- 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 +115 -0
- package/dist/controllers/carousel_controller.js +313 -0
- package/dist/controllers/carousel_controller.js.map +1 -0
- package/dist/controllers/clipboard_controller.d.ts +72 -0
- package/dist/controllers/clipboard_controller.js +144 -0
- package/dist/controllers/clipboard_controller.js.map +1 -0
- package/dist/controllers/collapsible_controller.d.ts +70 -0
- package/dist/controllers/collapsible_controller.js +327 -0
- package/dist/controllers/collapsible_controller.js.map +1 -0
- package/dist/controllers/color_picker_controller.d.ts +87 -0
- package/dist/controllers/color_picker_controller.js +252 -0
- package/dist/controllers/color_picker_controller.js.map +1 -0
- 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 +8 -1
- package/dist/controllers/count_up_controller.js.map +1 -1
- package/dist/controllers/currency_input_controller.d.ts +72 -0
- package/dist/controllers/currency_input_controller.js +147 -0
- package/dist/controllers/currency_input_controller.js.map +1 -0
- package/dist/controllers/data_grid_controller.d.ts +89 -0
- package/dist/controllers/data_grid_controller.js +246 -0
- package/dist/controllers/data_grid_controller.js.map +1 -0
- package/dist/controllers/date_range_picker_controller.d.ts +88 -0
- package/dist/controllers/date_range_picker_controller.js +441 -0
- package/dist/controllers/date_range_picker_controller.js.map +1 -0
- package/dist/controllers/dialog_controller.js.map +1 -1
- package/dist/controllers/dismissible_controller.d.ts +57 -0
- package/dist/controllers/dismissible_controller.js +117 -0
- package/dist/controllers/dismissible_controller.js.map +1 -0
- package/dist/controllers/drawer_controller.d.ts +96 -0
- package/dist/controllers/drawer_controller.js +630 -0
- package/dist/controllers/drawer_controller.js.map +1 -0
- package/dist/controllers/dropdown_controller.d.ts +1 -2
- package/dist/controllers/dropdown_controller.js.map +1 -1
- package/dist/controllers/editable_controller.d.ts +73 -0
- package/dist/controllers/editable_controller.js +169 -0
- package/dist/controllers/editable_controller.js.map +1 -0
- package/dist/controllers/file_dropzone_controller.d.ts +92 -0
- package/dist/controllers/file_dropzone_controller.js +165 -0
- package/dist/controllers/file_dropzone_controller.js.map +1 -0
- package/dist/controllers/filter_controller.d.ts +60 -0
- package/dist/controllers/filter_controller.js +86 -0
- package/dist/controllers/filter_controller.js.map +1 -0
- package/dist/controllers/flash_controller.js +36 -5
- package/dist/controllers/flash_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 +6 -4
- 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 +67 -19
- package/dist/controllers/intersection_controller.js.map +1 -1
- package/dist/controllers/lazy_frame_controller.js +54 -11
- 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/masonry_controller.d.ts +54 -0
- package/dist/controllers/masonry_controller.js +142 -0
- package/dist/controllers/masonry_controller.js.map +1 -0
- 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 +142 -0
- package/dist/controllers/menubar_controller.js +785 -0
- package/dist/controllers/menubar_controller.js.map +1 -0
- package/dist/controllers/meter_controller.js.map +1 -1
- package/dist/controllers/multi_select_controller.d.ts +141 -0
- package/dist/controllers/multi_select_controller.js +755 -0
- package/dist/controllers/multi_select_controller.js.map +1 -0
- package/dist/controllers/navigation_menu_controller.d.ts +146 -0
- package/dist/controllers/navigation_menu_controller.js +511 -0
- package/dist/controllers/navigation_menu_controller.js.map +1 -0
- 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 +15 -6
- package/dist/controllers/overflow_indicator_controller.js +246 -27
- 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/password_reveal_controller.d.ts +49 -0
- package/dist/controllers/password_reveal_controller.js +117 -0
- package/dist/controllers/password_reveal_controller.js.map +1 -0
- 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 +97 -0
- package/dist/controllers/range_slider_controller.js +192 -0
- package/dist/controllers/range_slider_controller.js.map +1 -0
- package/dist/controllers/rating_controller.js +16 -2
- package/dist/controllers/rating_controller.js.map +1 -1
- package/dist/controllers/read_more_controller.d.ts +52 -0
- package/dist/controllers/read_more_controller.js +238 -0
- package/dist/controllers/read_more_controller.js.map +1 -0
- 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 +101 -14
- package/dist/controllers/scroll_area_controller.js.map +1 -1
- package/dist/controllers/scroll_restore_controller.d.ts +54 -0
- package/dist/controllers/scroll_restore_controller.js +93 -0
- package/dist/controllers/scroll_restore_controller.js.map +1 -0
- package/dist/controllers/scroll_visibility_controller.d.ts +6 -3
- package/dist/controllers/scroll_visibility_controller.js +40 -6
- 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 +369 -74
- package/dist/controllers/scrollspy_controller.js.map +1 -1
- package/dist/controllers/separator_controller.d.ts +69 -0
- package/dist/controllers/separator_controller.js +96 -0
- package/dist/controllers/separator_controller.js.map +1 -0
- package/dist/controllers/sidebar_controller.d.ts +107 -0
- package/dist/controllers/sidebar_controller.js +761 -0
- package/dist/controllers/sidebar_controller.js.map +1 -0
- 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/stepper_controller.d.ts +4 -0
- package/dist/controllers/stepper_controller.js +28 -12
- package/dist/controllers/stepper_controller.js.map +1 -1
- package/dist/controllers/stick_to_bottom_controller.d.ts +5 -6
- package/dist/controllers/stick_to_bottom_controller.js +9 -4
- package/dist/controllers/stick_to_bottom_controller.js.map +1 -1
- package/dist/controllers/sticky_observer_controller.d.ts +17 -4
- package/dist/controllers/sticky_observer_controller.js +109 -20
- 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 +13 -4
- package/dist/controllers/tabs_controller.js +26 -3
- package/dist/controllers/tabs_controller.js.map +1 -1
- package/dist/controllers/tags_input_controller.d.ts +91 -0
- package/dist/controllers/tags_input_controller.js +295 -0
- package/dist/controllers/tags_input_controller.js.map +1 -0
- package/dist/controllers/theme_controller.d.ts +1 -1
- package/dist/controllers/theme_controller.js +42 -13
- package/dist/controllers/theme_controller.js.map +1 -1
- package/dist/controllers/time_picker_controller.d.ts +67 -0
- package/dist/controllers/time_picker_controller.js +231 -0
- package/dist/controllers/time_picker_controller.js.map +1 -0
- package/dist/controllers/toast_controller.d.ts +5 -14
- package/dist/controllers/toast_controller.js +40 -14
- 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.d.ts +14 -8
- package/dist/controllers/transition_controller.js +153 -38
- package/dist/controllers/transition_controller.js.map +1 -1
- package/dist/controllers/tree_view_controller.d.ts +135 -0
- package/dist/controllers/tree_view_controller.js +691 -0
- package/dist/controllers/tree_view_controller.js.map +1 -0
- package/dist/index.d.ts +27 -1818
- package/dist/index.js +4256 -915
- 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 +26 -26
- package/dist/inspector/manifest.json +1054 -127
- package/package.json +5 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/roving_tabindex.ts","../../src/controllers/roving_controller.ts"],"names":[],"mappings":";;;;;AAqBO,IAAM,iBAAN,MAAqB;AAAA;AAAA,EAEjB,SAAA;AAAA;AAAA;AAAA;AAAA;A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,CAAA;AAAA,EAC/C,CAAA;AAAA;AAAA,EAGA,SAAS,MAAA,EAAoC;AAC3C,IAAA,MAAM,IAAA,GAAO,MAAA;AACb,IAAA,IAAI,CAAC,MAAM,OAAO,EAAA;AAClB,IAAA,OAAO,IAAA,CAAK,WAAA,CAAY,SAAA,CAAU,CAAC,IAAA,KAAS,SAAS,IAAA,IAAQ,IAAA,CAAK,QAAA,CAAS,IAAI,CAAC,CAAA;AAAA,EAClF;AAAA;AAAA,EAGA,SAAA,CAAU,OAAe,KAAA,EAAsB;AAC7C,IAAA,MAAM,QAAA,GAAW,KAAK,OAAA,CAAQ,WAAA;AAC9B,IAAA,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,KAAA,EAAO,EAAE,OAAO,CAAA;AACvC,IAAA,IAAI,UAAU,QAAA,EAAU;AACtB,MAAA,IAAA,CAAK,QAAA,CAAS,QAAA,EAAU,EAAE,MAAA,EAAQ,EAAE,KAAA,EAAO,IAAA,EAAM,IAAA,CAAK,WAAA,CAAY,KAAK,CAAA,EAAE,EAAG,CAAA;AAAA,IAC9E;AAAA,EACF;AACF","file":"roving_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, type RovingWrap, rovingMove } from \"../utils/roving_tabindex\";\n\n/**\n * Headless **roving tabindex**: makes a set of `item`s a single Tab stop and\n * moves focus between them with the arrow keys — the APG roving-tabindex\n * technique, surfaced as a standalone controller. It is the policy layer over the\n * shared {@link RovingTabindex} util (counterpart to Focus Scope over `FocusTrap`),\n * giving the orientation / wrap / Home-End the util deliberately leaves out. No\n * dedicated APG pattern; it is the keyboard primitive Toolbar / Menu / Radio Group\n * and friends build on. Core (zero dependencies).\n *\n * Markup contract (identifier: `stimeo--roving`):\n * <div data-controller=\"stimeo--roving\"\n * data-stimeo--roving-orientation-value=\"horizontal\">\n * <button data-stimeo--roving-target=\"item\">A</button>\n * <button data-stimeo--roving-target=\"item\">B</button>\n * <button data-stimeo--roving-target=\"item\">C</button>\n * </div>\n *\n * Exactly one item is tabbable (`tabindex=\"0\"`); the arrow keys (per\n * `orientation`) move focus and that tab stop together, `Home`/`End` jump to the\n * ends (`homeEnd`), and `wrap` cycles past the ends or clamps at them. Listeners\n * are **delegated on the container** — `keydown` for movement and `focusin` to\n * sync the tab stop when focus arrives by click or programmatically — so\n * dynamically added/removed items need no per-item `data-action`. It emits\n * `change` whenever the tabbable item changes.\n *\n * @remarks\n * Behavior only: it owns `tabindex` and focus movement, nothing else. It does\n * **not** assign roles (`role=\"toolbar\"`/`\"radiogroup\"` is the author's), manage\n * selection / selection-follows-focus, typeahead, or activation (`Enter`/`Space`)\n * — those stay with the consuming pattern. `connect()` is idempotent: it keeps an\n * existing tab stop (reads it back from the DOM) and only defaults to the first\n * item when none is set, so a Turbo cache restore / morph never resets the user's\n * position. The delegated listeners are torn down on `disconnect()`.\n */\nexport class RovingController extends Controller<HTMLElement> {\n static override targets = [\"item\"];\n static override values = {\n orientation: { type: String, default: \"horizontal\" },\n wrap: { type: Boolean, default: true },\n homeEnd: { type: Boolean, default: true },\n };\n static events = [\"change\"] as const;\n\n declare readonly itemTargets: HTMLElement[];\n declare orientationValue: string;\n declare wrapValue: boolean;\n declare homeEndValue: boolean;\n\n readonly #roving = new RovingTabindex(() => this.itemTargets);\n\n override connect(): void {\n // Establish the single tab stop from the DOM (source of truth): keep an\n // existing tabbable item, else default to the first. Silent — no change event\n // for the initial mount.\n const active = this.#roving.activeIndex;\n this.#roving.setActive(active === -1 ? 0 : active);\n this.element.addEventListener(\"keydown\", this.#onKeydown);\n this.element.addEventListener(\"focusin\", this.#onFocusin);\n }\n\n override disconnect(): void {\n this.element.removeEventListener(\"keydown\", this.#onKeydown);\n this.element.removeEventListener(\"focusin\", this.#onFocusin);\n }\n\n /** Arrow keys move focus + the tab stop; Home/End jump to the ends. */\n readonly #onKeydown = (event: KeyboardEvent): void => {\n // A descendant widget that already claimed the key (e.g. a grabbed\n // `stimeo--pointer-drag` handle consuming arrows to move an item) must not\n // ALSO move the roving focus — composition depends on this yield.\n if (event.defaultPrevented) return;\n const items = this.itemTargets;\n const current = this.#indexOf(event.target);\n if (current === -1) return;\n\n const length = items.length;\n const wrap: RovingWrap = this.wrapValue ? \"wrap\" : \"clamp\";\n const orientation = this.orientationValue;\n const horizontal = orientation === \"horizontal\" || orientation === \"both\";\n const vertical = orientation === \"vertical\" || orientation === \"both\";\n\n let next: number;\n if ((horizontal && event.key === \"ArrowRight\") || (vertical && event.key === \"ArrowDown\")) {\n next = rovingMove(current, length, 1, wrap);\n } else if ((horizontal && event.key === \"ArrowLeft\") || (vertical && event.key === \"ArrowUp\")) {\n next = rovingMove(current, length, -1, wrap);\n } else if (this.homeEndValue && event.key === \"Home\") {\n next = 0;\n } else if (this.homeEndValue && event.key === \"End\") {\n next = length - 1;\n } else {\n return;\n }\n\n event.preventDefault();\n this.#activate(next, true);\n };\n\n /**\n * Syncs the single tab stop to an item that received focus by other means\n * (click, programmatic `focus()`), so returning via Tab lands on it. The\n * keyboard path's own `focus()` re-enters here but is a no-op (index unchanged).\n */\n readonly #onFocusin = (event: FocusEvent): void => {\n const index = this.#indexOf(event.target);\n if (index !== -1) this.#activate(index, false);\n };\n\n /** Resolves the item index owning an event target (the item or a descendant). */\n #indexOf(target: EventTarget | null): number {\n const node = target as Node | null;\n if (!node) return -1;\n return this.itemTargets.findIndex((item) => item === node || item.contains(node));\n }\n\n /** Makes `index` the tab stop (optionally focusing it), emitting `change` once. */\n #activate(index: number, focus: boolean): void {\n const previous = this.#roving.activeIndex;\n this.#roving.setActive(index, { focus });\n if (index !== previous) {\n this.dispatch(\"change\", { detail: { index, item: this.itemTargets[index] } });\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/logical_scroll.ts","../../src/utils/arrow_step.ts","../../src/utils/roving_tabindex.ts","../../src/controllers/roving_controller.ts"],"names":[],"mappings":";;;;;AAiBO,SAAS,MAAM,OAAA,EAA2B;AAC/C,EAAA,OAAO,MAAA,CAAO,gBAAA,CAAiB,OAAO,CAAA,CAAE,SAAA,KAAc,KAAA;AACxD;;;AC4EO,SAAS,oBAAA,CACd,KAAA,EACA,KAAA,GAAkC,EAAC,EAC1B;AACT,EAAA,IAAI,CAAC,KAAA,CAAM,GAAA,CAAI,UAAA,CAAW,OAAO,GAAG,OAAO,KAAA;AAC3C,EAAA,OACG,KAAA,CAAM,MAAA,IAAU,CAAC,KAAA,CAAM,QAAA,CAAS,KAAK,CAAA,IACrC,KAAA,CAAM,OAAA,IAAW,CAAC,KAAA,CAAM,QAAA,CAAS,MAAM,KACvC,KAAA,CAAM,OAAA,IAAW,CAAC,KAAA,CAAM,QAAA,CAAS,MAAM,CAAA,IACvC,KAAA,CAAM,QAAA,IAAY,CAAC,KAAA,CAAM,QAAA,CAAS,OAAO,CAAA;AAE9C;;;ACrFO,IAAM,iBAAN,MAAqB;AAAA;AAAA,EAEjB,SAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMT,YAAY,QAAA,EAA+B;AACzC,IAAA,IAAA,CAAK,SAAA,GAAY,QAAA;AAAA,EACnB;AAAA;AAAA,EAGA,IAAI,WAAA,GAAsB;AACxB,IAAA,OAAO,IAAA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AAM,MAAM,CAAA;AACxC,IAAA,IAAI,KAAA,KAAU,EAAA,EAAI,IAAA,CAAK,SAAA,CAAU,OAAO,KAAK,CAAA;AAAA,EAC/C,CAAA;AAAA;AAAA,EAGA,SAAS,MAAA,EAAoC;AAC3C,IAAA,MAAM,IAAA,GAAO,MAAA;AACb,IAAA,IAAI,CAAC,MAAM,OAAO,EAAA;AAClB,IAAA,OAAO,IAAA,CAAK,WAAA,CAAY,SAAA,CAAU,CAAC,IAAA,KAAS,SAAS,IAAA,IAAQ,IAAA,CAAK,QAAA,CAAS,IAAI,CAAC,CAAA;AAAA,EAClF;AAAA;AAAA,EAGA,SAAA,CAAU,OAAe,KAAA,EAAsB;AAC7C,IAAA,MAAM,QAAA,GAAW,KAAK,OAAA,CAAQ,WAAA;AAC9B,IAAA,IAAA,CAAK,OAAA,CAAQ,SAAA,CAAU,KAAA,EAAO,EAAE,OAAO,CAAA;AACvC,IAAA,IAAI,UAAU,QAAA,EAAU;AACtB,MAAA,IAAA,CAAK,QAAA,CAAS,QAAA,EAAU,EAAE,MAAA,EAAQ,EAAE,KAAA,EAAO,IAAA,EAAM,IAAA,CAAK,WAAA,CAAY,KAAK,CAAA,EAAE,EAAG,CAAA;AAAA,IAC9E;AAAA,EACF;AACF","file":"roving_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, type RovingWrap, rovingMove } from \"../utils/roving_tabindex\";\n\n/**\n * Headless **roving tabindex**: makes a set of `item`s a single Tab stop and\n * moves focus between them with the arrow keys — the APG roving-tabindex\n * technique, surfaced as a standalone controller. It is the policy layer over the\n * shared {@link RovingTabindex} util (counterpart to Focus Scope over `FocusTrap`),\n * giving the orientation / wrap / Home-End the util deliberately leaves out. No\n * dedicated APG pattern; it is the keyboard primitive Toolbar / Menu / Radio Group\n * and friends build on. Core (zero dependencies).\n *\n * Markup contract (identifier: `stimeo--roving`):\n * <div data-controller=\"stimeo--roving\"\n * data-stimeo--roving-orientation-value=\"horizontal\">\n * <button data-stimeo--roving-target=\"item\">A</button>\n * <button data-stimeo--roving-target=\"item\">B</button>\n * <button data-stimeo--roving-target=\"item\">C</button>\n * </div>\n *\n * Exactly one item is tabbable (`tabindex=\"0\"`); the arrow keys (per\n * `orientation`) move focus and that tab stop together, `Home`/`End` jump to the\n * ends (`homeEnd`), and `wrap` cycles past the ends or clamps at them. Listeners\n * are **delegated on the container** — `keydown` for movement and `focusin` to\n * sync the tab stop when focus arrives by click or programmatically — so\n * dynamically added/removed items need no per-item `data-action`. It emits\n * `change` whenever the tabbable item changes.\n *\n * @remarks\n * Behavior only: it owns `tabindex` and focus movement, nothing else. It does\n * **not** assign roles (`role=\"toolbar\"`/`\"radiogroup\"` is the author's), manage\n * selection / selection-follows-focus, typeahead, or activation (`Enter`/`Space`)\n * — those stay with the consuming pattern. `connect()` is idempotent: it keeps an\n * existing tab stop (reads it back from the DOM) and only defaults to the first\n * item when none is set, so a Turbo cache restore / morph never resets the user's\n * position. The delegated listeners are torn down on `disconnect()`.\n */\nexport class RovingController extends Controller<HTMLElement> {\n static override targets = [\"item\"];\n static override values = {\n orientation: { type: String, default: \"horizontal\" },\n wrap: { type: Boolean, default: true },\n homeEnd: { type: Boolean, default: true },\n };\n static events = [\"change\"] as const;\n\n declare readonly itemTargets: HTMLElement[];\n declare orientationValue: string;\n declare wrapValue: boolean;\n declare homeEndValue: boolean;\n\n readonly #roving = new RovingTabindex(() => this.itemTargets);\n\n override connect(): void {\n // Establish the single tab stop from the DOM (source of truth): keep an\n // existing tabbable item, else default to the first. Silent — no change event\n // for the initial mount.\n const active = this.#roving.activeIndex;\n this.#roving.setActive(active === -1 ? 0 : active);\n this.element.addEventListener(\"keydown\", this.#onKeydown);\n this.element.addEventListener(\"focusin\", this.#onFocusin);\n }\n\n override disconnect(): void {\n this.element.removeEventListener(\"keydown\", this.#onKeydown);\n this.element.removeEventListener(\"focusin\", this.#onFocusin);\n }\n\n /** Arrow keys move focus + the tab stop; Home/End jump to the ends. */\n readonly #onKeydown = (event: KeyboardEvent): void => {\n // A descendant widget that already claimed the key (e.g. a grabbed\n // `stimeo--pointer-drag` handle consuming arrows to move an item) must not\n // ALSO move the roving focus — composition depends on this yield.\n if (event.defaultPrevented) return;\n if (isReservedArrowChord(event)) return;\n const items = this.itemTargets;\n const current = this.#indexOf(event.target);\n if (current === -1) return;\n\n const length = items.length;\n const wrap: RovingWrap = this.wrapValue ? \"wrap\" : \"clamp\";\n const orientation = this.orientationValue;\n const horizontal = orientation === \"horizontal\" || orientation === \"both\";\n const vertical = orientation === \"vertical\" || orientation === \"both\";\n\n // Logical, not physical. APG defines these as \"next / previous\n // control\", and says a vertical arrangement swaps in Down/Up for the same\n // meaning — so the pair is one axis's spelling of an order, and the order\n // reverses with the writing direction. Read from the controller element: the\n // container is what lays the items out, and a child may carry its own `dir`\n // (an LTR input inside an RTL form is ordinary authoring).\n // Only the horizontal pair reverses; `orientation=\"both\"` keeps Down/Up as-is.\n const rtl = horizontal && isRtl(this.element);\n const forwardKey = rtl ? \"ArrowLeft\" : \"ArrowRight\";\n const backwardKey = rtl ? \"ArrowRight\" : \"ArrowLeft\";\n\n let next: number;\n if ((horizontal && event.key === forwardKey) || (vertical && event.key === \"ArrowDown\")) {\n next = rovingMove(current, length, 1, wrap);\n } else if ((horizontal && event.key === backwardKey) || (vertical && event.key === \"ArrowUp\")) {\n next = rovingMove(current, length, -1, wrap);\n } else if (this.homeEndValue && event.key === \"Home\") {\n next = 0;\n } else if (this.homeEndValue && event.key === \"End\") {\n next = length - 1;\n } else {\n return;\n }\n\n event.preventDefault();\n this.#activate(next, true);\n };\n\n /**\n * Syncs the single tab stop to an item that received focus by other means\n * (click, programmatic `focus()`), so returning via Tab lands on it. The\n * keyboard path's own `focus()` re-enters here but is a no-op (index unchanged).\n */\n readonly #onFocusin = (event: FocusEvent): void => {\n const index = this.#indexOf(event.target);\n if (index !== -1) this.#activate(index, false);\n };\n\n /** Resolves the item index owning an event target (the item or a descendant). */\n #indexOf(target: EventTarget | null): number {\n const node = target as Node | null;\n if (!node) return -1;\n return this.itemTargets.findIndex((item) => item === node || item.contains(node));\n }\n\n /** Makes `index` the tab stop (optionally focusing it), emitting `change` once. */\n #activate(index: number, focus: boolean): void {\n const previous = this.#roving.activeIndex;\n this.#roving.setActive(index, { focus });\n if (index !== previous) {\n this.dispatch(\"change\", { detail: { index, item: this.itemTargets[index] } });\n }\n }\n}\n"]}
|
|
@@ -58,6 +58,47 @@ var LayoutObserver = class {
|
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
+
// src/utils/logical_scroll.ts
|
|
62
|
+
function isRtl(element) {
|
|
63
|
+
return window.getComputedStyle(element).direction === "rtl";
|
|
64
|
+
}
|
|
65
|
+
function logicalScrollMetrics(element, horizontal) {
|
|
66
|
+
const max = Math.max(
|
|
67
|
+
0,
|
|
68
|
+
horizontal ? element.scrollWidth - element.clientWidth : element.scrollHeight - element.clientHeight
|
|
69
|
+
);
|
|
70
|
+
const raw = horizontal ? element.scrollLeft : element.scrollTop;
|
|
71
|
+
const position = horizontal && isRtl(element) ? -raw : raw;
|
|
72
|
+
return { position: Math.min(max, Math.max(0, position)), max };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/utils/tabindex_loan.ts
|
|
76
|
+
var TabindexLoan = class {
|
|
77
|
+
#value;
|
|
78
|
+
#lent = /* @__PURE__ */ new Set();
|
|
79
|
+
/**
|
|
80
|
+
* @param value - the `tabindex` to lend. `"-1"` (the default) is
|
|
81
|
+
* programmatically focusable but not a Tab stop; `"0"` is a real Tab stop,
|
|
82
|
+
* which a scroll region with no focusable content of its own needs.
|
|
83
|
+
*/
|
|
84
|
+
constructor(value = "-1") {
|
|
85
|
+
this.#value = value;
|
|
86
|
+
}
|
|
87
|
+
/** Lends `element` the value; no-ops when it already carries a `tabindex`. */
|
|
88
|
+
lend(element) {
|
|
89
|
+
if (element.hasAttribute("tabindex")) return;
|
|
90
|
+
element.setAttribute("tabindex", this.#value);
|
|
91
|
+
this.#lent.add(element);
|
|
92
|
+
}
|
|
93
|
+
/** Takes back every loan whose value is still the one that was lent. */
|
|
94
|
+
returnAll() {
|
|
95
|
+
for (const element of this.#lent) {
|
|
96
|
+
if (element.getAttribute("tabindex") === this.#value) element.removeAttribute("tabindex");
|
|
97
|
+
}
|
|
98
|
+
this.#lent.clear();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
61
102
|
// src/controllers/scroll_area_controller.ts
|
|
62
103
|
var FOCUSABLE_SELECTOR = [
|
|
63
104
|
"a[href]",
|
|
@@ -76,10 +117,12 @@ var ScrollAreaController = class extends Controller {
|
|
|
76
117
|
};
|
|
77
118
|
static events = ["reach"];
|
|
78
119
|
#layout = new LayoutObserver(() => this.#update());
|
|
120
|
+
/** Re-checks the tab stop when the viewport's focusable content comes or goes. */
|
|
121
|
+
#content = null;
|
|
79
122
|
/** Last edge reported via `reach`, so the event fires once per arrival. */
|
|
80
123
|
#lastEdge = null;
|
|
81
124
|
/** Whether this controller added `tabindex`, so teardown only removes its own. */
|
|
82
|
-
#
|
|
125
|
+
#tabindex = new TabindexLoan("0");
|
|
83
126
|
/** Whether this controller added `role="region"`, for symmetric teardown. */
|
|
84
127
|
#addedRole = false;
|
|
85
128
|
#onScroll = () => {
|
|
@@ -90,6 +133,18 @@ var ScrollAreaController = class extends Controller {
|
|
|
90
133
|
this.viewportTarget.addEventListener("scroll", this.#onScroll, { passive: true });
|
|
91
134
|
this.#layout.observe(this.viewportTarget);
|
|
92
135
|
this.#layout.observeViewport();
|
|
136
|
+
if (typeof MutationObserver !== "undefined") {
|
|
137
|
+
this.#content = new MutationObserver(() => {
|
|
138
|
+
if (!this.hasViewportTarget) return;
|
|
139
|
+
const vp = this.viewportTarget;
|
|
140
|
+
this.#syncKeyboardReach(vp, this.#syncOverflow(vp));
|
|
141
|
+
});
|
|
142
|
+
this.#content.observe(this.viewportTarget, {
|
|
143
|
+
subtree: true,
|
|
144
|
+
childList: true,
|
|
145
|
+
attributes: true
|
|
146
|
+
});
|
|
147
|
+
}
|
|
93
148
|
this.#update();
|
|
94
149
|
}
|
|
95
150
|
disconnect() {
|
|
@@ -98,14 +153,15 @@ var ScrollAreaController = class extends Controller {
|
|
|
98
153
|
this.#clearAddedAttributes(this.viewportTarget);
|
|
99
154
|
}
|
|
100
155
|
this.#layout.disconnect();
|
|
156
|
+
this.#content?.disconnect();
|
|
157
|
+
this.#content = null;
|
|
101
158
|
this.#lastEdge = null;
|
|
102
159
|
}
|
|
103
160
|
/** Re-measures overflow and scroll position and reflects the state hooks. */
|
|
104
161
|
#update() {
|
|
105
162
|
if (!this.hasViewportTarget) return;
|
|
106
163
|
const vp = this.viewportTarget;
|
|
107
|
-
const overflowing = this.#
|
|
108
|
-
this.element.setAttribute("data-overflow", overflowing ? "true" : "false");
|
|
164
|
+
const overflowing = this.#syncOverflow(vp);
|
|
109
165
|
this.#syncKeyboardReach(vp, overflowing);
|
|
110
166
|
const { position, progress } = this.#measurePosition(vp);
|
|
111
167
|
this.element.setAttribute("data-scroll", position);
|
|
@@ -119,6 +175,21 @@ var ScrollAreaController = class extends Controller {
|
|
|
119
175
|
}
|
|
120
176
|
}
|
|
121
177
|
/** Whether the viewport can scroll on the configured axis. */
|
|
178
|
+
/**
|
|
179
|
+
* Measures overflow and reflects the `data-overflow` hook.
|
|
180
|
+
*
|
|
181
|
+
* The write is skipped when the value is unchanged. An identical `setAttribute` still
|
|
182
|
+
* queues a MutationRecord, and markup that puts the viewport target on the controller
|
|
183
|
+
* element itself would then have the content observer trigger its own next callback.
|
|
184
|
+
*/
|
|
185
|
+
#syncOverflow(vp) {
|
|
186
|
+
const overflowing = this.#measureOverflow(vp);
|
|
187
|
+
const next = overflowing ? "true" : "false";
|
|
188
|
+
if (this.element.getAttribute("data-overflow") !== next) {
|
|
189
|
+
this.element.setAttribute("data-overflow", next);
|
|
190
|
+
}
|
|
191
|
+
return overflowing;
|
|
192
|
+
}
|
|
122
193
|
#measureOverflow(vp) {
|
|
123
194
|
const o = this.orientationValue;
|
|
124
195
|
const vertical = o !== "horizontal" && vp.scrollHeight > vp.clientHeight + EDGE_EPSILON;
|
|
@@ -131,8 +202,7 @@ var ScrollAreaController = class extends Controller {
|
|
|
131
202
|
*/
|
|
132
203
|
#measurePosition(vp) {
|
|
133
204
|
const horizontalPrimary = this.orientationValue === "horizontal" || this.orientationValue === "both" && vp.scrollHeight <= vp.clientHeight + EDGE_EPSILON;
|
|
134
|
-
const scrollPos
|
|
135
|
-
const maxScroll = horizontalPrimary ? vp.scrollWidth - vp.clientWidth : vp.scrollHeight - vp.clientHeight;
|
|
205
|
+
const { position: scrollPos, max: maxScroll } = logicalScrollMetrics(vp, horizontalPrimary);
|
|
136
206
|
if (maxScroll <= EDGE_EPSILON) return { position: "start", progress: 0 };
|
|
137
207
|
const progress = Math.min(1, Math.max(0, scrollPos / maxScroll));
|
|
138
208
|
if (scrollPos <= EDGE_EPSILON) return { position: "start", progress };
|
|
@@ -147,10 +217,7 @@ var ScrollAreaController = class extends Controller {
|
|
|
147
217
|
#syncKeyboardReach(vp, overflowing) {
|
|
148
218
|
const wantsTabindex = overflowing && !this.#hasFocusableContent(vp);
|
|
149
219
|
if (wantsTabindex) {
|
|
150
|
-
|
|
151
|
-
vp.setAttribute("tabindex", "0");
|
|
152
|
-
this.#addedTabindex = true;
|
|
153
|
-
}
|
|
220
|
+
this.#tabindex.lend(vp);
|
|
154
221
|
if (!vp.hasAttribute("role") && this.#hasAccessibleName(vp)) {
|
|
155
222
|
vp.setAttribute("role", "region");
|
|
156
223
|
this.#addedRole = true;
|
|
@@ -161,17 +228,37 @@ var ScrollAreaController = class extends Controller {
|
|
|
161
228
|
}
|
|
162
229
|
/** Removes (and resets the flags for) only the attributes this controller added. */
|
|
163
230
|
#clearAddedAttributes(vp) {
|
|
164
|
-
|
|
165
|
-
vp.removeAttribute("tabindex");
|
|
166
|
-
this.#addedTabindex = false;
|
|
167
|
-
}
|
|
231
|
+
this.#tabindex.returnAll();
|
|
168
232
|
if (this.#addedRole) {
|
|
169
233
|
vp.removeAttribute("role");
|
|
170
234
|
this.#addedRole = false;
|
|
171
235
|
}
|
|
172
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* Whether the viewport owns something the user can Tab to *right now*.
|
|
239
|
+
*
|
|
240
|
+
* The selector alone is not enough: a `display: none` button still matches it,
|
|
241
|
+
* so a viewport whose only control is revealed on demand would never get a tab
|
|
242
|
+
* stop — leaving it unreachable by keyboard exactly while it has nothing else to
|
|
243
|
+
* offer. Only rendered candidates count.
|
|
244
|
+
*/
|
|
173
245
|
#hasFocusableContent(vp) {
|
|
174
|
-
return vp.
|
|
246
|
+
return Array.from(vp.querySelectorAll(FOCUSABLE_SELECTOR)).some(
|
|
247
|
+
(el) => this.#isRendered(el)
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Whether `el` is actually rendered, and so can hold focus.
|
|
252
|
+
*
|
|
253
|
+
* `checkVisibility()` answers this for every way CSS can remove a box, including
|
|
254
|
+
* a class-driven `display: none` that no attribute reveals. The `hidden` walk in
|
|
255
|
+
* front of it is not redundant: it is the one case a DOM-only environment with no
|
|
256
|
+
* layout engine has to be told about explicitly.
|
|
257
|
+
*/
|
|
258
|
+
#isRendered(el) {
|
|
259
|
+
if (el.closest("[hidden]") !== null) return false;
|
|
260
|
+
const check = el.checkVisibility;
|
|
261
|
+
return typeof check === "function" ? check.call(el, { visibilityProperty: true }) : true;
|
|
175
262
|
}
|
|
176
263
|
#hasAccessibleName(vp) {
|
|
177
264
|
return vp.hasAttribute("aria-label") || vp.hasAttribute("aria-labelledby");
|
|
@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AY;AACnB,MAAA,EAAA,CAAG,gBAAgB,MAAM,CAAA;AACzB,MAAA,IAAA,CAAK,UAAA,GAAa,KAAA;AAAA,IACpB;AAAA,EACF;AAAA,EAEA,qBAAqB,EAAA,EAA0B;AAC7C,IAAA,OAAO,EAAA,CAAG,aAAA,CAAc,kBAAkB,CAAA,KAAM,IAAA;AAAA,EAClD;AAAA,EAEA,mBAAmB,EAAA,EAA0B;AAC3C,IAAA,OAAO,GAAG,YAAA,CAAa,YAAY,CAAA,IAAK,EAAA,CAAG,aAAa,iBAAiB,CAAA;AAAA,EAC3E;AACF","file":"scroll_area_controller.js","sourcesContent":["/**\n * Unified element-size and viewport observation for Stimeo controllers.\n *\n * Layout-sensitive widgets (sliders, resizable panes, scroll spies, popovers)\n * need to react both to their *own* box changing — via {@link ResizeObserver} —\n * and to the *viewport* changing — via the `window` `resize` event. Wiring those\n * two sources by hand in every controller risks leaked listeners on\n * `disconnect()`. {@link LayoutObserver} owns both behind one callback and one\n * {@link LayoutObserver.disconnect | disconnect()} that releases everything.\n *\n * Behavior only: the helper reports *that* layout changed; it never reads or\n * writes styles. Consumers decide what to recompute.\n */\n\n/** Invoked whenever an observed element or the viewport changes size. */\nexport type LayoutCallback = () => void;\n\n/** Constructs a {@link ResizeObserver}; injectable so tests stay deterministic. */\nexport type ResizeObserverFactory = (callback: ResizeObserverCallback) => ResizeObserver;\n\n/** Options for {@link LayoutObserver}. */\nexport interface LayoutObserverOptions {\n /**\n * Factory for the {@link ResizeObserver} used by {@link LayoutObserver.observe}.\n * Defaults to the global constructor; override it in tests, or to no-op in\n * environments where `ResizeObserver` is unavailable.\n */\n resizeObserverFactory?: ResizeObserverFactory;\n}\n\n/**\n * Observes element resizes and/or viewport resizes through a single callback,\n * with guaranteed teardown.\n *\n * @example\n * ```ts\n * #layout = new LayoutObserver(() => this.#reposition());\n *\n * connect() {\n * this.#layout.observe(this.panelTarget);\n * this.#layout.observeViewport();\n * }\n *\n * disconnect() {\n * this.#layout.disconnect();\n * }\n * ```\n */\nexport class LayoutObserver {\n readonly #callback: LayoutCallback;\n readonly #resizeObserverFactory: ResizeObserverFactory | null;\n #resizeObserver: ResizeObserver | null = null;\n #observingViewport = false;\n\n /** Stable bound handler so add/removeEventListener target the same reference. */\n readonly #handleViewportResize = (): void => {\n this.#callback();\n };\n\n constructor(callback: LayoutCallback, options: LayoutObserverOptions = {}) {\n this.#callback = callback;\n this.#resizeObserverFactory =\n options.resizeObserverFactory ??\n (typeof ResizeObserver === \"undefined\" ? null : (cb) => new ResizeObserver(cb));\n }\n\n /**\n * Starts observing an element's size. Repeated calls observe additional\n * elements through the same shared observer. No-ops when no\n * `ResizeObserver` implementation is available.\n */\n observe(element: Element): void {\n if (!this.#resizeObserverFactory) return;\n if (!this.#resizeObserver) {\n this.#resizeObserver = this.#resizeObserverFactory(() => {\n this.#callback();\n });\n }\n this.#resizeObserver.observe(element);\n }\n\n /** Stops observing a single element while leaving any others in place. */\n unobserve(element: Element): void {\n this.#resizeObserver?.unobserve(element);\n }\n\n /** Starts observing viewport resizes. Idempotent: the listener is added once. */\n observeViewport(): void {\n if (this.#observingViewport) return;\n this.#observingViewport = true;\n window.addEventListener(\"resize\", this.#handleViewportResize);\n }\n\n /** Stops observing viewport resizes without affecting element observation. */\n unobserveViewport(): void {\n if (!this.#observingViewport) return;\n this.#observingViewport = false;\n window.removeEventListener(\"resize\", this.#handleViewportResize);\n }\n\n /**\n * Releases every observation: disconnects the {@link ResizeObserver} and\n * removes the viewport listener. Safe to call multiple times. Call this from a\n * controller's `disconnect()`.\n */\n disconnect(): void {\n this.#resizeObserver?.disconnect();\n this.#resizeObserver = null;\n this.unobserveViewport();\n }\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { LayoutObserver } from \"../utils/layout_observer\";\n\n/** A CSS selector for natively focusable / author-focusable descendants. */\nconst FOCUSABLE_SELECTOR = [\n \"a[href]\",\n \"button:not([disabled])\",\n \"input:not([disabled])\",\n \"select:not([disabled])\",\n \"textarea:not([disabled])\",\n \"[tabindex]:not([tabindex='-1'])\",\n \"[contenteditable='true']\",\n].join(\",\");\n\n/** Distance from an edge (px) treated as fully reached; absorbs sub-pixel scroll. */\nconst EDGE_EPSILON = 1;\n\n/**\n * Headless **Scroll Area** behavior: keyboard reachability and scroll-state hooks\n * for a natively scrolling region. No custom scrollbar — the native one is\n * respected; this only adds a11y and CSS state.\n *\n * Markup contract (identifier: `stimeo--scroll-area`):\n * <div data-controller=\"stimeo--scroll-area\"\n * data-stimeo--scroll-area-orientation-value=\"vertical\">\n * <div data-stimeo--scroll-area-target=\"viewport\" aria-label=\"Log output\">\n * <!-- long content -->\n * </div>\n * </div>\n *\n * When the content overflows and the viewport holds no focusable elements of its\n * own, the viewport is made keyboard-scrollable (`tabindex=\"0\"`, plus `role=\"region\"`\n * when it already has an accessible name). Scroll position is published as\n * `data-scroll` (`start`/`middle`/`end`), overflow as `data-overflow`, and progress\n * as `--stimeo-scroll-progress` (0–1) so consumer CSS can draw scroll shadows.\n *\n * @remarks\n * Behavior only. The `scroll` listener and {@link LayoutObserver} (element +\n * viewport resize) are torn down on `disconnect()` (Turbo navigation included).\n * `role=\"region\"` is added only when the viewport is already named, so a scrollable\n * region never becomes an unlabeled landmark.\n */\nexport class ScrollAreaController extends Controller<HTMLElement> {\n static override targets = [\"viewport\"];\n static override values = {\n orientation: { type: String, default: \"vertical\" },\n };\n static events = [\"reach\"] as const;\n\n declare readonly viewportTarget: HTMLElement;\n declare readonly hasViewportTarget: boolean;\n\n declare orientationValue: string;\n\n readonly #layout = new LayoutObserver(() => this.#update());\n /** Last edge reported via `reach`, so the event fires once per arrival. */\n #lastEdge: \"start\" | \"end\" | null = null;\n /** Whether this controller added `tabindex`, so teardown only removes its own. */\n #addedTabindex = false;\n /** Whether this controller added `role=\"region\"`, for symmetric teardown. */\n #addedRole = false;\n\n readonly #onScroll = (): void => {\n this.#update();\n };\n\n override connect(): void {\n if (!this.hasViewportTarget) return;\n this.viewportTarget.addEventListener(\"scroll\", this.#onScroll, { passive: true });\n this.#layout.observe(this.viewportTarget);\n this.#layout.observeViewport();\n this.#update();\n }\n\n override disconnect(): void {\n if (this.hasViewportTarget) {\n this.viewportTarget.removeEventListener(\"scroll\", this.#onScroll);\n // Remove only the keyboard-reach attributes this controller added, so a\n // Turbo cache snapshot never preserves a controller-owned tab stop /\n // landmark (controller-added state must not outlive the controller).\n this.#clearAddedAttributes(this.viewportTarget);\n }\n this.#layout.disconnect();\n this.#lastEdge = null;\n }\n\n /** Re-measures overflow and scroll position and reflects the state hooks. */\n #update(): void {\n if (!this.hasViewportTarget) return;\n const vp = this.viewportTarget;\n const overflowing = this.#measureOverflow(vp);\n\n this.element.setAttribute(\"data-overflow\", overflowing ? \"true\" : \"false\");\n this.#syncKeyboardReach(vp, overflowing);\n\n const { position, progress } = this.#measurePosition(vp);\n this.element.setAttribute(\"data-scroll\", position);\n this.element.style.setProperty(\"--stimeo-scroll-progress\", String(progress));\n\n const edge = position === \"start\" ? \"start\" : position === \"end\" ? \"end\" : null;\n if (overflowing && edge && edge !== this.#lastEdge) {\n this.#lastEdge = edge;\n this.dispatch(\"reach\", { detail: { edge } });\n } else if (!edge) {\n this.#lastEdge = null;\n }\n }\n\n /** Whether the viewport can scroll on the configured axis. */\n #measureOverflow(vp: HTMLElement): boolean {\n const o = this.orientationValue;\n const vertical = o !== \"horizontal\" && vp.scrollHeight > vp.clientHeight + EDGE_EPSILON;\n const horizontal = o !== \"vertical\" && vp.scrollWidth > vp.clientWidth + EDGE_EPSILON;\n return vertical || horizontal;\n }\n\n /**\n * Reports the scroll position bucket and 0–1 progress on the primary axis. For\n * `both`, the vertical axis is used when it overflows, otherwise the horizontal.\n */\n #measurePosition(vp: HTMLElement): {\n position: \"start\" | \"middle\" | \"end\";\n progress: number;\n } {\n const horizontalPrimary =\n this.orientationValue === \"horizontal\" ||\n (this.orientationValue === \"both\" && vp.scrollHeight <= vp.clientHeight + EDGE_EPSILON);\n\n const scrollPos = horizontalPrimary ? vp.scrollLeft : vp.scrollTop;\n const maxScroll = horizontalPrimary\n ? vp.scrollWidth - vp.clientWidth\n : vp.scrollHeight - vp.clientHeight;\n\n if (maxScroll <= EDGE_EPSILON) return { position: \"start\", progress: 0 };\n\n const progress = Math.min(1, Math.max(0, scrollPos / maxScroll));\n if (scrollPos <= EDGE_EPSILON) return { position: \"start\", progress };\n if (scrollPos >= maxScroll - EDGE_EPSILON) return { position: \"end\", progress };\n return { position: \"middle\", progress };\n }\n\n /**\n * Makes the viewport keyboard-scrollable when it overflows and contains no\n * focusable elements of its own (avoiding a double tab stop). Adds `role=\"region\"`\n * only when the viewport already carries an accessible name.\n */\n #syncKeyboardReach(vp: HTMLElement, overflowing: boolean): void {\n const wantsTabindex = overflowing && !this.#hasFocusableContent(vp);\n\n if (wantsTabindex) {\n if (!vp.hasAttribute(\"tabindex\")) {\n vp.setAttribute(\"tabindex\", \"0\");\n this.#addedTabindex = true;\n }\n if (!vp.hasAttribute(\"role\") && this.#hasAccessibleName(vp)) {\n vp.setAttribute(\"role\", \"region\");\n this.#addedRole = true;\n }\n } else {\n this.#clearAddedAttributes(vp);\n }\n }\n\n /** Removes (and resets the flags for) only the attributes this controller added. */\n #clearAddedAttributes(vp: HTMLElement): void {\n if (this.#addedTabindex) {\n vp.removeAttribute(\"tabindex\");\n this.#addedTabindex = false;\n }\n if (this.#addedRole) {\n vp.removeAttribute(\"role\");\n this.#addedRole = false;\n }\n }\n\n #hasFocusableContent(vp: HTMLElement): boolean {\n return vp.querySelector(FOCUSABLE_SELECTOR) !== null;\n }\n\n #hasAccessibleName(vp: HTMLElement): boolean {\n return vp.hasAttribute(\"aria-label\") || vp.hasAttribute(\"aria-labelledby\");\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/layout_observer.ts","../../src/utils/logical_scroll.ts","../../src/utils/tabindex_loan.ts","../../src/controllers/scroll_area_controller.ts"],"names":[],"mappings":";;;;;AAgDO,IAAM,iBAAN,MAAqB;AAAA,EACj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KAAA,KAAU,UAAA,GAAa,KAAA,CAAM,IAAA,CAAK,IAAI,EAAE,kBAAA,EAAoB,IAAA,EAAM,CAAA,GAAI,IAAA;AAAA,EACtF;AAAA,EAEA,mBAAmB,EAAA,EAA0B;AAC3C,IAAA,OAAO,GAAG,YAAA,CAAa,YAAY,CAAA,IAAK,EAAA,CAAG,aAAa,iBAAiB,CAAA;AAAA,EAC3E;AACF","file":"scroll_area_controller.js","sourcesContent":["/**\n * Unified element-size and viewport observation for Stimeo controllers.\n *\n * Layout-sensitive widgets (sliders, resizable panes, scroll spies, popovers)\n * need to react both to their *own* box changing — via {@link ResizeObserver} —\n * and to the *viewport* changing — via the `window` `resize` event. Wiring those\n * two sources by hand in every controller risks leaked listeners on\n * `disconnect()`. {@link LayoutObserver} owns both behind one callback and one\n * {@link LayoutObserver.disconnect | disconnect()} that releases everything.\n *\n * Behavior only: the helper reports *that* layout changed; it never reads or\n * writes styles. Consumers decide what to recompute.\n */\n\n/** Invoked whenever an observed element or the viewport changes size. */\nexport type LayoutCallback = () => void;\n\n/** Constructs a {@link ResizeObserver}; injectable so tests stay deterministic. */\nexport type ResizeObserverFactory = (callback: ResizeObserverCallback) => ResizeObserver;\n\n/** Options for {@link LayoutObserver}. */\nexport interface LayoutObserverOptions {\n /**\n * Factory for the {@link ResizeObserver} used by {@link LayoutObserver.observe}.\n * Defaults to the global constructor; override it in tests, or to no-op in\n * environments where `ResizeObserver` is unavailable.\n */\n resizeObserverFactory?: ResizeObserverFactory;\n}\n\n/**\n * Observes element resizes and/or viewport resizes through a single callback,\n * with guaranteed teardown.\n *\n * @example\n * ```ts\n * #layout = new LayoutObserver(() => this.#reposition());\n *\n * connect() {\n * this.#layout.observe(this.panelTarget);\n * this.#layout.observeViewport();\n * }\n *\n * disconnect() {\n * this.#layout.disconnect();\n * }\n * ```\n */\nexport class LayoutObserver {\n readonly #callback: LayoutCallback;\n readonly #resizeObserverFactory: ResizeObserverFactory | null;\n #resizeObserver: ResizeObserver | null = null;\n #observingViewport = false;\n\n /** Stable bound handler so add/removeEventListener target the same reference. */\n readonly #handleViewportResize = (): void => {\n this.#callback();\n };\n\n constructor(callback: LayoutCallback, options: LayoutObserverOptions = {}) {\n this.#callback = callback;\n this.#resizeObserverFactory =\n options.resizeObserverFactory ??\n (typeof ResizeObserver === \"undefined\" ? null : (cb) => new ResizeObserver(cb));\n }\n\n /**\n * Starts observing an element's size. Repeated calls observe additional\n * elements through the same shared observer. No-ops when no\n * `ResizeObserver` implementation is available.\n */\n observe(element: Element): void {\n if (!this.#resizeObserverFactory) return;\n if (!this.#resizeObserver) {\n this.#resizeObserver = this.#resizeObserverFactory(() => {\n this.#callback();\n });\n }\n this.#resizeObserver.observe(element);\n }\n\n /** Stops observing a single element while leaving any others in place. */\n unobserve(element: Element): void {\n this.#resizeObserver?.unobserve(element);\n }\n\n /** Starts observing viewport resizes. Idempotent: the listener is added once. */\n observeViewport(): void {\n if (this.#observingViewport) return;\n this.#observingViewport = true;\n window.addEventListener(\"resize\", this.#handleViewportResize);\n }\n\n /** Stops observing viewport resizes without affecting element observation. */\n unobserveViewport(): void {\n if (!this.#observingViewport) return;\n this.#observingViewport = false;\n window.removeEventListener(\"resize\", this.#handleViewportResize);\n }\n\n /**\n * Releases every observation: disconnects the {@link ResizeObserver} and\n * removes the viewport listener. Safe to call multiple times. Call this from a\n * controller's `disconnect()`.\n */\n disconnect(): void {\n this.#resizeObserver?.disconnect();\n this.#resizeObserver = null;\n this.unobserveViewport();\n }\n}\n","/** 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","/**\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 * 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\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 }\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 }\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { LayoutObserver } from \"../utils/layout_observer\";\nimport { logicalScrollMetrics } from \"../utils/logical_scroll\";\nimport { TabindexLoan } from \"../utils/tabindex_loan\";\n\n/** A CSS selector for natively focusable / author-focusable descendants. */\nconst FOCUSABLE_SELECTOR = [\n \"a[href]\",\n \"button:not([disabled])\",\n \"input:not([disabled])\",\n \"select:not([disabled])\",\n \"textarea:not([disabled])\",\n \"[tabindex]:not([tabindex='-1'])\",\n \"[contenteditable='true']\",\n].join(\",\");\n\n/** Distance from an edge (px) treated as fully reached; absorbs sub-pixel scroll. */\nconst EDGE_EPSILON = 1;\n\n/** `Element.checkVisibility` (widely available); absent in older engines. */\ninterface VisibilityCheckable {\n checkVisibility?: (options?: { visibilityProperty?: boolean }) => boolean;\n}\n\n/**\n * Headless **Scroll Area** behavior: keyboard reachability and scroll-state hooks\n * for a natively scrolling region. No custom scrollbar — the native one is\n * respected; this only adds a11y and CSS state.\n *\n * Markup contract (identifier: `stimeo--scroll-area`):\n * <div data-controller=\"stimeo--scroll-area\"\n * data-stimeo--scroll-area-orientation-value=\"vertical\">\n * <div data-stimeo--scroll-area-target=\"viewport\" aria-label=\"Log output\">\n * <!-- long content -->\n * </div>\n * </div>\n *\n * When the content overflows and the viewport holds no focusable elements of its\n * own, the viewport is made keyboard-scrollable (`tabindex=\"0\"`, plus `role=\"region\"`\n * when it already has an accessible name). Scroll position is published as\n * `data-scroll` (`start`/`middle`/`end`), overflow as `data-overflow`, and progress\n * as `--stimeo-scroll-progress` (0–1) so consumer CSS can draw scroll shadows.\n *\n * @remarks\n * Behavior only. The `scroll` listener and {@link LayoutObserver} (element +\n * viewport resize) are torn down on `disconnect()` (Turbo navigation included).\n * `role=\"region\"` is added only when the viewport is already named, so a scrollable\n * region never becomes an unlabeled landmark.\n */\nexport class ScrollAreaController extends Controller<HTMLElement> {\n static override targets = [\"viewport\"];\n static override values = {\n orientation: { type: String, default: \"vertical\" },\n };\n static events = [\"reach\"] as const;\n\n declare readonly viewportTarget: HTMLElement;\n declare readonly hasViewportTarget: boolean;\n\n declare orientationValue: string;\n\n readonly #layout = new LayoutObserver(() => this.#update());\n /** Re-checks the tab stop when the viewport's focusable content comes or goes. */\n #content: MutationObserver | null = null;\n /** Last edge reported via `reach`, so the event fires once per arrival. */\n #lastEdge: \"start\" | \"end\" | null = null;\n /** Whether this controller added `tabindex`, so teardown only removes its own. */\n readonly #tabindex = new TabindexLoan(\"0\");\n /** Whether this controller added `role=\"region\"`, for symmetric teardown. */\n #addedRole = false;\n\n readonly #onScroll = (): void => {\n this.#update();\n };\n\n override connect(): void {\n if (!this.hasViewportTarget) return;\n this.viewportTarget.addEventListener(\"scroll\", this.#onScroll, { passive: true });\n this.#layout.observe(this.viewportTarget);\n this.#layout.observeViewport();\n // Overflow follows the box, but focusability follows the content, and the two\n // change independently: revealing a button inside a fixed-height viewport fires\n // no resize and no scroll. Without this the tab stop would be stale until the\n // next unrelated event.\n //\n // No `attributeFilter`: what makes a control appear is not confined to its own\n // attributes — a state hook on an ancestor (`[data-has-new] .jump { display: block }`)\n // flips it just as well, and that set cannot be enumerated.\n //\n // The overflow value is re-measured here rather than reused. A content change moves\n // the scroll extent without touching the viewport's own box, so a fixed-height\n // viewport fires no resize when its content shrinks — reusing a cached value would\n // hand the tab stop to a box that does not scroll. Position and `reach` are\n // deliberately left alone: the event contract is arrival at an edge, and a content\n // change is not an arrival.\n if (typeof MutationObserver !== \"undefined\") {\n this.#content = new MutationObserver(() => {\n if (!this.hasViewportTarget) return;\n const vp = this.viewportTarget;\n this.#syncKeyboardReach(vp, this.#syncOverflow(vp));\n });\n this.#content.observe(this.viewportTarget, {\n subtree: true,\n childList: true,\n attributes: true,\n });\n }\n this.#update();\n }\n\n override disconnect(): void {\n if (this.hasViewportTarget) {\n this.viewportTarget.removeEventListener(\"scroll\", this.#onScroll);\n // Remove only the keyboard-reach attributes this controller added, so a\n // Turbo cache snapshot never preserves a controller-owned tab stop /\n // landmark (controller-added state must not outlive the controller).\n this.#clearAddedAttributes(this.viewportTarget);\n }\n this.#layout.disconnect();\n this.#content?.disconnect();\n this.#content = null;\n this.#lastEdge = null;\n }\n\n /** Re-measures overflow and scroll position and reflects the state hooks. */\n #update(): void {\n if (!this.hasViewportTarget) return;\n const vp = this.viewportTarget;\n const overflowing = this.#syncOverflow(vp);\n this.#syncKeyboardReach(vp, overflowing);\n\n const { position, progress } = this.#measurePosition(vp);\n this.element.setAttribute(\"data-scroll\", position);\n this.element.style.setProperty(\"--stimeo-scroll-progress\", String(progress));\n\n const edge = position === \"start\" ? \"start\" : position === \"end\" ? \"end\" : null;\n if (overflowing && edge && edge !== this.#lastEdge) {\n this.#lastEdge = edge;\n this.dispatch(\"reach\", { detail: { edge } });\n } else if (!edge) {\n this.#lastEdge = null;\n }\n }\n\n /** Whether the viewport can scroll on the configured axis. */\n /**\n * Measures overflow and reflects the `data-overflow` hook.\n *\n * The write is skipped when the value is unchanged. An identical `setAttribute` still\n * queues a MutationRecord, and markup that puts the viewport target on the controller\n * element itself would then have the content observer trigger its own next callback.\n */\n #syncOverflow(vp: HTMLElement): boolean {\n const overflowing = this.#measureOverflow(vp);\n const next = overflowing ? \"true\" : \"false\";\n if (this.element.getAttribute(\"data-overflow\") !== next) {\n this.element.setAttribute(\"data-overflow\", next);\n }\n return overflowing;\n }\n\n #measureOverflow(vp: HTMLElement): boolean {\n const o = this.orientationValue;\n const vertical = o !== \"horizontal\" && vp.scrollHeight > vp.clientHeight + EDGE_EPSILON;\n const horizontal = o !== \"vertical\" && vp.scrollWidth > vp.clientWidth + EDGE_EPSILON;\n return vertical || horizontal;\n }\n\n /**\n * Reports the scroll position bucket and 0–1 progress on the primary axis. For\n * `both`, the vertical axis is used when it overflows, otherwise the horizontal.\n */\n #measurePosition(vp: HTMLElement): {\n position: \"start\" | \"middle\" | \"end\";\n progress: number;\n } {\n const horizontalPrimary =\n this.orientationValue === \"horizontal\" ||\n (this.orientationValue === \"both\" && vp.scrollHeight <= vp.clientHeight + EDGE_EPSILON);\n\n const { position: scrollPos, max: maxScroll } = logicalScrollMetrics(vp, horizontalPrimary);\n\n if (maxScroll <= EDGE_EPSILON) return { position: \"start\", progress: 0 };\n\n const progress = Math.min(1, Math.max(0, scrollPos / maxScroll));\n if (scrollPos <= EDGE_EPSILON) return { position: \"start\", progress };\n if (scrollPos >= maxScroll - EDGE_EPSILON) return { position: \"end\", progress };\n return { position: \"middle\", progress };\n }\n\n /**\n * Makes the viewport keyboard-scrollable when it overflows and contains no\n * focusable elements of its own (avoiding a double tab stop). Adds `role=\"region\"`\n * only when the viewport already carries an accessible name.\n */\n #syncKeyboardReach(vp: HTMLElement, overflowing: boolean): void {\n const wantsTabindex = overflowing && !this.#hasFocusableContent(vp);\n\n if (wantsTabindex) {\n this.#tabindex.lend(vp);\n if (!vp.hasAttribute(\"role\") && this.#hasAccessibleName(vp)) {\n vp.setAttribute(\"role\", \"region\");\n this.#addedRole = true;\n }\n } else {\n this.#clearAddedAttributes(vp);\n }\n }\n\n /** Removes (and resets the flags for) only the attributes this controller added. */\n #clearAddedAttributes(vp: HTMLElement): void {\n this.#tabindex.returnAll();\n if (this.#addedRole) {\n vp.removeAttribute(\"role\");\n this.#addedRole = false;\n }\n }\n\n /**\n * Whether the viewport owns something the user can Tab to *right now*.\n *\n * The selector alone is not enough: a `display: none` button still matches it,\n * so a viewport whose only control is revealed on demand would never get a tab\n * stop — leaving it unreachable by keyboard exactly while it has nothing else to\n * offer. Only rendered candidates count.\n */\n #hasFocusableContent(vp: HTMLElement): boolean {\n return Array.from(vp.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)).some((el) =>\n this.#isRendered(el),\n );\n }\n\n /**\n * Whether `el` is actually rendered, and so can hold focus.\n *\n * `checkVisibility()` answers this for every way CSS can remove a box, including\n * a class-driven `display: none` that no attribute reveals. The `hidden` walk in\n * front of it is not redundant: it is the one case a DOM-only environment with no\n * layout engine has to be told about explicitly.\n */\n #isRendered(el: HTMLElement): boolean {\n if (el.closest(\"[hidden]\") !== null) return false;\n const check = (el as HTMLElement & VisibilityCheckable).checkVisibility;\n return typeof check === \"function\" ? check.call(el, { visibilityProperty: true }) : true;\n }\n\n #hasAccessibleName(vp: HTMLElement): boolean {\n return vp.hasAttribute(\"aria-label\") || vp.hasAttribute(\"aria-labelledby\");\n }\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Headless **Scroll Restore** behavior: persists and restores the scroll
|
|
5
|
+
* position of an inner scrolling region across Turbo Drive navigations (and full
|
|
6
|
+
* reloads within the same tab session). No APG widget — a pure state-preservation
|
|
7
|
+
* utility, like {@link import("./scroll_visibility_controller").ScrollVisibilityController}
|
|
8
|
+
* and the Sticky State Observer.
|
|
9
|
+
*
|
|
10
|
+
* Markup contract (identifier: `stimeo--scroll-restore`):
|
|
11
|
+
* <div data-controller="stimeo--scroll-restore"
|
|
12
|
+
* data-stimeo--scroll-restore-key-value="sidebar"
|
|
13
|
+
* style="overflow: auto">
|
|
14
|
+
* …long content…
|
|
15
|
+
* </div>
|
|
16
|
+
*
|
|
17
|
+
* Turbo swaps the whole `<body>` on navigation, so an inner scroll container is
|
|
18
|
+
* rebuilt with `scrollTop` reset to 0. Rather than have every app hand-write a
|
|
19
|
+
* controller for this (which contradicts a "ship the behavior" library), this
|
|
20
|
+
* persists the offset under a stable key in `sessionStorage` and restores it on
|
|
21
|
+
* `connect()`.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Behavior only — it sets no ARIA/`data-*`/CSS and never moves focus (restore is
|
|
25
|
+
* a plain `scrollTop`/`scrollLeft` assignment). The `scroll` listener is internal
|
|
26
|
+
* and `passive` (no consumer `data-action` needed): each event records the live
|
|
27
|
+
* offset synchronously, and a `requestAnimationFrame` coalesces the writes to
|
|
28
|
+
* `sessionStorage`. On `disconnect()` (Turbo navigation included) it flushes the
|
|
29
|
+
* **last captured** offset rather than re-reading the element — by the time Turbo
|
|
30
|
+
* fires `disconnect` it has already detached the node, whose `scrollTop` then
|
|
31
|
+
* reads `0`, so a fresh read would clobber the saved position with `0`. Keying by
|
|
32
|
+
* `key` (falling back to the element `id`) in `sessionStorage` makes it
|
|
33
|
+
* multi-instance safe and survives full reloads — unlike a module-scope variable,
|
|
34
|
+
* which only survives Turbo Drive and assumes a single instance.
|
|
35
|
+
*/
|
|
36
|
+
declare class ScrollRestoreController extends Controller<HTMLElement> {
|
|
37
|
+
#private;
|
|
38
|
+
static values: {
|
|
39
|
+
key: {
|
|
40
|
+
type: StringConstructor;
|
|
41
|
+
default: string;
|
|
42
|
+
};
|
|
43
|
+
axis: {
|
|
44
|
+
type: StringConstructor;
|
|
45
|
+
default: string;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
keyValue: string;
|
|
49
|
+
axisValue: string;
|
|
50
|
+
connect(): void;
|
|
51
|
+
disconnect(): void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { ScrollRestoreController };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
|
|
3
|
+
// src/controllers/scroll_restore_controller.ts
|
|
4
|
+
var ScrollRestoreController = class extends Controller {
|
|
5
|
+
static values = {
|
|
6
|
+
key: { type: String, default: "" },
|
|
7
|
+
axis: { type: String, default: "vertical" }
|
|
8
|
+
};
|
|
9
|
+
/** Pending rAF id that coalesces scroll bursts into one save. */
|
|
10
|
+
#rafId = null;
|
|
11
|
+
/** Resolved storage key; empty disables persistence (no key and no id). */
|
|
12
|
+
#storageKey = "";
|
|
13
|
+
/** Last offset captured while the element was live; persisted as-is on teardown. */
|
|
14
|
+
#lastTop = 0;
|
|
15
|
+
#lastLeft = 0;
|
|
16
|
+
#onScroll = () => {
|
|
17
|
+
this.#capture();
|
|
18
|
+
if (this.#rafId !== null) return;
|
|
19
|
+
this.#rafId = requestAnimationFrame(() => {
|
|
20
|
+
this.#rafId = null;
|
|
21
|
+
this.#persist();
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
connect() {
|
|
25
|
+
this.#storageKey = this.#resolveKey();
|
|
26
|
+
if (!this.#storageKey) return;
|
|
27
|
+
this.#restore();
|
|
28
|
+
this.element.addEventListener("scroll", this.#onScroll, { passive: true });
|
|
29
|
+
}
|
|
30
|
+
disconnect() {
|
|
31
|
+
if (!this.#storageKey) return;
|
|
32
|
+
this.element.removeEventListener("scroll", this.#onScroll);
|
|
33
|
+
if (this.#rafId !== null) {
|
|
34
|
+
cancelAnimationFrame(this.#rafId);
|
|
35
|
+
this.#rafId = null;
|
|
36
|
+
}
|
|
37
|
+
this.#persist();
|
|
38
|
+
}
|
|
39
|
+
/** Records the live scroll offset for the configured axis. */
|
|
40
|
+
#capture() {
|
|
41
|
+
if (this.#tracksVertical) this.#lastTop = this.element.scrollTop;
|
|
42
|
+
if (this.#tracksHorizontal) this.#lastLeft = this.element.scrollLeft;
|
|
43
|
+
}
|
|
44
|
+
/** Persists the last captured scroll offset for the configured axis. */
|
|
45
|
+
#persist() {
|
|
46
|
+
const data = {};
|
|
47
|
+
if (this.#tracksVertical) data.top = this.#lastTop;
|
|
48
|
+
if (this.#tracksHorizontal) data.left = this.#lastLeft;
|
|
49
|
+
try {
|
|
50
|
+
sessionStorage.setItem(this.#storageKey, JSON.stringify(data));
|
|
51
|
+
} catch {
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Applies the persisted scroll offset, if any, without moving focus. */
|
|
55
|
+
#restore() {
|
|
56
|
+
let raw = null;
|
|
57
|
+
try {
|
|
58
|
+
raw = sessionStorage.getItem(this.#storageKey);
|
|
59
|
+
} catch {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (raw === null) return;
|
|
63
|
+
let data;
|
|
64
|
+
try {
|
|
65
|
+
data = JSON.parse(raw);
|
|
66
|
+
} catch {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (this.#tracksVertical && typeof data.top === "number") {
|
|
70
|
+
this.element.scrollTop = data.top;
|
|
71
|
+
this.#lastTop = data.top;
|
|
72
|
+
}
|
|
73
|
+
if (this.#tracksHorizontal && typeof data.left === "number") {
|
|
74
|
+
this.element.scrollLeft = data.left;
|
|
75
|
+
this.#lastLeft = data.left;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** The `sessionStorage` key: explicit `key`, else the element `id`, else none. */
|
|
79
|
+
#resolveKey() {
|
|
80
|
+
const base = this.keyValue || this.element.id;
|
|
81
|
+
return base ? `stimeo--scroll-restore:${base}` : "";
|
|
82
|
+
}
|
|
83
|
+
get #tracksVertical() {
|
|
84
|
+
return this.axisValue !== "horizontal";
|
|
85
|
+
}
|
|
86
|
+
get #tracksHorizontal() {
|
|
87
|
+
return this.axisValue === "horizontal" || this.axisValue === "both";
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { ScrollRestoreController };
|
|
92
|
+
//# sourceMappingURL=scroll_restore_controller.js.map
|
|
93
|
+
//# sourceMappingURL=scroll_restore_controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/controllers/scroll_restore_controller.ts"],"names":[],"mappings":";;;AAyCO,IAAM,uBAAA,GAAN,cAAsC,UAAA,CAAwB;AAAA,EACnE,OAAgB,MAAA,GAAS;AAAA,IACvB,GAAA,EAAK,EAAE,IAAA,EAAM,MAAA,EAAQ,SAAS,EAAA,EAAG;AAAA,IACjC,IAAA,EAAM,EAAE,IAAA,EAAM,MAAA,EAAQ,SAAS,UAAA;AAAW,GAC5C;AAAA;AAAA,EAMA,MAAA,GAAwB,IAAA;AAAA;AAAA,EAExB,WAAA,GAAc,EAAA;AAAA;AAAA,EAEd,QAAA,GAAW,CAAA;AAAA,EACX,SAAA,GAAY,CAAA;AAAA,EAEH,YAAY,MAAY;AAG/B,IAAA,IAAA,CAAK,QAAA,EAAS;AACd,IAAA,IAAI,IAAA,CAAK,WAAW,IAAA,EAAM;AAC1B,IAAA,IAAA,CAAK,MAAA,GAAS,sBAAsB,MAAM;AACxC,MAAA,IAAA,CAAK,MAAA,GAAS,IAAA;AACd,MAAA,IAAA,CAAK,QAAA,EAAS;AAAA,IAChB,CAAC,CAAA;AAAA,EACH,CAAA;AAAA,EAES,OAAA,GAAgB;AACvB,IAAA,IAAA,CAAK,WAAA,GAAc,KAAK,WAAA,EAAY;AACpC,IAAA,IAAI,CAAC,KAAK,WAAA,EAAa;AACvB,IAAA,IAAA,CAAK,QAAA,EAAS;AACd,IAAA,IAAA,CAAK,OAAA,CAAQ,iBAAiB,QAAA,EAAU,IAAA,CAAK,WAAW,EAAE,OAAA,EA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AAA,IAAI,QAAQ,IAAA,EAAM;AAClB,IAAA,IAAI,IAAA;AACJ,IAAA,IAAI;AACF,MAAA,IAAA,GAAO,IAAA,CAAK,MAAM,GAAG,CAAA;AAAA,IACvB,CAAA,CAAA,MAAQ;AACN,MAAA;AAAA,IACF;AAIA,IAAA,IAAI,IAAA,CAAK,eAAA,IAAmB,OAAO,IAAA,CAAK,QAAQ,QAAA,EAAU;AACxD,MAAA,IAAA,CAAK,OAAA,CAAQ,YAAY,IAAA,CAAK,GAAA;AAC9B,MAAA,IAAA,CAAK,WAAW,IAAA,CAAK,GAAA;AAAA,IACvB;AACA,IAAA,IAAI,IAAA,CAAK,iBAAA,IAAqB,OAAO,IAAA,CAAK,SAAS,QAAA,EAAU;AAC3D,MAAA,IAAA,CAAK,OAAA,CAAQ,aAAa,IAAA,CAAK,IAAA;AAC/B,MAAA,IAAA,CAAK,YAAY,IAAA,CAAK,IAAA;AAAA,IACxB;AAAA,EACF;AAAA;AAAA,EAGA,WAAA,GAAsB;AACpB,IAAA,MAAM,IAAA,GAAO,IAAA,CAAK,QAAA,IAAY,IAAA,CAAK,OAAA,CAAQ,EAAA;AAC3C,IAAA,OAAO,IAAA,GAAO,CAAA,uBAAA,EAA0B,IAAI,CAAA,CAAA,GAAK,EAAA;AAAA,EACnD;AAAA,EAEA,IAAI,eAAA,GAA2B;AAC7B,IAAA,OAAO,KAAK,SAAA,KAAc,YAAA;AAAA,EAC5B;AAAA,EAEA,IAAI,iBAAA,GAA6B;AAC/B,IAAA,OAAO,IAAA,CAAK,SAAA,KAAc,YAAA,IAAgB,IAAA,CAAK,SAAA,KAAc,MAAA;AAAA,EAC/D;AACF","file":"scroll_restore_controller.js","sourcesContent":["import { Controller } from \"@hotwired/stimulus\";\n\n/** Persisted scroll position; fields are present only for the saved axis. */\ninterface StoredScroll {\n top?: number;\n left?: number;\n}\n\n/**\n * Headless **Scroll Restore** behavior: persists and restores the scroll\n * position of an inner scrolling region across Turbo Drive navigations (and full\n * reloads within the same tab session). No APG widget — a pure state-preservation\n * utility, like {@link import(\"./scroll_visibility_controller\").ScrollVisibilityController}\n * and the Sticky State Observer.\n *\n * Markup contract (identifier: `stimeo--scroll-restore`):\n * <div data-controller=\"stimeo--scroll-restore\"\n * data-stimeo--scroll-restore-key-value=\"sidebar\"\n * style=\"overflow: auto\">\n * …long content…\n * </div>\n *\n * Turbo swaps the whole `<body>` on navigation, so an inner scroll container is\n * rebuilt with `scrollTop` reset to 0. Rather than have every app hand-write a\n * controller for this (which contradicts a \"ship the behavior\" library), this\n * persists the offset under a stable key in `sessionStorage` and restores it on\n * `connect()`.\n *\n * @remarks\n * Behavior only — it sets no ARIA/`data-*`/CSS and never moves focus (restore is\n * a plain `scrollTop`/`scrollLeft` assignment). The `scroll` listener is internal\n * and `passive` (no consumer `data-action` needed): each event records the live\n * offset synchronously, and a `requestAnimationFrame` coalesces the writes to\n * `sessionStorage`. On `disconnect()` (Turbo navigation included) it flushes the\n * **last captured** offset rather than re-reading the element — by the time Turbo\n * fires `disconnect` it has already detached the node, whose `scrollTop` then\n * reads `0`, so a fresh read would clobber the saved position with `0`. Keying by\n * `key` (falling back to the element `id`) in `sessionStorage` makes it\n * multi-instance safe and survives full reloads — unlike a module-scope variable,\n * which only survives Turbo Drive and assumes a single instance.\n */\nexport class ScrollRestoreController extends Controller<HTMLElement> {\n static override values = {\n key: { type: String, default: \"\" },\n axis: { type: String, default: \"vertical\" },\n };\n\n declare keyValue: string;\n declare axisValue: string;\n\n /** Pending rAF id that coalesces scroll bursts into one save. */\n #rafId: number | null = null;\n /** Resolved storage key; empty disables persistence (no key and no id). */\n #storageKey = \"\";\n /** Last offset captured while the element was live; persisted as-is on teardown. */\n #lastTop = 0;\n #lastLeft = 0;\n\n readonly #onScroll = (): void => {\n // Capture synchronously while the element is still connected and measurable;\n // the rAF only debounces the sessionStorage write, never the read.\n this.#capture();\n if (this.#rafId !== null) return;\n this.#rafId = requestAnimationFrame(() => {\n this.#rafId = null;\n this.#persist();\n });\n };\n\n override connect(): void {\n this.#storageKey = this.#resolveKey();\n if (!this.#storageKey) return; // No stable key → do nothing (avoid mixing pages).\n this.#restore();\n this.element.addEventListener(\"scroll\", this.#onScroll, { passive: true });\n }\n\n override disconnect(): void {\n if (!this.#storageKey) return;\n this.element.removeEventListener(\"scroll\", this.#onScroll);\n if (this.#rafId !== null) {\n cancelAnimationFrame(this.#rafId);\n this.#rafId = null;\n }\n // Flush the last captured offset (a throttled burst may have left the final\n // frame unsaved). We do NOT re-read the element here: Turbo detaches the node\n // before disconnect, so its scrollTop is 0 and would overwrite the real value.\n this.#persist();\n }\n\n /** Records the live scroll offset for the configured axis. */\n #capture(): void {\n if (this.#tracksVertical) this.#lastTop = this.element.scrollTop;\n if (this.#tracksHorizontal) this.#lastLeft = this.element.scrollLeft;\n }\n\n /** Persists the last captured scroll offset for the configured axis. */\n #persist(): void {\n const data: StoredScroll = {};\n if (this.#tracksVertical) data.top = this.#lastTop;\n if (this.#tracksHorizontal) data.left = this.#lastLeft;\n try {\n sessionStorage.setItem(this.#storageKey, JSON.stringify(data));\n } catch {\n // sessionStorage can throw (private mode / quota); persistence is best-effort.\n }\n }\n\n /** Applies the persisted scroll offset, if any, without moving focus. */\n #restore(): void {\n let raw: string | null = null;\n try {\n raw = sessionStorage.getItem(this.#storageKey);\n } catch {\n return;\n }\n if (raw === null) return;\n let data: StoredScroll;\n try {\n data = JSON.parse(raw) as StoredScroll;\n } catch {\n return;\n }\n // Seed the capture cache from the restored value so an immediate teardown\n // (before any scroll) re-persists the restored position, not 0. Guard by the\n // configured axis so a stale field from a different axis is never applied.\n if (this.#tracksVertical && typeof data.top === \"number\") {\n this.element.scrollTop = data.top;\n this.#lastTop = data.top;\n }\n if (this.#tracksHorizontal && typeof data.left === \"number\") {\n this.element.scrollLeft = data.left;\n this.#lastLeft = data.left;\n }\n }\n\n /** The `sessionStorage` key: explicit `key`, else the element `id`, else none. */\n #resolveKey(): string {\n const base = this.keyValue || this.element.id;\n return base ? `stimeo--scroll-restore:${base}` : \"\";\n }\n\n get #tracksVertical(): boolean {\n return this.axisValue !== \"horizontal\";\n }\n\n get #tracksHorizontal(): boolean {\n return this.axisValue === \"horizontal\" || this.axisValue === \"both\";\n }\n}\n"]}
|
|
@@ -30,9 +30,12 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
30
30
|
* Behavior only — the look and any transition are the consumer's CSS. The scroll
|
|
31
31
|
* listener is `passive`, coalesced through `requestAnimationFrame`, and removed on
|
|
32
32
|
* `disconnect()` (Turbo navigation included). `toTop` honors
|
|
33
|
-
* `prefers-reduced-motion` by
|
|
34
|
-
*
|
|
35
|
-
*
|
|
33
|
+
* `prefers-reduced-motion` by forcing an instant jump independently of the
|
|
34
|
+
* consumer's CSS `scroll-behavior`, and can move focus to a `focusSelector`
|
|
35
|
+
* target (given `tabindex="-1"` if needed) to keep keyboard users oriented
|
|
36
|
+
* after the scroll. A live disconnect removes only a `tabindex="-1"` this
|
|
37
|
+
* controller instance added; authored tabindex values remain. This teardown does
|
|
38
|
+
* not claim to rewrite a Turbo cache snapshot that was cloned before disconnect.
|
|
36
39
|
*/
|
|
37
40
|
declare class ScrollVisibilityController extends Controller<HTMLElement> {
|
|
38
41
|
#private;
|