@sdxc/ui 2026.11.11 → 2026.11.17
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/README.md +114 -61
- package/dist/app/app-shell-toggle.d.ts +2 -0
- package/dist/app/app-shell-toggle.js +5 -2
- package/dist/app/app-shell.d.ts +25 -1
- package/dist/app/app-shell.js +73 -38
- package/dist/app/auto-refresh.d.ts +2 -0
- package/dist/app/auto-refresh.js +3 -3
- package/dist/app/behaviors/refresh-schedule.d.ts +8 -3
- package/dist/app/behaviors/refresh-schedule.js +4 -3
- package/dist/app/board.d.ts +123 -0
- package/dist/app/board.js +224 -0
- package/dist/app/chart.d.ts +2 -2
- package/dist/app/chart.js +7 -3
- package/dist/app/check-runs.d.ts +116 -0
- package/dist/app/check-runs.js +201 -0
- package/dist/app/code-view.d.ts +172 -0
- package/dist/app/code-view.js +414 -0
- package/dist/app/command-palette.d.ts +38 -3
- package/dist/app/command-palette.js +53 -5
- package/dist/app/contribution-graph.d.ts +106 -0
- package/dist/app/contribution-graph.js +289 -0
- package/dist/app/detail-sheet.d.ts +7 -0
- package/dist/app/detail-sheet.js +7 -7
- package/dist/app/diff.d.ts +200 -0
- package/dist/app/diff.js +702 -0
- package/dist/app/filter-bar.d.ts +20 -0
- package/dist/app/filter-bar.js +35 -15
- package/dist/app/filter-menu.d.ts +144 -0
- package/dist/app/filter-menu.js +480 -0
- package/dist/app/gantt.d.ts +108 -0
- package/dist/app/gantt.js +420 -0
- package/dist/app/index.d.ts +20 -0
- package/dist/app/index.js +20 -0
- package/dist/app/log-viewer.d.ts +120 -0
- package/dist/app/log-viewer.js +384 -0
- package/dist/app/merge-box.d.ts +97 -0
- package/dist/app/merge-box.js +227 -0
- package/dist/app/mixins/app-shell-controls.d.ts +4 -1
- package/dist/app/mixins/app-shell-controls.js +64 -4
- package/dist/app/mixins/auto-refresh.d.ts +5 -2
- package/dist/app/mixins/auto-refresh.js +9 -3
- package/dist/app/mixins/close-popover-on-submit.d.ts +16 -0
- package/dist/app/mixins/close-popover-on-submit.js +32 -0
- package/dist/app/mixins/code-view-selection.d.ts +60 -0
- package/dist/app/mixins/code-view-selection.js +276 -0
- package/dist/app/mixins/command-palette.d.ts +15 -4
- package/dist/app/mixins/command-palette.js +94 -6
- package/dist/app/mixins/contribution-graph-keys.d.ts +19 -0
- package/dist/app/mixins/contribution-graph-keys.js +155 -0
- package/dist/app/mixins/diff-interactions.d.ts +72 -0
- package/dist/app/mixins/diff-interactions.js +269 -0
- package/dist/app/mixins/filter-menu-keys.d.ts +41 -0
- package/dist/app/mixins/filter-menu-keys.js +91 -0
- package/dist/app/mixins/gantt-today.d.ts +17 -0
- package/dist/app/mixins/gantt-today.js +50 -0
- package/dist/app/mixins/log-viewer.d.ts +29 -0
- package/dist/app/mixins/log-viewer.js +347 -0
- package/dist/app/mixins/optimistic-toggle-sync.d.ts +5 -0
- package/dist/app/mixins/optimistic-toggle-sync.js +25 -1
- package/dist/app/mixins/save-bar-sync.d.ts +7 -1
- package/dist/app/mixins/save-bar-sync.js +23 -4
- package/dist/app/mixins/save-status-sync.js +23 -1
- package/dist/app/mixins/split-view.js +1 -1
- package/dist/app/mixins/submit-on-close.d.ts +17 -0
- package/dist/app/mixins/submit-on-close.js +52 -0
- package/dist/app/mixins/tree-filter.d.ts +44 -0
- package/dist/app/mixins/tree-filter.js +111 -0
- package/dist/app/mixins/tree-navigation.d.ts +50 -0
- package/dist/app/mixins/tree-navigation.js +216 -0
- package/dist/app/optimistic-toggle.d.ts +8 -0
- package/dist/app/optimistic-toggle.js +3 -2
- package/dist/app/page-header.d.ts +18 -3
- package/dist/app/page-header.js +105 -50
- package/dist/app/profile-picker.d.ts +47 -0
- package/dist/app/profile-picker.js +104 -0
- package/dist/app/review-thread.d.ts +115 -0
- package/dist/app/review-thread.js +116 -0
- package/dist/app/save-bar.d.ts +5 -0
- package/dist/app/save-bar.js +1 -1
- package/dist/app/save-status.js +1 -1
- package/dist/app/settings-layout.d.ts +5 -0
- package/dist/app/settings-layout.js +5 -5
- package/dist/app/skeleton.d.ts +9 -0
- package/dist/app/skeleton.js +28 -0
- package/dist/app/split-view.d.ts +4 -0
- package/dist/app/split-view.js +3 -3
- package/dist/app/styles/code-tokens.d.ts +24 -0
- package/dist/app/styles/code-tokens.js +47 -0
- package/dist/app/tab-bar.d.ts +59 -0
- package/dist/app/tab-bar.js +111 -0
- package/dist/app/tree.d.ts +111 -14
- package/dist/app/tree.js +202 -15
- package/dist/app/utils/ansi.d.ts +55 -0
- package/dist/app/utils/ansi.js +157 -0
- package/dist/app/utils/diff-rows.d.ts +82 -0
- package/dist/app/utils/diff-rows.js +209 -0
- package/dist/app/utils/fuzzy-path.d.ts +58 -0
- package/dist/app/utils/fuzzy-path.js +202 -0
- package/dist/app/utils/line-range.d.ts +48 -0
- package/dist/app/utils/line-range.js +61 -0
- package/dist/app/utils/log-lines.d.ts +107 -0
- package/dist/app/utils/log-lines.js +170 -0
- package/dist/app/utils/tree-filter.d.ts +19 -0
- package/dist/app/utils/tree-filter.js +26 -0
- package/dist/app/view-options.d.ts +2 -0
- package/dist/app/view-options.js +2 -2
- package/dist/chat/activity.d.ts +72 -0
- package/dist/chat/activity.js +184 -0
- package/dist/chat/composer-submit.d.ts +38 -0
- package/dist/chat/composer-submit.js +44 -0
- package/dist/chat/composer.d.ts +47 -0
- package/dist/chat/composer.js +100 -0
- package/dist/chat/index.d.ts +6 -0
- package/dist/chat/index.js +6 -0
- package/dist/chat/mixins/composer-send.d.ts +32 -0
- package/dist/chat/mixins/composer-send.js +121 -0
- package/dist/chat/mixins/reveal-stream.d.ts +68 -0
- package/dist/chat/mixins/reveal-stream.js +184 -0
- package/dist/chat/mixins/stream-target.d.ts +18 -0
- package/dist/chat/mixins/stream-target.js +38 -0
- package/dist/components/button.js +7 -1
- package/dist/components/character-count.d.ts +44 -0
- package/dist/components/character-count.js +68 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.js +2 -0
- package/dist/components/label-chip.d.ts +42 -0
- package/dist/components/label-chip.js +86 -0
- package/dist/components/property-list.d.ts +9 -2
- package/dist/components/property-list.js +37 -10
- package/dist/components/textarea.js +6 -1
- package/dist/components/timeline.d.ts +62 -5
- package/dist/components/timeline.js +124 -5
- package/dist/docs/code-block.js +8 -31
- package/dist/docs/file-tree.js +3 -48
- package/dist/landing/case-study.d.ts +79 -0
- package/dist/landing/case-study.js +162 -0
- package/dist/landing/contributor-wall.d.ts +72 -0
- package/dist/landing/contributor-wall.js +129 -0
- package/dist/landing/countdown.d.ts +88 -0
- package/dist/landing/countdown.js +91 -0
- package/dist/landing/episode-player.d.ts +87 -0
- package/dist/landing/episode-player.js +113 -0
- package/dist/landing/faq-band.d.ts +3 -2
- package/dist/landing/faq-band.js +4 -3
- package/dist/landing/index.d.ts +7 -0
- package/dist/landing/index.js +7 -0
- package/dist/landing/landing-header.js +15 -4
- package/dist/landing/mixins/countdown-tick.d.ts +29 -0
- package/dist/landing/mixins/countdown-tick.js +65 -0
- package/dist/landing/mixins/usage-estimate-live.d.ts +32 -0
- package/dist/landing/mixins/usage-estimate-live.js +120 -0
- package/dist/landing/phone-frame.d.ts +2 -1
- package/dist/landing/price-list.d.ts +68 -0
- package/dist/landing/price-list.js +125 -0
- package/dist/landing/scroll-scene.d.ts +59 -0
- package/dist/landing/scroll-scene.js +70 -0
- package/dist/landing/sponsor-wall.d.ts +30 -6
- package/dist/landing/sponsor-wall.js +70 -14
- package/dist/landing/typed-terminal.d.ts +24 -6
- package/dist/landing/typed-terminal.js +22 -14
- package/dist/landing/usage-estimate.d.ts +127 -0
- package/dist/landing/usage-estimate.js +164 -0
- package/dist/landing/utils/countdown.d.ts +33 -0
- package/dist/landing/utils/countdown.js +42 -0
- package/dist/landing/utils/pause-toggle.d.ts +2 -1
- package/dist/mixins/character-count.d.ts +62 -0
- package/dist/mixins/character-count.js +139 -0
- package/dist/mixins/index.d.ts +6 -2
- package/dist/mixins/index.js +6 -2
- package/dist/mixins/invoker-fields.d.ts +18 -0
- package/dist/mixins/invoker-fields.js +60 -0
- package/dist/mixins/media-sync.d.ts +58 -0
- package/dist/mixins/media-sync.js +102 -0
- package/dist/mixins/menu-keys.d.ts +11 -0
- package/dist/mixins/menu-keys.js +79 -70
- package/dist/mixins/sortable.d.ts +96 -0
- package/dist/mixins/sortable.js +433 -0
- package/dist/utils/code-lines.d.ts +26 -0
- package/dist/utils/code-lines.js +29 -0
- package/dist/utils/file-icon.d.ts +31 -0
- package/dist/utils/file-icon.js +60 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/write-cookie.d.ts +15 -15
- package/dist/utils/write-cookie.js +10 -16
- package/dist/video/episode-list.d.ts +48 -0
- package/dist/video/episode-list.js +161 -0
- package/dist/video/index.d.ts +15 -0
- package/dist/video/index.js +15 -0
- package/dist/video/mixins/video-player-sync.d.ts +27 -0
- package/dist/video/mixins/video-player-sync.js +413 -0
- package/dist/video/mixins/video-shelf-steps.d.ts +20 -0
- package/dist/video/mixins/video-shelf-steps.js +79 -0
- package/dist/video/title-hero.d.ts +51 -0
- package/dist/video/title-hero.js +170 -0
- package/dist/video/utils/timeline.d.ts +55 -0
- package/dist/video/utils/timeline.js +90 -0
- package/dist/video/video-card.d.ts +51 -0
- package/dist/video/video-card.js +135 -0
- package/dist/video/video-player.d.ts +261 -0
- package/dist/video/video-player.js +927 -0
- package/dist/video/video-shelf.d.ts +58 -0
- package/dist/video/video-shelf.js +155 -0
- package/package.json +6 -3
package/dist/mixins/menu-keys.js
CHANGED
|
@@ -30,6 +30,82 @@ function activeIndexOf(host, items) {
|
|
|
30
30
|
let active = host.ownerDocument.activeElement;
|
|
31
31
|
return active instanceof HTMLElement ? items.indexOf(active) : -1;
|
|
32
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Runs the WAI-ARIA menu keyboard pattern on `host` until `signal` aborts: roving tabindex,
|
|
35
|
+
* arrow keys, Home/End and typeahead across its items. Script that owns server-rendered
|
|
36
|
+
* markup calls it directly; components reach it through {@link menuKeys}.
|
|
37
|
+
*
|
|
38
|
+
* @param host The Menu surface, usually a `role="menu"` element.
|
|
39
|
+
* @param signal Ends the listeners when it aborts.
|
|
40
|
+
* @param options Item-selector override for non-standard markup.
|
|
41
|
+
* @example installMenuKeys(panel, controller.signal, { itemSelector: "[data-item]" });
|
|
42
|
+
*/
|
|
43
|
+
export function installMenuKeys(host, signal, options = {}) {
|
|
44
|
+
let buffer = "";
|
|
45
|
+
let resetBufferId;
|
|
46
|
+
let selector = options.itemSelector ?? DEFAULT_ITEM_SELECTOR;
|
|
47
|
+
let initialItems = queryItems(host, selector);
|
|
48
|
+
if (initialItems.length > 0)
|
|
49
|
+
setRovingTabindex(initialItems, initialItems[0]);
|
|
50
|
+
host.addEventListener("focusin", (event) => {
|
|
51
|
+
if (!belongsToSurface(event, host))
|
|
52
|
+
return;
|
|
53
|
+
let target = event.target;
|
|
54
|
+
if (!(target instanceof HTMLElement))
|
|
55
|
+
return;
|
|
56
|
+
let items = queryItems(host, selector);
|
|
57
|
+
if (items.includes(target))
|
|
58
|
+
setRovingTabindex(items, target);
|
|
59
|
+
}, { signal });
|
|
60
|
+
host.addEventListener("keydown", (event) => {
|
|
61
|
+
if (!belongsToSurface(event, host))
|
|
62
|
+
return;
|
|
63
|
+
let items = queryItems(host, selector);
|
|
64
|
+
if (items.length === 0)
|
|
65
|
+
return;
|
|
66
|
+
let index = activeIndexOf(host, items);
|
|
67
|
+
switch (event.key) {
|
|
68
|
+
case "ArrowDown": {
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
let next = index === -1 ? items[0] : items[index + 1];
|
|
71
|
+
if (next)
|
|
72
|
+
focusItem(items, next);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
case "ArrowUp": {
|
|
76
|
+
event.preventDefault();
|
|
77
|
+
let previous = index === -1 ? items[items.length - 1] : items[index - 1];
|
|
78
|
+
if (previous)
|
|
79
|
+
focusItem(items, previous);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
case "Home":
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
focusItem(items, items[0]);
|
|
85
|
+
return;
|
|
86
|
+
case "End":
|
|
87
|
+
event.preventDefault();
|
|
88
|
+
focusItem(items, items[items.length - 1]);
|
|
89
|
+
return;
|
|
90
|
+
case "Backspace":
|
|
91
|
+
buffer = buffer.slice(0, -1);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (!isPrintableKey(event))
|
|
95
|
+
return;
|
|
96
|
+
clearTimeout(resetBufferId);
|
|
97
|
+
buffer += event.key.toLowerCase();
|
|
98
|
+
resetBufferId = setTimeout(() => {
|
|
99
|
+
buffer = "";
|
|
100
|
+
}, TYPEAHEAD_RESET_MS);
|
|
101
|
+
let start = index === -1 ? 0 : index + 1;
|
|
102
|
+
let searchOrder = [...items.slice(start), ...items.slice(0, start)];
|
|
103
|
+
let match = searchOrder.find((item) => labelFor(item).startsWith(buffer));
|
|
104
|
+
if (match)
|
|
105
|
+
focusItem(items, match);
|
|
106
|
+
}, { signal });
|
|
107
|
+
signal.addEventListener("abort", () => clearTimeout(resetBufferId));
|
|
108
|
+
}
|
|
33
109
|
/**
|
|
34
110
|
* Adapts the WAI-ARIA menu keyboard pattern — roving tabindex, arrow-key
|
|
35
111
|
* navigation, Home/End, and typeahead — onto a Menu surface's
|
|
@@ -43,74 +119,7 @@ function activeIndexOf(host, items) {
|
|
|
43
119
|
* <Menu.Item>Delete</Menu.Item>
|
|
44
120
|
* </Menu.List>
|
|
45
121
|
*/
|
|
46
|
-
export const menuKeys = createMixin((_handle) => {
|
|
47
|
-
let
|
|
48
|
-
|
|
49
|
-
return (options = {}, props = options) => {
|
|
50
|
-
let resolved = props === options ? {} : options;
|
|
51
|
-
let selector = resolved.itemSelector ?? DEFAULT_ITEM_SELECTOR;
|
|
52
|
-
return ref((host, signal) => {
|
|
53
|
-
let initialItems = queryItems(host, selector);
|
|
54
|
-
if (initialItems.length > 0)
|
|
55
|
-
setRovingTabindex(initialItems, initialItems[0]);
|
|
56
|
-
host.addEventListener("focusin", (event) => {
|
|
57
|
-
if (!belongsToSurface(event, host))
|
|
58
|
-
return;
|
|
59
|
-
let target = event.target;
|
|
60
|
-
if (!(target instanceof HTMLElement))
|
|
61
|
-
return;
|
|
62
|
-
let items = queryItems(host, selector);
|
|
63
|
-
if (items.includes(target))
|
|
64
|
-
setRovingTabindex(items, target);
|
|
65
|
-
}, { signal });
|
|
66
|
-
host.addEventListener("keydown", (event) => {
|
|
67
|
-
if (!belongsToSurface(event, host))
|
|
68
|
-
return;
|
|
69
|
-
let items = queryItems(host, selector);
|
|
70
|
-
if (items.length === 0)
|
|
71
|
-
return;
|
|
72
|
-
let index = activeIndexOf(host, items);
|
|
73
|
-
switch (event.key) {
|
|
74
|
-
case "ArrowDown": {
|
|
75
|
-
event.preventDefault();
|
|
76
|
-
let next = index === -1 ? items[0] : items[index + 1];
|
|
77
|
-
if (next)
|
|
78
|
-
focusItem(items, next);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
case "ArrowUp": {
|
|
82
|
-
event.preventDefault();
|
|
83
|
-
let previous = index === -1 ? items[items.length - 1] : items[index - 1];
|
|
84
|
-
if (previous)
|
|
85
|
-
focusItem(items, previous);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
case "Home":
|
|
89
|
-
event.preventDefault();
|
|
90
|
-
focusItem(items, items[0]);
|
|
91
|
-
return;
|
|
92
|
-
case "End":
|
|
93
|
-
event.preventDefault();
|
|
94
|
-
focusItem(items, items[items.length - 1]);
|
|
95
|
-
return;
|
|
96
|
-
case "Backspace":
|
|
97
|
-
buffer = buffer.slice(0, -1);
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
if (!isPrintableKey(event))
|
|
101
|
-
return;
|
|
102
|
-
clearTimeout(resetBufferId);
|
|
103
|
-
buffer += event.key.toLowerCase();
|
|
104
|
-
resetBufferId = setTimeout(() => {
|
|
105
|
-
buffer = "";
|
|
106
|
-
}, TYPEAHEAD_RESET_MS);
|
|
107
|
-
let start = index === -1 ? 0 : index + 1;
|
|
108
|
-
let searchOrder = [...items.slice(start), ...items.slice(0, start)];
|
|
109
|
-
let match = searchOrder.find((item) => labelFor(item).startsWith(buffer));
|
|
110
|
-
if (match)
|
|
111
|
-
focusItem(items, match);
|
|
112
|
-
}, { signal });
|
|
113
|
-
signal.addEventListener("abort", () => clearTimeout(resetBufferId));
|
|
114
|
-
});
|
|
115
|
-
};
|
|
122
|
+
export const menuKeys = createMixin((_handle) => (options = {}, props = options) => {
|
|
123
|
+
let resolved = props === options ? {} : options;
|
|
124
|
+
return ref((host, signal) => installMenuKeys(host, signal, resolved));
|
|
116
125
|
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: dragging an item between or within lists, and moving one from the keyboard, need pointer
|
|
3
|
+
* and key listeners. No-JS baseline: each item's own move form, or a menu inside it, still moves it
|
|
4
|
+
* with a plain post, and the lists stay readable markup.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { MixinFactory } from "remix/component";
|
|
10
|
+
/** Attribute on the element holding every list the mixin moves items between. */
|
|
11
|
+
export declare const SORTABLE_ATTRIBUTE = "data-sortable";
|
|
12
|
+
/** Attribute a list carries its value on, which a move sends in the item's form. */
|
|
13
|
+
export declare const SORTABLE_LIST_ATTRIBUTE = "data-sortable-list";
|
|
14
|
+
/** Attribute on the element inside a list that holds its items; the list itself without one. */
|
|
15
|
+
export declare const SORTABLE_ITEMS_ATTRIBUTE = "data-sortable-items";
|
|
16
|
+
/** Attribute an item carries its value on, unique across the lists. */
|
|
17
|
+
export declare const SORTABLE_ITEM_ATTRIBUTE = "data-sortable-item";
|
|
18
|
+
/** Attribute an item's move form names its item with, so a move finds the form to post. */
|
|
19
|
+
export declare const SORTABLE_FORM_ATTRIBUTE = "data-sortable-form";
|
|
20
|
+
/**
|
|
21
|
+
* Attribute a move form names its list field with. A move appends that field after the form's
|
|
22
|
+
* own, with the list's value, and the next render of the form drops it again.
|
|
23
|
+
*/
|
|
24
|
+
export declare const SORTABLE_FIELD_ATTRIBUTE = "data-sortable-field";
|
|
25
|
+
/**
|
|
26
|
+
* Attribute a move form names its position field with. A move sends the value of the item it
|
|
27
|
+
* lands before in that field, or an empty value when it lands last. A form without one moves
|
|
28
|
+
* its item only between lists, to the top.
|
|
29
|
+
*/
|
|
30
|
+
export declare const SORTABLE_POSITION_ATTRIBUTE = "data-sortable-position";
|
|
31
|
+
/** Attribute on a list's count, which a move between lists keeps current until the page answers. */
|
|
32
|
+
export declare const SORTABLE_COUNT_ATTRIBUTE = "data-sortable-count";
|
|
33
|
+
/** Attribute on a list's title, which a keyboard move announces. */
|
|
34
|
+
export declare const SORTABLE_TITLE_ATTRIBUTE = "data-sortable-title";
|
|
35
|
+
/** Attribute on the live region a keyboard move is announced through. */
|
|
36
|
+
export declare const SORTABLE_STATUS_ATTRIBUTE = "data-sortable-status";
|
|
37
|
+
/** Attribute on the element that scrolls while the pointer rests near its edge mid-drag. */
|
|
38
|
+
export declare const SORTABLE_SCROLLER_ATTRIBUTE = "data-sortable-scroller";
|
|
39
|
+
/** Attribute set on the root for as long as a drag lasts. */
|
|
40
|
+
export declare const SORTABLE_ACTIVE_ATTRIBUTE = "data-sortable-active";
|
|
41
|
+
/** Attribute set on the item being dragged, for as long as the drag lasts. */
|
|
42
|
+
export declare const SORTABLE_DRAGGING_ATTRIBUTE = "data-sortable-dragging";
|
|
43
|
+
/** Attribute set on another list while the dragged item would land in it. */
|
|
44
|
+
export declare const SORTABLE_TARGET_ATTRIBUTE = "data-sortable-target";
|
|
45
|
+
/** Attribute on the copy of the item that follows the pointer. */
|
|
46
|
+
export declare const SORTABLE_GHOST_ATTRIBUTE = "data-sortable-ghost";
|
|
47
|
+
/** DOM event type {@link SortableMoveEvent} is dispatched under. */
|
|
48
|
+
declare const MOVE_EVENT: "ui:sortable-move";
|
|
49
|
+
declare global {
|
|
50
|
+
interface HTMLElementEventMap {
|
|
51
|
+
[MOVE_EVENT]: SortableMoveEvent;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Dispatched on an item once it is dropped or moved by key, before its move form posts;
|
|
56
|
+
* `preventDefault()` keeps the item in place and the form from posting, for a consumer that
|
|
57
|
+
* saves the move itself.
|
|
58
|
+
*/
|
|
59
|
+
export declare class SortableMoveEvent extends Event {
|
|
60
|
+
/** The moved item's value. */
|
|
61
|
+
readonly item: string;
|
|
62
|
+
/** The list it left. */
|
|
63
|
+
readonly from: string;
|
|
64
|
+
/** The list it landed in, the same as `from` for a move within a list. */
|
|
65
|
+
readonly to: string;
|
|
66
|
+
/** The item it landed before, `null` when it landed last or its form takes no position. */
|
|
67
|
+
readonly before: string | null;
|
|
68
|
+
/**
|
|
69
|
+
* @param init The item, the two lists, and the item it landed before.
|
|
70
|
+
*/
|
|
71
|
+
constructor(init: {
|
|
72
|
+
item: string;
|
|
73
|
+
from: string;
|
|
74
|
+
to: string;
|
|
75
|
+
before?: string | null;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/** Options for {@link sortable}. */
|
|
79
|
+
export interface SortableOptions {
|
|
80
|
+
/**
|
|
81
|
+
* What a keyboard move announces, with `{list}` where the list's title goes, such as
|
|
82
|
+
* "Moved to {list}". Without it a keyboard move is silent.
|
|
83
|
+
*/
|
|
84
|
+
moved?: string;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Moves items within and between the lists inside the nearest {@link SORTABLE_ATTRIBUTE} root.
|
|
88
|
+
* A mouse or pen press on an item that travels past a few pixels lifts it under a ghost, places
|
|
89
|
+
* it between the items under the pointer, and posts its move form on release; Escape puts it
|
|
90
|
+
* back. Shift+ArrowUp/Down move the focused item past its neighbour, and Shift+ArrowLeft/Right to
|
|
91
|
+
* the top of the list on either side. A touch press scrolls as usual.
|
|
92
|
+
*
|
|
93
|
+
* @example <span hidden mix={[sortable({ moved: "Moved to {list}" })]} />
|
|
94
|
+
*/
|
|
95
|
+
export declare const sortable: MixinFactory<HTMLElement, [options?: SortableOptions]>;
|
|
96
|
+
export {};
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: dragging an item between or within lists, and moving one from the keyboard, need pointer
|
|
3
|
+
* and key listeners. No-JS baseline: each item's own move form, or a menu inside it, still moves it
|
|
4
|
+
* with a plain post, and the lists stay readable markup.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import { createMixin } from "remix/component";
|
|
10
|
+
import { prefersReducedMotion } from "../utils/prefers-reduced-motion.js";
|
|
11
|
+
/** Attribute on the element holding every list the mixin moves items between. */
|
|
12
|
+
export const SORTABLE_ATTRIBUTE = "data-sortable";
|
|
13
|
+
/** Attribute a list carries its value on, which a move sends in the item's form. */
|
|
14
|
+
export const SORTABLE_LIST_ATTRIBUTE = "data-sortable-list";
|
|
15
|
+
/** Attribute on the element inside a list that holds its items; the list itself without one. */
|
|
16
|
+
export const SORTABLE_ITEMS_ATTRIBUTE = "data-sortable-items";
|
|
17
|
+
/** Attribute an item carries its value on, unique across the lists. */
|
|
18
|
+
export const SORTABLE_ITEM_ATTRIBUTE = "data-sortable-item";
|
|
19
|
+
/** Attribute an item's move form names its item with, so a move finds the form to post. */
|
|
20
|
+
export const SORTABLE_FORM_ATTRIBUTE = "data-sortable-form";
|
|
21
|
+
/**
|
|
22
|
+
* Attribute a move form names its list field with. A move appends that field after the form's
|
|
23
|
+
* own, with the list's value, and the next render of the form drops it again.
|
|
24
|
+
*/
|
|
25
|
+
export const SORTABLE_FIELD_ATTRIBUTE = "data-sortable-field";
|
|
26
|
+
/**
|
|
27
|
+
* Attribute a move form names its position field with. A move sends the value of the item it
|
|
28
|
+
* lands before in that field, or an empty value when it lands last. A form without one moves
|
|
29
|
+
* its item only between lists, to the top.
|
|
30
|
+
*/
|
|
31
|
+
export const SORTABLE_POSITION_ATTRIBUTE = "data-sortable-position";
|
|
32
|
+
/** Attribute on a list's count, which a move between lists keeps current until the page answers. */
|
|
33
|
+
export const SORTABLE_COUNT_ATTRIBUTE = "data-sortable-count";
|
|
34
|
+
/** Attribute on a list's title, which a keyboard move announces. */
|
|
35
|
+
export const SORTABLE_TITLE_ATTRIBUTE = "data-sortable-title";
|
|
36
|
+
/** Attribute on the live region a keyboard move is announced through. */
|
|
37
|
+
export const SORTABLE_STATUS_ATTRIBUTE = "data-sortable-status";
|
|
38
|
+
/** Attribute on the element that scrolls while the pointer rests near its edge mid-drag. */
|
|
39
|
+
export const SORTABLE_SCROLLER_ATTRIBUTE = "data-sortable-scroller";
|
|
40
|
+
/** Attribute set on the root for as long as a drag lasts. */
|
|
41
|
+
export const SORTABLE_ACTIVE_ATTRIBUTE = "data-sortable-active";
|
|
42
|
+
/** Attribute set on the item being dragged, for as long as the drag lasts. */
|
|
43
|
+
export const SORTABLE_DRAGGING_ATTRIBUTE = "data-sortable-dragging";
|
|
44
|
+
/** Attribute set on another list while the dragged item would land in it. */
|
|
45
|
+
export const SORTABLE_TARGET_ATTRIBUTE = "data-sortable-target";
|
|
46
|
+
/** Attribute on the copy of the item that follows the pointer. */
|
|
47
|
+
export const SORTABLE_GHOST_ATTRIBUTE = "data-sortable-ghost";
|
|
48
|
+
/** How far a press travels before it becomes a drag, so a plain click still opens the item. */
|
|
49
|
+
const DRAG_THRESHOLD_PX = 5;
|
|
50
|
+
/** How close to the scroller's edge the pointer comes before it scrolls toward it. */
|
|
51
|
+
const EDGE_PX = 48;
|
|
52
|
+
/** Elements a press on starts nothing from, so an item's own controls keep working. */
|
|
53
|
+
const INTERACTIVE = "button, input, select, textarea, summary, label, [popover], [contenteditable]";
|
|
54
|
+
/** DOM event type {@link SortableMoveEvent} is dispatched under. */
|
|
55
|
+
const MOVE_EVENT = "ui:sortable-move";
|
|
56
|
+
/**
|
|
57
|
+
* Dispatched on an item once it is dropped or moved by key, before its move form posts;
|
|
58
|
+
* `preventDefault()` keeps the item in place and the form from posting, for a consumer that
|
|
59
|
+
* saves the move itself.
|
|
60
|
+
*/
|
|
61
|
+
export class SortableMoveEvent extends Event {
|
|
62
|
+
/** The moved item's value. */
|
|
63
|
+
item;
|
|
64
|
+
/** The list it left. */
|
|
65
|
+
from;
|
|
66
|
+
/** The list it landed in, the same as `from` for a move within a list. */
|
|
67
|
+
to;
|
|
68
|
+
/** The item it landed before, `null` when it landed last or its form takes no position. */
|
|
69
|
+
before;
|
|
70
|
+
/**
|
|
71
|
+
* @param init The item, the two lists, and the item it landed before.
|
|
72
|
+
*/
|
|
73
|
+
constructor(init) {
|
|
74
|
+
super(MOVE_EVENT, { bubbles: true, cancelable: true });
|
|
75
|
+
this.item = init.item;
|
|
76
|
+
this.from = init.from;
|
|
77
|
+
this.to = init.to;
|
|
78
|
+
this.before = init.before ?? null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Moves items within and between the lists inside the nearest {@link SORTABLE_ATTRIBUTE} root.
|
|
83
|
+
* A mouse or pen press on an item that travels past a few pixels lifts it under a ghost, places
|
|
84
|
+
* it between the items under the pointer, and posts its move form on release; Escape puts it
|
|
85
|
+
* back. Shift+ArrowUp/Down move the focused item past its neighbour, and Shift+ArrowLeft/Right to
|
|
86
|
+
* the top of the list on either side. A touch press scrolls as usual.
|
|
87
|
+
*
|
|
88
|
+
* @example <span hidden mix={[sortable({ moved: "Moved to {list}" })]} />
|
|
89
|
+
*/
|
|
90
|
+
export const sortable = createMixin((handle) => {
|
|
91
|
+
let controller;
|
|
92
|
+
let options = {};
|
|
93
|
+
/** Starts listening once the marker is in the page, replacing any earlier listeners. */
|
|
94
|
+
function attach(event) {
|
|
95
|
+
controller?.abort();
|
|
96
|
+
controller = new AbortController();
|
|
97
|
+
let marker = event.node;
|
|
98
|
+
connect(findRoot, readOptions, controller.signal);
|
|
99
|
+
/** The root around the marker, or `null` while the marker is detached. */
|
|
100
|
+
function findRoot() {
|
|
101
|
+
return marker.closest(`[${SORTABLE_ATTRIBUTE}]`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** Stops listening, ending a drag in progress. */
|
|
105
|
+
function detach() {
|
|
106
|
+
controller?.abort();
|
|
107
|
+
controller = undefined;
|
|
108
|
+
}
|
|
109
|
+
/** The latest options, read at each keyboard move. */
|
|
110
|
+
function readOptions() {
|
|
111
|
+
return options;
|
|
112
|
+
}
|
|
113
|
+
handle.addEventListener("insert", attach);
|
|
114
|
+
handle.addEventListener("remove", detach);
|
|
115
|
+
return (next) => {
|
|
116
|
+
options = next ?? {};
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
/**
|
|
120
|
+
* Listens for presses and keys on one root until `signal` aborts. The root is looked up from the
|
|
121
|
+
* marker on each event, so a marker inserted before its root is attached still finds it.
|
|
122
|
+
*
|
|
123
|
+
* @param findRoot Returns the root, or `null` while the marker is detached.
|
|
124
|
+
* @param readOptions Returns the mixin's latest options.
|
|
125
|
+
* @param signal Ends every listener, a drag in progress included.
|
|
126
|
+
*/
|
|
127
|
+
function connect(findRoot, readOptions, signal) {
|
|
128
|
+
let press;
|
|
129
|
+
/**
|
|
130
|
+
* Ends the press: clears every drag mark and puts a placed item back where it started, then
|
|
131
|
+
* moves it to where it was dropped, so a listener that prevents the move leaves it in place.
|
|
132
|
+
*/
|
|
133
|
+
function end(drop) {
|
|
134
|
+
if (!press)
|
|
135
|
+
return;
|
|
136
|
+
let { root, item, from, ghost, target, origin, positioned } = press;
|
|
137
|
+
press = undefined;
|
|
138
|
+
ghost?.remove();
|
|
139
|
+
item.removeAttribute(SORTABLE_DRAGGING_ATTRIBUTE);
|
|
140
|
+
root.removeAttribute(SORTABLE_ACTIVE_ATTRIBUTE);
|
|
141
|
+
for (let list of root.querySelectorAll(`[${SORTABLE_TARGET_ATTRIBUTE}]`)) {
|
|
142
|
+
list.removeAttribute(SORTABLE_TARGET_ATTRIBUTE);
|
|
143
|
+
}
|
|
144
|
+
if (!ghost)
|
|
145
|
+
return;
|
|
146
|
+
suppressNextClick(item);
|
|
147
|
+
if (!positioned) {
|
|
148
|
+
if (drop && target && target !== from)
|
|
149
|
+
move(item, from, target, firstItem(target, item));
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
let to = item.closest(`[${SORTABLE_LIST_ATTRIBUTE}]`);
|
|
153
|
+
let moved = item.parentNode !== origin.parent || item.nextSibling !== origin.next;
|
|
154
|
+
let before = nextItem(item);
|
|
155
|
+
origin.parent?.insertBefore(item, origin.next);
|
|
156
|
+
if (drop && to && moved)
|
|
157
|
+
move(item, from, to, before);
|
|
158
|
+
}
|
|
159
|
+
/** Records a mouse or pen press on an item, outside the item's own controls. */
|
|
160
|
+
function onPointerDown(event) {
|
|
161
|
+
if (event.button !== 0 || event.pointerType === "touch")
|
|
162
|
+
return;
|
|
163
|
+
let root = findRoot();
|
|
164
|
+
if (!root)
|
|
165
|
+
return;
|
|
166
|
+
let origin = event.target instanceof Element ? event.target : null;
|
|
167
|
+
if (!origin || origin.closest(INTERACTIVE))
|
|
168
|
+
return;
|
|
169
|
+
let item = origin.closest(`[${SORTABLE_ITEM_ATTRIBUTE}]`);
|
|
170
|
+
let from = item?.closest(`[${SORTABLE_LIST_ATTRIBUTE}]`);
|
|
171
|
+
if (!item || !from || !root.contains(item))
|
|
172
|
+
return;
|
|
173
|
+
let rect = item.getBoundingClientRect();
|
|
174
|
+
press = {
|
|
175
|
+
root,
|
|
176
|
+
item,
|
|
177
|
+
from,
|
|
178
|
+
origin: { parent: item.parentNode, next: item.nextSibling },
|
|
179
|
+
positioned: takesPosition(item),
|
|
180
|
+
pointerId: event.pointerId,
|
|
181
|
+
x: event.clientX,
|
|
182
|
+
y: event.clientY,
|
|
183
|
+
offsetX: event.clientX - rect.left,
|
|
184
|
+
offsetY: event.clientY - rect.top,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/** Lifts the item past the threshold, then carries the ghost and places the item under it. */
|
|
188
|
+
function onPointerMove(event) {
|
|
189
|
+
if (!press || event.pointerId !== press.pointerId)
|
|
190
|
+
return;
|
|
191
|
+
if (!press.ghost) {
|
|
192
|
+
let distance = Math.hypot(event.clientX - press.x, event.clientY - press.y);
|
|
193
|
+
if (distance < DRAG_THRESHOLD_PX)
|
|
194
|
+
return;
|
|
195
|
+
press.ghost = lift(press.item);
|
|
196
|
+
press.item.setAttribute(SORTABLE_DRAGGING_ATTRIBUTE, "");
|
|
197
|
+
press.root.setAttribute(SORTABLE_ACTIVE_ATTRIBUTE, "");
|
|
198
|
+
}
|
|
199
|
+
event.preventDefault();
|
|
200
|
+
let tilt = prefersReducedMotion() ? "" : " rotate(1.5deg)";
|
|
201
|
+
press.ghost.style.transform = `translate(${event.clientX - press.offsetX}px, ${event.clientY - press.offsetY}px)${tilt}`;
|
|
202
|
+
let under = document.elementFromPoint(event.clientX, event.clientY);
|
|
203
|
+
let target = under?.closest(`[${SORTABLE_LIST_ATTRIBUTE}]`) ?? null;
|
|
204
|
+
if (target && !press.root.contains(target))
|
|
205
|
+
target = null;
|
|
206
|
+
if (target !== press.target) {
|
|
207
|
+
press.target?.removeAttribute(SORTABLE_TARGET_ATTRIBUTE);
|
|
208
|
+
if (target && target !== press.from)
|
|
209
|
+
target.setAttribute(SORTABLE_TARGET_ATTRIBUTE, "");
|
|
210
|
+
press.target = target;
|
|
211
|
+
}
|
|
212
|
+
if (press.positioned && target)
|
|
213
|
+
place(press.item, target, event.clientY);
|
|
214
|
+
scrollNearEdge(press.root, event.clientX, event.clientY);
|
|
215
|
+
}
|
|
216
|
+
/** Drops the item where the pointer let go. */
|
|
217
|
+
function onPointerUp() {
|
|
218
|
+
end(true);
|
|
219
|
+
}
|
|
220
|
+
/** Puts everything back when the pointer is taken away or the listeners end. */
|
|
221
|
+
function cancel() {
|
|
222
|
+
end(false);
|
|
223
|
+
}
|
|
224
|
+
/** Cancels a drag on Escape, before anything else on the page reads the key. */
|
|
225
|
+
function onKeyDown(event) {
|
|
226
|
+
if (event.key !== "Escape" || !press?.ghost)
|
|
227
|
+
return;
|
|
228
|
+
event.preventDefault();
|
|
229
|
+
event.stopPropagation();
|
|
230
|
+
end(false);
|
|
231
|
+
}
|
|
232
|
+
/** Moves the focused item on Shift+Arrow, keeping its focus, and announces where it went. */
|
|
233
|
+
function onMoveKey(event) {
|
|
234
|
+
if (!event.shiftKey || event.altKey || event.metaKey || event.ctrlKey)
|
|
235
|
+
return;
|
|
236
|
+
if (!["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key))
|
|
237
|
+
return;
|
|
238
|
+
let root = findRoot();
|
|
239
|
+
let focused = document.activeElement;
|
|
240
|
+
let item = focused?.closest(`[${SORTABLE_ITEM_ATTRIBUTE}]`);
|
|
241
|
+
let from = item?.closest(`[${SORTABLE_LIST_ATTRIBUTE}]`);
|
|
242
|
+
if (!root || !item || !from || !root.contains(item))
|
|
243
|
+
return;
|
|
244
|
+
if (focused?.matches("input, textarea, select, [contenteditable]"))
|
|
245
|
+
return;
|
|
246
|
+
let target = keyTarget(root, item, from, event.key);
|
|
247
|
+
if (!target)
|
|
248
|
+
return;
|
|
249
|
+
event.preventDefault();
|
|
250
|
+
move(item, from, target.list, target.before);
|
|
251
|
+
if (focused instanceof HTMLElement)
|
|
252
|
+
focused.focus({ preventScroll: false });
|
|
253
|
+
announce(root, target.list, readOptions().moved);
|
|
254
|
+
}
|
|
255
|
+
document.addEventListener("pointerdown", onPointerDown, { signal });
|
|
256
|
+
document.addEventListener("pointermove", onPointerMove, { signal });
|
|
257
|
+
document.addEventListener("pointerup", onPointerUp, { signal });
|
|
258
|
+
document.addEventListener("pointercancel", cancel, { signal });
|
|
259
|
+
document.addEventListener("keydown", onKeyDown, { signal, capture: true });
|
|
260
|
+
document.addEventListener("keydown", onMoveKey, { signal });
|
|
261
|
+
signal.addEventListener("abort", cancel);
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Draws the ghost: a copy of the item fixed to the viewport at the item's size, which the
|
|
265
|
+
* pointer carries, with ids dropped so the page keeps one element per id.
|
|
266
|
+
*/
|
|
267
|
+
function lift(item) {
|
|
268
|
+
let rect = item.getBoundingClientRect();
|
|
269
|
+
let ghost = item.cloneNode(true);
|
|
270
|
+
ghost.removeAttribute(SORTABLE_ITEM_ATTRIBUTE);
|
|
271
|
+
for (let node of [ghost, ...ghost.querySelectorAll("[id]")])
|
|
272
|
+
node.removeAttribute("id");
|
|
273
|
+
ghost.setAttribute("aria-hidden", "true");
|
|
274
|
+
ghost.setAttribute(SORTABLE_GHOST_ATTRIBUTE, "");
|
|
275
|
+
Object.assign(ghost.style, {
|
|
276
|
+
position: "fixed",
|
|
277
|
+
insetBlockStart: "0",
|
|
278
|
+
insetInlineStart: "0",
|
|
279
|
+
inlineSize: `${rect.width}px`,
|
|
280
|
+
margin: "0",
|
|
281
|
+
pointerEvents: "none",
|
|
282
|
+
zIndex: "1000",
|
|
283
|
+
listStyle: "none",
|
|
284
|
+
boxShadow: "var(--ui-shadow-xl, 0 12px 28px rgb(0 0 0 / 0.18))",
|
|
285
|
+
});
|
|
286
|
+
document.body.appendChild(ghost);
|
|
287
|
+
return ghost;
|
|
288
|
+
}
|
|
289
|
+
/** Scrolls the root's scroller toward whichever of its edges the pointer rests near. */
|
|
290
|
+
function scrollNearEdge(root, x, y) {
|
|
291
|
+
let scroller = root.querySelector(`[${SORTABLE_SCROLLER_ATTRIBUTE}]`) ?? root;
|
|
292
|
+
let rect = scroller.getBoundingClientRect();
|
|
293
|
+
if (x < rect.left + EDGE_PX)
|
|
294
|
+
scroller.scrollLeft -= 12;
|
|
295
|
+
else if (x > rect.right - EDGE_PX)
|
|
296
|
+
scroller.scrollLeft += 12;
|
|
297
|
+
if (y < rect.top + EDGE_PX)
|
|
298
|
+
scroller.scrollTop -= 12;
|
|
299
|
+
else if (y > rect.bottom - EDGE_PX)
|
|
300
|
+
scroller.scrollTop += 12;
|
|
301
|
+
}
|
|
302
|
+
/** Swallows the click a drag's release fires on an item that is a link. */
|
|
303
|
+
function suppressNextClick(item) {
|
|
304
|
+
item.addEventListener("click", swallow, { capture: true, once: true });
|
|
305
|
+
setTimeout(release, 0);
|
|
306
|
+
/** Keeps the click from following the item's link. */
|
|
307
|
+
function swallow(event) {
|
|
308
|
+
event.preventDefault();
|
|
309
|
+
event.stopPropagation();
|
|
310
|
+
}
|
|
311
|
+
/** Lets later clicks through once the release's own click has passed. */
|
|
312
|
+
function release() {
|
|
313
|
+
item.removeEventListener("click", swallow, { capture: true });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Puts the item in the target list before `before` (last when `null`) at once, bumps both counts
|
|
318
|
+
* when it changed list, then posts the item's move form with the list's value and, when the form
|
|
319
|
+
* takes one, the position, unless a listener prevented the move event.
|
|
320
|
+
*/
|
|
321
|
+
function move(item, from, to, before) {
|
|
322
|
+
let form = moveForm(item);
|
|
323
|
+
let event = new SortableMoveEvent({
|
|
324
|
+
item: item.getAttribute(SORTABLE_ITEM_ATTRIBUTE) ?? "",
|
|
325
|
+
from: from.getAttribute(SORTABLE_LIST_ATTRIBUTE) ?? "",
|
|
326
|
+
to: to.getAttribute(SORTABLE_LIST_ATTRIBUTE) ?? "",
|
|
327
|
+
before: takesPosition(item) ? (before?.getAttribute(SORTABLE_ITEM_ATTRIBUTE) ?? null) : null,
|
|
328
|
+
});
|
|
329
|
+
if (!item.dispatchEvent(event))
|
|
330
|
+
return;
|
|
331
|
+
itemsOf(to).insertBefore(item, before);
|
|
332
|
+
if (from !== to) {
|
|
333
|
+
bumpCount(from, -1);
|
|
334
|
+
bumpCount(to, 1);
|
|
335
|
+
}
|
|
336
|
+
if (!prefersReducedMotion()) {
|
|
337
|
+
item.animate?.([{ opacity: 0.4 }, { opacity: 1 }], { duration: 180, easing: "ease-out" });
|
|
338
|
+
}
|
|
339
|
+
let name = form?.getAttribute(SORTABLE_FIELD_ATTRIBUTE);
|
|
340
|
+
if (!form || !name)
|
|
341
|
+
return;
|
|
342
|
+
form.appendChild(hiddenInput(name, event.to));
|
|
343
|
+
let position = form.getAttribute(SORTABLE_POSITION_ATTRIBUTE);
|
|
344
|
+
if (position)
|
|
345
|
+
form.appendChild(hiddenInput(position, event.before ?? ""));
|
|
346
|
+
form.requestSubmit();
|
|
347
|
+
}
|
|
348
|
+
/** A hidden field, appended after the form's own so a reader of the post takes its value last. */
|
|
349
|
+
function hiddenInput(name, value) {
|
|
350
|
+
let input = document.createElement("input");
|
|
351
|
+
input.type = "hidden";
|
|
352
|
+
input.name = name;
|
|
353
|
+
input.value = value;
|
|
354
|
+
return input;
|
|
355
|
+
}
|
|
356
|
+
/** The item's own move form, when it has one. */
|
|
357
|
+
function moveForm(item) {
|
|
358
|
+
let value = item.getAttribute(SORTABLE_ITEM_ATTRIBUTE) ?? "";
|
|
359
|
+
return document.querySelector(`form[${SORTABLE_FORM_ATTRIBUTE}="${CSS.escape(value)}"]`);
|
|
360
|
+
}
|
|
361
|
+
/** Whether the item can land anywhere: it has no move form, or its form takes a position. */
|
|
362
|
+
function takesPosition(item) {
|
|
363
|
+
let form = moveForm(item);
|
|
364
|
+
return !form || form.hasAttribute(SORTABLE_POSITION_ATTRIBUTE);
|
|
365
|
+
}
|
|
366
|
+
/** The element a list's items live in. */
|
|
367
|
+
function itemsOf(list) {
|
|
368
|
+
return list.querySelector(`[${SORTABLE_ITEMS_ATTRIBUTE}]`) ?? list;
|
|
369
|
+
}
|
|
370
|
+
/** The items of a list, in order, leaving `except` out. */
|
|
371
|
+
function itemsIn(list, except) {
|
|
372
|
+
return [...itemsOf(list).children].filter((child) => child instanceof HTMLElement &&
|
|
373
|
+
child.hasAttribute(SORTABLE_ITEM_ATTRIBUTE) &&
|
|
374
|
+
child !== except);
|
|
375
|
+
}
|
|
376
|
+
/** The list's first item other than `except`, before which an item lands at the top. */
|
|
377
|
+
function firstItem(list, except) {
|
|
378
|
+
return itemsIn(list, except)[0] ?? null;
|
|
379
|
+
}
|
|
380
|
+
/** The item right after `item` in its list, or `null` when it is last. */
|
|
381
|
+
function nextItem(item) {
|
|
382
|
+
let next = item.nextElementSibling;
|
|
383
|
+
while (next && !next.hasAttribute(SORTABLE_ITEM_ATTRIBUTE))
|
|
384
|
+
next = next.nextElementSibling;
|
|
385
|
+
return next instanceof HTMLElement ? next : null;
|
|
386
|
+
}
|
|
387
|
+
/** Moves the dragged item into `list` before the first item whose middle is below `y`. */
|
|
388
|
+
function place(item, list, y) {
|
|
389
|
+
let container = itemsOf(list);
|
|
390
|
+
let before = itemsIn(list, item).find((other) => {
|
|
391
|
+
let rect = other.getBoundingClientRect();
|
|
392
|
+
return y < rect.top + rect.height / 2;
|
|
393
|
+
}) ?? null;
|
|
394
|
+
if (item.parentNode === container && nextItem(item) === before)
|
|
395
|
+
return;
|
|
396
|
+
container.insertBefore(item, before);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Where a Shift+Arrow key sends an item: the top of the list to either side, or past its
|
|
400
|
+
* neighbour above or below when it can land anywhere.
|
|
401
|
+
*/
|
|
402
|
+
function keyTarget(root, item, from, key) {
|
|
403
|
+
if (key === "ArrowLeft" || key === "ArrowRight") {
|
|
404
|
+
let lists = [...root.querySelectorAll(`[${SORTABLE_LIST_ATTRIBUTE}]`)];
|
|
405
|
+
let list = lists[lists.indexOf(from) + (key === "ArrowLeft" ? -1 : 1)];
|
|
406
|
+
return list ? { list, before: firstItem(list, item) } : null;
|
|
407
|
+
}
|
|
408
|
+
if (!takesPosition(item))
|
|
409
|
+
return null;
|
|
410
|
+
let items = itemsIn(from);
|
|
411
|
+
let index = items.indexOf(item);
|
|
412
|
+
if (key === "ArrowUp") {
|
|
413
|
+
return index > 0 ? { list: from, before: items[index - 1] } : null;
|
|
414
|
+
}
|
|
415
|
+
if (index === items.length - 1)
|
|
416
|
+
return null;
|
|
417
|
+
return { list: from, before: items[index + 2] ?? null };
|
|
418
|
+
}
|
|
419
|
+
/** Says where a keyboard move put the item, through the root's own live region. */
|
|
420
|
+
function announce(root, list, template) {
|
|
421
|
+
let region = root.querySelector(`[${SORTABLE_STATUS_ATTRIBUTE}]`);
|
|
422
|
+
if (!region || template === undefined)
|
|
423
|
+
return;
|
|
424
|
+
let title = list.querySelector(`[${SORTABLE_TITLE_ATTRIBUTE}]`)?.textContent ?? "";
|
|
425
|
+
region.textContent = template.replace("{list}", title);
|
|
426
|
+
}
|
|
427
|
+
/** Adds `delta` to a list's count, when the list shows one. */
|
|
428
|
+
function bumpCount(list, delta) {
|
|
429
|
+
let count = list.querySelector(`[${SORTABLE_COUNT_ATTRIBUTE}]`);
|
|
430
|
+
let value = Number(count?.textContent);
|
|
431
|
+
if (count && Number.isFinite(value))
|
|
432
|
+
count.textContent = String(Math.max(0, value + delta));
|
|
433
|
+
}
|