@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,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headless state model for calendar grids: tracks the keyboard-focused day,
|
|
3
|
+
* the visible month page, and an in-progress range selection (anchor plus
|
|
4
|
+
* hover preview), leaving value storage and rendering to the consumer.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { TypedEventTarget } from "remix/ui";
|
|
10
|
+
import { dispatchChange } from "../utils/dispatch-change.js";
|
|
11
|
+
const DAYS_PER_WEEK = 7;
|
|
12
|
+
/**
|
|
13
|
+
* Owns the ephemeral state around picking a date: the keyboard-focused day,
|
|
14
|
+
* the visible month page, and a range's anchor plus hover preview. The chosen
|
|
15
|
+
* value stays with the consumer, and every state change dispatches `"change"`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* let model = new CalendarModel({ focusedDate: new Date(2026, 0, 15) });
|
|
19
|
+
* model.addEventListener("change", () => update());
|
|
20
|
+
* model.focusNextDay();
|
|
21
|
+
*/
|
|
22
|
+
export class CalendarModel extends TypedEventTarget {
|
|
23
|
+
#focusedDate;
|
|
24
|
+
#visibleMonth;
|
|
25
|
+
#anchorDate = null;
|
|
26
|
+
#previewDate = null;
|
|
27
|
+
#min;
|
|
28
|
+
#max;
|
|
29
|
+
#isDateDisabled;
|
|
30
|
+
/**
|
|
31
|
+
* Builds the model with an initial focused day and optional selectable
|
|
32
|
+
* bounds. The visible month follows from the focused day.
|
|
33
|
+
*
|
|
34
|
+
* @param options Initial focus, selectable bounds, and disabled-day rule.
|
|
35
|
+
*/
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
super();
|
|
38
|
+
this.#min = options.min ? startOfDay(options.min) : undefined;
|
|
39
|
+
this.#max = options.max ? startOfDay(options.max) : undefined;
|
|
40
|
+
this.#isDateDisabled = options.isDateDisabled;
|
|
41
|
+
let focused = clampDate(startOfDay(options.focusedDate ?? new Date()), this.#min, this.#max);
|
|
42
|
+
this.#focusedDate = focused;
|
|
43
|
+
this.#visibleMonth = startOfMonth(focused);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The day that currently carries keyboard/roving-tabindex focus. Always
|
|
47
|
+
* a fresh `Date` instance, so callers may mutate it freely.
|
|
48
|
+
*/
|
|
49
|
+
get focusedDate() {
|
|
50
|
+
return new Date(this.#focusedDate);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The first day of the month page on screen. Follows focus into a new
|
|
54
|
+
* month, and moves on its own when the consumer pages the grid via
|
|
55
|
+
* {@link showMonth}/{@link showNextMonth}/{@link showPreviousMonth}.
|
|
56
|
+
*/
|
|
57
|
+
get visibleMonth() {
|
|
58
|
+
return new Date(this.#visibleMonth);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The first day picked while building a range, or `null` when no range
|
|
62
|
+
* selection is in progress.
|
|
63
|
+
*/
|
|
64
|
+
get anchorDate() {
|
|
65
|
+
return this.#anchorDate ? new Date(this.#anchorDate) : null;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The day currently under hover/focus while a range's second endpoint is
|
|
69
|
+
* being chosen, or `null` when there is no pending range or no day has
|
|
70
|
+
* been previewed yet.
|
|
71
|
+
*/
|
|
72
|
+
get previewDate() {
|
|
73
|
+
return this.#previewDate ? new Date(this.#previewDate) : null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The range implied by the current anchor and preview day, normalized so
|
|
77
|
+
* `start` comes first; `null` when no range selection is in progress.
|
|
78
|
+
* Before a preview day is set it collapses to a single day on the anchor.
|
|
79
|
+
*/
|
|
80
|
+
get previewRange() {
|
|
81
|
+
if (this.#anchorDate === null)
|
|
82
|
+
return null;
|
|
83
|
+
let end = this.#previewDate ?? this.#anchorDate;
|
|
84
|
+
return this.#anchorDate <= end
|
|
85
|
+
? { start: new Date(this.#anchorDate), end: new Date(end) }
|
|
86
|
+
: { start: new Date(end), end: new Date(this.#anchorDate) };
|
|
87
|
+
}
|
|
88
|
+
/** Earliest day the model accepts, or `null` when unbounded. */
|
|
89
|
+
get min() {
|
|
90
|
+
return this.#min ? new Date(this.#min) : null;
|
|
91
|
+
}
|
|
92
|
+
/** Latest day the model accepts, or `null` when unbounded. */
|
|
93
|
+
get max() {
|
|
94
|
+
return this.#max ? new Date(this.#max) : null;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Moves focus to an arbitrary day, clamped to the selectable bounds, and
|
|
98
|
+
* pages the visible month to match. Suits pointer selection of a grid
|
|
99
|
+
* cell; the directional `focus*` methods own the keyboard offset math.
|
|
100
|
+
*
|
|
101
|
+
* @param date Day to focus.
|
|
102
|
+
*/
|
|
103
|
+
focusDate(date) {
|
|
104
|
+
this.#setFocusedDate(date);
|
|
105
|
+
}
|
|
106
|
+
/** Moves focus one day forward (typically the ArrowRight key). */
|
|
107
|
+
focusNextDay() {
|
|
108
|
+
this.#setFocusedDate(addDays(this.#focusedDate, 1));
|
|
109
|
+
}
|
|
110
|
+
/** Moves focus one day back (typically the ArrowLeft key). */
|
|
111
|
+
focusPreviousDay() {
|
|
112
|
+
this.#setFocusedDate(addDays(this.#focusedDate, -1));
|
|
113
|
+
}
|
|
114
|
+
/** Moves focus one week forward (typically the ArrowDown key). */
|
|
115
|
+
focusNextWeek() {
|
|
116
|
+
this.#setFocusedDate(addDays(this.#focusedDate, DAYS_PER_WEEK));
|
|
117
|
+
}
|
|
118
|
+
/** Moves focus one week back (typically the ArrowUp key). */
|
|
119
|
+
focusPreviousWeek() {
|
|
120
|
+
this.#setFocusedDate(addDays(this.#focusedDate, -DAYS_PER_WEEK));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Moves focus one month forward, clamping the day of month to the
|
|
124
|
+
* target month's last day when it's shorter (typically the PageDown
|
|
125
|
+
* key).
|
|
126
|
+
*/
|
|
127
|
+
focusNextMonth() {
|
|
128
|
+
this.#setFocusedDate(addMonths(this.#focusedDate, 1));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Moves focus one month back, clamping the day of month to the target
|
|
132
|
+
* month's last day when it's shorter (typically the PageUp key).
|
|
133
|
+
*/
|
|
134
|
+
focusPreviousMonth() {
|
|
135
|
+
this.#setFocusedDate(addMonths(this.#focusedDate, -1));
|
|
136
|
+
}
|
|
137
|
+
/** Moves focus to the first day of the focused month (typically the Home key). */
|
|
138
|
+
focusMonthStart() {
|
|
139
|
+
this.#setFocusedDate(startOfMonth(this.#focusedDate));
|
|
140
|
+
}
|
|
141
|
+
/** Moves focus to the last day of the focused month (typically the End key). */
|
|
142
|
+
focusMonthEnd() {
|
|
143
|
+
this.#setFocusedDate(endOfMonth(this.#focusedDate));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Pages the visible month to the month containing `date`, leaving focus
|
|
147
|
+
* where it is — the focused day scrolls out of the rendered grid until
|
|
148
|
+
* focus moves again. Suits a header's month/year picker.
|
|
149
|
+
*
|
|
150
|
+
* @param date Any day within the month to display.
|
|
151
|
+
*/
|
|
152
|
+
showMonth(date) {
|
|
153
|
+
let next = startOfMonth(date);
|
|
154
|
+
if (isSameDay(next, this.#visibleMonth))
|
|
155
|
+
return;
|
|
156
|
+
this.#visibleMonth = next;
|
|
157
|
+
dispatchChange(this);
|
|
158
|
+
}
|
|
159
|
+
/** Pages the visible month forward by one, leaving focus where it is. */
|
|
160
|
+
showNextMonth() {
|
|
161
|
+
this.showMonth(addMonths(this.#visibleMonth, 1));
|
|
162
|
+
}
|
|
163
|
+
/** Pages the visible month back by one, leaving focus where it is. */
|
|
164
|
+
showPreviousMonth() {
|
|
165
|
+
this.showMonth(addMonths(this.#visibleMonth, -1));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Starts a range selection at `date`, clearing any preview from a
|
|
169
|
+
* previous attempt. Call this when the user picks the first endpoint
|
|
170
|
+
* (e.g. the first click in a range calendar).
|
|
171
|
+
*
|
|
172
|
+
* @param date Day to anchor the range at.
|
|
173
|
+
*/
|
|
174
|
+
beginRange(date) {
|
|
175
|
+
this.#anchorDate = clampDate(startOfDay(date), this.#min, this.#max);
|
|
176
|
+
this.#previewDate = null;
|
|
177
|
+
dispatchChange(this);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Updates the pending second endpoint so {@link previewRange} tracks the
|
|
181
|
+
* day under hover or focus. A no-op while no range is in progress, so
|
|
182
|
+
* hover handlers can call it unconditionally.
|
|
183
|
+
*
|
|
184
|
+
* @param date Day currently under hover/focus.
|
|
185
|
+
*/
|
|
186
|
+
updateRangePreview(date) {
|
|
187
|
+
if (this.#anchorDate === null)
|
|
188
|
+
return;
|
|
189
|
+
this.#previewDate = clampDate(startOfDay(date), this.#min, this.#max);
|
|
190
|
+
dispatchChange(this);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Commits the range from the current anchor to `date` and clears the
|
|
194
|
+
* pending selection, returning it normalized. With no range in progress
|
|
195
|
+
* the consumer's selected value stays untouched.
|
|
196
|
+
*
|
|
197
|
+
* @param date Day chosen as the second endpoint.
|
|
198
|
+
* @returns The committed range, or `null` when there was no anchor.
|
|
199
|
+
*/
|
|
200
|
+
completeRange(date) {
|
|
201
|
+
if (this.#anchorDate === null)
|
|
202
|
+
return null;
|
|
203
|
+
let end = clampDate(startOfDay(date), this.#min, this.#max);
|
|
204
|
+
let range = this.#anchorDate <= end
|
|
205
|
+
? { start: new Date(this.#anchorDate), end: new Date(end) }
|
|
206
|
+
: { start: new Date(end), end: new Date(this.#anchorDate) };
|
|
207
|
+
this.#anchorDate = null;
|
|
208
|
+
this.#previewDate = null;
|
|
209
|
+
dispatchChange(this);
|
|
210
|
+
return range;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Abandons an in-progress range selection (e.g. on Escape or blur),
|
|
214
|
+
* leaving the consumer's selected value untouched. A no-op when no range
|
|
215
|
+
* is in progress.
|
|
216
|
+
*/
|
|
217
|
+
cancelRange() {
|
|
218
|
+
if (this.#anchorDate === null && this.#previewDate === null)
|
|
219
|
+
return;
|
|
220
|
+
this.#anchorDate = null;
|
|
221
|
+
this.#previewDate = null;
|
|
222
|
+
dispatchChange(this);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Whether `date` is the day currently carrying keyboard focus.
|
|
226
|
+
*
|
|
227
|
+
* @param date Day to test.
|
|
228
|
+
*/
|
|
229
|
+
isFocused(date) {
|
|
230
|
+
return isSameDay(this.#focusedDate, date);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Whether `date` is the anchor of an in-progress range selection.
|
|
234
|
+
*
|
|
235
|
+
* @param date Day to test.
|
|
236
|
+
*/
|
|
237
|
+
isRangeAnchor(date) {
|
|
238
|
+
return this.#anchorDate !== null && isSameDay(this.#anchorDate, date);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Whether `date` falls within the current {@link previewRange},
|
|
242
|
+
* inclusive of both endpoints. Always `false` when no range is in
|
|
243
|
+
* progress.
|
|
244
|
+
*
|
|
245
|
+
* @param date Day to test.
|
|
246
|
+
*/
|
|
247
|
+
isInPreviewRange(date) {
|
|
248
|
+
let range = this.previewRange;
|
|
249
|
+
if (range === null)
|
|
250
|
+
return false;
|
|
251
|
+
let day = startOfDay(date);
|
|
252
|
+
return day >= range.start && day <= range.end;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Whether `date` falls in the same month as {@link visibleMonth}. Useful
|
|
256
|
+
* for dimming the leading/trailing days a month grid renders from
|
|
257
|
+
* adjacent months.
|
|
258
|
+
*
|
|
259
|
+
* @param date Day to test.
|
|
260
|
+
*/
|
|
261
|
+
isInVisibleMonth(date) {
|
|
262
|
+
return (date.getFullYear() === this.#visibleMonth.getFullYear() &&
|
|
263
|
+
date.getMonth() === this.#visibleMonth.getMonth());
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Whether `date` falls outside the selectable bounds — before
|
|
267
|
+
* {@link min}, after {@link max}, or rejected by the `isDateDisabled`
|
|
268
|
+
* predicate.
|
|
269
|
+
*
|
|
270
|
+
* @param date Day to test.
|
|
271
|
+
*/
|
|
272
|
+
isDisabled(date) {
|
|
273
|
+
let day = startOfDay(date);
|
|
274
|
+
if (this.#min && day < this.#min)
|
|
275
|
+
return true;
|
|
276
|
+
if (this.#max && day > this.#max)
|
|
277
|
+
return true;
|
|
278
|
+
return this.#isDateDisabled?.(day) ?? false;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Shared by every `focus*` method: clamps to the selectable bounds, pages
|
|
282
|
+
* the visible month to match, and dispatches `"change"` only when the
|
|
283
|
+
* focused day actually moves.
|
|
284
|
+
*/
|
|
285
|
+
#setFocusedDate(date) {
|
|
286
|
+
let next = clampDate(startOfDay(date), this.#min, this.#max);
|
|
287
|
+
if (isSameDay(next, this.#focusedDate))
|
|
288
|
+
return;
|
|
289
|
+
this.#focusedDate = next;
|
|
290
|
+
this.#visibleMonth = startOfMonth(next);
|
|
291
|
+
dispatchChange(this);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Reports whether two dates fall on the same calendar day, ignoring
|
|
296
|
+
* time-of-day.
|
|
297
|
+
*
|
|
298
|
+
* @param a First date to compare.
|
|
299
|
+
* @param b Second date to compare.
|
|
300
|
+
* @returns `true` when both dates share the same year, month, and day.
|
|
301
|
+
*/
|
|
302
|
+
function isSameDay(a, b) {
|
|
303
|
+
return (a.getFullYear() === b.getFullYear() &&
|
|
304
|
+
a.getMonth() === b.getMonth() &&
|
|
305
|
+
a.getDate() === b.getDate());
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Normalizes a date to midnight local time, discarding its time-of-day
|
|
309
|
+
* component so day-level comparisons (`<`, `>`, equality) behave correctly
|
|
310
|
+
* regardless of what time the input carried.
|
|
311
|
+
*
|
|
312
|
+
* @param date Date to normalize.
|
|
313
|
+
* @returns A new `Date` at midnight on the same calendar day.
|
|
314
|
+
*/
|
|
315
|
+
function startOfDay(date) {
|
|
316
|
+
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
|
|
317
|
+
}
|
|
318
|
+
function startOfMonth(date) {
|
|
319
|
+
return new Date(date.getFullYear(), date.getMonth(), 1);
|
|
320
|
+
}
|
|
321
|
+
function endOfMonth(date) {
|
|
322
|
+
return new Date(date.getFullYear(), date.getMonth() + 1, 0);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Counts the days in a given year/month, accounting for leap years.
|
|
326
|
+
*
|
|
327
|
+
* @param year Full year (e.g. `2026`).
|
|
328
|
+
* @param month Zero-based month index (`0` for January).
|
|
329
|
+
* @returns The number of days in that month.
|
|
330
|
+
*/
|
|
331
|
+
function daysInMonth(year, month) {
|
|
332
|
+
return new Date(year, month + 1, 0).getDate();
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Adds a whole number of days to a date. Safe across month and year
|
|
336
|
+
* boundaries: the `Date` constructor rolls an overflowing day count into
|
|
337
|
+
* the neighbouring month.
|
|
338
|
+
*
|
|
339
|
+
* @param date Date to offset.
|
|
340
|
+
* @param amount Number of days to add; negative moves backward.
|
|
341
|
+
* @returns A new `Date` offset by `amount` days.
|
|
342
|
+
*/
|
|
343
|
+
function addDays(date, amount) {
|
|
344
|
+
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + amount);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Adds a whole number of months to a date, clamping the day of month to the
|
|
348
|
+
* target month's last day when the target month is shorter (January 31 plus
|
|
349
|
+
* one month lands on February 28).
|
|
350
|
+
*
|
|
351
|
+
* @param date Date to offset.
|
|
352
|
+
* @param amount Number of months to add; negative moves backward.
|
|
353
|
+
* @returns A new `Date` offset by `amount` months.
|
|
354
|
+
*/
|
|
355
|
+
function addMonths(date, amount) {
|
|
356
|
+
let year = date.getFullYear();
|
|
357
|
+
let month = date.getMonth() + amount;
|
|
358
|
+
let day = Math.min(date.getDate(), daysInMonth(year, month));
|
|
359
|
+
return new Date(year, month, day);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Constrains a date to an optional `[min, max]` bound, leaving it untouched
|
|
363
|
+
* when it already falls inside the bound (or when a bound is omitted).
|
|
364
|
+
*
|
|
365
|
+
* @param date Date to constrain.
|
|
366
|
+
* @param min Earliest allowed date, or `undefined` for no lower bound.
|
|
367
|
+
* @param max Latest allowed date, or `undefined` for no upper bound.
|
|
368
|
+
* @returns `date`, or the nearer bound when `date` falls outside it.
|
|
369
|
+
*/
|
|
370
|
+
function clampDate(date, min, max) {
|
|
371
|
+
if (min && date < min)
|
|
372
|
+
return new Date(min);
|
|
373
|
+
if (max && date > max)
|
|
374
|
+
return new Date(max);
|
|
375
|
+
return date;
|
|
376
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headless drag-and-drop session: owns the item being dragged, the drop
|
|
3
|
+
* candidate under the pointer, and the position of the pending drop computed
|
|
4
|
+
* against it. List reordering, drop zones, and drop indicators read one
|
|
5
|
+
* shared instance, independent of pointer input and of rendering.
|
|
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 DragSession}: the dragged item, the drop
|
|
13
|
+
* candidate it tracks, and the event it dispatches.
|
|
14
|
+
*/
|
|
15
|
+
export declare namespace DragSession {
|
|
16
|
+
/**
|
|
17
|
+
* Position of a pending drop relative to its current target. `"before"` and
|
|
18
|
+
* `"after"` insert the dragged item next to the target for reordering;
|
|
19
|
+
* `"on"` drops onto the target itself, nesting into it or filling a drop zone.
|
|
20
|
+
*/
|
|
21
|
+
type Position = "before" | "after" | "on";
|
|
22
|
+
/**
|
|
23
|
+
* Item that starts a drag session: a stable key identifying it, plus an
|
|
24
|
+
* optional consumer-defined payload carried for the life of the session
|
|
25
|
+
* and read back when the drop commits.
|
|
26
|
+
*
|
|
27
|
+
* @template TData Shape of the optional payload.
|
|
28
|
+
*/
|
|
29
|
+
interface Source<TData = unknown> {
|
|
30
|
+
/** Stable identifier for the dragged item. */
|
|
31
|
+
key: string;
|
|
32
|
+
/** Consumer-defined payload carried for the life of the session. */
|
|
33
|
+
data?: TData;
|
|
34
|
+
}
|
|
35
|
+
/** Drop candidate currently under the pointer. */
|
|
36
|
+
interface Target {
|
|
37
|
+
/** Stable identifier for the candidate drop target. */
|
|
38
|
+
key: string;
|
|
39
|
+
/** Position of the pending drop relative to this target. */
|
|
40
|
+
position: Position;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Snapshot returned when a drag session ends in a committed drop.
|
|
44
|
+
*
|
|
45
|
+
* @template TData Shape of the source's optional payload.
|
|
46
|
+
*/
|
|
47
|
+
interface DropDetail<TData = unknown> {
|
|
48
|
+
/** Item that was dragged. */
|
|
49
|
+
source: Source<TData>;
|
|
50
|
+
/** Target the item was dropped on. */
|
|
51
|
+
target: Target;
|
|
52
|
+
}
|
|
53
|
+
/** Events dispatched by {@link DragSession} as its state changes. */
|
|
54
|
+
interface EventMap {
|
|
55
|
+
/** Dispatched after the drag source, the current target, or its position changes. */
|
|
56
|
+
change: Event;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Owns one drag-and-drop interaction and dispatches a plain `"change"` event
|
|
61
|
+
* from every mutating method, so a pointer- or keyboard-driven mixin
|
|
62
|
+
* subscribes once and keeps every view it drives in sync from this state.
|
|
63
|
+
*
|
|
64
|
+
* @template TData Shape of the optional payload carried by the drag source.
|
|
65
|
+
* @example
|
|
66
|
+
* let session = new DragSession<{ index: number }>();
|
|
67
|
+
* session.addEventListener("change", () => update());
|
|
68
|
+
* session.start({ key: "row-1", data: { index: 0 } });
|
|
69
|
+
* session.moveOver({ key: "row-3", position: "after" });
|
|
70
|
+
* session.drop();
|
|
71
|
+
*/
|
|
72
|
+
export declare class DragSession<TData = unknown> extends TypedEventTarget<DragSession.EventMap> {
|
|
73
|
+
#private;
|
|
74
|
+
/** Item currently being dragged, or `null` when no session is active. */
|
|
75
|
+
get source(): DragSession.Source<TData> | null;
|
|
76
|
+
/**
|
|
77
|
+
* Drop candidate currently under the pointer; `null` while the session is
|
|
78
|
+
* idle or the pointer sits away from every valid target.
|
|
79
|
+
*/
|
|
80
|
+
get target(): DragSession.Target | null;
|
|
81
|
+
/** Whether a drag session is currently in progress. */
|
|
82
|
+
get active(): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Begins a drag session for the given item, implicitly ending any
|
|
85
|
+
* session already in progress first. Always dispatches `"change"`.
|
|
86
|
+
*
|
|
87
|
+
* @param source Item that begins the drag session.
|
|
88
|
+
*/
|
|
89
|
+
start(source: DragSession.Source<TData>): void;
|
|
90
|
+
/**
|
|
91
|
+
* Records the drop candidate currently under the pointer. A no-op outside
|
|
92
|
+
* an active session and when key and position match the last call, so a
|
|
93
|
+
* drop indicator repositions only on actual movement.
|
|
94
|
+
*
|
|
95
|
+
* @param target Drop candidate under the pointer.
|
|
96
|
+
*/
|
|
97
|
+
moveOver(target: DragSession.Target): void;
|
|
98
|
+
/**
|
|
99
|
+
* Clears the current drop candidate and keeps the session running, e.g.
|
|
100
|
+
* when the pointer moves past every valid target mid-drag. A no-op while
|
|
101
|
+
* the target is already clear.
|
|
102
|
+
*/
|
|
103
|
+
clearTarget(): void;
|
|
104
|
+
/**
|
|
105
|
+
* Commits a drop at the current target and ends the session. Returns
|
|
106
|
+
* `null` and leaves the state untouched while the session is idle or the
|
|
107
|
+
* target is unset.
|
|
108
|
+
*
|
|
109
|
+
* @returns The dragged item and the target it was dropped on, or `null`
|
|
110
|
+
* when there was nothing to drop.
|
|
111
|
+
*/
|
|
112
|
+
drop(): DragSession.DropDetail<TData> | null;
|
|
113
|
+
/**
|
|
114
|
+
* Ends the current session and discards the pending drop, e.g. on Escape
|
|
115
|
+
* or a pointer release away from every valid target. A no-op while the
|
|
116
|
+
* session is idle.
|
|
117
|
+
*/
|
|
118
|
+
cancel(): void;
|
|
119
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headless drag-and-drop session: owns the item being dragged, the drop
|
|
3
|
+
* candidate under the pointer, and the position of the pending drop computed
|
|
4
|
+
* against it. List reordering, drop zones, and drop indicators read one
|
|
5
|
+
* shared instance, independent of pointer input and of rendering.
|
|
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
|
+
/**
|
|
13
|
+
* Owns one drag-and-drop interaction and dispatches a plain `"change"` event
|
|
14
|
+
* from every mutating method, so a pointer- or keyboard-driven mixin
|
|
15
|
+
* subscribes once and keeps every view it drives in sync from this state.
|
|
16
|
+
*
|
|
17
|
+
* @template TData Shape of the optional payload carried by the drag source.
|
|
18
|
+
* @example
|
|
19
|
+
* let session = new DragSession<{ index: number }>();
|
|
20
|
+
* session.addEventListener("change", () => update());
|
|
21
|
+
* session.start({ key: "row-1", data: { index: 0 } });
|
|
22
|
+
* session.moveOver({ key: "row-3", position: "after" });
|
|
23
|
+
* session.drop();
|
|
24
|
+
*/
|
|
25
|
+
export class DragSession extends TypedEventTarget {
|
|
26
|
+
#source = null;
|
|
27
|
+
#target = null;
|
|
28
|
+
/** Item currently being dragged, or `null` when no session is active. */
|
|
29
|
+
get source() {
|
|
30
|
+
return this.#source;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Drop candidate currently under the pointer; `null` while the session is
|
|
34
|
+
* idle or the pointer sits away from every valid target.
|
|
35
|
+
*/
|
|
36
|
+
get target() {
|
|
37
|
+
return this.#target;
|
|
38
|
+
}
|
|
39
|
+
/** Whether a drag session is currently in progress. */
|
|
40
|
+
get active() {
|
|
41
|
+
return this.#source !== null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Begins a drag session for the given item, implicitly ending any
|
|
45
|
+
* session already in progress first. Always dispatches `"change"`.
|
|
46
|
+
*
|
|
47
|
+
* @param source Item that begins the drag session.
|
|
48
|
+
*/
|
|
49
|
+
start(source) {
|
|
50
|
+
this.#source = source;
|
|
51
|
+
this.#target = null;
|
|
52
|
+
dispatchChange(this);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Records the drop candidate currently under the pointer. A no-op outside
|
|
56
|
+
* an active session and when key and position match the last call, so a
|
|
57
|
+
* drop indicator repositions only on actual movement.
|
|
58
|
+
*
|
|
59
|
+
* @param target Drop candidate under the pointer.
|
|
60
|
+
*/
|
|
61
|
+
moveOver(target) {
|
|
62
|
+
if (!this.active)
|
|
63
|
+
return;
|
|
64
|
+
if (this.#target?.key === target.key && this.#target?.position === target.position)
|
|
65
|
+
return;
|
|
66
|
+
this.#target = target;
|
|
67
|
+
dispatchChange(this);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Clears the current drop candidate and keeps the session running, e.g.
|
|
71
|
+
* when the pointer moves past every valid target mid-drag. A no-op while
|
|
72
|
+
* the target is already clear.
|
|
73
|
+
*/
|
|
74
|
+
clearTarget() {
|
|
75
|
+
if (this.#target === null)
|
|
76
|
+
return;
|
|
77
|
+
this.#target = null;
|
|
78
|
+
dispatchChange(this);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Commits a drop at the current target and ends the session. Returns
|
|
82
|
+
* `null` and leaves the state untouched while the session is idle or the
|
|
83
|
+
* target is unset.
|
|
84
|
+
*
|
|
85
|
+
* @returns The dragged item and the target it was dropped on, or `null`
|
|
86
|
+
* when there was nothing to drop.
|
|
87
|
+
*/
|
|
88
|
+
drop() {
|
|
89
|
+
if (!this.#source || !this.#target)
|
|
90
|
+
return null;
|
|
91
|
+
let detail = { source: this.#source, target: this.#target };
|
|
92
|
+
this.#reset();
|
|
93
|
+
return detail;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Ends the current session and discards the pending drop, e.g. on Escape
|
|
97
|
+
* or a pointer release away from every valid target. A no-op while the
|
|
98
|
+
* session is idle.
|
|
99
|
+
*/
|
|
100
|
+
cancel() {
|
|
101
|
+
if (!this.active)
|
|
102
|
+
return;
|
|
103
|
+
this.#reset();
|
|
104
|
+
}
|
|
105
|
+
#reset() {
|
|
106
|
+
this.#source = null;
|
|
107
|
+
this.#target = null;
|
|
108
|
+
dispatchChange(this);
|
|
109
|
+
}
|
|
110
|
+
}
|