@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,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arrow-key and Enter navigation for a Command root already paired with
|
|
3
|
+
* `commandFilter(model)`: arrow keys move `model`'s active match across the
|
|
4
|
+
* visible items, mirrored onto `aria-selected` and `aria-activedescendant`;
|
|
5
|
+
* Enter clicks whatever control the active item nests. Without JS every
|
|
6
|
+
* item stays independently activatable in ordinary Tab order.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
import { createElement, createMixin, on } from "remix/ui";
|
|
12
|
+
import { COMMAND_INPUT_ATTRIBUTE, COMMAND_ITEM_ATTRIBUTE, getCommandItemId, } from "./command-filter.js";
|
|
13
|
+
/** `KeyboardEvent.key` value that moves the active match to the previous visible option. */
|
|
14
|
+
const ARROW_UP_KEY = "ArrowUp";
|
|
15
|
+
/** `KeyboardEvent.key` value that moves the active match to the next visible option. */
|
|
16
|
+
const ARROW_DOWN_KEY = "ArrowDown";
|
|
17
|
+
/** `KeyboardEvent.key` value that activates the current active match. */
|
|
18
|
+
const ENTER_KEY = "Enter";
|
|
19
|
+
/**
|
|
20
|
+
* Mirrors `model`'s active match onto every item beneath `root` as
|
|
21
|
+
* `aria-selected` and onto the search input as `aria-activedescendant`,
|
|
22
|
+
* scrolling the active item into view past {@link Command.List}'s scroll boundary.
|
|
23
|
+
*
|
|
24
|
+
* @param root Command root element the items and search input are read from.
|
|
25
|
+
* @param model Model whose current active match gets mirrored onto the DOM.
|
|
26
|
+
*/
|
|
27
|
+
function syncActive(root, model) {
|
|
28
|
+
let items = root.querySelectorAll(`[${COMMAND_ITEM_ATTRIBUTE}]`);
|
|
29
|
+
let activeItem;
|
|
30
|
+
for (let [index, item] of items.entries()) {
|
|
31
|
+
let isActive = getCommandItemId(item, index) === model.activeId;
|
|
32
|
+
item.setAttribute("aria-selected", String(isActive));
|
|
33
|
+
if (isActive)
|
|
34
|
+
activeItem = item;
|
|
35
|
+
}
|
|
36
|
+
let input = root.querySelector(`[${COMMAND_INPUT_ATTRIBUTE}]`);
|
|
37
|
+
if (input !== null)
|
|
38
|
+
input.setAttribute("aria-activedescendant", model.activeId ?? "");
|
|
39
|
+
activeItem?.scrollIntoView({ block: "nearest" });
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Finds the item beneath `root` whose resolved id matches `model.activeId`
|
|
43
|
+
* and clicks whatever button or link its own children nest for activation.
|
|
44
|
+
*
|
|
45
|
+
* @param root Command root element the active item is read from.
|
|
46
|
+
* @param model Model naming which item is currently active.
|
|
47
|
+
*/
|
|
48
|
+
function activateCurrent(root, model) {
|
|
49
|
+
if (model.activeId === null)
|
|
50
|
+
return;
|
|
51
|
+
let items = root.querySelectorAll(`[${COMMAND_ITEM_ATTRIBUTE}]`);
|
|
52
|
+
for (let [index, item] of items.entries()) {
|
|
53
|
+
if (getCommandItemId(item, index) !== model.activeId)
|
|
54
|
+
continue;
|
|
55
|
+
item.querySelector("a, button")?.click();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Adds ArrowDown/ArrowUp/Enter navigation to a Command root already paired
|
|
61
|
+
* with `commandFilter(model)`, mirroring the active match onto
|
|
62
|
+
* `aria-selected` and `aria-activedescendant`, and activating it on Enter.
|
|
63
|
+
*
|
|
64
|
+
* @param model The same `FilterModel` instance passed to `commandFilter()`.
|
|
65
|
+
* @example
|
|
66
|
+
* let model = new FilterModel();
|
|
67
|
+
* <Command mix={[commandFilter(model), commandKeys(model)]}>
|
|
68
|
+
* <Command.Input />
|
|
69
|
+
* <Command.Item id="home" value="Home">
|
|
70
|
+
* <a href="/">Home</a>
|
|
71
|
+
* </Command.Item>
|
|
72
|
+
* <Command.Item id="settings" value="Settings">
|
|
73
|
+
* <a href="/settings">Settings</a>
|
|
74
|
+
* </Command.Item>
|
|
75
|
+
* <Command.Empty>No matches</Command.Empty>
|
|
76
|
+
* </Command>
|
|
77
|
+
*/
|
|
78
|
+
export const commandKeys = createMixin((handle) => {
|
|
79
|
+
let hostNode;
|
|
80
|
+
let boundModel;
|
|
81
|
+
handle.addEventListener("insert", (event) => {
|
|
82
|
+
hostNode = event.node;
|
|
83
|
+
if (boundModel !== undefined)
|
|
84
|
+
syncActive(hostNode, boundModel);
|
|
85
|
+
});
|
|
86
|
+
handle.addEventListener("remove", () => {
|
|
87
|
+
hostNode = undefined;
|
|
88
|
+
});
|
|
89
|
+
return (model) => {
|
|
90
|
+
if (boundModel !== model) {
|
|
91
|
+
boundModel = model;
|
|
92
|
+
model.addEventListener("change", () => {
|
|
93
|
+
if (hostNode !== undefined)
|
|
94
|
+
syncActive(hostNode, model);
|
|
95
|
+
}, { signal: handle.signal });
|
|
96
|
+
}
|
|
97
|
+
return createElement(handle.element, {
|
|
98
|
+
mix: [
|
|
99
|
+
on("keydown", (event) => {
|
|
100
|
+
if (event.key === ARROW_DOWN_KEY) {
|
|
101
|
+
event.preventDefault();
|
|
102
|
+
model.moveNext();
|
|
103
|
+
}
|
|
104
|
+
else if (event.key === ARROW_UP_KEY) {
|
|
105
|
+
event.preventDefault();
|
|
106
|
+
model.movePrevious();
|
|
107
|
+
}
|
|
108
|
+
else if (event.key === ENTER_KEY && hostNode !== undefined) {
|
|
109
|
+
event.preventDefault();
|
|
110
|
+
activateCurrent(hostNode, model);
|
|
111
|
+
}
|
|
112
|
+
}),
|
|
113
|
+
],
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opens a ContextMenu's popover surface at the pointer position when its
|
|
3
|
+
* host receives a right-click, anchoring it with `remix/ui/anchor`'s
|
|
4
|
+
* point-based positioning since no markup can anchor a floating surface to
|
|
5
|
+
* a pointer position. Without this mixin the host falls back to the
|
|
6
|
+
* platform's native right-click menu.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
import { type ElementProps } from "remix/ui";
|
|
12
|
+
import { type AnchorOptions } from "remix/ui/anchor";
|
|
13
|
+
/**
|
|
14
|
+
* Opens the popover surface identified by `id` at the pointer position on a
|
|
15
|
+
* `contextmenu` event, or the host's bounding box from the keyboard, since a
|
|
16
|
+
* `popover="auto"` surface already handles its own Escape/outside-click dismissal.
|
|
17
|
+
*
|
|
18
|
+
* @param id `id` of the popover surface to open.
|
|
19
|
+
* @param options Anchor placement and offsets forwarded to `remix/ui/anchor`; placement defaults to `"bottom-start"`. Safe to omit — `contextMenu(id)` resets the runtime's trailing current-props argument back to an empty options object.
|
|
20
|
+
* @example
|
|
21
|
+
* <div id="row-1" mix={contextMenu("row-1-menu")}>Row 1</div>
|
|
22
|
+
* <div id="row-1-menu" popover="auto" role="menu">...</div>
|
|
23
|
+
*/
|
|
24
|
+
export declare const contextMenu: import("remix/ui").MixinFactory<HTMLElement, [id: string, options?: AnchorOptions | undefined], ElementProps>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opens a ContextMenu's popover surface at the pointer position when its
|
|
3
|
+
* host receives a right-click, anchoring it with `remix/ui/anchor`'s
|
|
4
|
+
* point-based positioning since no markup can anchor a floating surface to
|
|
5
|
+
* a pointer position. Without this mixin the host falls back to the
|
|
6
|
+
* platform's native right-click menu.
|
|
7
|
+
*
|
|
8
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
9
|
+
* @copyright Sergio Xalambrí 2026
|
|
10
|
+
*/
|
|
11
|
+
import { attrs, createElement, createMixin, on } from "remix/ui";
|
|
12
|
+
import { anchor } from "remix/ui/anchor";
|
|
13
|
+
/** Placement `contextMenu()` anchors the surface to when `options` doesn't specify one. */
|
|
14
|
+
const DEFAULT_PLACEMENT = "bottom-start";
|
|
15
|
+
/**
|
|
16
|
+
* Opens the popover surface identified by `id` at the pointer position on a
|
|
17
|
+
* `contextmenu` event, or the host's bounding box from the keyboard, since a
|
|
18
|
+
* `popover="auto"` surface already handles its own Escape/outside-click dismissal.
|
|
19
|
+
*
|
|
20
|
+
* @param id `id` of the popover surface to open.
|
|
21
|
+
* @param options Anchor placement and offsets forwarded to `remix/ui/anchor`; placement defaults to `"bottom-start"`. Safe to omit — `contextMenu(id)` resets the runtime's trailing current-props argument back to an empty options object.
|
|
22
|
+
* @example
|
|
23
|
+
* <div id="row-1" mix={contextMenu("row-1-menu")}>Row 1</div>
|
|
24
|
+
* <div id="row-1-menu" popover="auto" role="menu">...</div>
|
|
25
|
+
*/
|
|
26
|
+
export const contextMenu = createMixin((handle) => {
|
|
27
|
+
let cleanupAnchor = () => { };
|
|
28
|
+
handle.addEventListener("remove", () => cleanupAnchor());
|
|
29
|
+
/** Shows the surface `id` refers to, if any, and anchors it to `point`, replacing any previous anchor tracking. */
|
|
30
|
+
function openSurfaceAt(id, point, options) {
|
|
31
|
+
let surface = document.getElementById(id);
|
|
32
|
+
if (!surface) {
|
|
33
|
+
if (import.meta.env.DEV) {
|
|
34
|
+
console.warn(`contextMenu(): no element with id "${id}" found to open.`);
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!surface.matches(":popover-open"))
|
|
39
|
+
surface.showPopover();
|
|
40
|
+
cleanupAnchor();
|
|
41
|
+
cleanupAnchor = anchor(surface, point, { placement: DEFAULT_PLACEMENT, ...options });
|
|
42
|
+
}
|
|
43
|
+
return (id, options = {}, props = options) => {
|
|
44
|
+
if (props === options) {
|
|
45
|
+
options = {};
|
|
46
|
+
}
|
|
47
|
+
return createElement(handle.element, {
|
|
48
|
+
mix: [
|
|
49
|
+
attrs({ "aria-controls": id, "aria-haspopup": "menu" }),
|
|
50
|
+
on("contextmenu", (event) => {
|
|
51
|
+
event.preventDefault();
|
|
52
|
+
openSurfaceAt(id, { x: event.clientX, y: event.clientY }, options);
|
|
53
|
+
}),
|
|
54
|
+
on("keydown", (event) => {
|
|
55
|
+
if (event.key !== "ContextMenu" && !(event.shiftKey && event.key === "F10"))
|
|
56
|
+
return;
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
let rect = event.currentTarget.getBoundingClientRect();
|
|
59
|
+
openSurfaceAt(id, { x: rect.left, y: rect.top, width: rect.width, height: rect.height }, options);
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copies text from an element a Message footer copy button's `commandfor`
|
|
3
|
+
* targets onto the system clipboard. Only script can reach the clipboard
|
|
4
|
+
* API, so the copy action alone depends on script; the button's markup,
|
|
5
|
+
* tab order, and label render normally either way.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import type { MixinFactory } from "remix/ui";
|
|
11
|
+
/**
|
|
12
|
+
* Custom Invoker Command a Message footer copy button declares via
|
|
13
|
+
* `command={COPY_COMMAND}` and `commandfor`, giving the pairing a genuine
|
|
14
|
+
* invoker relationship in markup, read by {@link copyToClipboard} on click.
|
|
15
|
+
*/
|
|
16
|
+
export declare const COPY_COMMAND: "--copy";
|
|
17
|
+
/** DOM event type dispatched on a Message footer copy button by {@link copyToClipboard} once a clipboard write settles. */
|
|
18
|
+
declare const COPY_EVENT: "ui:copy";
|
|
19
|
+
declare global {
|
|
20
|
+
interface HTMLElementEventMap {
|
|
21
|
+
[COPY_EVENT]: CopyEvent;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Dispatched on a Message footer copy button by {@link copyToClipboard} once
|
|
26
|
+
* a clipboard write settles, carrying success and the attempted text so
|
|
27
|
+
* consumers can render their own feedback for either outcome.
|
|
28
|
+
*/
|
|
29
|
+
export declare class CopyEvent extends Event {
|
|
30
|
+
/** `true` once the text reached the clipboard, `false` if the write was rejected or the platform API is unavailable. */
|
|
31
|
+
readonly success: boolean;
|
|
32
|
+
/** The text {@link copyToClipboard} attempted to copy. */
|
|
33
|
+
readonly text: string;
|
|
34
|
+
/**
|
|
35
|
+
* @param success Whether the write reached the clipboard.
|
|
36
|
+
* @param text The text that was attempted.
|
|
37
|
+
*/
|
|
38
|
+
constructor(success: boolean, text: string);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Writes the text content of a Message footer copy button's `commandfor`
|
|
42
|
+
* target onto the system clipboard, resolving the target fresh from the
|
|
43
|
+
* button on every press so it stays correct even as the DOM changes.
|
|
44
|
+
*
|
|
45
|
+
* @returns A mixin descriptor for a Message footer copy button's `mix` prop.
|
|
46
|
+
* @example
|
|
47
|
+
* <div id="reply-1-content">{reply.text}</div>
|
|
48
|
+
* <button
|
|
49
|
+
* type="button"
|
|
50
|
+
* commandfor="reply-1-content"
|
|
51
|
+
* command={COPY_COMMAND}
|
|
52
|
+
* aria-label={t("message.copy")}
|
|
53
|
+
* mix={[
|
|
54
|
+
* copyToClipboard(),
|
|
55
|
+
* on("ui:copy", (event) => {
|
|
56
|
+
* announce(event.success ? t("message.copied") : t("message.copyFailed"));
|
|
57
|
+
* }),
|
|
58
|
+
* ]}
|
|
59
|
+
* >
|
|
60
|
+
* <CopyIcon />
|
|
61
|
+
* </button>
|
|
62
|
+
*/
|
|
63
|
+
export declare const copyToClipboard: MixinFactory<HTMLButtonElement>;
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copies text from an element a Message footer copy button's `commandfor`
|
|
3
|
+
* targets onto the system clipboard. Only script can reach the clipboard
|
|
4
|
+
* API, so the copy action alone depends on script; the button's markup,
|
|
5
|
+
* tab order, and label render normally either way.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import { createElement, createMixin, on } from "remix/ui";
|
|
11
|
+
import { DISABLED_SELECTOR } from "../utils/disabled-selector.js";
|
|
12
|
+
/**
|
|
13
|
+
* Custom Invoker Command a Message footer copy button declares via
|
|
14
|
+
* `command={COPY_COMMAND}` and `commandfor`, giving the pairing a genuine
|
|
15
|
+
* invoker relationship in markup, read by {@link copyToClipboard} on click.
|
|
16
|
+
*/
|
|
17
|
+
export const COPY_COMMAND = "--copy";
|
|
18
|
+
/** DOM event type dispatched on a Message footer copy button by {@link copyToClipboard} once a clipboard write settles. */
|
|
19
|
+
const COPY_EVENT = "ui:copy";
|
|
20
|
+
/**
|
|
21
|
+
* Dispatched on a Message footer copy button by {@link copyToClipboard} once
|
|
22
|
+
* a clipboard write settles, carrying success and the attempted text so
|
|
23
|
+
* consumers can render their own feedback for either outcome.
|
|
24
|
+
*/
|
|
25
|
+
export class CopyEvent extends Event {
|
|
26
|
+
/** `true` once the text reached the clipboard, `false` if the write was rejected or the platform API is unavailable. */
|
|
27
|
+
success;
|
|
28
|
+
/** The text {@link copyToClipboard} attempted to copy. */
|
|
29
|
+
text;
|
|
30
|
+
/**
|
|
31
|
+
* @param success Whether the write reached the clipboard.
|
|
32
|
+
* @param text The text that was attempted.
|
|
33
|
+
*/
|
|
34
|
+
constructor(success, text) {
|
|
35
|
+
super(COPY_EVENT, { bubbles: true });
|
|
36
|
+
this.success = success;
|
|
37
|
+
this.text = text;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Finds the element a copy button's `commandfor` targets, preferring the
|
|
42
|
+
* live `commandForElement` reference and falling back to an `id` lookup for
|
|
43
|
+
* runtimes that parse `commandfor` without yet reflecting the IDL property.
|
|
44
|
+
*
|
|
45
|
+
* @param button The copy button found inside a Message footer.
|
|
46
|
+
*/
|
|
47
|
+
function resolveCopyTarget(button) {
|
|
48
|
+
if (button.commandForElement)
|
|
49
|
+
return button.commandForElement;
|
|
50
|
+
let commandForId = button.getAttribute("commandfor");
|
|
51
|
+
if (!commandForId)
|
|
52
|
+
return undefined;
|
|
53
|
+
return document.getElementById(commandForId) ?? undefined;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Writes the text content of a Message footer copy button's `commandfor`
|
|
57
|
+
* target onto the system clipboard, resolving the target fresh from the
|
|
58
|
+
* button on every press so it stays correct even as the DOM changes.
|
|
59
|
+
*
|
|
60
|
+
* @returns A mixin descriptor for a Message footer copy button's `mix` prop.
|
|
61
|
+
* @example
|
|
62
|
+
* <div id="reply-1-content">{reply.text}</div>
|
|
63
|
+
* <button
|
|
64
|
+
* type="button"
|
|
65
|
+
* commandfor="reply-1-content"
|
|
66
|
+
* command={COPY_COMMAND}
|
|
67
|
+
* aria-label={t("message.copy")}
|
|
68
|
+
* mix={[
|
|
69
|
+
* copyToClipboard(),
|
|
70
|
+
* on("ui:copy", (event) => {
|
|
71
|
+
* announce(event.success ? t("message.copied") : t("message.copyFailed"));
|
|
72
|
+
* }),
|
|
73
|
+
* ]}
|
|
74
|
+
* >
|
|
75
|
+
* <CopyIcon />
|
|
76
|
+
* </button>
|
|
77
|
+
*/
|
|
78
|
+
export const copyToClipboard = createMixin((handle) => {
|
|
79
|
+
return () => createElement(handle.element, {
|
|
80
|
+
mix: [
|
|
81
|
+
on("click", async (event, signal) => {
|
|
82
|
+
let button = event.currentTarget;
|
|
83
|
+
if (button.matches(DISABLED_SELECTOR))
|
|
84
|
+
return;
|
|
85
|
+
let target = resolveCopyTarget(button);
|
|
86
|
+
if (!target) {
|
|
87
|
+
if (import.meta.env.DEV) {
|
|
88
|
+
console.warn('copyToClipboard(): button has no "commandfor" target to copy text from.');
|
|
89
|
+
}
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
let text = target.textContent?.trim() ?? "";
|
|
93
|
+
if (text === "")
|
|
94
|
+
return;
|
|
95
|
+
try {
|
|
96
|
+
await navigator.clipboard.writeText(text);
|
|
97
|
+
if (signal.aborted)
|
|
98
|
+
return;
|
|
99
|
+
button.dispatchEvent(new CopyEvent(true, text));
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
if (signal.aborted)
|
|
103
|
+
return;
|
|
104
|
+
button.dispatchEvent(new CopyEvent(false, text));
|
|
105
|
+
}
|
|
106
|
+
}),
|
|
107
|
+
],
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-dismiss behavior for an alert or notification host: counts down to
|
|
3
|
+
* dismissal, pausing while the pointer or focus is inside, and answers the
|
|
4
|
+
* `--ui-dismiss` invoker command from any trigger targeting the host. Both
|
|
5
|
+
* paths only dispatch a `ui:dismiss` event, leaving removal to a listener.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import type { ElementProps, MixinFactory } from "remix/ui";
|
|
11
|
+
/** DOM event type dispatched on a host by {@link dismiss} whenever it dismisses, by timeout or by the invoker command. */
|
|
12
|
+
declare const DISMISS_EVENT: "ui:dismiss";
|
|
13
|
+
declare global {
|
|
14
|
+
interface HTMLElementEventMap {
|
|
15
|
+
[DISMISS_EVENT]: DismissEvent;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Types associated with {@link dismiss}: the options it accepts and the
|
|
20
|
+
* reasons it dismisses its host for.
|
|
21
|
+
*/
|
|
22
|
+
export declare namespace Dismiss {
|
|
23
|
+
/** What triggered a dismissal: the auto-dismiss timer running out, or the `--ui-dismiss` invoker command firing. */
|
|
24
|
+
type Reason = "timeout" | "manual";
|
|
25
|
+
/**
|
|
26
|
+
* Configuration accepted by {@link dismiss}.
|
|
27
|
+
*/
|
|
28
|
+
interface Options {
|
|
29
|
+
/**
|
|
30
|
+
* Milliseconds of visible, unpaused time before the host dismisses
|
|
31
|
+
* itself, or `null` to leave the timer disabled so the host only ever
|
|
32
|
+
* dismisses through the `--ui-dismiss` command. Defaults to 5000.
|
|
33
|
+
*/
|
|
34
|
+
duration?: number | null;
|
|
35
|
+
/**
|
|
36
|
+
* Whether the countdown pauses while the pointer hovers the host or
|
|
37
|
+
* focus rests somewhere inside it, resuming from the time left once
|
|
38
|
+
* both leave. Defaults to `true`.
|
|
39
|
+
*/
|
|
40
|
+
pauseOnHover?: boolean;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Dispatched on a host by {@link dismiss} when it dismisses, so a consumer
|
|
45
|
+
* can remove the host from the page or from whatever queue produced it.
|
|
46
|
+
*/
|
|
47
|
+
export declare class DismissEvent extends Event {
|
|
48
|
+
/** What triggered this dismissal. */
|
|
49
|
+
readonly reason: Dismiss.Reason;
|
|
50
|
+
/**
|
|
51
|
+
* @param reason What triggered this dismissal.
|
|
52
|
+
*/
|
|
53
|
+
constructor(reason: Dismiss.Reason);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Adds an auto-dismiss countdown to a host, pausing while the pointer or
|
|
57
|
+
* focus is inside, and answers the `--ui-dismiss` invoker command from any
|
|
58
|
+
* trigger targeting it, dispatching {@link DismissEvent} either way.
|
|
59
|
+
*
|
|
60
|
+
* @param options Duration and hover-pause configuration; see
|
|
61
|
+
* {@link Dismiss.Options}. A call that omits it receives the mixin
|
|
62
|
+
* runtime's trailing current-props argument in its place, which is
|
|
63
|
+
* normalized back to an empty options object.
|
|
64
|
+
* @example
|
|
65
|
+
* <div id="toast-1" role="status" mix={[dismiss({ duration: 4000 })]}>
|
|
66
|
+
* Changes saved
|
|
67
|
+
* <button commandfor="toast-1" command="--ui-dismiss">Close</button>
|
|
68
|
+
* </div>
|
|
69
|
+
* @example
|
|
70
|
+
* // An Alert that only ever dismisses through its own close button.
|
|
71
|
+
* <div id="banner" role="alert" mix={[dismiss({ duration: null })]}>
|
|
72
|
+
* Your session expires soon.
|
|
73
|
+
* <button commandfor="banner" command="--ui-dismiss">Dismiss</button>
|
|
74
|
+
* </div>
|
|
75
|
+
*/
|
|
76
|
+
export declare const dismiss: MixinFactory<HTMLElement, [options?: Dismiss.Options], ElementProps>;
|
|
77
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-dismiss behavior for an alert or notification host: counts down to
|
|
3
|
+
* dismissal, pausing while the pointer or focus is inside, and answers the
|
|
4
|
+
* `--ui-dismiss` invoker command from any trigger targeting the host. Both
|
|
5
|
+
* paths only dispatch a `ui:dismiss` event, leaving removal to a listener.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import { createElement, createMixin, on } from "remix/ui";
|
|
11
|
+
import { asCommandEvent } from "../utils/command-event.js";
|
|
12
|
+
/** Auto-dismiss delay, in milliseconds, {@link dismiss} uses when {@link Dismiss.Options.duration} is omitted. */
|
|
13
|
+
const DEFAULT_DURATION = 5000;
|
|
14
|
+
/** Invoker command {@link dismiss} answers on its host: any `commandfor` trigger button invoking it dismisses the host right away. */
|
|
15
|
+
const DISMISS_COMMAND = "--ui-dismiss";
|
|
16
|
+
/** DOM event type dispatched on a host by {@link dismiss} whenever it dismisses, by timeout or by the invoker command. */
|
|
17
|
+
const DISMISS_EVENT = "ui:dismiss";
|
|
18
|
+
/**
|
|
19
|
+
* Dispatched on a host by {@link dismiss} when it dismisses, so a consumer
|
|
20
|
+
* can remove the host from the page or from whatever queue produced it.
|
|
21
|
+
*/
|
|
22
|
+
export class DismissEvent extends Event {
|
|
23
|
+
/** What triggered this dismissal. */
|
|
24
|
+
reason;
|
|
25
|
+
/**
|
|
26
|
+
* @param reason What triggered this dismissal.
|
|
27
|
+
*/
|
|
28
|
+
constructor(reason) {
|
|
29
|
+
super(DISMISS_EVENT, { bubbles: true, cancelable: true });
|
|
30
|
+
this.reason = reason;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Adds an auto-dismiss countdown to a host, pausing while the pointer or
|
|
35
|
+
* focus is inside, and answers the `--ui-dismiss` invoker command from any
|
|
36
|
+
* trigger targeting it, dispatching {@link DismissEvent} either way.
|
|
37
|
+
*
|
|
38
|
+
* @param options Duration and hover-pause configuration; see
|
|
39
|
+
* {@link Dismiss.Options}. A call that omits it receives the mixin
|
|
40
|
+
* runtime's trailing current-props argument in its place, which is
|
|
41
|
+
* normalized back to an empty options object.
|
|
42
|
+
* @example
|
|
43
|
+
* <div id="toast-1" role="status" mix={[dismiss({ duration: 4000 })]}>
|
|
44
|
+
* Changes saved
|
|
45
|
+
* <button commandfor="toast-1" command="--ui-dismiss">Close</button>
|
|
46
|
+
* </div>
|
|
47
|
+
* @example
|
|
48
|
+
* // An Alert that only ever dismisses through its own close button.
|
|
49
|
+
* <div id="banner" role="alert" mix={[dismiss({ duration: null })]}>
|
|
50
|
+
* Your session expires soon.
|
|
51
|
+
* <button commandfor="banner" command="--ui-dismiss">Dismiss</button>
|
|
52
|
+
* </div>
|
|
53
|
+
*/
|
|
54
|
+
export const dismiss = createMixin((handle) => {
|
|
55
|
+
let hostNode;
|
|
56
|
+
let timer;
|
|
57
|
+
let duration = null;
|
|
58
|
+
let remaining = 0;
|
|
59
|
+
let startedAt = null;
|
|
60
|
+
let pointerInside = false;
|
|
61
|
+
let focusInside = false;
|
|
62
|
+
let initialized = false;
|
|
63
|
+
handle.addEventListener("insert", (event) => {
|
|
64
|
+
hostNode = event.node;
|
|
65
|
+
});
|
|
66
|
+
handle.addEventListener("remove", () => {
|
|
67
|
+
clearTimer();
|
|
68
|
+
hostNode = undefined;
|
|
69
|
+
});
|
|
70
|
+
/** Clears the pending timeout, if any, without touching `startedAt` or `remaining`. */
|
|
71
|
+
function clearTimer() {
|
|
72
|
+
if (timer === undefined)
|
|
73
|
+
return;
|
|
74
|
+
clearTimeout(timer);
|
|
75
|
+
timer = undefined;
|
|
76
|
+
}
|
|
77
|
+
/** Dispatches {@link DismissEvent} on the host for `reason` and clears any pending timer. */
|
|
78
|
+
function fire(reason) {
|
|
79
|
+
clearTimer();
|
|
80
|
+
startedAt = null;
|
|
81
|
+
hostNode?.dispatchEvent(new DismissEvent(reason));
|
|
82
|
+
}
|
|
83
|
+
/** Starts (or restarts) the timeout counting down whatever time is left in `remaining`. */
|
|
84
|
+
function scheduleRemaining() {
|
|
85
|
+
clearTimer();
|
|
86
|
+
startedAt = Date.now();
|
|
87
|
+
timer = setTimeout(() => fire("timeout"), remaining);
|
|
88
|
+
}
|
|
89
|
+
/** Pauses the countdown, recording how much time was left. A no-op when no timer is currently running. */
|
|
90
|
+
function pauseTimer() {
|
|
91
|
+
if (duration === null || startedAt === null)
|
|
92
|
+
return;
|
|
93
|
+
remaining = Math.max(0, remaining - (Date.now() - startedAt));
|
|
94
|
+
clearTimer();
|
|
95
|
+
startedAt = null;
|
|
96
|
+
}
|
|
97
|
+
/** Resumes the countdown from the time left, unless the pointer or focus is still inside the host. */
|
|
98
|
+
function resumeTimer() {
|
|
99
|
+
if (duration === null || startedAt !== null)
|
|
100
|
+
return;
|
|
101
|
+
if (pointerInside || focusInside)
|
|
102
|
+
return;
|
|
103
|
+
scheduleRemaining();
|
|
104
|
+
}
|
|
105
|
+
return (options = {}, props = options) => {
|
|
106
|
+
if (props === options) {
|
|
107
|
+
options = {};
|
|
108
|
+
}
|
|
109
|
+
let pauseOnHover = options.pauseOnHover ?? true;
|
|
110
|
+
if (!initialized) {
|
|
111
|
+
initialized = true;
|
|
112
|
+
duration = options.duration === undefined ? DEFAULT_DURATION : options.duration;
|
|
113
|
+
if (duration !== null) {
|
|
114
|
+
remaining = duration;
|
|
115
|
+
scheduleRemaining();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return createElement(handle.element, {
|
|
119
|
+
mix: [
|
|
120
|
+
on("pointerenter", () => {
|
|
121
|
+
pointerInside = true;
|
|
122
|
+
if (pauseOnHover)
|
|
123
|
+
pauseTimer();
|
|
124
|
+
}),
|
|
125
|
+
on("pointerleave", () => {
|
|
126
|
+
pointerInside = false;
|
|
127
|
+
if (pauseOnHover)
|
|
128
|
+
resumeTimer();
|
|
129
|
+
}),
|
|
130
|
+
on("focusin", () => {
|
|
131
|
+
focusInside = true;
|
|
132
|
+
if (pauseOnHover)
|
|
133
|
+
pauseTimer();
|
|
134
|
+
}),
|
|
135
|
+
on("focusout", (event) => {
|
|
136
|
+
let related = event.relatedTarget;
|
|
137
|
+
if (related instanceof Node && event.currentTarget.contains(related))
|
|
138
|
+
return;
|
|
139
|
+
focusInside = false;
|
|
140
|
+
if (pauseOnHover)
|
|
141
|
+
resumeTimer();
|
|
142
|
+
}),
|
|
143
|
+
on("command", (event) => {
|
|
144
|
+
if (asCommandEvent(event).command !== DISMISS_COMMAND)
|
|
145
|
+
return;
|
|
146
|
+
fire("manual");
|
|
147
|
+
}),
|
|
148
|
+
],
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
});
|