@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,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selection state for list-shaped widgets: a set of selected keys plus the
|
|
3
|
+
* toggle, contiguous-range, and select-all semantics behind row selection.
|
|
4
|
+
* Consumers subscribe to the `"change"` event and render from
|
|
5
|
+
* `selectedKeys`, so selection lives on independently of the rendering.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import { TypedEventTarget } from "remix/ui";
|
|
11
|
+
const DEFAULT_MODE = "multiple";
|
|
12
|
+
/**
|
|
13
|
+
* State model for list, grid, tree, and table row selection. Every mutating
|
|
14
|
+
* method dispatches a plain `"change"` event exactly once, and only when the
|
|
15
|
+
* selected-key set really changes, so a subscriber can re-render blindly.
|
|
16
|
+
*/
|
|
17
|
+
export class SelectionModel extends TypedEventTarget {
|
|
18
|
+
#mode;
|
|
19
|
+
#keys;
|
|
20
|
+
#selected;
|
|
21
|
+
#disabled;
|
|
22
|
+
#anchorKey = null;
|
|
23
|
+
/**
|
|
24
|
+
* @param options - Initial mode, known key order, disabled keys, and
|
|
25
|
+
* pre-selected keys. All are optional and default to an empty,
|
|
26
|
+
* `"multiple"`-mode model with no known keys.
|
|
27
|
+
*/
|
|
28
|
+
constructor(options = {}) {
|
|
29
|
+
super();
|
|
30
|
+
this.#mode = options.mode ?? DEFAULT_MODE;
|
|
31
|
+
this.#keys = options.keys ? Array.from(new Set(options.keys)) : [];
|
|
32
|
+
this.#disabled = new Set(options.disabledKeys ?? []);
|
|
33
|
+
let initial = new Set();
|
|
34
|
+
for (let key of options.selectedKeys ?? []) {
|
|
35
|
+
if (!this.#disabled.has(key))
|
|
36
|
+
initial.add(key);
|
|
37
|
+
}
|
|
38
|
+
this.#selected = normalizeForMode(this.#mode, initial);
|
|
39
|
+
}
|
|
40
|
+
/** Current selection cardinality. */
|
|
41
|
+
get mode() {
|
|
42
|
+
return this.#mode;
|
|
43
|
+
}
|
|
44
|
+
/** Known selectable keys, in the order last provided to the constructor or {@link setKeys}. */
|
|
45
|
+
get keys() {
|
|
46
|
+
return this.#keys;
|
|
47
|
+
}
|
|
48
|
+
/** Currently selected keys. */
|
|
49
|
+
get selectedKeys() {
|
|
50
|
+
return this.#selected;
|
|
51
|
+
}
|
|
52
|
+
/** Keys excluded from selection. */
|
|
53
|
+
get disabledKeys() {
|
|
54
|
+
return this.#disabled;
|
|
55
|
+
}
|
|
56
|
+
/** Key that anchors the next {@link selectRange} call, or `null` before any point interaction. */
|
|
57
|
+
get anchorKey() {
|
|
58
|
+
return this.#anchorKey;
|
|
59
|
+
}
|
|
60
|
+
/** Number of currently selected keys. */
|
|
61
|
+
get size() {
|
|
62
|
+
return this.#selected.size;
|
|
63
|
+
}
|
|
64
|
+
/** `true` when no key is selected. */
|
|
65
|
+
get isEmpty() {
|
|
66
|
+
return this.#selected.size === 0;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* `true` when every non-disabled key in {@link keys} is selected. Always
|
|
70
|
+
* `false` when {@link keys} is empty, since there is nothing to select.
|
|
71
|
+
*/
|
|
72
|
+
get isAll() {
|
|
73
|
+
let selectable = this.#keys.filter((key) => !this.#disabled.has(key));
|
|
74
|
+
return selectable.length > 0 && selectable.every((key) => this.#selected.has(key));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Replaces the selection mode. The current selection clamps to it
|
|
78
|
+
* (`"none"` empties it, `"single"` keeps at most the first selected key),
|
|
79
|
+
* dispatching `"change"` when that clamp shrinks the selected-key set.
|
|
80
|
+
*/
|
|
81
|
+
setMode(mode) {
|
|
82
|
+
if (mode === this.#mode)
|
|
83
|
+
return;
|
|
84
|
+
this.#mode = mode;
|
|
85
|
+
this.#replaceSelection(normalizeForMode(mode, this.#selected));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Replaces the known universe of selectable keys, in order. Selected and
|
|
89
|
+
* anchor keys no longer present in `keys` are dropped, so removing a
|
|
90
|
+
* rendered row also removes it from the selection.
|
|
91
|
+
*/
|
|
92
|
+
setKeys(keys) {
|
|
93
|
+
this.#keys = Array.from(new Set(keys));
|
|
94
|
+
let keySet = new Set(this.#keys);
|
|
95
|
+
let next = new Set();
|
|
96
|
+
for (let key of this.#selected) {
|
|
97
|
+
if (keySet.has(key))
|
|
98
|
+
next.add(key);
|
|
99
|
+
}
|
|
100
|
+
if (this.#anchorKey !== null && !keySet.has(this.#anchorKey))
|
|
101
|
+
this.#anchorKey = null;
|
|
102
|
+
this.#replaceSelection(next);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Replaces the set of keys excluded from selection. Any of those keys
|
|
106
|
+
* currently selected are dropped from the selection.
|
|
107
|
+
*/
|
|
108
|
+
setDisabledKeys(keys) {
|
|
109
|
+
this.#disabled = new Set(keys);
|
|
110
|
+
let next = new Set();
|
|
111
|
+
for (let key of this.#selected) {
|
|
112
|
+
if (!this.#disabled.has(key))
|
|
113
|
+
next.add(key);
|
|
114
|
+
}
|
|
115
|
+
this.#replaceSelection(next);
|
|
116
|
+
}
|
|
117
|
+
/** Reports whether `key` is currently selected. */
|
|
118
|
+
isSelected(key) {
|
|
119
|
+
return this.#selected.has(key);
|
|
120
|
+
}
|
|
121
|
+
/** Reports whether `key` is excluded from selection. */
|
|
122
|
+
isDisabled(key) {
|
|
123
|
+
return this.#disabled.has(key);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Selects `key`, replacing the selection in `"single"` mode or adding to
|
|
127
|
+
* it in `"multiple"` mode. No-op for a disabled key or in `"none"` mode.
|
|
128
|
+
* Sets {@link anchorKey} to `key` on success.
|
|
129
|
+
*/
|
|
130
|
+
select(key) {
|
|
131
|
+
if (this.#mode === "none" || this.#disabled.has(key))
|
|
132
|
+
return;
|
|
133
|
+
this.#anchorKey = key;
|
|
134
|
+
if (this.#mode === "single") {
|
|
135
|
+
this.#replaceSelection(new Set([key]));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (this.#selected.has(key))
|
|
139
|
+
return;
|
|
140
|
+
let next = new Set(this.#selected);
|
|
141
|
+
next.add(key);
|
|
142
|
+
this.#replaceSelection(next);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Removes `key` from the selection, regardless of mode. Sets
|
|
146
|
+
* {@link anchorKey} to `key` when it was selected; no-op otherwise.
|
|
147
|
+
*/
|
|
148
|
+
deselect(key) {
|
|
149
|
+
if (!this.#selected.has(key))
|
|
150
|
+
return;
|
|
151
|
+
this.#anchorKey = key;
|
|
152
|
+
let next = new Set(this.#selected);
|
|
153
|
+
next.delete(key);
|
|
154
|
+
this.#replaceSelection(next);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Flips whether `key` is selected, replacing the selection in `"single"`
|
|
158
|
+
* mode and staying a no-op for a disabled key or in `"none"` mode. Sets
|
|
159
|
+
* {@link anchorKey} on success; a plain click maps to this operation.
|
|
160
|
+
*/
|
|
161
|
+
toggle(key) {
|
|
162
|
+
if (this.#mode === "none" || this.#disabled.has(key))
|
|
163
|
+
return;
|
|
164
|
+
this.#anchorKey = key;
|
|
165
|
+
if (this.#selected.has(key)) {
|
|
166
|
+
let next = new Set(this.#selected);
|
|
167
|
+
next.delete(key);
|
|
168
|
+
this.#replaceSelection(next);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (this.#mode === "single") {
|
|
172
|
+
this.#replaceSelection(new Set([key]));
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
let next = new Set(this.#selected);
|
|
176
|
+
next.add(key);
|
|
177
|
+
this.#replaceSelection(next);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Selects the contiguous span between {@link anchorKey} and `key` in
|
|
181
|
+
* {@link keys} order, skipping disabled keys; the anchor stays put so
|
|
182
|
+
* repeated shift-clicks re-span. Anchorless calls run {@link toggle}.
|
|
183
|
+
*/
|
|
184
|
+
selectRange(key) {
|
|
185
|
+
if (this.#disabled.has(key))
|
|
186
|
+
return;
|
|
187
|
+
if (this.#mode !== "multiple" || this.#anchorKey === null) {
|
|
188
|
+
this.toggle(key);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
let anchorIndex = this.#keys.indexOf(this.#anchorKey);
|
|
192
|
+
let targetIndex = this.#keys.indexOf(key);
|
|
193
|
+
if (anchorIndex === -1 || targetIndex === -1) {
|
|
194
|
+
this.toggle(key);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
let start = Math.min(anchorIndex, targetIndex);
|
|
198
|
+
let end = Math.max(anchorIndex, targetIndex);
|
|
199
|
+
let next = new Set();
|
|
200
|
+
for (let index = start; index <= end; index += 1) {
|
|
201
|
+
let candidate = this.#keys[index];
|
|
202
|
+
if (!this.#disabled.has(candidate))
|
|
203
|
+
next.add(candidate);
|
|
204
|
+
}
|
|
205
|
+
this.#replaceSelection(next);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Selects every non-disabled key in {@link keys}. No-op outside
|
|
209
|
+
* `"multiple"` mode or when {@link keys} is empty.
|
|
210
|
+
*/
|
|
211
|
+
selectAll() {
|
|
212
|
+
if (this.#mode !== "multiple")
|
|
213
|
+
return;
|
|
214
|
+
let next = new Set();
|
|
215
|
+
for (let key of this.#keys) {
|
|
216
|
+
if (!this.#disabled.has(key))
|
|
217
|
+
next.add(key);
|
|
218
|
+
}
|
|
219
|
+
this.#replaceSelection(next);
|
|
220
|
+
}
|
|
221
|
+
/** Deselects every key, regardless of mode. */
|
|
222
|
+
clear() {
|
|
223
|
+
this.#replaceSelection(new Set());
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Swaps in `next` and dispatches `"change"` only when it differs from
|
|
227
|
+
* the current selection, so every public method can call this
|
|
228
|
+
* unconditionally and stay free of no-op guards.
|
|
229
|
+
*/
|
|
230
|
+
#replaceSelection(next) {
|
|
231
|
+
if (setsAreEqual(this.#selected, next))
|
|
232
|
+
return;
|
|
233
|
+
this.#selected = next;
|
|
234
|
+
this.dispatchEvent(new Event("change"));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Clamps `keys` to what `mode` allows: empties it for `"none"`, keeps at
|
|
239
|
+
* most the first key (in iteration order) for `"single"`, and passes
|
|
240
|
+
* `"multiple"` through unchanged.
|
|
241
|
+
*/
|
|
242
|
+
function normalizeForMode(mode, keys) {
|
|
243
|
+
if (mode === "none")
|
|
244
|
+
return new Set();
|
|
245
|
+
if (mode === "single") {
|
|
246
|
+
let [first] = keys;
|
|
247
|
+
return first === undefined ? new Set() : new Set([first]);
|
|
248
|
+
}
|
|
249
|
+
return keys;
|
|
250
|
+
}
|
|
251
|
+
function setsAreEqual(a, b) {
|
|
252
|
+
if (a.size !== b.size)
|
|
253
|
+
return false;
|
|
254
|
+
for (let key of a) {
|
|
255
|
+
if (!b.has(key))
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headless queue of toast notifications a Toast.Region island subscribes to,
|
|
3
|
+
* re-rendering on change. Owns each toast's auto-dismiss timer, restarting it
|
|
4
|
+
* with whatever time was left whenever a paused toast (or the whole queue)
|
|
5
|
+
* resumes.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import { TypedEventTarget } from "remix/ui";
|
|
11
|
+
/**
|
|
12
|
+
* Types associated with {@link Toaster}: the toast shape it queues, its
|
|
13
|
+
* construction and per-toast options, and the events it dispatches.
|
|
14
|
+
*/
|
|
15
|
+
export declare namespace Toaster {
|
|
16
|
+
/** One queued toast. */
|
|
17
|
+
interface Toast<Data = unknown> {
|
|
18
|
+
/**
|
|
19
|
+
* Stable id used to target this toast with {@link Toaster.dismiss},
|
|
20
|
+
* {@link Toaster.update}, {@link Toaster.pause}, and {@link Toaster.resume}.
|
|
21
|
+
*/
|
|
22
|
+
readonly id: string;
|
|
23
|
+
/** Consumer-supplied payload the island renders — copy, variant, action, and any other data the toast needs. */
|
|
24
|
+
readonly data: Data;
|
|
25
|
+
/** Milliseconds until this toast auto-dismisses, or `null` when it only leaves the queue through {@link Toaster.dismiss}. */
|
|
26
|
+
readonly duration: number | null;
|
|
27
|
+
/** `Date.now()` timestamp this toast was queued at. */
|
|
28
|
+
readonly createdAt: number;
|
|
29
|
+
/** `true` while this toast's auto-dismiss timer is paused. */
|
|
30
|
+
readonly paused: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** Options accepted by {@link Toaster.add}. */
|
|
33
|
+
interface AddOptions {
|
|
34
|
+
/** Id to queue the toast under. Defaults to a generated id; reusing an id already queued replaces that toast. */
|
|
35
|
+
id?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Milliseconds until auto-dismiss, or `null` for a toast that only
|
|
38
|
+
* leaves the queue through {@link Toaster.dismiss}. Defaults to the
|
|
39
|
+
* constructor's {@link Toaster.Init.defaultDuration}.
|
|
40
|
+
*/
|
|
41
|
+
duration?: number | null;
|
|
42
|
+
}
|
|
43
|
+
/** Options accepted by {@link Toaster.update}. */
|
|
44
|
+
interface UpdateOptions {
|
|
45
|
+
/**
|
|
46
|
+
* Replacement duration. When provided, restarts the toast's
|
|
47
|
+
* auto-dismiss timer from full, preserving whether the toast is
|
|
48
|
+
* currently paused.
|
|
49
|
+
*/
|
|
50
|
+
duration?: number | null;
|
|
51
|
+
}
|
|
52
|
+
/** Construction options accepted by {@link Toaster}. */
|
|
53
|
+
interface Init {
|
|
54
|
+
/** Auto-dismiss delay, in milliseconds, used when {@link Toaster.AddOptions.duration} is omitted. Defaults to 5000. */
|
|
55
|
+
defaultDuration?: number;
|
|
56
|
+
}
|
|
57
|
+
/** Events dispatched by {@link Toaster}. */
|
|
58
|
+
interface Events {
|
|
59
|
+
/** Dispatched after a toast is added, updated, dismissed (by timeout or by id), paused, resumed, or the queue is cleared. */
|
|
60
|
+
change: Event;
|
|
61
|
+
/** Dispatched after a new toast is added, ahead of `"change"`, so a listener can react to the arrival alone. */
|
|
62
|
+
toast: Event;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Owns a queue of toast notifications and each one's auto-dismiss timer. An
|
|
67
|
+
* island subscribes to `"change"` and calls {@link Toaster.pause} and
|
|
68
|
+
* {@link Toaster.resume} so a toast under the cursor stays readable.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* toaster.addEventListener("change", () => handle.update(), { signal: handle.signal });
|
|
72
|
+
*/
|
|
73
|
+
export declare class Toaster<Data = unknown> extends TypedEventTarget<Toaster.Events> {
|
|
74
|
+
#private;
|
|
75
|
+
/**
|
|
76
|
+
* @param init Construction options; see {@link Toaster.Init}.
|
|
77
|
+
*/
|
|
78
|
+
constructor(init?: Toaster.Init);
|
|
79
|
+
/** Every queued toast, in the order it was added. */
|
|
80
|
+
get toasts(): readonly Toaster.Toast<Data>[];
|
|
81
|
+
/** Number of toasts currently queued. */
|
|
82
|
+
get size(): number;
|
|
83
|
+
/**
|
|
84
|
+
* Looks up one queued toast by id.
|
|
85
|
+
*
|
|
86
|
+
* @param id Id returned by {@link Toaster.add}.
|
|
87
|
+
* @returns The toast, or `undefined` when no toast with that id is queued.
|
|
88
|
+
*/
|
|
89
|
+
get(id: string): Toaster.Toast<Data> | undefined;
|
|
90
|
+
/**
|
|
91
|
+
* Queues a toast and starts its auto-dismiss timer. Reusing an id already
|
|
92
|
+
* queued replaces that toast in place, clearing its previous timer.
|
|
93
|
+
* Dispatches `"toast"` and then `"change"`.
|
|
94
|
+
*
|
|
95
|
+
* @param data Consumer-supplied payload the island renders.
|
|
96
|
+
* @param options Id and duration overrides; see {@link Toaster.AddOptions}.
|
|
97
|
+
* @returns The id the toast was queued under.
|
|
98
|
+
* @example
|
|
99
|
+
* toaster.add({ title: "Saved" }, { duration: 3000 })
|
|
100
|
+
*/
|
|
101
|
+
add(data: Data, options?: Toaster.AddOptions): string;
|
|
102
|
+
/**
|
|
103
|
+
* Patches a queued toast's data in place. Passing `duration` also restarts
|
|
104
|
+
* its timer from full, preserving whether the toast is currently paused.
|
|
105
|
+
*
|
|
106
|
+
* @param id Id returned by {@link Toaster.add}.
|
|
107
|
+
* @param data Replacement payload the island renders.
|
|
108
|
+
* @param options Duration override; see {@link Toaster.UpdateOptions}.
|
|
109
|
+
* @returns `true` when a toast with that id was found and updated, `false`
|
|
110
|
+
* otherwise.
|
|
111
|
+
*/
|
|
112
|
+
update(id: string, data: Data, options?: Toaster.UpdateOptions): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Removes one queued toast by id and clears its timer.
|
|
115
|
+
*
|
|
116
|
+
* @param id Id returned by {@link Toaster.add}.
|
|
117
|
+
* @returns `true` when a toast with that id was found and removed, `false`
|
|
118
|
+
* otherwise.
|
|
119
|
+
*/
|
|
120
|
+
dismiss(id: string): boolean;
|
|
121
|
+
/** Empties the queue and clears every timer, dispatching `"change"` when it held at least one toast. */
|
|
122
|
+
dismissAll(): void;
|
|
123
|
+
/**
|
|
124
|
+
* Pauses the auto-dismiss timer for one toast, or every toast when `id` is
|
|
125
|
+
* omitted, recording how much time was left on each. Dispatches `"change"`
|
|
126
|
+
* only when at least one running timer paused.
|
|
127
|
+
*
|
|
128
|
+
* @param id Toast to pause, or every toast when omitted.
|
|
129
|
+
*/
|
|
130
|
+
pause(id?: string): void;
|
|
131
|
+
/**
|
|
132
|
+
* Resumes the auto-dismiss timer for one toast, or every toast when `id` is
|
|
133
|
+
* omitted, continuing from the time left when it paused. Dispatches
|
|
134
|
+
* `"change"` only when at least one paused timer resumed.
|
|
135
|
+
*
|
|
136
|
+
* @param id Toast to resume, or every toast when omitted.
|
|
137
|
+
*/
|
|
138
|
+
resume(id?: string): void;
|
|
139
|
+
/**
|
|
140
|
+
* Empties the queue and clears every pending timer silently, for an island
|
|
141
|
+
* to call as it unmounts so each countdown ends with it.
|
|
142
|
+
*/
|
|
143
|
+
dispose(): void;
|
|
144
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headless queue of toast notifications a Toast.Region island subscribes to,
|
|
3
|
+
* re-rendering on change. Owns each toast's auto-dismiss timer, restarting it
|
|
4
|
+
* with whatever time was left whenever a paused toast (or the whole queue)
|
|
5
|
+
* resumes.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import { TypedEventTarget } from "remix/ui";
|
|
11
|
+
import { dispatchChange } from "../utils/dispatch-change.js";
|
|
12
|
+
/** Auto-dismiss delay, in milliseconds, a toast uses when {@link Toaster.AddOptions.duration} is omitted. */
|
|
13
|
+
const DEFAULT_DURATION = 5000;
|
|
14
|
+
function toPublicToast(entry) {
|
|
15
|
+
return {
|
|
16
|
+
id: entry.id,
|
|
17
|
+
data: entry.data,
|
|
18
|
+
duration: entry.duration,
|
|
19
|
+
createdAt: entry.createdAt,
|
|
20
|
+
paused: entry.duration !== null && entry.startedAt === null,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Owns a queue of toast notifications and each one's auto-dismiss timer. An
|
|
25
|
+
* island subscribes to `"change"` and calls {@link Toaster.pause} and
|
|
26
|
+
* {@link Toaster.resume} so a toast under the cursor stays readable.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* toaster.addEventListener("change", () => handle.update(), { signal: handle.signal });
|
|
30
|
+
*/
|
|
31
|
+
export class Toaster extends TypedEventTarget {
|
|
32
|
+
#toasts = new Map();
|
|
33
|
+
#defaultDuration;
|
|
34
|
+
/**
|
|
35
|
+
* @param init Construction options; see {@link Toaster.Init}.
|
|
36
|
+
*/
|
|
37
|
+
constructor(init = {}) {
|
|
38
|
+
super();
|
|
39
|
+
this.#defaultDuration = init.defaultDuration ?? DEFAULT_DURATION;
|
|
40
|
+
}
|
|
41
|
+
/** Every queued toast, in the order it was added. */
|
|
42
|
+
get toasts() {
|
|
43
|
+
return Array.from(this.#toasts.values(), toPublicToast);
|
|
44
|
+
}
|
|
45
|
+
/** Number of toasts currently queued. */
|
|
46
|
+
get size() {
|
|
47
|
+
return this.#toasts.size;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Looks up one queued toast by id.
|
|
51
|
+
*
|
|
52
|
+
* @param id Id returned by {@link Toaster.add}.
|
|
53
|
+
* @returns The toast, or `undefined` when no toast with that id is queued.
|
|
54
|
+
*/
|
|
55
|
+
get(id) {
|
|
56
|
+
let entry = this.#toasts.get(id);
|
|
57
|
+
return entry ? toPublicToast(entry) : undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Queues a toast and starts its auto-dismiss timer. Reusing an id already
|
|
61
|
+
* queued replaces that toast in place, clearing its previous timer.
|
|
62
|
+
* Dispatches `"toast"` and then `"change"`.
|
|
63
|
+
*
|
|
64
|
+
* @param data Consumer-supplied payload the island renders.
|
|
65
|
+
* @param options Id and duration overrides; see {@link Toaster.AddOptions}.
|
|
66
|
+
* @returns The id the toast was queued under.
|
|
67
|
+
* @example
|
|
68
|
+
* toaster.add({ title: "Saved" }, { duration: 3000 })
|
|
69
|
+
*/
|
|
70
|
+
add(data, options = {}) {
|
|
71
|
+
let id = options.id ?? crypto.randomUUID();
|
|
72
|
+
let duration = options.duration === undefined ? this.#defaultDuration : options.duration;
|
|
73
|
+
let existing = this.#toasts.get(id);
|
|
74
|
+
if (existing)
|
|
75
|
+
this.#clearTimer(existing);
|
|
76
|
+
let now = Date.now();
|
|
77
|
+
let entry = {
|
|
78
|
+
id,
|
|
79
|
+
data,
|
|
80
|
+
duration,
|
|
81
|
+
createdAt: now,
|
|
82
|
+
remaining: duration ?? 0,
|
|
83
|
+
startedAt: duration === null ? null : now,
|
|
84
|
+
timer: null,
|
|
85
|
+
};
|
|
86
|
+
this.#toasts.set(id, entry);
|
|
87
|
+
if (duration !== null)
|
|
88
|
+
this.#schedule(entry, duration);
|
|
89
|
+
this.dispatchEvent(new Event("toast"));
|
|
90
|
+
dispatchChange(this);
|
|
91
|
+
return id;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Patches a queued toast's data in place. Passing `duration` also restarts
|
|
95
|
+
* its timer from full, preserving whether the toast is currently paused.
|
|
96
|
+
*
|
|
97
|
+
* @param id Id returned by {@link Toaster.add}.
|
|
98
|
+
* @param data Replacement payload the island renders.
|
|
99
|
+
* @param options Duration override; see {@link Toaster.UpdateOptions}.
|
|
100
|
+
* @returns `true` when a toast with that id was found and updated, `false`
|
|
101
|
+
* otherwise.
|
|
102
|
+
*/
|
|
103
|
+
update(id, data, options = {}) {
|
|
104
|
+
let entry = this.#toasts.get(id);
|
|
105
|
+
if (!entry)
|
|
106
|
+
return false;
|
|
107
|
+
entry.data = data;
|
|
108
|
+
if (options.duration !== undefined) {
|
|
109
|
+
let wasRunning = entry.startedAt !== null;
|
|
110
|
+
this.#clearTimer(entry);
|
|
111
|
+
entry.duration = options.duration;
|
|
112
|
+
entry.remaining = entry.duration ?? 0;
|
|
113
|
+
entry.startedAt = null;
|
|
114
|
+
if (entry.duration !== null && wasRunning) {
|
|
115
|
+
entry.startedAt = Date.now();
|
|
116
|
+
this.#schedule(entry, entry.remaining);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
dispatchChange(this);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Removes one queued toast by id and clears its timer.
|
|
124
|
+
*
|
|
125
|
+
* @param id Id returned by {@link Toaster.add}.
|
|
126
|
+
* @returns `true` when a toast with that id was found and removed, `false`
|
|
127
|
+
* otherwise.
|
|
128
|
+
*/
|
|
129
|
+
dismiss(id) {
|
|
130
|
+
let entry = this.#toasts.get(id);
|
|
131
|
+
if (!entry)
|
|
132
|
+
return false;
|
|
133
|
+
this.#clearTimer(entry);
|
|
134
|
+
this.#toasts.delete(id);
|
|
135
|
+
dispatchChange(this);
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
/** Empties the queue and clears every timer, dispatching `"change"` when it held at least one toast. */
|
|
139
|
+
dismissAll() {
|
|
140
|
+
if (this.#toasts.size === 0)
|
|
141
|
+
return;
|
|
142
|
+
for (let entry of this.#toasts.values())
|
|
143
|
+
this.#clearTimer(entry);
|
|
144
|
+
this.#toasts.clear();
|
|
145
|
+
dispatchChange(this);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Pauses the auto-dismiss timer for one toast, or every toast when `id` is
|
|
149
|
+
* omitted, recording how much time was left on each. Dispatches `"change"`
|
|
150
|
+
* only when at least one running timer paused.
|
|
151
|
+
*
|
|
152
|
+
* @param id Toast to pause, or every toast when omitted.
|
|
153
|
+
*/
|
|
154
|
+
pause(id) {
|
|
155
|
+
let changed = false;
|
|
156
|
+
if (id === undefined) {
|
|
157
|
+
for (let entry of this.#toasts.values())
|
|
158
|
+
changed = this.#pauseEntry(entry) || changed;
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
let entry = this.#toasts.get(id);
|
|
162
|
+
if (entry)
|
|
163
|
+
changed = this.#pauseEntry(entry);
|
|
164
|
+
}
|
|
165
|
+
if (changed)
|
|
166
|
+
dispatchChange(this);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Resumes the auto-dismiss timer for one toast, or every toast when `id` is
|
|
170
|
+
* omitted, continuing from the time left when it paused. Dispatches
|
|
171
|
+
* `"change"` only when at least one paused timer resumed.
|
|
172
|
+
*
|
|
173
|
+
* @param id Toast to resume, or every toast when omitted.
|
|
174
|
+
*/
|
|
175
|
+
resume(id) {
|
|
176
|
+
let changed = false;
|
|
177
|
+
if (id === undefined) {
|
|
178
|
+
for (let entry of this.#toasts.values())
|
|
179
|
+
changed = this.#resumeEntry(entry) || changed;
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
let entry = this.#toasts.get(id);
|
|
183
|
+
if (entry)
|
|
184
|
+
changed = this.#resumeEntry(entry);
|
|
185
|
+
}
|
|
186
|
+
if (changed)
|
|
187
|
+
dispatchChange(this);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Empties the queue and clears every pending timer silently, for an island
|
|
191
|
+
* to call as it unmounts so each countdown ends with it.
|
|
192
|
+
*/
|
|
193
|
+
dispose() {
|
|
194
|
+
for (let entry of this.#toasts.values())
|
|
195
|
+
this.#clearTimer(entry);
|
|
196
|
+
this.#toasts.clear();
|
|
197
|
+
}
|
|
198
|
+
/** Pauses one entry's timer, recording the time left. Returns `true` when it was running and is now paused. */
|
|
199
|
+
#pauseEntry(entry) {
|
|
200
|
+
if (entry.duration === null || entry.startedAt === null)
|
|
201
|
+
return false;
|
|
202
|
+
entry.remaining = Math.max(0, entry.remaining - (Date.now() - entry.startedAt));
|
|
203
|
+
this.#clearTimer(entry);
|
|
204
|
+
entry.startedAt = null;
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
/** Resumes one entry's timer from its recorded time left. Returns `true` when it was paused and is now running. */
|
|
208
|
+
#resumeEntry(entry) {
|
|
209
|
+
if (entry.duration === null || entry.startedAt !== null)
|
|
210
|
+
return false;
|
|
211
|
+
entry.startedAt = Date.now();
|
|
212
|
+
this.#schedule(entry, entry.remaining);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
/** Schedules an entry to be removed from the queue, dispatching `"change"`, after `delay` milliseconds. */
|
|
216
|
+
#schedule(entry, delay) {
|
|
217
|
+
entry.timer = setTimeout(() => {
|
|
218
|
+
this.#toasts.delete(entry.id);
|
|
219
|
+
dispatchChange(this);
|
|
220
|
+
}, delay);
|
|
221
|
+
}
|
|
222
|
+
#clearTimer(entry) {
|
|
223
|
+
if (entry.timer === null)
|
|
224
|
+
return;
|
|
225
|
+
clearTimeout(entry.timer);
|
|
226
|
+
entry.timer = null;
|
|
227
|
+
}
|
|
228
|
+
}
|