@marktiderman/genesis-ui 2.6.0 → 2.8.0
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/EXTENDING.md +40 -1
- package/SKILL.md +145 -9
- package/dist/{AppShell-CX0M-dfc.d.ts → AppShell-LXQykEqe.d.ts} +47 -5
- package/dist/{chunk-M62CHO3N.js → chunk-4C4ESW6O.js} +9 -4
- package/dist/chunk-4C4ESW6O.js.map +1 -0
- package/dist/{chunk-IXGAEECT.js → chunk-DWCAC2E7.js} +6 -3
- package/dist/chunk-DWCAC2E7.js.map +1 -0
- package/dist/chunk-FUVWKTPL.js +19 -0
- package/dist/chunk-FUVWKTPL.js.map +1 -0
- package/dist/{chunk-PLIYF6KI.js → chunk-MPI33I4K.js} +2 -2
- package/dist/{chunk-PLIYF6KI.js.map → chunk-MPI33I4K.js.map} +1 -1
- package/dist/{chunk-JA3ZKULS.js → chunk-OITWCIG2.js} +3 -3
- package/dist/chunk-OITWCIG2.js.map +1 -0
- package/dist/chunk-PKFDGGZV.js +65 -0
- package/dist/chunk-PKFDGGZV.js.map +1 -0
- package/dist/chunk-S7NXD37O.js +321 -0
- package/dist/chunk-S7NXD37O.js.map +1 -0
- package/dist/{chunk-DWM7ELFI.js → chunk-UYI3GLFJ.js} +163 -20
- package/dist/chunk-UYI3GLFJ.js.map +1 -0
- package/dist/{chunk-CDYZRZ6U.js → chunk-WGMEVX3Y.js} +2 -2
- package/dist/chunk-WGMEVX3Y.js.map +1 -0
- package/dist/chunk-ZDHWNQTR.js +552 -0
- package/dist/chunk-ZDHWNQTR.js.map +1 -0
- package/dist/components/data/index.d.ts +239 -15
- package/dist/components/data/index.js +524 -109
- package/dist/components/data/index.js.map +1 -1
- package/dist/components/layout/form-page.d.ts +1 -1
- package/dist/components/layout/form-page.js +1 -1
- package/dist/components/layout/index.d.ts +2 -2
- package/dist/components/layout/index.js +9 -9
- package/dist/components/patterns/index.d.ts +1 -0
- package/dist/components/patterns/index.js +6 -4
- package/dist/components/patterns/sortable-list.d.ts +78 -0
- package/dist/components/patterns/sortable-list.js +5 -0
- package/dist/components/patterns/sortable-list.js.map +1 -0
- package/dist/components/rich-text/rich-text-editor.d.ts +194 -0
- package/dist/components/rich-text/rich-text-editor.js +833 -0
- package/dist/components/rich-text/rich-text-editor.js.map +1 -0
- package/dist/components/rich-text/rich-text.d.ts +51 -0
- package/dist/components/rich-text/rich-text.js +4 -0
- package/dist/components/rich-text/rich-text.js.map +1 -0
- package/dist/components/ui/alert-dialog.d.ts +12 -1
- package/dist/components/ui/alert-dialog.js +1 -1
- package/dist/hooks/index.d.ts +3 -3
- package/dist/hooks/index.js +4 -4
- package/dist/hooks/use-data-filters.d.ts +5 -3
- package/dist/hooks/use-data-filters.js +2 -2
- package/dist/hooks/use-resource-page.d.ts +2 -1
- package/dist/hooks/use-resource-page.js +3 -2
- package/dist/hooks/use-view-preference.d.ts +17 -2
- package/dist/hooks/use-view-preference.js +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +30 -29
- package/dist/{navigation-DBwcZ6vb.d.ts → navigation-Bha0R3Fu.d.ts} +6 -3
- package/dist/rich-text-doc-C54fME2C.d.ts +180 -0
- package/dist/router/react-router.d.ts +47 -0
- package/dist/router/react-router.js +53 -0
- package/dist/router/react-router.js.map +1 -0
- package/dist/router/tanstack-router.d.ts +47 -0
- package/dist/router/tanstack-router.js +105 -0
- package/dist/router/tanstack-router.js.map +1 -0
- package/dist/{use-resource-page-Dd2VUv30.d.ts → use-resource-page-D4byuwAs.d.ts} +20 -3
- package/package.json +62 -7
- package/dist/chunk-CDYZRZ6U.js.map +0 -1
- package/dist/chunk-DWM7ELFI.js.map +0 -1
- package/dist/chunk-FTTNGGLA.js +0 -20
- package/dist/chunk-FTTNGGLA.js.map +0 -1
- package/dist/chunk-IXGAEECT.js.map +0 -1
- package/dist/chunk-JA3ZKULS.js.map +0 -1
- package/dist/chunk-M62CHO3N.js.map +0 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useIsMobile } from './chunk-VFO3I7PV.js';
|
|
3
|
+
import { useCallback, useSyncExternalStore, useState } from 'react';
|
|
4
|
+
import { useStorage } from '@marktiderman/genesis-core';
|
|
5
|
+
|
|
6
|
+
var VIEW_MODES = ["grid", "table", "list"];
|
|
7
|
+
function isViewMode(value) {
|
|
8
|
+
return typeof value === "string" && VIEW_MODES.includes(value);
|
|
9
|
+
}
|
|
10
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
11
|
+
function subscribe(onChange) {
|
|
12
|
+
listeners.add(onChange);
|
|
13
|
+
window.addEventListener("storage", onChange);
|
|
14
|
+
return () => {
|
|
15
|
+
listeners.delete(onChange);
|
|
16
|
+
window.removeEventListener("storage", onChange);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function getServerSnapshot() {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
var choiceKey = (key) => `genesis-view-choice-${key}`;
|
|
23
|
+
var legacyKey = (key) => `genesis-view-${key}`;
|
|
24
|
+
function useViewPreference(key, defaultMode = "grid") {
|
|
25
|
+
const storage = useStorage();
|
|
26
|
+
const storageKey = choiceKey(key);
|
|
27
|
+
const isMobile = useIsMobile();
|
|
28
|
+
const responsive = typeof defaultMode !== "string";
|
|
29
|
+
const getSnapshot = useCallback(
|
|
30
|
+
() => storage.getItem(storageKey),
|
|
31
|
+
[storage, storageKey]
|
|
32
|
+
);
|
|
33
|
+
const getLegacySnapshot = useCallback(
|
|
34
|
+
() => responsive ? null : storage.getItem(legacyKey(key)),
|
|
35
|
+
[storage, key, responsive]
|
|
36
|
+
);
|
|
37
|
+
const stored = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
38
|
+
const legacy = useSyncExternalStore(
|
|
39
|
+
subscribe,
|
|
40
|
+
getLegacySnapshot,
|
|
41
|
+
getServerSnapshot
|
|
42
|
+
);
|
|
43
|
+
const chosen = isViewMode(stored) ? stored : isViewMode(legacy) ? legacy : void 0;
|
|
44
|
+
const fallback = !responsive ? defaultMode : isMobile ? defaultMode.mobile : defaultMode.desktop;
|
|
45
|
+
const [unsaved, setUnsaved] = useState(void 0);
|
|
46
|
+
const setMode = useCallback(
|
|
47
|
+
(mode) => {
|
|
48
|
+
let saved = null;
|
|
49
|
+
try {
|
|
50
|
+
storage.setItem(storageKey, mode);
|
|
51
|
+
saved = storage.getItem(storageKey);
|
|
52
|
+
} catch {
|
|
53
|
+
saved = null;
|
|
54
|
+
}
|
|
55
|
+
setUnsaved(saved === mode ? void 0 : mode);
|
|
56
|
+
for (const listener of listeners) listener();
|
|
57
|
+
},
|
|
58
|
+
[storage, storageKey]
|
|
59
|
+
);
|
|
60
|
+
return [unsaved ?? chosen ?? fallback, setMode];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { useViewPreference };
|
|
64
|
+
//# sourceMappingURL=chunk-PKFDGGZV.js.map
|
|
65
|
+
//# sourceMappingURL=chunk-PKFDGGZV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-view-preference.ts"],"names":[],"mappings":";;;;AAwBA,IAAM,UAAA,GAAkC,CAAC,MAAA,EAAQ,OAAA,EAAS,MAAM,CAAA;AAEhE,SAAS,WAAW,KAAA,EAAmC;AACrD,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAa,UAAA,CAAiC,SAAS,KAAK,CAAA;AACtF;AAkBA,IAAM,SAAA,uBAAgB,GAAA,EAAgB;AAEtC,SAAS,UAAU,QAAA,EAAkC;AACnD,EAAA,SAAA,CAAU,IAAI,QAAQ,CAAA;AACtB,EAAA,MAAA,CAAO,gBAAA,CAAiB,WAAW,QAAQ,CAAA;AAC3C,EAAA,OAAO,MAAM;AACX,IAAA,SAAA,CAAU,OAAO,QAAQ,CAAA;AACzB,IAAA,MAAA,CAAO,mBAAA,CAAoB,WAAW,QAAQ,CAAA;AAAA,EAChD,CAAA;AACF;A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E,EAAA,MAAM,OAAA,GAAU,WAAA;AAAA,IACd,CAAC,IAAA,KAAmB;AAClB,MAAA,IAAI,KAAA,GAAuB,IAAA;AAC3B,MAAA,IAAI;AACF,QAAA,OAAA,CAAQ,OAAA,CAAQ,YAAY,IAAI,CAAA;AAChC,QAAA,KAAA,GAAQ,OAAA,CAAQ,QAAQ,UAAU,CAAA;AAAA,MACpC,CAAA,CAAA,MAAQ;AACN,QAAA,KAAA,GAAQ,IAAA;AAAA,MACV;AACA,MAAA,UAAA,CAAW,KAAA,KAAU,IAAA,GAAO,MAAA,GAAY,IAAI,CAAA;AAC5C,MAAA,KAAA,MAAW,QAAA,IAAY,WAAW,QAAA,EAAS;AAAA,IAC7C,CAAA;AAAA,IACA,CAAC,SAAS,UAAU;AAAA,GACtB;AAEA,EAAA,OAAO,CAAC,OAAA,IAAW,MAAA,IAAU,QAAA,EAAU,OAAO,CAAA;AAChD","file":"chunk-PKFDGGZV.js","sourcesContent":["\"use client\";\n\nimport { useState, useCallback, useSyncExternalStore } from \"react\";\nimport { useStorage } from \"@marktiderman/genesis-core\";\nimport type { ViewMode } from \"../components/patterns/view-toggle\";\nimport { useIsMobile } from \"./use-mobile\";\n\n/**\n * A default view that depends on the viewport: cards on a phone, a table on\n * a desktop. Only the DEFAULT is responsive — once the user picks a view it\n * is theirs on every screen size.\n *\n * @stability Beta\n */\nexport interface ResponsiveViewMode {\n /** Below the mobile breakpoint (768px). */\n mobile: ViewMode;\n /** At or above the mobile breakpoint. */\n desktop: ViewMode;\n}\n\n/** @stability Beta */\nexport type ViewModeDefault = ViewMode | ResponsiveViewMode;\n\nconst VIEW_MODES: readonly ViewMode[] = [\"grid\", \"table\", \"list\"];\n\nfunction isViewMode(value: unknown): value is ViewMode {\n return typeof value === \"string\" && (VIEW_MODES as readonly string[]).includes(value);\n}\n\n/**\n * The user's view mode for a list, persisted under `genesis-view-choice-<key>`.\n *\n * Only an explicit choice is written. The default is never stored: writing\n * it on mount would turn \"cards on phones\" into a stored choice the first\n * time the page rendered on a phone, and the desktop would then open in\n * cards too — the default would stop being a default. Likewise a later\n * change to the default reaches every user who has not chosen.\n *\n * SSR: the server renders the desktop default (it has no viewport), and a\n * phone switches to its mobile default right after hydration — the same\n * contract as `useIsMobile`, so there is no hydration mismatch.\n */\n// Same-tab subscribers: a choice made by one hook instance reaches every\n// other instance reading the same key (two lists of one resource, a header\n// toggle and a page body). Other tabs arrive through the `storage` event.\nconst listeners = new Set<() => void>();\n\nfunction subscribe(onChange: () => void): () => void {\n listeners.add(onChange);\n window.addEventListener(\"storage\", onChange);\n return () => {\n listeners.delete(onChange);\n window.removeEventListener(\"storage\", onChange);\n };\n}\n\n/** The server has no stored preference; hydrate against the default. */\nfunction getServerSnapshot(): null {\n return null;\n}\n\n/**\n * Storage keys. Earlier versions wrote the DEFAULT to `genesis-view-<key>`\n * on mount, so a value there may be a real choice or just a stale default —\n * there is no telling which. Explicit choices now go to their own key. The\n * legacy key is still honoured for a plain default (where a stale default\n * and the default agree, so reading it changes nothing), but not for a\n * responsive one, which it would otherwise pin to whatever the first device\n * stored.\n */\nconst choiceKey = (key: string) => `genesis-view-choice-${key}`;\nconst legacyKey = (key: string) => `genesis-view-${key}`;\n\nexport function useViewPreference(\n key: string,\n defaultMode: ViewModeDefault = \"grid\",\n): [ViewMode, (mode: ViewMode) => void] {\n const storage = useStorage();\n const storageKey = choiceKey(key);\n const isMobile = useIsMobile();\n const responsive = typeof defaultMode !== \"string\";\n\n // `useSyncExternalStore` rather than a `useState` initializer: the server\n // renders with no stored value, so reading storage during the client's\n // first render would mismatch the server's HTML. React hydrates against\n // `getServerSnapshot` and then re-renders with the stored choice; a\n // client-only render reads storage directly, with no flash.\n const getSnapshot = useCallback(\n () => storage.getItem(storageKey),\n [storage, storageKey],\n );\n const getLegacySnapshot = useCallback(\n () => (responsive ? null : storage.getItem(legacyKey(key))),\n [storage, key, responsive],\n );\n const stored = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n const legacy = useSyncExternalStore(\n subscribe,\n getLegacySnapshot,\n getServerSnapshot,\n );\n // `undefined` = the user has not chosen; fall through to the default.\n const chosen = isViewMode(stored)\n ? stored\n : isViewMode(legacy)\n ? legacy\n : undefined;\n\n const fallback = !responsive\n ? defaultMode\n : isMobile\n ? defaultMode.mobile\n : defaultMode.desktop;\n\n // Holds a choice storage did not keep (quota, private mode, a no-op\n // adapter), so it still applies for this hook's lifetime. Adapters\n // swallow write errors, so the write is checked by reading it back.\n const [unsaved, setUnsaved] = useState<ViewMode | undefined>(undefined);\n\n const setMode = useCallback(\n (mode: ViewMode) => {\n let saved: string | null = null;\n try {\n storage.setItem(storageKey, mode);\n saved = storage.getItem(storageKey);\n } catch {\n saved = null;\n }\n setUnsaved(saved === mode ? undefined : mode);\n for (const listener of listeners) listener();\n },\n [storage, storageKey],\n );\n\n return [unsaved ?? chosen ?? fallback, setMode];\n}\n"]}
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem } from './chunk-QJ6P2DW6.js';
|
|
3
|
+
import { cn } from './chunk-NZ5WBA4R.js';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import { GripVertical, MoreVertical } from 'lucide-react';
|
|
6
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
7
|
+
|
|
8
|
+
var DRAG_TYPE = "application/x-genesis-sortable-item";
|
|
9
|
+
var useIsomorphicLayoutEffect = typeof window === "undefined" ? React.useEffect : React.useLayoutEffect;
|
|
10
|
+
function moved(keys, from, to) {
|
|
11
|
+
const next = keys.slice();
|
|
12
|
+
const [key] = next.splice(from, 1);
|
|
13
|
+
next.splice(to, 0, key);
|
|
14
|
+
return next;
|
|
15
|
+
}
|
|
16
|
+
function SortableList({
|
|
17
|
+
items,
|
|
18
|
+
getKey,
|
|
19
|
+
getItemLabel,
|
|
20
|
+
renderItem,
|
|
21
|
+
onReorder,
|
|
22
|
+
label,
|
|
23
|
+
disabled,
|
|
24
|
+
moveMenu = true,
|
|
25
|
+
className,
|
|
26
|
+
itemClassName
|
|
27
|
+
}) {
|
|
28
|
+
const [armed, setArmed] = React.useState(null);
|
|
29
|
+
const [dragging, setDragging] = React.useState(null);
|
|
30
|
+
const [dropAt, setDropAt] = React.useState(null);
|
|
31
|
+
const [grabbed, setGrabbed] = React.useState(null);
|
|
32
|
+
const [preview, setPreview] = React.useState(null);
|
|
33
|
+
const [pending, setPending] = React.useState(false);
|
|
34
|
+
const pendingRef = React.useRef(false);
|
|
35
|
+
const [announcement, setAnnouncement] = React.useState("");
|
|
36
|
+
const handles = React.useRef(/* @__PURE__ */ new Map());
|
|
37
|
+
const restoreFocus = React.useRef(null);
|
|
38
|
+
const instructionsId = React.useId();
|
|
39
|
+
const byKey = new Map(items.map((item) => [getKey(item), item]));
|
|
40
|
+
const itemKeys = items.map(getKey);
|
|
41
|
+
const keys = preview && preview.length === itemKeys.length && preview.every((k) => byKey.has(k)) ? preview : itemKeys;
|
|
42
|
+
const total = keys.length;
|
|
43
|
+
const nameOf = (key) => {
|
|
44
|
+
const item = byKey.get(key);
|
|
45
|
+
return item === void 0 ? "Item" : getItemLabel(item);
|
|
46
|
+
};
|
|
47
|
+
const announce = (message) => setAnnouncement((previous) => previous === message ? `${message}\xA0` : message);
|
|
48
|
+
const keepFocus = () => {
|
|
49
|
+
const active = typeof document === "undefined" ? null : document.activeElement;
|
|
50
|
+
restoreFocus.current = active instanceof HTMLElement && active !== document.body ? active : null;
|
|
51
|
+
};
|
|
52
|
+
useIsomorphicLayoutEffect(() => {
|
|
53
|
+
const element = restoreFocus.current;
|
|
54
|
+
restoreFocus.current = null;
|
|
55
|
+
if (!element || !element.isConnected) return;
|
|
56
|
+
const active = document.activeElement;
|
|
57
|
+
if (active !== element && (active === null || active === document.body)) element.focus();
|
|
58
|
+
});
|
|
59
|
+
React.useEffect(() => {
|
|
60
|
+
if (disabled && (grabbed || armed || dragging)) {
|
|
61
|
+
setGrabbed(null);
|
|
62
|
+
setArmed(null);
|
|
63
|
+
setDragging(null);
|
|
64
|
+
setDropAt(null);
|
|
65
|
+
if (!pendingRef.current) setPreview(null);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (dragging && !byKey.has(dragging)) {
|
|
69
|
+
setDragging(null);
|
|
70
|
+
setDropAt(null);
|
|
71
|
+
setArmed(null);
|
|
72
|
+
}
|
|
73
|
+
if (grabbed && !byKey.has(grabbed.key)) {
|
|
74
|
+
setGrabbed(null);
|
|
75
|
+
setPreview(null);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
const commit = (key, from, to, message) => {
|
|
79
|
+
if (from === to) return;
|
|
80
|
+
pendingRef.current = true;
|
|
81
|
+
setPending(true);
|
|
82
|
+
setPreview(moved(itemKeys, from, to));
|
|
83
|
+
let result;
|
|
84
|
+
try {
|
|
85
|
+
result = onReorder(from, to, { key });
|
|
86
|
+
} catch (error) {
|
|
87
|
+
result = Promise.reject(error);
|
|
88
|
+
}
|
|
89
|
+
Promise.resolve(result).then(
|
|
90
|
+
() => announce(message),
|
|
91
|
+
() => announce(`Couldn\u2019t move ${nameOf(key)}.`)
|
|
92
|
+
).finally(() => {
|
|
93
|
+
keepFocus();
|
|
94
|
+
pendingRef.current = false;
|
|
95
|
+
setPending(false);
|
|
96
|
+
setPreview(null);
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
const move = (key, to) => {
|
|
100
|
+
if (disabled || pendingRef.current || grabbed) return;
|
|
101
|
+
if (!Number.isFinite(to)) return;
|
|
102
|
+
const from = itemKeys.indexOf(key);
|
|
103
|
+
const target = Math.max(0, Math.min(total - 1, Math.round(to)));
|
|
104
|
+
if (from < 0 || target === from) return;
|
|
105
|
+
keepFocus();
|
|
106
|
+
commit(key, from, target, `${nameOf(key)} moved to position ${target + 1} of ${total}.`);
|
|
107
|
+
};
|
|
108
|
+
const pickUp = (key) => {
|
|
109
|
+
if (disabled || pendingRef.current) return;
|
|
110
|
+
const from = itemKeys.indexOf(key);
|
|
111
|
+
if (from < 0) return;
|
|
112
|
+
setGrabbed({ key, from });
|
|
113
|
+
setPreview(itemKeys);
|
|
114
|
+
announce(
|
|
115
|
+
`${nameOf(key)} picked up, position ${from + 1} of ${total}. Use the up and down arrow keys to move, Space to drop, Escape to cancel.`
|
|
116
|
+
);
|
|
117
|
+
};
|
|
118
|
+
const drop = (key) => {
|
|
119
|
+
if (!grabbed) return;
|
|
120
|
+
const to = keys.indexOf(key);
|
|
121
|
+
const from = grabbed.from;
|
|
122
|
+
setGrabbed(null);
|
|
123
|
+
if (to === from) {
|
|
124
|
+
setPreview(null);
|
|
125
|
+
announce(`${nameOf(key)} dropped at position ${to + 1} of ${total}.`);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
keepFocus();
|
|
129
|
+
commit(key, from, to, `${nameOf(key)} dropped at position ${to + 1} of ${total}.`);
|
|
130
|
+
};
|
|
131
|
+
const cancel = (key) => {
|
|
132
|
+
if (!grabbed) return;
|
|
133
|
+
const from = grabbed.from;
|
|
134
|
+
keepFocus();
|
|
135
|
+
setGrabbed(null);
|
|
136
|
+
setPreview(null);
|
|
137
|
+
announce(`Move cancelled. ${nameOf(key)} is back at position ${from + 1} of ${total}.`);
|
|
138
|
+
};
|
|
139
|
+
const step = (key, to) => {
|
|
140
|
+
const index = keys.indexOf(key);
|
|
141
|
+
const target = Math.max(0, Math.min(total - 1, to));
|
|
142
|
+
if (index < 0 || target === index) return;
|
|
143
|
+
keepFocus();
|
|
144
|
+
setPreview(moved(keys, index, target));
|
|
145
|
+
announce(`${nameOf(key)} moved to position ${target + 1} of ${total}.`);
|
|
146
|
+
};
|
|
147
|
+
React.useEffect(() => {
|
|
148
|
+
if (!grabbed) return;
|
|
149
|
+
const onKey = (event) => {
|
|
150
|
+
if (event.key !== "Escape") return;
|
|
151
|
+
event.preventDefault();
|
|
152
|
+
event.stopPropagation();
|
|
153
|
+
cancel(grabbed.key);
|
|
154
|
+
};
|
|
155
|
+
window.addEventListener("keydown", onKey, true);
|
|
156
|
+
return () => window.removeEventListener("keydown", onKey, true);
|
|
157
|
+
});
|
|
158
|
+
React.useEffect(() => {
|
|
159
|
+
if (!armed || dragging) return;
|
|
160
|
+
const disarm = () => setArmed(null);
|
|
161
|
+
const events = ["pointerup", "pointercancel", "contextmenu", "blur"];
|
|
162
|
+
for (const type of events) window.addEventListener(type, disarm, { once: true });
|
|
163
|
+
return () => {
|
|
164
|
+
for (const type of events) window.removeEventListener(type, disarm);
|
|
165
|
+
};
|
|
166
|
+
}, [armed, dragging]);
|
|
167
|
+
const onHandleKeyDown = (event, key) => {
|
|
168
|
+
if (disabled) return;
|
|
169
|
+
const isGrabbed = grabbed?.key === key;
|
|
170
|
+
const index = keys.indexOf(key);
|
|
171
|
+
const delta = event.key === "ArrowUp" ? -1 : event.key === "ArrowDown" ? 1 : 0;
|
|
172
|
+
if (isGrabbed) {
|
|
173
|
+
if (delta) {
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
step(key, index + delta);
|
|
176
|
+
} else if (event.key === "Home" || event.key === "End") {
|
|
177
|
+
event.preventDefault();
|
|
178
|
+
step(key, event.key === "Home" ? 0 : total - 1);
|
|
179
|
+
}
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (delta && event.altKey) {
|
|
183
|
+
event.preventDefault();
|
|
184
|
+
move(key, index + delta);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
const onHandleClick = (event, key) => {
|
|
188
|
+
if (disabled || event.detail !== 0) return;
|
|
189
|
+
if (grabbed?.key === key) drop(key);
|
|
190
|
+
else if (!grabbed) pickUp(key);
|
|
191
|
+
};
|
|
192
|
+
const onDragOver = (event, key) => {
|
|
193
|
+
if (!dragging) return;
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
event.dataTransfer.dropEffect = "move";
|
|
196
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
197
|
+
const edge = event.clientY < rect.top + rect.height / 2 ? "before" : "after";
|
|
198
|
+
if (dropAt?.key !== key || dropAt.edge !== edge) setDropAt({ key, edge });
|
|
199
|
+
};
|
|
200
|
+
const endDrag = () => {
|
|
201
|
+
setDragging(null);
|
|
202
|
+
setDropAt(null);
|
|
203
|
+
setArmed(null);
|
|
204
|
+
};
|
|
205
|
+
const onDrop = (event) => {
|
|
206
|
+
event.preventDefault();
|
|
207
|
+
const key = event.dataTransfer.getData(DRAG_TYPE) || dragging;
|
|
208
|
+
const target = dropAt;
|
|
209
|
+
endDrag();
|
|
210
|
+
if (!key || !target || pendingRef.current || grabbed) return;
|
|
211
|
+
const from = itemKeys.indexOf(key);
|
|
212
|
+
const over = itemKeys.indexOf(target.key);
|
|
213
|
+
if (from < 0 || over < 0) return;
|
|
214
|
+
let to = target.edge === "before" ? over : over + 1;
|
|
215
|
+
if (from < to) to -= 1;
|
|
216
|
+
commit(key, from, to, `${nameOf(key)} dropped at position ${to + 1} of ${total}.`);
|
|
217
|
+
};
|
|
218
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
219
|
+
/* @__PURE__ */ jsx("ul", { "aria-label": label, "aria-busy": pending || void 0, className: cn("flex flex-col gap-1", className), children: keys.map((key, index) => {
|
|
220
|
+
const item = byKey.get(key);
|
|
221
|
+
const itemLabel = getItemLabel(item);
|
|
222
|
+
const isGrabbed = grabbed?.key === key;
|
|
223
|
+
const isDragging = dragging === key;
|
|
224
|
+
const handle = disabled ? null : /* @__PURE__ */ jsx(
|
|
225
|
+
"button",
|
|
226
|
+
{
|
|
227
|
+
type: "button",
|
|
228
|
+
ref: (node) => {
|
|
229
|
+
if (node) handles.current.set(key, node);
|
|
230
|
+
else handles.current.delete(key);
|
|
231
|
+
},
|
|
232
|
+
"aria-label": `Reorder ${itemLabel}`,
|
|
233
|
+
"aria-describedby": instructionsId,
|
|
234
|
+
"data-grabbed": isGrabbed || void 0,
|
|
235
|
+
className: cn(
|
|
236
|
+
"inline-flex h-7 w-5 shrink-0 cursor-grab items-center justify-center rounded text-muted-foreground",
|
|
237
|
+
"hover:bg-accent hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
238
|
+
isGrabbed && "bg-accent text-foreground"
|
|
239
|
+
),
|
|
240
|
+
onPointerDown: () => {
|
|
241
|
+
if (!pendingRef.current && !grabbed) setArmed(key);
|
|
242
|
+
},
|
|
243
|
+
onPointerCancel: () => setArmed(null),
|
|
244
|
+
onClick: (event) => onHandleClick(event, key),
|
|
245
|
+
onKeyDown: (event) => onHandleKeyDown(event, key),
|
|
246
|
+
onBlur: () => {
|
|
247
|
+
if (isGrabbed && restoreFocus.current !== handles.current.get(key)) cancel(key);
|
|
248
|
+
},
|
|
249
|
+
children: /* @__PURE__ */ jsx(GripVertical, { className: "h-4 w-4", "aria-hidden": true })
|
|
250
|
+
}
|
|
251
|
+
);
|
|
252
|
+
return /* @__PURE__ */ jsxs(
|
|
253
|
+
"li",
|
|
254
|
+
{
|
|
255
|
+
"data-sortable-key": key,
|
|
256
|
+
draggable: !disabled && armed === key || void 0,
|
|
257
|
+
className: cn(
|
|
258
|
+
"relative rounded-md",
|
|
259
|
+
moveMenu && !disabled && "pr-8",
|
|
260
|
+
isDragging && "opacity-50",
|
|
261
|
+
isGrabbed && "ring-2 ring-primary/50",
|
|
262
|
+
itemClassName
|
|
263
|
+
),
|
|
264
|
+
onDragStart: (event) => {
|
|
265
|
+
if (disabled || armed !== key) return;
|
|
266
|
+
event.dataTransfer.setData(DRAG_TYPE, key);
|
|
267
|
+
event.dataTransfer.effectAllowed = "move";
|
|
268
|
+
setDragging(key);
|
|
269
|
+
},
|
|
270
|
+
onDragEnd: endDrag,
|
|
271
|
+
onDragOver: (event) => onDragOver(event, key),
|
|
272
|
+
onDrop,
|
|
273
|
+
children: [
|
|
274
|
+
dropAt?.key === key && dragging !== key && /* @__PURE__ */ jsx(
|
|
275
|
+
"span",
|
|
276
|
+
{
|
|
277
|
+
"aria-hidden": true,
|
|
278
|
+
className: cn(
|
|
279
|
+
"pointer-events-none absolute inset-x-0 h-0.5 rounded bg-primary",
|
|
280
|
+
dropAt.edge === "before" ? "-top-0.5" : "-bottom-0.5"
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
),
|
|
284
|
+
renderItem(item, {
|
|
285
|
+
index,
|
|
286
|
+
handle,
|
|
287
|
+
isDragging,
|
|
288
|
+
isGrabbed,
|
|
289
|
+
moveUp: () => move(key, index - 1),
|
|
290
|
+
moveDown: () => move(key, index + 1),
|
|
291
|
+
moveTo: (to) => move(key, to)
|
|
292
|
+
}),
|
|
293
|
+
moveMenu && !disabled && total > 1 && /* @__PURE__ */ jsxs(DropdownMenu, { children: [
|
|
294
|
+
/* @__PURE__ */ jsx(
|
|
295
|
+
DropdownMenuTrigger,
|
|
296
|
+
{
|
|
297
|
+
"aria-label": `Move ${itemLabel}`,
|
|
298
|
+
className: "absolute right-1 top-1 rounded p-1 text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
299
|
+
children: /* @__PURE__ */ jsx(MoreVertical, { className: "h-4 w-4", "aria-hidden": true })
|
|
300
|
+
}
|
|
301
|
+
),
|
|
302
|
+
/* @__PURE__ */ jsxs(DropdownMenuContent, { align: "end", children: [
|
|
303
|
+
/* @__PURE__ */ jsx(DropdownMenuItem, { disabled: index === 0, onSelect: () => move(key, index - 1), children: "Move up" }),
|
|
304
|
+
/* @__PURE__ */ jsx(DropdownMenuItem, { disabled: index === total - 1, onSelect: () => move(key, index + 1), children: "Move down" }),
|
|
305
|
+
/* @__PURE__ */ jsx(DropdownMenuItem, { disabled: index === 0, onSelect: () => move(key, 0), children: "Move to top" }),
|
|
306
|
+
/* @__PURE__ */ jsx(DropdownMenuItem, { disabled: index === total - 1, onSelect: () => move(key, total - 1), children: "Move to bottom" })
|
|
307
|
+
] })
|
|
308
|
+
] })
|
|
309
|
+
]
|
|
310
|
+
},
|
|
311
|
+
key
|
|
312
|
+
);
|
|
313
|
+
}) }),
|
|
314
|
+
/* @__PURE__ */ jsx("p", { id: instructionsId, hidden: true, children: "Space or Enter picks the item up. Up and down arrows, Home and End move it. Space or Enter drops it, Escape puts it back. Alt with an arrow moves it one step." }),
|
|
315
|
+
/* @__PURE__ */ jsx("div", { "aria-live": "polite", "aria-atomic": "true", className: "sr-only", children: announcement })
|
|
316
|
+
] });
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export { SortableList };
|
|
320
|
+
//# sourceMappingURL=chunk-S7NXD37O.js.map
|
|
321
|
+
//# sourceMappingURL=chunk-S7NXD37O.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/components/patterns/sortable-list.tsx"],"names":[],"mappings":";;;;;;AAkEA,IAAM,SAAA,GAAY,qCAAA;AAGlB,IAAM,yBAAA,GAA4B,OAAO,MAAA,KAAW,WAAA,GAAoB,KAAA,CAAA,SAAA,GAAkB,KAAA,CAAA,eAAA;AAE1F,SAAS,KAAA,CAAM,IAAA,EAAyB,IAAA,EAAc,EAAA,EAAY;AAChE,EAAA,MAAM,IAAA,GAAO,KAAK,KAAA,EAAM;AACxB,EAAA,MAAM,CAAC,GAAG,CAAA,GAAI,IAAA,CAAK,MAAA,CAAO,MAAM,CAAC,CAAA;AACjC,EAAA,IAAA,CAAK,MAAA,CAAO,EAAA,EAAI,CAAA,EAAG,GAAI,CAAA;AACvB,EAAA,OAAO,IAAA;AACT;AAqBO,SAAS,YAAA,CAAgB;AAAA,EAC9B,KAAA;AAAA,EACA,MAAA;AAAA,EACA,YAAA;AAAA,EACA,UAAA;AAAA,EACA,SAAA;AAAA,EACA,KAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA,GAAW,IAAA;AAAA,EACX,SAAA;AAAA,EACA;AACF,CAAA,EAAyB;AACvB,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAU,eAAwB,IAAI,CAAA;AAC5D,EAAA,MAAM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\nimport { GripVertical, MoreVertical } from \"lucide-react\";\nimport { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from \"../ui/dropdown-menu\";\nimport { cn } from \"../../utils\";\n\nexport interface SortableListItemContext {\n /** The item's position as shown, from 0. */\n index: number;\n /**\n * The drag handle: drag it with a pointer, or focus it and press Space or\n * Enter to pick the item up, the arrow keys to move it and Space or Enter\n * again to drop it. Render it somewhere in the row.\n */\n handle: React.ReactNode;\n /** True while this item is being dragged with a pointer. */\n isDragging: boolean;\n /** True while this item is picked up from the keyboard. */\n isGrabbed: boolean;\n /** Move this item without dragging, for a row menu of your own. */\n moveUp: () => void;\n moveDown: () => void;\n moveTo: (index: number) => void;\n}\n\nexport interface SortableListReorder {\n /** Key of the item that moved. */\n key: string;\n}\n\nexport interface SortableListProps<T> {\n items: readonly T[];\n /** A stable key per item. Required: dragging finds the item again by key. */\n getKey: (item: T) => string;\n /** A name for an item, for the handle's label and announcements (\"Week 1\"). */\n getItemLabel: (item: T) => string;\n renderItem: (item: T, context: SortableListItemContext) => React.ReactNode;\n /**\n * Move the item at `from` to `to` (both from 0, `to` counted after the item\n * is taken out). Each finished move calls this once: a keyboard pick-up\n * that wanders and comes back calls nothing.\n *\n * The list shows the new order straight away and keeps showing it until the\n * returned promise settles; by then `items` must be in the new order. A\n * synchronous handler must update `items` in the same tick. A rejected\n * promise puts the old order back and announces that the move failed. Other\n * moves wait until this one settles.\n */\n onReorder: (from: number, to: number, move: SortableListReorder) => void | Promise<void>;\n /** Accessible name of the list (\"Course steps\"). */\n label: string;\n /** Turns off dragging and every kind of move. */\n disabled?: boolean | undefined;\n /**\n * A \"Move\" menu on each row (up, down, to top, to bottom), on by default so\n * touch and screen-reader users always have a way to move an item. Turn it\n * off only when the row offers `moveUp` / `moveDown` / `moveTo` itself.\n */\n moveMenu?: boolean | undefined;\n className?: string | undefined;\n itemClassName?: string | undefined;\n}\n\ntype DropAt = { key: string; edge: \"before\" | \"after\" };\n\nconst DRAG_TYPE = \"application/x-genesis-sortable-item\";\n\n// useLayoutEffect warns during server rendering; nothing needs refocusing there.\nconst useIsomorphicLayoutEffect = typeof window === \"undefined\" ? React.useEffect : React.useLayoutEffect;\n\nfunction moved(keys: readonly string[], from: number, to: number) {\n const next = keys.slice();\n const [key] = next.splice(from, 1);\n next.splice(to, 0, key!);\n return next;\n}\n\n/**\n * A vertical list the user can rearrange: course steps, lesson blocks, form\n * fields.\n *\n * Every way to move an item is equivalent, so dragging is never the only one\n * (the same stance as KanbanBoard's \"Move to…\" menu):\n * - drag the handle with a pointer;\n * - focus the handle, Space or Enter to pick up, ↑/↓ and Home/End to move,\n * Space or Enter to drop, Escape to put it back (tabbing away also puts it\n * back); Alt+↑/↓ moves one step without picking up;\n * - the row's \"Move\" menu, or `moveUp` / `moveDown` / `moveTo` from the render\n * context.\n *\n * Moves are announced in a polite live region once they succeed. Native drag\n * and drop needs no dependency and Radix has none to offer (docs/FRAMEWORK.md\n * rule 3).\n *\n * @stability Beta\n */\nexport function SortableList<T>({\n items,\n getKey,\n getItemLabel,\n renderItem,\n onReorder,\n label,\n disabled,\n moveMenu = true,\n className,\n itemClassName,\n}: SortableListProps<T>) {\n const [armed, setArmed] = React.useState<string | null>(null);\n const [dragging, setDragging] = React.useState<string | null>(null);\n const [dropAt, setDropAt] = React.useState<DropAt | null>(null);\n // While a key is grabbed, `preview` is the order on screen; nothing is\n // written until the drop.\n const [grabbed, setGrabbed] = React.useState<{ key: string; from: number } | null>(null);\n // The order to show instead of `items`: during a keyboard pick-up, and from\n // a committed move until its promise settles.\n const [preview, setPreview] = React.useState<string[] | null>(null);\n const [pending, setPending] = React.useState(false);\n // The same, readable within one tick: two moves fired back to back must\n // not both start from the old order.\n const pendingRef = React.useRef(false);\n const [announcement, setAnnouncement] = React.useState(\"\");\n const handles = React.useRef(new Map<string, HTMLButtonElement>());\n // The element that had focus when a move started, to put focus back on if\n // the DOM move drops it to the body. Cleared after every commit.\n const restoreFocus = React.useRef<HTMLElement | null>(null);\n const instructionsId = React.useId();\n\n const byKey = new Map(items.map((item) => [getKey(item), item] as const));\n const itemKeys = items.map(getKey);\n const keys = preview && preview.length === itemKeys.length && preview.every((k) => byKey.has(k)) ? preview : itemKeys;\n const total = keys.length;\n const nameOf = (key: string) => {\n const item = byKey.get(key);\n return item === undefined ? \"Item\" : getItemLabel(item);\n };\n\n // A live region only speaks when its text changes, so the same message\n // twice in a row is told apart by a trailing no-break space.\n const announce = (message: string) =>\n setAnnouncement((previous) => (previous === message ? `${message} ` : message));\n\n // Moving a focused element in the DOM drops focus to the body in real\n // browsers. Remember what had focus, the handle or a row's own button, and\n // put it back after the commit.\n const keepFocus = () => {\n const active = typeof document === \"undefined\" ? null : document.activeElement;\n restoreFocus.current = active instanceof HTMLElement && active !== document.body ? active : null;\n };\n useIsomorphicLayoutEffect(() => {\n const element = restoreFocus.current;\n restoreFocus.current = null;\n if (!element || !element.isConnected) return;\n const active = document.activeElement;\n if (active !== element && (active === null || active === document.body)) element.focus();\n });\n\n // An item that disappears mid-gesture, or the list turning disabled, ends\n // the gesture.\n React.useEffect(() => {\n if (disabled && (grabbed || armed || dragging)) {\n setGrabbed(null);\n setArmed(null);\n setDragging(null);\n setDropAt(null);\n if (!pendingRef.current) setPreview(null);\n return;\n }\n if (dragging && !byKey.has(dragging)) {\n setDragging(null);\n setDropAt(null);\n setArmed(null);\n }\n if (grabbed && !byKey.has(grabbed.key)) {\n setGrabbed(null);\n setPreview(null);\n }\n });\n\n const commit = (key: string, from: number, to: number, message: string) => {\n if (from === to) return;\n pendingRef.current = true;\n setPending(true);\n setPreview(moved(itemKeys, from, to));\n // Called synchronously, so a synchronous handler updates `items` in the\n // same tick as the preview.\n let result: void | Promise<void>;\n try {\n result = onReorder(from, to, { key });\n } catch (error) {\n result = Promise.reject(error);\n }\n Promise.resolve(result)\n .then(\n () => announce(message),\n () => announce(`Couldn’t move ${nameOf(key)}.`),\n )\n .finally(() => {\n // A rejected move reverts the order, which moves rows again.\n keepFocus();\n pendingRef.current = false;\n setPending(false);\n setPreview(null);\n });\n };\n\n const move = (key: string, to: number) => {\n if (disabled || pendingRef.current || grabbed) return;\n if (!Number.isFinite(to)) return;\n const from = itemKeys.indexOf(key);\n const target = Math.max(0, Math.min(total - 1, Math.round(to)));\n if (from < 0 || target === from) return;\n keepFocus();\n commit(key, from, target, `${nameOf(key)} moved to position ${target + 1} of ${total}.`);\n };\n\n const pickUp = (key: string) => {\n if (disabled || pendingRef.current) return;\n const from = itemKeys.indexOf(key);\n if (from < 0) return;\n setGrabbed({ key, from });\n setPreview(itemKeys);\n announce(\n `${nameOf(key)} picked up, position ${from + 1} of ${total}. Use the up and down arrow keys to move, Space to drop, Escape to cancel.`,\n );\n };\n\n const drop = (key: string) => {\n if (!grabbed) return;\n const to = keys.indexOf(key);\n const from = grabbed.from;\n setGrabbed(null);\n if (to === from) {\n setPreview(null);\n announce(`${nameOf(key)} dropped at position ${to + 1} of ${total}.`);\n return;\n }\n keepFocus();\n commit(key, from, to, `${nameOf(key)} dropped at position ${to + 1} of ${total}.`);\n };\n\n const cancel = (key: string) => {\n if (!grabbed) return;\n const from = grabbed.from;\n keepFocus();\n setGrabbed(null);\n setPreview(null);\n announce(`Move cancelled. ${nameOf(key)} is back at position ${from + 1} of ${total}.`);\n };\n\n const step = (key: string, to: number) => {\n const index = keys.indexOf(key);\n const target = Math.max(0, Math.min(total - 1, to));\n if (index < 0 || target === index) return;\n keepFocus();\n setPreview(moved(keys, index, target));\n announce(`${nameOf(key)} moved to position ${target + 1} of ${total}.`);\n };\n\n // Escape while an item is picked up must not also close a surrounding\n // Dialog or Sheet, which listen before the handle does.\n React.useEffect(() => {\n if (!grabbed) return;\n const onKey = (event: KeyboardEvent) => {\n if (event.key !== \"Escape\") return;\n event.preventDefault();\n event.stopPropagation();\n cancel(grabbed.key);\n };\n window.addEventListener(\"keydown\", onKey, true);\n return () => window.removeEventListener(\"keydown\", onKey, true);\n });\n\n // A press on the handle arms its row for native dragging; any release or\n // cancel, wherever it lands, disarms it.\n React.useEffect(() => {\n if (!armed || dragging) return;\n const disarm = () => setArmed(null);\n // A context menu or a release outside the window never sends pointerup.\n const events = [\"pointerup\", \"pointercancel\", \"contextmenu\", \"blur\"] as const;\n for (const type of events) window.addEventListener(type, disarm, { once: true });\n return () => {\n for (const type of events) window.removeEventListener(type, disarm);\n };\n }, [armed, dragging]);\n\n const onHandleKeyDown = (event: React.KeyboardEvent, key: string) => {\n if (disabled) return;\n const isGrabbed = grabbed?.key === key;\n const index = keys.indexOf(key);\n const delta = event.key === \"ArrowUp\" ? -1 : event.key === \"ArrowDown\" ? 1 : 0;\n if (isGrabbed) {\n if (delta) {\n event.preventDefault();\n step(key, index + delta);\n } else if (event.key === \"Home\" || event.key === \"End\") {\n event.preventDefault();\n step(key, event.key === \"Home\" ? 0 : total - 1);\n }\n return;\n }\n if (delta && event.altKey) {\n event.preventDefault();\n move(key, index + delta);\n }\n };\n\n // Space and Enter reach the handle as a click with `detail === 0`, as do\n // screen readers' activations; a pointer click is a press for dragging.\n const onHandleClick = (event: React.MouseEvent, key: string) => {\n if (disabled || event.detail !== 0) return;\n if (grabbed?.key === key) drop(key);\n else if (!grabbed) pickUp(key);\n };\n\n const onDragOver = (event: React.DragEvent<HTMLLIElement>, key: string) => {\n if (!dragging) return;\n event.preventDefault();\n event.dataTransfer.dropEffect = \"move\";\n const rect = event.currentTarget.getBoundingClientRect();\n const edge = event.clientY < rect.top + rect.height / 2 ? \"before\" : \"after\";\n if (dropAt?.key !== key || dropAt.edge !== edge) setDropAt({ key, edge });\n };\n\n const endDrag = () => {\n setDragging(null);\n setDropAt(null);\n setArmed(null);\n };\n\n const onDrop = (event: React.DragEvent) => {\n event.preventDefault();\n const key = event.dataTransfer.getData(DRAG_TYPE) || dragging;\n const target = dropAt;\n endDrag();\n if (!key || !target || pendingRef.current || grabbed) return;\n const from = itemKeys.indexOf(key);\n const over = itemKeys.indexOf(target.key);\n if (from < 0 || over < 0) return;\n // `to` counts positions after the dragged item is taken out.\n let to = target.edge === \"before\" ? over : over + 1;\n if (from < to) to -= 1;\n commit(key, from, to, `${nameOf(key)} dropped at position ${to + 1} of ${total}.`);\n };\n\n return (\n <>\n <ul aria-label={label} aria-busy={pending || undefined} className={cn(\"flex flex-col gap-1\", className)}>\n {keys.map((key, index) => {\n const item = byKey.get(key)!;\n const itemLabel = getItemLabel(item);\n const isGrabbed = grabbed?.key === key;\n const isDragging = dragging === key;\n const handle = disabled ? null : (\n <button\n type=\"button\"\n ref={(node) => {\n if (node) handles.current.set(key, node);\n else handles.current.delete(key);\n }}\n aria-label={`Reorder ${itemLabel}`}\n aria-describedby={instructionsId}\n data-grabbed={isGrabbed || undefined}\n className={cn(\n \"inline-flex h-7 w-5 shrink-0 cursor-grab items-center justify-center rounded text-muted-foreground\",\n \"hover:bg-accent hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n isGrabbed && \"bg-accent text-foreground\",\n )}\n onPointerDown={() => {\n if (!pendingRef.current && !grabbed) setArmed(key);\n }}\n onPointerCancel={() => setArmed(null)}\n onClick={(event) => onHandleClick(event, key)}\n onKeyDown={(event) => onHandleKeyDown(event, key)}\n onBlur={() => {\n // A DOM move during the pick-up blurs the handle for a moment;\n // that is not the user leaving.\n if (isGrabbed && restoreFocus.current !== handles.current.get(key)) cancel(key);\n }}\n >\n <GripVertical className=\"h-4 w-4\" aria-hidden />\n </button>\n );\n return (\n <li\n key={key}\n data-sortable-key={key}\n draggable={(!disabled && armed === key) || undefined}\n className={cn(\n \"relative rounded-md\",\n moveMenu && !disabled && \"pr-8\",\n isDragging && \"opacity-50\",\n isGrabbed && \"ring-2 ring-primary/50\",\n itemClassName,\n )}\n onDragStart={(event) => {\n if (disabled || armed !== key) return;\n event.dataTransfer.setData(DRAG_TYPE, key);\n event.dataTransfer.effectAllowed = \"move\";\n setDragging(key);\n }}\n onDragEnd={endDrag}\n onDragOver={(event) => onDragOver(event, key)}\n onDrop={onDrop}\n >\n {dropAt?.key === key && dragging !== key && (\n <span\n aria-hidden\n className={cn(\n \"pointer-events-none absolute inset-x-0 h-0.5 rounded bg-primary\",\n dropAt.edge === \"before\" ? \"-top-0.5\" : \"-bottom-0.5\",\n )}\n />\n )}\n {renderItem(item, {\n index,\n handle,\n isDragging,\n isGrabbed,\n moveUp: () => move(key, index - 1),\n moveDown: () => move(key, index + 1),\n moveTo: (to) => move(key, to),\n })}\n {moveMenu && !disabled && total > 1 && (\n <DropdownMenu>\n <DropdownMenuTrigger\n aria-label={`Move ${itemLabel}`}\n className=\"absolute right-1 top-1 rounded p-1 text-muted-foreground hover:bg-accent hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n >\n <MoreVertical className=\"h-4 w-4\" aria-hidden />\n </DropdownMenuTrigger>\n <DropdownMenuContent align=\"end\">\n <DropdownMenuItem disabled={index === 0} onSelect={() => move(key, index - 1)}>\n Move up\n </DropdownMenuItem>\n <DropdownMenuItem disabled={index === total - 1} onSelect={() => move(key, index + 1)}>\n Move down\n </DropdownMenuItem>\n <DropdownMenuItem disabled={index === 0} onSelect={() => move(key, 0)}>\n Move to top\n </DropdownMenuItem>\n <DropdownMenuItem disabled={index === total - 1} onSelect={() => move(key, total - 1)}>\n Move to bottom\n </DropdownMenuItem>\n </DropdownMenuContent>\n </DropdownMenu>\n )}\n </li>\n );\n })}\n </ul>\n <p id={instructionsId} hidden>\n Space or Enter picks the item up. Up and down arrows, Home and End move it. Space or Enter drops it, Escape puts\n it back. Alt with an arrow moves it one step.\n </p>\n <div aria-live=\"polite\" aria-atomic=\"true\" className=\"sr-only\">\n {announcement}\n </div>\n </>\n );\n}\n"]}
|