@sdxc/ui 0.0.0-pre.1
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/LICENSE.md +21 -0
- package/README.md +812 -0
- package/dist/animations/index.d.ts +12 -0
- package/dist/animations/index.js +12 -0
- package/dist/animations/keyframes.d.ts +162 -0
- package/dist/animations/keyframes.js +297 -0
- package/dist/animations/scroll.d.ts +154 -0
- package/dist/animations/scroll.js +228 -0
- package/dist/animations/tokens.d.ts +47 -0
- package/dist/animations/tokens.js +47 -0
- package/dist/animations/transitions.d.ts +143 -0
- package/dist/animations/transitions.js +147 -0
- package/dist/behaviors/announcer.d.ts +90 -0
- package/dist/behaviors/announcer.js +93 -0
- package/dist/behaviors/calendar-model.d.ts +218 -0
- package/dist/behaviors/calendar-model.js +376 -0
- package/dist/behaviors/drag-session.d.ts +119 -0
- package/dist/behaviors/drag-session.js +110 -0
- package/dist/behaviors/filter-model.d.ts +117 -0
- package/dist/behaviors/filter-model.js +185 -0
- package/dist/behaviors/index.d.ts +16 -0
- package/dist/behaviors/index.js +16 -0
- package/dist/behaviors/resize-session.d.ts +115 -0
- package/dist/behaviors/resize-session.js +157 -0
- package/dist/behaviors/scroll-follow-model.d.ts +180 -0
- package/dist/behaviors/scroll-follow-model.js +197 -0
- package/dist/behaviors/selection-model.d.ts +129 -0
- package/dist/behaviors/selection-model.js +259 -0
- package/dist/behaviors/toaster.d.ts +144 -0
- package/dist/behaviors/toaster.js +228 -0
- package/dist/components/accordion.d.ts +93 -0
- package/dist/components/accordion.js +165 -0
- package/dist/components/alert-dialog.d.ts +110 -0
- package/dist/components/alert-dialog.js +169 -0
- package/dist/components/alert.d.ts +109 -0
- package/dist/components/alert.js +160 -0
- package/dist/components/aspect-ratio.d.ts +44 -0
- package/dist/components/aspect-ratio.js +35 -0
- package/dist/components/attachment.d.ts +146 -0
- package/dist/components/attachment.js +320 -0
- package/dist/components/avatar.d.ts +118 -0
- package/dist/components/avatar.js +89 -0
- package/dist/components/badge.d.ts +66 -0
- package/dist/components/badge.js +129 -0
- package/dist/components/breadcrumbs.d.ts +67 -0
- package/dist/components/breadcrumbs.js +121 -0
- package/dist/components/bubble.d.ts +91 -0
- package/dist/components/bubble.js +216 -0
- package/dist/components/button.d.ts +78 -0
- package/dist/components/button.js +207 -0
- package/dist/components/calendar.d.ts +166 -0
- package/dist/components/calendar.js +320 -0
- package/dist/components/card.d.ts +82 -0
- package/dist/components/card.js +126 -0
- package/dist/components/carousel.d.ts +96 -0
- package/dist/components/carousel.js +214 -0
- package/dist/components/chart.d.ts +485 -0
- package/dist/components/chart.js +563 -0
- package/dist/components/checkbox-group.d.ts +58 -0
- package/dist/components/checkbox-group.js +53 -0
- package/dist/components/checkbox.d.ts +61 -0
- package/dist/components/checkbox.js +81 -0
- package/dist/components/color-area.d.ts +83 -0
- package/dist/components/color-area.js +251 -0
- package/dist/components/color-field.d.ts +128 -0
- package/dist/components/color-field.js +73 -0
- package/dist/components/color-picker.d.ts +132 -0
- package/dist/components/color-picker.js +183 -0
- package/dist/components/color-slider.d.ts +103 -0
- package/dist/components/color-slider.js +198 -0
- package/dist/components/color-swatch-picker.d.ts +106 -0
- package/dist/components/color-swatch-picker.js +75 -0
- package/dist/components/color-swatch.d.ts +57 -0
- package/dist/components/color-swatch.js +82 -0
- package/dist/components/color-wheel.d.ts +58 -0
- package/dist/components/color-wheel.js +160 -0
- package/dist/components/combobox.d.ts +73 -0
- package/dist/components/combobox.js +108 -0
- package/dist/components/command.d.ts +80 -0
- package/dist/components/command.js +151 -0
- package/dist/components/confirm.d.ts +124 -0
- package/dist/components/confirm.js +56 -0
- package/dist/components/context-menu.d.ts +108 -0
- package/dist/components/context-menu.js +200 -0
- package/dist/components/date-field.d.ts +110 -0
- package/dist/components/date-field.js +49 -0
- package/dist/components/date-picker.d.ts +136 -0
- package/dist/components/date-picker.js +155 -0
- package/dist/components/date-range-picker.d.ts +152 -0
- package/dist/components/date-range-picker.js +106 -0
- package/dist/components/description.d.ts +36 -0
- package/dist/components/description.js +21 -0
- package/dist/components/dialog.d.ts +97 -0
- package/dist/components/dialog.js +203 -0
- package/dist/components/disclosure.d.ts +85 -0
- package/dist/components/disclosure.js +180 -0
- package/dist/components/drawer.d.ts +82 -0
- package/dist/components/drawer.js +162 -0
- package/dist/components/drop-indicator.d.ts +43 -0
- package/dist/components/drop-indicator.js +47 -0
- package/dist/components/drop-zone.d.ts +73 -0
- package/dist/components/drop-zone.js +54 -0
- package/dist/components/empty.d.ts +89 -0
- package/dist/components/empty.js +129 -0
- package/dist/components/field-error.d.ts +41 -0
- package/dist/components/field-error.js +36 -0
- package/dist/components/file-trigger.d.ts +84 -0
- package/dist/components/file-trigger.js +169 -0
- package/dist/components/form.d.ts +118 -0
- package/dist/components/form.js +113 -0
- package/dist/components/grid-list.d.ts +112 -0
- package/dist/components/grid-list.js +220 -0
- package/dist/components/group.d.ts +45 -0
- package/dist/components/group.js +44 -0
- package/dist/components/header.d.ts +33 -0
- package/dist/components/header.js +29 -0
- package/dist/components/heading-scope.d.ts +83 -0
- package/dist/components/heading-scope.js +82 -0
- package/dist/components/heading.d.ts +50 -0
- package/dist/components/heading.js +31 -0
- package/dist/components/hover-card.d.ts +77 -0
- package/dist/components/hover-card.js +151 -0
- package/dist/components/image-placeholder.d.ts +105 -0
- package/dist/components/image-placeholder.js +212 -0
- package/dist/components/index.d.ts +109 -0
- package/dist/components/index.js +109 -0
- package/dist/components/input.d.ts +53 -0
- package/dist/components/input.js +76 -0
- package/dist/components/item.d.ts +97 -0
- package/dist/components/item.js +185 -0
- package/dist/components/keyboard.d.ts +33 -0
- package/dist/components/keyboard.js +20 -0
- package/dist/components/label.d.ts +38 -0
- package/dist/components/label.js +24 -0
- package/dist/components/link-button.d.ts +59 -0
- package/dist/components/link-button.js +174 -0
- package/dist/components/link.d.ts +43 -0
- package/dist/components/link.js +85 -0
- package/dist/components/listbox.d.ts +119 -0
- package/dist/components/listbox.js +114 -0
- package/dist/components/logo.d.ts +117 -0
- package/dist/components/logo.js +89 -0
- package/dist/components/marker.d.ts +82 -0
- package/dist/components/marker.js +163 -0
- package/dist/components/menu.d.ts +90 -0
- package/dist/components/menu.js +146 -0
- package/dist/components/menubar.d.ts +54 -0
- package/dist/components/menubar.js +118 -0
- package/dist/components/message-scroller.d.ts +109 -0
- package/dist/components/message-scroller.js +180 -0
- package/dist/components/message.d.ts +104 -0
- package/dist/components/message.js +200 -0
- package/dist/components/meter.d.ts +60 -0
- package/dist/components/meter.js +110 -0
- package/dist/components/modal.d.ts +71 -0
- package/dist/components/modal.js +120 -0
- package/dist/components/nav-link.d.ts +53 -0
- package/dist/components/nav-link.js +83 -0
- package/dist/components/navigation-menu.d.ts +150 -0
- package/dist/components/navigation-menu.js +381 -0
- package/dist/components/number-field.d.ts +76 -0
- package/dist/components/number-field.js +190 -0
- package/dist/components/otp-field.d.ts +68 -0
- package/dist/components/otp-field.js +46 -0
- package/dist/components/overlay-arrow.d.ts +51 -0
- package/dist/components/overlay-arrow.js +77 -0
- package/dist/components/pagination.d.ts +67 -0
- package/dist/components/pagination.js +164 -0
- package/dist/components/popover.d.ts +69 -0
- package/dist/components/popover.js +68 -0
- package/dist/components/progress-bar.d.ts +53 -0
- package/dist/components/progress-bar.js +90 -0
- package/dist/components/radio-group.d.ts +102 -0
- package/dist/components/radio-group.js +114 -0
- package/dist/components/range-calendar.d.ts +169 -0
- package/dist/components/range-calendar.js +216 -0
- package/dist/components/resizable.d.ts +98 -0
- package/dist/components/resizable.js +151 -0
- package/dist/components/scroll-area.d.ts +54 -0
- package/dist/components/scroll-area.js +89 -0
- package/dist/components/search-field.d.ts +55 -0
- package/dist/components/search-field.js +49 -0
- package/dist/components/section.d.ts +37 -0
- package/dist/components/section.js +22 -0
- package/dist/components/select.d.ts +96 -0
- package/dist/components/select.js +285 -0
- package/dist/components/selection-indicator.d.ts +47 -0
- package/dist/components/selection-indicator.js +50 -0
- package/dist/components/sentinel-row.d.ts +22 -0
- package/dist/components/sentinel-row.js +29 -0
- package/dist/components/separator.d.ts +35 -0
- package/dist/components/separator.js +48 -0
- package/dist/components/shared-element.d.ts +45 -0
- package/dist/components/shared-element.js +48 -0
- package/dist/components/sheet.d.ts +78 -0
- package/dist/components/sheet.js +167 -0
- package/dist/components/sidebar.d.ts +288 -0
- package/dist/components/sidebar.js +1068 -0
- package/dist/components/skeleton.d.ts +36 -0
- package/dist/components/skeleton.js +38 -0
- package/dist/components/slider.d.ts +102 -0
- package/dist/components/slider.js +179 -0
- package/dist/components/spinner.d.ts +55 -0
- package/dist/components/spinner.js +51 -0
- package/dist/components/switch.d.ts +47 -0
- package/dist/components/switch.js +82 -0
- package/dist/components/table.d.ts +139 -0
- package/dist/components/table.js +216 -0
- package/dist/components/tabs.d.ts +124 -0
- package/dist/components/tabs.js +246 -0
- package/dist/components/tag-group.d.ts +83 -0
- package/dist/components/tag-group.js +157 -0
- package/dist/components/text-field.d.ts +119 -0
- package/dist/components/text-field.js +51 -0
- package/dist/components/text.d.ts +34 -0
- package/dist/components/text.js +19 -0
- package/dist/components/textarea.d.ts +52 -0
- package/dist/components/textarea.js +82 -0
- package/dist/components/time-field.d.ts +110 -0
- package/dist/components/time-field.js +48 -0
- package/dist/components/toast.d.ts +149 -0
- package/dist/components/toast.js +365 -0
- package/dist/components/toggle-button.d.ts +115 -0
- package/dist/components/toggle-button.js +237 -0
- package/dist/components/toolbar.d.ts +47 -0
- package/dist/components/toolbar.js +54 -0
- package/dist/components/tooltip.d.ts +52 -0
- package/dist/components/tooltip.js +92 -0
- package/dist/components/tree.d.ts +123 -0
- package/dist/components/tree.js +239 -0
- package/dist/components/typeset.d.ts +53 -0
- package/dist/components/typeset.js +271 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/mixins/aria-checked.d.ts +42 -0
- package/dist/mixins/aria-checked.js +127 -0
- package/dist/mixins/attachment-trigger.d.ts +64 -0
- package/dist/mixins/attachment-trigger.js +190 -0
- package/dist/mixins/calendar-keys.d.ts +27 -0
- package/dist/mixins/calendar-keys.js +113 -0
- package/dist/mixins/carousel-controls.d.ts +30 -0
- package/dist/mixins/carousel-controls.js +222 -0
- package/dist/mixins/chart-tooltip.d.ts +118 -0
- package/dist/mixins/chart-tooltip.js +323 -0
- package/dist/mixins/clear-field.d.ts +60 -0
- package/dist/mixins/clear-field.js +100 -0
- package/dist/mixins/color-area-drag.d.ts +58 -0
- package/dist/mixins/color-area-drag.js +162 -0
- package/dist/mixins/color-channel-sync.d.ts +73 -0
- package/dist/mixins/color-channel-sync.js +169 -0
- package/dist/mixins/color-preview.d.ts +28 -0
- package/dist/mixins/color-preview.js +77 -0
- package/dist/mixins/color-wheel-drag.d.ts +58 -0
- package/dist/mixins/color-wheel-drag.js +139 -0
- package/dist/mixins/combobox-filter.d.ts +59 -0
- package/dist/mixins/combobox-filter.js +81 -0
- package/dist/mixins/command-filter.d.ts +55 -0
- package/dist/mixins/command-filter.js +121 -0
- package/dist/mixins/command-keys.d.ts +31 -0
- package/dist/mixins/command-keys.js +116 -0
- package/dist/mixins/context-menu.d.ts +24 -0
- package/dist/mixins/context-menu.js +64 -0
- package/dist/mixins/copy-to-clipboard.d.ts +64 -0
- package/dist/mixins/copy-to-clipboard.js +109 -0
- package/dist/mixins/dismiss.d.ts +77 -0
- package/dist/mixins/dismiss.js +151 -0
- package/dist/mixins/drag-reorder.d.ts +88 -0
- package/dist/mixins/drag-reorder.js +214 -0
- package/dist/mixins/drop-zone.d.ts +52 -0
- package/dist/mixins/drop-zone.js +132 -0
- package/dist/mixins/dual-range.d.ts +60 -0
- package/dist/mixins/dual-range.js +100 -0
- package/dist/mixins/grid-list-keys.d.ts +34 -0
- package/dist/mixins/grid-list-keys.js +260 -0
- package/dist/mixins/heading-level-fallback.d.ts +81 -0
- package/dist/mixins/heading-level-fallback.js +112 -0
- package/dist/mixins/hotkey.d.ts +26 -0
- package/dist/mixins/hotkey.js +132 -0
- package/dist/mixins/image-fallback.d.ts +57 -0
- package/dist/mixins/image-fallback.js +86 -0
- package/dist/mixins/index.d.ts +48 -0
- package/dist/mixins/index.js +48 -0
- package/dist/mixins/listbox-keys.d.ts +52 -0
- package/dist/mixins/listbox-keys.js +63 -0
- package/dist/mixins/long-press.d.ts +76 -0
- package/dist/mixins/long-press.js +130 -0
- package/dist/mixins/menu-keys.d.ts +40 -0
- package/dist/mixins/menu-keys.js +117 -0
- package/dist/mixins/menubar-keys.d.ts +59 -0
- package/dist/mixins/menubar-keys.js +230 -0
- package/dist/mixins/message-follow.d.ts +56 -0
- package/dist/mixins/message-follow.js +368 -0
- package/dist/mixins/otp-slots.d.ts +53 -0
- package/dist/mixins/otp-slots.js +175 -0
- package/dist/mixins/persist.d.ts +36 -0
- package/dist/mixins/persist.js +78 -0
- package/dist/mixins/press-toggle.d.ts +43 -0
- package/dist/mixins/press-toggle.js +61 -0
- package/dist/mixins/range-preview.d.ts +26 -0
- package/dist/mixins/range-preview.js +120 -0
- package/dist/mixins/resize-handle.d.ts +101 -0
- package/dist/mixins/resize-handle.js +288 -0
- package/dist/mixins/stepper.d.ts +81 -0
- package/dist/mixins/stepper.js +194 -0
- package/dist/mixins/tab-keys.d.ts +30 -0
- package/dist/mixins/tab-keys.js +63 -0
- package/dist/mixins/theme-toggle.d.ts +82 -0
- package/dist/mixins/theme-toggle.js +158 -0
- package/dist/mixins/track-host-node.d.ts +34 -0
- package/dist/mixins/track-host-node.js +42 -0
- package/dist/mixins/treeKeys.d.ts +57 -0
- package/dist/mixins/treeKeys.js +266 -0
- package/dist/mixins/validate.d.ts +65 -0
- package/dist/mixins/validate.js +139 -0
- package/dist/mixins/view-transition.d.ts +56 -0
- package/dist/mixins/view-transition.js +111 -0
- package/dist/styles/chart-palette.d.ts +34 -0
- package/dist/styles/chart-palette.js +27 -0
- package/dist/styles/field-stack-layout.d.ts +27 -0
- package/dist/styles/field-stack-layout.js +16 -0
- package/dist/styles/floating-surface.d.ts +32 -0
- package/dist/styles/floating-surface.js +30 -0
- package/dist/styles/graphic-host.d.ts +25 -0
- package/dist/styles/graphic-host.js +18 -0
- package/dist/styles/index.d.ts +20 -0
- package/dist/styles/index.js +20 -0
- package/dist/styles/interactive-transition.d.ts +32 -0
- package/dist/styles/interactive-transition.js +32 -0
- package/dist/styles/legend-toggle.d.ts +29 -0
- package/dist/styles/legend-toggle.js +24 -0
- package/dist/styles/output-caption-text.d.ts +22 -0
- package/dist/styles/output-caption-text.js +15 -0
- package/dist/styles/panel-chrome.d.ts +34 -0
- package/dist/styles/panel-chrome.js +26 -0
- package/dist/styles/range-thumb-appearance.d.ts +36 -0
- package/dist/styles/range-thumb-appearance.js +76 -0
- package/dist/styles/rtl-aware-gradient-direction.d.ts +28 -0
- package/dist/styles/rtl-aware-gradient-direction.js +28 -0
- package/dist/styles/semantic-color-panel.d.ts +35 -0
- package/dist/styles/semantic-color-panel.js +41 -0
- package/dist/utils/chart-marker-indices.d.ts +22 -0
- package/dist/utils/chart-marker-indices.js +45 -0
- package/dist/utils/chart-path.d.ts +86 -0
- package/dist/utils/chart-path.js +122 -0
- package/dist/utils/chart-scale.d.ts +182 -0
- package/dist/utils/chart-scale.js +221 -0
- package/dist/utils/color-math.d.ts +252 -0
- package/dist/utils/color-math.js +480 -0
- package/dist/utils/command-event.d.ts +24 -0
- package/dist/utils/command-event.js +26 -0
- package/dist/utils/css-styles.d.ts +21 -0
- package/dist/utils/css-styles.js +11 -0
- package/dist/utils/decorative-icon.d.ts +15 -0
- package/dist/utils/decorative-icon.js +15 -0
- package/dist/utils/disabled-selector.d.ts +22 -0
- package/dist/utils/disabled-selector.js +22 -0
- package/dist/utils/dispatch-change.d.ts +17 -0
- package/dist/utils/dispatch-change.js +19 -0
- package/dist/utils/field-parts.d.ts +26 -0
- package/dist/utils/field-parts.js +12 -0
- package/dist/utils/full-turn-radians.d.ts +16 -0
- package/dist/utils/full-turn-radians.js +16 -0
- package/dist/utils/geometry.d.ts +14 -0
- package/dist/utils/geometry.js +9 -0
- package/dist/utils/has-accessible-text.d.ts +19 -0
- package/dist/utils/has-accessible-text.js +30 -0
- package/dist/utils/hue-spectrum.d.ts +19 -0
- package/dist/utils/hue-spectrum.js +19 -0
- package/dist/utils/index.d.ts +35 -0
- package/dist/utils/index.js +35 -0
- package/dist/utils/is-new-primary-press.d.ts +29 -0
- package/dist/utils/is-new-primary-press.js +31 -0
- package/dist/utils/keyboard-nav.d.ts +64 -0
- package/dist/utils/keyboard-nav.js +79 -0
- package/dist/utils/merge-style.d.ts +33 -0
- package/dist/utils/merge-style.js +30 -0
- package/dist/utils/paired-range-inputs.d.ts +26 -0
- package/dist/utils/paired-range-inputs.js +29 -0
- package/dist/utils/placement.d.ts +18 -0
- package/dist/utils/placement.js +12 -0
- package/dist/utils/prefers-reduced-motion.d.ts +17 -0
- package/dist/utils/prefers-reduced-motion.js +19 -0
- package/dist/utils/resolve-field-wiring.d.ts +83 -0
- package/dist/utils/resolve-field-wiring.js +53 -0
- package/dist/utils/resolve-fill-percent.d.ts +28 -0
- package/dist/utils/resolve-fill-percent.js +34 -0
- package/dist/utils/round-precision.d.ts +22 -0
- package/dist/utils/round-precision.js +25 -0
- package/dist/utils/semantic-color-panel.d.ts +35 -0
- package/dist/utils/semantic-color-panel.js +28 -0
- package/dist/utils/semantic-color.d.ts +17 -0
- package/dist/utils/semantic-color.js +11 -0
- package/dist/utils/warn-if-no-accessible-name.d.ts +52 -0
- package/dist/utils/warn-if-no-accessible-name.js +55 -0
- package/dist/utils/write-cookie.d.ts +35 -0
- package/dist/utils/write-cookie.js +37 -0
- package/package.json +32 -0
- package/src/reset.css +223 -0
- package/src/theme.css +146 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Switches a theme switch control's page-wide scheme — light, dark, or
|
|
3
|
+
* system — and persists it in a cookie so a return visit's server render
|
|
4
|
+
* starts in the same mode. The mode must reach `<html>`, outside the
|
|
5
|
+
* control, and answer Invoker Commands, neither of which CSS expresses alone.
|
|
6
|
+
* Without JS the same options still work as a plain form writing the cookie.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
import type { ElementProps, MixinFactory } from "remix/ui";
|
|
12
|
+
/** DOM event type dispatched by {@link themeToggle} whenever it switches `<html>`'s active mode. */
|
|
13
|
+
declare const THEME_CHANGE_EVENT: "ui:theme-change";
|
|
14
|
+
declare global {
|
|
15
|
+
interface HTMLElementEventMap {
|
|
16
|
+
[THEME_CHANGE_EVENT]: ThemeChangeEvent;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Types associated with {@link themeToggle}: the color-scheme modes it
|
|
21
|
+
* switches `<html>` between, and the options it accepts.
|
|
22
|
+
*/
|
|
23
|
+
export declare namespace ThemeToggle {
|
|
24
|
+
/**
|
|
25
|
+
* A color scheme {@link themeToggle} can switch `<html>` to: `"light"`
|
|
26
|
+
* removes `.dark`/`.system` for the light palette, `"dark"` adds `.dark`,
|
|
27
|
+
* and `"system"` adds `.system` to follow `prefers-color-scheme`.
|
|
28
|
+
*/
|
|
29
|
+
type Mode = "light" | "dark" | "system";
|
|
30
|
+
/**
|
|
31
|
+
* Configuration accepted by {@link themeToggle}.
|
|
32
|
+
*/
|
|
33
|
+
interface Options {
|
|
34
|
+
/**
|
|
35
|
+
* Cookie name the active mode persists under. Defaults to
|
|
36
|
+
* `"ui:theme"` — override only if the consuming app's server reads
|
|
37
|
+
* the cookie back under a different name.
|
|
38
|
+
*/
|
|
39
|
+
cookieName?: string;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Dispatched on a theme switch control by {@link themeToggle} right after a
|
|
44
|
+
* switch, carrying the new mode so a consumer can resync another instance or
|
|
45
|
+
* update `<meta name="theme-color">` without reading `<html>`'s class list.
|
|
46
|
+
*/
|
|
47
|
+
export declare class ThemeChangeEvent extends Event {
|
|
48
|
+
/** The mode {@link themeToggle} just switched `<html>` to. */
|
|
49
|
+
readonly mode: ThemeToggle.Mode;
|
|
50
|
+
/**
|
|
51
|
+
* @param mode The mode {@link themeToggle} just switched `<html>` to.
|
|
52
|
+
*/
|
|
53
|
+
constructor(mode: ThemeToggle.Mode);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Turns a theme switch control into the source of a page-wide color-scheme
|
|
57
|
+
* change, switching `<html>` and persisting the mode to a cookie together,
|
|
58
|
+
* every time, so `<html>`'s class list stays the only source of truth.
|
|
59
|
+
*
|
|
60
|
+
* @param options Cookie name to persist the mode under; see
|
|
61
|
+
* {@link ThemeToggle.Options}. Optional — when a call site omits it
|
|
62
|
+
* (`themeToggle()`), the runtime passes its trailing current-props argument
|
|
63
|
+
* in its place, so that value is reset back to an empty options object.
|
|
64
|
+
* @returns A mixin descriptor for a theme switch control's `mix` prop.
|
|
65
|
+
* @example
|
|
66
|
+
* <div id="theme-switch" mix={[themeToggle()]}>
|
|
67
|
+
* <button commandfor="theme-switch" command="--ui-theme-light">{t("theme.light")}</button>
|
|
68
|
+
* <button commandfor="theme-switch" command="--ui-theme-dark">{t("theme.dark")}</button>
|
|
69
|
+
* <button commandfor="theme-switch" command="--ui-theme-system">{t("theme.system")}</button>
|
|
70
|
+
* </div>
|
|
71
|
+
* @example
|
|
72
|
+
* // A form-native radio group, still fully functional as a plain form
|
|
73
|
+
* // submission when JavaScript never runs.
|
|
74
|
+
* <fieldset mix={[themeToggle()]}>
|
|
75
|
+
* <legend>{t("theme.label")}</legend>
|
|
76
|
+
* <label><input type="radio" name="theme" value="light" defaultChecked /> {t("theme.light")}</label>
|
|
77
|
+
* <label><input type="radio" name="theme" value="dark" /> {t("theme.dark")}</label>
|
|
78
|
+
* <label><input type="radio" name="theme" value="system" /> {t("theme.system")}</label>
|
|
79
|
+
* </fieldset>
|
|
80
|
+
*/
|
|
81
|
+
export declare const themeToggle: MixinFactory<HTMLElement, [options?: ThemeToggle.Options], ElementProps>;
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Switches a theme switch control's page-wide scheme — light, dark, or
|
|
3
|
+
* system — and persists it in a cookie so a return visit's server render
|
|
4
|
+
* starts in the same mode. The mode must reach `<html>`, outside the
|
|
5
|
+
* control, and answer Invoker Commands, neither of which CSS expresses alone.
|
|
6
|
+
* Without JS the same options still work as a plain form writing the cookie.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
import { createElement, createMixin, on } from "remix/ui";
|
|
12
|
+
import { asCommandEvent } from "../utils/command-event.js";
|
|
13
|
+
import { writeCookie } from "../utils/write-cookie.js";
|
|
14
|
+
/**
|
|
15
|
+
* Cookie {@link themeToggle} persists the active mode under by default, so
|
|
16
|
+
* an app's server-side render can read the same name back; override via
|
|
17
|
+
* {@link ThemeToggle.Options.cookieName} for a different name.
|
|
18
|
+
*/
|
|
19
|
+
const THEME_COOKIE_NAME = "ui:theme";
|
|
20
|
+
/** Invoker command {@link themeToggle} answers to force the light palette. */
|
|
21
|
+
const LIGHT_COMMAND = "--ui-theme-light";
|
|
22
|
+
/** Invoker command {@link themeToggle} answers to force the dark palette. */
|
|
23
|
+
const DARK_COMMAND = "--ui-theme-dark";
|
|
24
|
+
/** Invoker command {@link themeToggle} answers to follow the operating system's `prefers-color-scheme`. */
|
|
25
|
+
const SYSTEM_COMMAND = "--ui-theme-system";
|
|
26
|
+
/** DOM event type dispatched by {@link themeToggle} whenever it switches `<html>`'s active mode. */
|
|
27
|
+
const THEME_CHANGE_EVENT = "ui:theme-change";
|
|
28
|
+
/**
|
|
29
|
+
* Dispatched on a theme switch control by {@link themeToggle} right after a
|
|
30
|
+
* switch, carrying the new mode so a consumer can resync another instance or
|
|
31
|
+
* update `<meta name="theme-color">` without reading `<html>`'s class list.
|
|
32
|
+
*/
|
|
33
|
+
export class ThemeChangeEvent extends Event {
|
|
34
|
+
/** The mode {@link themeToggle} just switched `<html>` to. */
|
|
35
|
+
mode;
|
|
36
|
+
/**
|
|
37
|
+
* @param mode The mode {@link themeToggle} just switched `<html>` to.
|
|
38
|
+
*/
|
|
39
|
+
constructor(mode) {
|
|
40
|
+
super(THEME_CHANGE_EVENT, { bubbles: true });
|
|
41
|
+
this.mode = mode;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Maps an invoker command {@link themeToggle} answers to the mode it
|
|
46
|
+
* switches `<html>` to, or `undefined` for any other command, letting an
|
|
47
|
+
* unrelated `command` event on the same host pass through untouched.
|
|
48
|
+
*
|
|
49
|
+
* @param command `CommandEvent.command` read off a `command` event.
|
|
50
|
+
*/
|
|
51
|
+
function commandToMode(command) {
|
|
52
|
+
switch (command) {
|
|
53
|
+
case LIGHT_COMMAND:
|
|
54
|
+
return "light";
|
|
55
|
+
case DARK_COMMAND:
|
|
56
|
+
return "dark";
|
|
57
|
+
case SYSTEM_COMMAND:
|
|
58
|
+
return "system";
|
|
59
|
+
default:
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Reads `value` back as a {@link ThemeToggle.Mode}, or `undefined` when it
|
|
65
|
+
* names none of the three modes {@link themeToggle} recognizes, keeping an
|
|
66
|
+
* unrelated control's `change` event from ever reaching {@link applyMode}.
|
|
67
|
+
*
|
|
68
|
+
* @param value Raw `value` read off a native radio input or `<select>`.
|
|
69
|
+
*/
|
|
70
|
+
function parseMode(value) {
|
|
71
|
+
if (value === "light" || value === "dark" || value === "system")
|
|
72
|
+
return value;
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Reads the `value` a `change` event's target carries when that target
|
|
77
|
+
* is a radio input or `<select>`, or `undefined` otherwise, so a `change`
|
|
78
|
+
* from an unrelated descendant never reaches {@link parseMode}.
|
|
79
|
+
*
|
|
80
|
+
* @param target `EventTarget` a `change` event fired on.
|
|
81
|
+
*/
|
|
82
|
+
function readControlValue(target) {
|
|
83
|
+
if (target instanceof HTMLInputElement && target.type === "radio")
|
|
84
|
+
return target.value;
|
|
85
|
+
if (target instanceof HTMLSelectElement)
|
|
86
|
+
return target.value;
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Switches `<html>` to `mode`, following `theme.css`'s contract: `.dark`
|
|
91
|
+
* for a forced dark palette, `.system` for `prefers-color-scheme`, or
|
|
92
|
+
* neither class for a forced light palette.
|
|
93
|
+
*
|
|
94
|
+
* @param mode Mode to switch `<html>` to.
|
|
95
|
+
*/
|
|
96
|
+
function applyMode(mode) {
|
|
97
|
+
let root = document.documentElement;
|
|
98
|
+
root.classList.toggle("dark", mode === "dark");
|
|
99
|
+
root.classList.toggle("system", mode === "system");
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Turns a theme switch control into the source of a page-wide color-scheme
|
|
103
|
+
* change, switching `<html>` and persisting the mode to a cookie together,
|
|
104
|
+
* every time, so `<html>`'s class list stays the only source of truth.
|
|
105
|
+
*
|
|
106
|
+
* @param options Cookie name to persist the mode under; see
|
|
107
|
+
* {@link ThemeToggle.Options}. Optional — when a call site omits it
|
|
108
|
+
* (`themeToggle()`), the runtime passes its trailing current-props argument
|
|
109
|
+
* in its place, so that value is reset back to an empty options object.
|
|
110
|
+
* @returns A mixin descriptor for a theme switch control's `mix` prop.
|
|
111
|
+
* @example
|
|
112
|
+
* <div id="theme-switch" mix={[themeToggle()]}>
|
|
113
|
+
* <button commandfor="theme-switch" command="--ui-theme-light">{t("theme.light")}</button>
|
|
114
|
+
* <button commandfor="theme-switch" command="--ui-theme-dark">{t("theme.dark")}</button>
|
|
115
|
+
* <button commandfor="theme-switch" command="--ui-theme-system">{t("theme.system")}</button>
|
|
116
|
+
* </div>
|
|
117
|
+
* @example
|
|
118
|
+
* // A form-native radio group, still fully functional as a plain form
|
|
119
|
+
* // submission when JavaScript never runs.
|
|
120
|
+
* <fieldset mix={[themeToggle()]}>
|
|
121
|
+
* <legend>{t("theme.label")}</legend>
|
|
122
|
+
* <label><input type="radio" name="theme" value="light" defaultChecked /> {t("theme.light")}</label>
|
|
123
|
+
* <label><input type="radio" name="theme" value="dark" /> {t("theme.dark")}</label>
|
|
124
|
+
* <label><input type="radio" name="theme" value="system" /> {t("theme.system")}</label>
|
|
125
|
+
* </fieldset>
|
|
126
|
+
*/
|
|
127
|
+
export const themeToggle = createMixin((handle) => {
|
|
128
|
+
return (options = {}, props = options) => {
|
|
129
|
+
if (props === options) {
|
|
130
|
+
options = {};
|
|
131
|
+
}
|
|
132
|
+
let cookieName = options.cookieName ?? THEME_COOKIE_NAME;
|
|
133
|
+
return createElement(handle.element, {
|
|
134
|
+
mix: [
|
|
135
|
+
on("command", (event) => {
|
|
136
|
+
let commandEvent = asCommandEvent(event);
|
|
137
|
+
let mode = commandToMode(commandEvent.command);
|
|
138
|
+
if (mode === undefined)
|
|
139
|
+
return;
|
|
140
|
+
applyMode(mode);
|
|
141
|
+
writeCookie(cookieName, mode);
|
|
142
|
+
event.currentTarget.dispatchEvent(new ThemeChangeEvent(mode));
|
|
143
|
+
}),
|
|
144
|
+
on("change", (event) => {
|
|
145
|
+
let value = readControlValue(event.target);
|
|
146
|
+
if (value === undefined)
|
|
147
|
+
return;
|
|
148
|
+
let mode = parseMode(value);
|
|
149
|
+
if (mode === undefined)
|
|
150
|
+
return;
|
|
151
|
+
applyMode(mode);
|
|
152
|
+
writeCookie(cookieName, mode);
|
|
153
|
+
event.currentTarget.dispatchEvent(new ThemeChangeEvent(mode));
|
|
154
|
+
}),
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The insert/remove lifecycle bookkeeping a mixin repeats on its own before
|
|
3
|
+
* it can read or write anything on its own host element from outside its
|
|
4
|
+
* render callback — a document-level listener, an observer callback, a
|
|
5
|
+
* queued task — since the host node doesn't exist yet while a mixin's setup
|
|
6
|
+
* function runs, and stops existing again once the host unmounts.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
import type { MixinHandle } from "remix/ui";
|
|
12
|
+
/**
|
|
13
|
+
* Caches the DOM node behind a mixin's own host element for as long as it
|
|
14
|
+
* stays mounted, since no host node exists while setup runs; `insert`/`remove`
|
|
15
|
+
* keep the returned getter's node current, or `undefined` while unmounted.
|
|
16
|
+
*
|
|
17
|
+
* @param handle Mixin handle to track the host node of.
|
|
18
|
+
* @returns A getter returning the currently mounted host node, or `undefined` while unmounted.
|
|
19
|
+
* @example
|
|
20
|
+
* export const example = createMixin<HTMLElement>((handle) => {
|
|
21
|
+
* let hostNode = trackHostNode(handle);
|
|
22
|
+
*
|
|
23
|
+
* addEventListeners(document, handle.signal, {
|
|
24
|
+
* keydown() {
|
|
25
|
+
* let node = hostNode();
|
|
26
|
+
* if (node === undefined) return;
|
|
27
|
+
* // ...
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* return () => {};
|
|
32
|
+
* });
|
|
33
|
+
*/
|
|
34
|
+
export declare function trackHostNode<node extends EventTarget = Element>(handle: MixinHandle<node>): () => node | undefined;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The insert/remove lifecycle bookkeeping a mixin repeats on its own before
|
|
3
|
+
* it can read or write anything on its own host element from outside its
|
|
4
|
+
* render callback — a document-level listener, an observer callback, a
|
|
5
|
+
* queued task — since the host node doesn't exist yet while a mixin's setup
|
|
6
|
+
* function runs, and stops existing again once the host unmounts.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Caches the DOM node behind a mixin's own host element for as long as it
|
|
13
|
+
* stays mounted, since no host node exists while setup runs; `insert`/`remove`
|
|
14
|
+
* keep the returned getter's node current, or `undefined` while unmounted.
|
|
15
|
+
*
|
|
16
|
+
* @param handle Mixin handle to track the host node of.
|
|
17
|
+
* @returns A getter returning the currently mounted host node, or `undefined` while unmounted.
|
|
18
|
+
* @example
|
|
19
|
+
* export const example = createMixin<HTMLElement>((handle) => {
|
|
20
|
+
* let hostNode = trackHostNode(handle);
|
|
21
|
+
*
|
|
22
|
+
* addEventListeners(document, handle.signal, {
|
|
23
|
+
* keydown() {
|
|
24
|
+
* let node = hostNode();
|
|
25
|
+
* if (node === undefined) return;
|
|
26
|
+
* // ...
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* return () => {};
|
|
31
|
+
* });
|
|
32
|
+
*/
|
|
33
|
+
export function trackHostNode(handle) {
|
|
34
|
+
let hostNode;
|
|
35
|
+
handle.addEventListener("insert", (event) => {
|
|
36
|
+
hostNode = event.node;
|
|
37
|
+
});
|
|
38
|
+
handle.addEventListener("remove", () => {
|
|
39
|
+
hostNode = undefined;
|
|
40
|
+
});
|
|
41
|
+
return () => hostNode;
|
|
42
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapts the WAI-ARIA tree keyboard pattern onto a Tree's rows: a roving
|
|
3
|
+
* focus position moves between visible rows, arrow keys expand, collapse, or
|
|
4
|
+
* step into and out of a subtree, and typed text matches labels, with every
|
|
5
|
+
* selection change delegated to a shared `SelectionModel`. Each row still
|
|
6
|
+
* renders in document order behind a disclosure control, staying expandable.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
import type { SelectionModel } from "../behaviors/selection-model.js";
|
|
12
|
+
/**
|
|
13
|
+
* Attribute every Tree row exposes itself on; its value doubles as the
|
|
14
|
+
* `SelectionModel.Key` {@link treeKeys} toggles, ranges over, and mirrors
|
|
15
|
+
* `aria-selected` onto, while `aria-level`/`aria-expanded` derive each row's place.
|
|
16
|
+
*/
|
|
17
|
+
export declare const TREE_ITEM_ATTRIBUTE = "data-tree-item";
|
|
18
|
+
/** DOM event type dispatched by {@link treeKeys} whenever the bound model's selection or the active row changes. */
|
|
19
|
+
declare const TREE_CHANGE_EVENT: "ui:tree-change";
|
|
20
|
+
declare global {
|
|
21
|
+
interface HTMLElementEventMap {
|
|
22
|
+
[TREE_CHANGE_EVENT]: TreeChangeEvent;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Dispatched on a Tree's row list by {@link treeKeys} whenever the bound
|
|
27
|
+
* `SelectionModel`'s selection changes or the roving cursor moves rows, so
|
|
28
|
+
* a consumer can react without reading the model or DOM focus directly.
|
|
29
|
+
*/
|
|
30
|
+
export declare class TreeChangeEvent extends Event {
|
|
31
|
+
/** Keys the bound model currently reports as selected. */
|
|
32
|
+
readonly selectedKeys: ReadonlySet<SelectionModel.Key>;
|
|
33
|
+
/** Key of the row currently holding the roving keyboard cursor, or `null` when no row has focus. */
|
|
34
|
+
readonly activeKey: SelectionModel.Key | null;
|
|
35
|
+
/**
|
|
36
|
+
* @param init Snapshot of the tree's selection and active row at dispatch time.
|
|
37
|
+
*/
|
|
38
|
+
constructor(init: {
|
|
39
|
+
selectedKeys: ReadonlySet<SelectionModel.Key>;
|
|
40
|
+
activeKey: SelectionModel.Key | null;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Turns a Tree's row list into the ARIA tree keyboard surface for its rows:
|
|
45
|
+
* arrow keys move, expand, and collapse; typed text searches labels; and
|
|
46
|
+
* every selection delegates to `model`, mirroring back and firing {@link TreeChangeEvent}.
|
|
47
|
+
*
|
|
48
|
+
* @param model Behavior class instance owning the tree's selected-key set.
|
|
49
|
+
* @example
|
|
50
|
+
* let model = new SelectionModel({ mode: "multiple" });
|
|
51
|
+
* <div role="tree" mix={[treeKeys(model)]}>
|
|
52
|
+
* <div role="treeitem" aria-level={1} aria-expanded="true" data-tree-item="docs">Docs</div>
|
|
53
|
+
* <div role="treeitem" aria-level={2} data-tree-item="docs/readme">README</div>
|
|
54
|
+
* </div>
|
|
55
|
+
*/
|
|
56
|
+
export declare const treeKeys: import("remix/ui").MixinFactory<HTMLElement, [model: SelectionModel], import("remix/ui").ElementProps>;
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapts the WAI-ARIA tree keyboard pattern onto a Tree's rows: a roving
|
|
3
|
+
* focus position moves between visible rows, arrow keys expand, collapse, or
|
|
4
|
+
* step into and out of a subtree, and typed text matches labels, with every
|
|
5
|
+
* selection change delegated to a shared `SelectionModel`. Each row still
|
|
6
|
+
* renders in document order behind a disclosure control, staying expandable.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
import { createElement, createMixin, on } from "remix/ui";
|
|
12
|
+
import { isPrintableKey, labelFor, setRovingTabindex } from "../utils/keyboard-nav.js";
|
|
13
|
+
/**
|
|
14
|
+
* Attribute every Tree row exposes itself on; its value doubles as the
|
|
15
|
+
* `SelectionModel.Key` {@link treeKeys} toggles, ranges over, and mirrors
|
|
16
|
+
* `aria-selected` onto, while `aria-level`/`aria-expanded` derive each row's place.
|
|
17
|
+
*/
|
|
18
|
+
export const TREE_ITEM_ATTRIBUTE = "data-tree-item";
|
|
19
|
+
/** Selector matching a row excluded from keyboard navigation and selection. */
|
|
20
|
+
const DISABLED_SELECTOR = '[aria-disabled="true"]';
|
|
21
|
+
/** Idle time after the last keystroke before the typeahead buffer resets. */
|
|
22
|
+
const TYPEAHEAD_RESET_MS = 500;
|
|
23
|
+
/** DOM event type dispatched by {@link treeKeys} whenever the bound model's selection or the active row changes. */
|
|
24
|
+
const TREE_CHANGE_EVENT = "ui:tree-change";
|
|
25
|
+
/**
|
|
26
|
+
* Dispatched on a Tree's row list by {@link treeKeys} whenever the bound
|
|
27
|
+
* `SelectionModel`'s selection changes or the roving cursor moves rows, so
|
|
28
|
+
* a consumer can react without reading the model or DOM focus directly.
|
|
29
|
+
*/
|
|
30
|
+
export class TreeChangeEvent extends Event {
|
|
31
|
+
/** Keys the bound model currently reports as selected. */
|
|
32
|
+
selectedKeys;
|
|
33
|
+
/** Key of the row currently holding the roving keyboard cursor, or `null` when no row has focus. */
|
|
34
|
+
activeKey;
|
|
35
|
+
/**
|
|
36
|
+
* @param init Snapshot of the tree's selection and active row at dispatch time.
|
|
37
|
+
*/
|
|
38
|
+
constructor(init) {
|
|
39
|
+
super(TREE_CHANGE_EVENT, { bubbles: true });
|
|
40
|
+
this.selectedKeys = init.selectedKeys;
|
|
41
|
+
this.activeKey = init.activeKey;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Reads rows beneath `host` carrying {@link TREE_ITEM_ATTRIBUTE} into a flat
|
|
46
|
+
* {@link TreeRow} list, deriving level, parent, children, and visibility from
|
|
47
|
+
* `aria-level`/`aria-expanded` alone, via one level-ordered stack pass so no row re-scans its ancestors.
|
|
48
|
+
*/
|
|
49
|
+
function collectRows(host) {
|
|
50
|
+
let elements = Array.from(host.querySelectorAll(`[${TREE_ITEM_ATTRIBUTE}]`));
|
|
51
|
+
let rows = [];
|
|
52
|
+
let stack = [];
|
|
53
|
+
for (let [index, element] of elements.entries()) {
|
|
54
|
+
let level = Number(element.getAttribute("aria-level")) || 1;
|
|
55
|
+
while (stack.length > 0 && stack[stack.length - 1].level >= level)
|
|
56
|
+
stack.pop();
|
|
57
|
+
let parent = stack[stack.length - 1] ?? null;
|
|
58
|
+
let next = elements[index + 1];
|
|
59
|
+
let nextLevel = next ? Number(next.getAttribute("aria-level")) || 1 : level;
|
|
60
|
+
let row = {
|
|
61
|
+
element,
|
|
62
|
+
key: element.getAttribute(TREE_ITEM_ATTRIBUTE) ?? "",
|
|
63
|
+
level,
|
|
64
|
+
parent,
|
|
65
|
+
hasChildren: nextLevel > level,
|
|
66
|
+
expanded: element.getAttribute("aria-expanded") === "true",
|
|
67
|
+
disabled: element.matches(DISABLED_SELECTOR),
|
|
68
|
+
visible: parent === null || (parent.visible && parent.expanded),
|
|
69
|
+
};
|
|
70
|
+
rows.push(row);
|
|
71
|
+
stack.push(row);
|
|
72
|
+
}
|
|
73
|
+
return rows;
|
|
74
|
+
}
|
|
75
|
+
/** Rows in `rows` that are both reachable (every ancestor expanded) and enabled — the set arrow keys, `Home`/`End`, typeahead, and range/select-all actually operate over. */
|
|
76
|
+
function navigableRows(rows) {
|
|
77
|
+
return rows.filter((row) => row.visible && !row.disabled);
|
|
78
|
+
}
|
|
79
|
+
/** Moves roving `tabindex` and real DOM focus onto `row`, scrolling it into view. */
|
|
80
|
+
function focusRow(rows, row) {
|
|
81
|
+
setRovingTabindex(rows.map((candidate) => candidate.element), row.element);
|
|
82
|
+
row.element.focus();
|
|
83
|
+
row.element.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
84
|
+
}
|
|
85
|
+
/** Mirrors `model.selectedKeys` onto every row's `aria-selected` beneath `host`, whether currently visible or collapsed out of view, so a selection made before a subtree collapsed is never lost from the DOM. */
|
|
86
|
+
function syncSelection(host, model) {
|
|
87
|
+
for (let row of collectRows(host)) {
|
|
88
|
+
row.element.setAttribute("aria-selected", model.isSelected(row.key) ? "true" : "false");
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Refreshes `model`'s known key universe to the rows currently reachable by
|
|
93
|
+
* keyboard, so range selection and select-all span only what's visible — a
|
|
94
|
+
* row inside a collapsed subtree is skipped, matching arrow-key movement.
|
|
95
|
+
*/
|
|
96
|
+
function refreshKeys(host, model) {
|
|
97
|
+
model.setKeys(navigableRows(collectRows(host)).map((row) => row.key));
|
|
98
|
+
}
|
|
99
|
+
/** Dispatches {@link TreeChangeEvent} on `host`, reading the active row's key straight off whichever element currently holds DOM focus. */
|
|
100
|
+
function dispatchTreeChange(host, model) {
|
|
101
|
+
let active = host.ownerDocument.activeElement;
|
|
102
|
+
let activeKey = active instanceof HTMLElement && active.hasAttribute(TREE_ITEM_ATTRIBUTE)
|
|
103
|
+
? (active.getAttribute(TREE_ITEM_ATTRIBUTE) ?? "")
|
|
104
|
+
: null;
|
|
105
|
+
host.dispatchEvent(new TreeChangeEvent({ selectedKeys: model.selectedKeys, activeKey }));
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Turns a Tree's row list into the ARIA tree keyboard surface for its rows:
|
|
109
|
+
* arrow keys move, expand, and collapse; typed text searches labels; and
|
|
110
|
+
* every selection delegates to `model`, mirroring back and firing {@link TreeChangeEvent}.
|
|
111
|
+
*
|
|
112
|
+
* @param model Behavior class instance owning the tree's selected-key set.
|
|
113
|
+
* @example
|
|
114
|
+
* let model = new SelectionModel({ mode: "multiple" });
|
|
115
|
+
* <div role="tree" mix={[treeKeys(model)]}>
|
|
116
|
+
* <div role="treeitem" aria-level={1} aria-expanded="true" data-tree-item="docs">Docs</div>
|
|
117
|
+
* <div role="treeitem" aria-level={2} data-tree-item="docs/readme">README</div>
|
|
118
|
+
* </div>
|
|
119
|
+
*/
|
|
120
|
+
export const treeKeys = createMixin((handle) => {
|
|
121
|
+
let hostNode;
|
|
122
|
+
let boundModel;
|
|
123
|
+
let buffer = "";
|
|
124
|
+
let resetBufferId;
|
|
125
|
+
handle.addEventListener("insert", (event) => {
|
|
126
|
+
hostNode = event.node;
|
|
127
|
+
let rows = collectRows(hostNode);
|
|
128
|
+
let navigable = navigableRows(rows);
|
|
129
|
+
if (navigable.length > 0) {
|
|
130
|
+
setRovingTabindex(rows.map((row) => row.element), navigable[0].element);
|
|
131
|
+
}
|
|
132
|
+
if (boundModel) {
|
|
133
|
+
refreshKeys(hostNode, boundModel);
|
|
134
|
+
syncSelection(hostNode, boundModel);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
handle.addEventListener("remove", () => {
|
|
138
|
+
hostNode = undefined;
|
|
139
|
+
});
|
|
140
|
+
handle.signal.addEventListener("abort", () => clearTimeout(resetBufferId));
|
|
141
|
+
return (model) => {
|
|
142
|
+
if (boundModel !== model) {
|
|
143
|
+
boundModel = model;
|
|
144
|
+
model.addEventListener("change", () => {
|
|
145
|
+
if (hostNode === undefined)
|
|
146
|
+
return;
|
|
147
|
+
syncSelection(hostNode, model);
|
|
148
|
+
dispatchTreeChange(hostNode, model);
|
|
149
|
+
}, { signal: handle.signal });
|
|
150
|
+
}
|
|
151
|
+
return createElement(handle.element, {
|
|
152
|
+
mix: [
|
|
153
|
+
on("focusin", (event) => {
|
|
154
|
+
if (!hostNode)
|
|
155
|
+
return;
|
|
156
|
+
let target = event.target;
|
|
157
|
+
if (!(target instanceof HTMLElement))
|
|
158
|
+
return;
|
|
159
|
+
let rows = collectRows(hostNode);
|
|
160
|
+
let row = rows.find((candidate) => candidate.element === target);
|
|
161
|
+
if (row) {
|
|
162
|
+
setRovingTabindex(rows.map((candidate) => candidate.element), row.element);
|
|
163
|
+
}
|
|
164
|
+
}),
|
|
165
|
+
on("keydown", (event) => {
|
|
166
|
+
if (!hostNode)
|
|
167
|
+
return;
|
|
168
|
+
let rows = collectRows(hostNode);
|
|
169
|
+
if (rows.length === 0)
|
|
170
|
+
return;
|
|
171
|
+
let navigable = navigableRows(rows);
|
|
172
|
+
let activeIndex = navigable.findIndex((row) => row.element === hostNode.ownerDocument.activeElement);
|
|
173
|
+
let current = activeIndex === -1 ? undefined : navigable[activeIndex];
|
|
174
|
+
switch (event.key) {
|
|
175
|
+
case "ArrowDown": {
|
|
176
|
+
event.preventDefault();
|
|
177
|
+
let next = navigable[activeIndex + 1];
|
|
178
|
+
if (!next)
|
|
179
|
+
return;
|
|
180
|
+
focusRow(rows, next);
|
|
181
|
+
if (event.shiftKey)
|
|
182
|
+
model.selectRange(next.key);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
case "ArrowUp": {
|
|
186
|
+
event.preventDefault();
|
|
187
|
+
let previous = activeIndex > 0 ? navigable[activeIndex - 1] : undefined;
|
|
188
|
+
if (!previous)
|
|
189
|
+
return;
|
|
190
|
+
focusRow(rows, previous);
|
|
191
|
+
if (event.shiftKey)
|
|
192
|
+
model.selectRange(previous.key);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
case "ArrowRight": {
|
|
196
|
+
if (!current)
|
|
197
|
+
return;
|
|
198
|
+
event.preventDefault();
|
|
199
|
+
if (current.hasChildren && !current.expanded) {
|
|
200
|
+
current.element.setAttribute("aria-expanded", "true");
|
|
201
|
+
refreshKeys(hostNode, model);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (current.hasChildren && current.expanded) {
|
|
205
|
+
let child = rows.find((row) => row.parent === current && !row.disabled);
|
|
206
|
+
if (child)
|
|
207
|
+
focusRow(rows, child);
|
|
208
|
+
}
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
case "ArrowLeft": {
|
|
212
|
+
if (!current)
|
|
213
|
+
return;
|
|
214
|
+
event.preventDefault();
|
|
215
|
+
if (current.hasChildren && current.expanded) {
|
|
216
|
+
current.element.setAttribute("aria-expanded", "false");
|
|
217
|
+
refreshKeys(hostNode, model);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (current.parent && !current.parent.disabled)
|
|
221
|
+
focusRow(rows, current.parent);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
case "Home":
|
|
225
|
+
event.preventDefault();
|
|
226
|
+
if (navigable[0])
|
|
227
|
+
focusRow(rows, navigable[0]);
|
|
228
|
+
return;
|
|
229
|
+
case "End":
|
|
230
|
+
event.preventDefault();
|
|
231
|
+
if (navigable[navigable.length - 1])
|
|
232
|
+
focusRow(rows, navigable[navigable.length - 1]);
|
|
233
|
+
return;
|
|
234
|
+
case "Enter":
|
|
235
|
+
case " ":
|
|
236
|
+
if (!current)
|
|
237
|
+
return;
|
|
238
|
+
event.preventDefault();
|
|
239
|
+
model.toggle(current.key);
|
|
240
|
+
return;
|
|
241
|
+
case "Backspace":
|
|
242
|
+
buffer = buffer.slice(0, -1);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "a") {
|
|
246
|
+
event.preventDefault();
|
|
247
|
+
model.selectAll();
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!isPrintableKey(event))
|
|
251
|
+
return;
|
|
252
|
+
clearTimeout(resetBufferId);
|
|
253
|
+
buffer += event.key.toLowerCase();
|
|
254
|
+
resetBufferId = setTimeout(() => {
|
|
255
|
+
buffer = "";
|
|
256
|
+
}, TYPEAHEAD_RESET_MS);
|
|
257
|
+
let start = activeIndex === -1 ? 0 : activeIndex + 1;
|
|
258
|
+
let searchOrder = [...navigable.slice(start), ...navigable.slice(0, start)];
|
|
259
|
+
let match = searchOrder.find((row) => labelFor(row.element).startsWith(buffer));
|
|
260
|
+
if (match)
|
|
261
|
+
focusRow(rows, match);
|
|
262
|
+
}),
|
|
263
|
+
],
|
|
264
|
+
});
|
|
265
|
+
};
|
|
266
|
+
});
|