@noya-app/noya-designsystem 0.1.55 → 0.1.57
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/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +14 -0
- package/dist/index.d.mts +113 -36
- package/dist/index.d.ts +113 -36
- package/dist/index.js +1042 -665
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +995 -625
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/validateDropIndicator.test.ts +35 -9
- package/src/components/Collection.tsx +3 -0
- package/src/components/List.tsx +4 -0
- package/src/components/ListView.tsx +12 -8
- package/src/components/Toolbar.tsx +15 -2
- package/src/components/sorting/DragRegistration.tsx +107 -0
- package/src/components/sorting/SharedDragProvider.tsx +376 -0
- package/src/components/sorting/Sortable.tsx +357 -0
- package/src/components/sorting/createSharedDrag.tsx +41 -0
- package/src/components/sorting/sorting.ts +215 -0
- package/src/index.tsx +4 -2
- package/src/utils/moveTreeItem.ts +4 -2
- package/src/components/Sortable.tsx +0 -468
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noya-app/noya-designsystem",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.57",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"module": "./dist/index.mjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"@noya-app/noya-colorpicker": "0.1.21",
|
|
26
26
|
"@noya-app/noya-utils": "0.1.5",
|
|
27
27
|
"@noya-app/noya-geometry": "0.1.11",
|
|
28
|
-
"@noya-app/noya-icons": "0.1.
|
|
28
|
+
"@noya-app/noya-icons": "0.1.10",
|
|
29
29
|
"@noya-app/noya-keymap": "0.1.3",
|
|
30
30
|
"@noya-app/noya-tailwind-config": "0.1.3",
|
|
31
31
|
"@radix-ui/primitive": "^1.0.0",
|
|
@@ -2,46 +2,46 @@ import { describe, expect, it } from "bun:test";
|
|
|
2
2
|
import {
|
|
3
3
|
defaultAcceptsDrop,
|
|
4
4
|
validateDropIndicator,
|
|
5
|
-
} from "../components/
|
|
5
|
+
} from "../components/sorting/sorting";
|
|
6
6
|
|
|
7
7
|
describe("defaultAcceptsDrop", () => {
|
|
8
8
|
it("should return false if the active item is the same as the over item", () => {
|
|
9
|
-
const result = defaultAcceptsDrop(0, 0, "inside");
|
|
9
|
+
const result = defaultAcceptsDrop(0, 0, "inside", "", "");
|
|
10
10
|
expect(result).toBe(false);
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
it("should return false for 'inside' position", () => {
|
|
14
|
-
const result = defaultAcceptsDrop(0, 1, "inside");
|
|
14
|
+
const result = defaultAcceptsDrop(0, 1, "inside", "", "");
|
|
15
15
|
expect(result).toBe(false);
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
it("should return false when dropping above the same item", () => {
|
|
19
|
-
const result = defaultAcceptsDrop(0, 0, "above");
|
|
19
|
+
const result = defaultAcceptsDrop(0, 0, "above", "", "");
|
|
20
20
|
expect(result).toBe(false);
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
it("should return false when dropping below the same item", () => {
|
|
24
|
-
const result = defaultAcceptsDrop(0, 0, "below");
|
|
24
|
+
const result = defaultAcceptsDrop(0, 0, "below", "", "");
|
|
25
25
|
expect(result).toBe(false);
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
it("should return false when dropping above the item below the source", () => {
|
|
29
|
-
const result = defaultAcceptsDrop(0, 1, "above");
|
|
29
|
+
const result = defaultAcceptsDrop(0, 1, "above", "", "");
|
|
30
30
|
expect(result).toBe(false);
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
it("should return false when dropping below the item above the source", () => {
|
|
34
|
-
const result = defaultAcceptsDrop(1, 0, "below");
|
|
34
|
+
const result = defaultAcceptsDrop(1, 0, "below", "", "");
|
|
35
35
|
expect(result).toBe(false);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
it("should return true when dropping above a valid target", () => {
|
|
39
|
-
const result = defaultAcceptsDrop(0, 2, "above");
|
|
39
|
+
const result = defaultAcceptsDrop(0, 2, "above", "", "");
|
|
40
40
|
expect(result).toBe(true);
|
|
41
41
|
});
|
|
42
42
|
|
|
43
43
|
it("should return true when dropping below a valid target", () => {
|
|
44
|
-
const result = defaultAcceptsDrop(2, 0, "below");
|
|
44
|
+
const result = defaultAcceptsDrop(2, 0, "below", "", "");
|
|
45
45
|
expect(result).toBe(true);
|
|
46
46
|
});
|
|
47
47
|
});
|
|
@@ -97,6 +97,8 @@ describe("validateDropIndicator", () => {
|
|
|
97
97
|
offsetStart: 50, // offsetStart in middle third
|
|
98
98
|
elementStart: 0, // elementStart
|
|
99
99
|
elementSize: 100, // elementSize
|
|
100
|
+
sourceListId: "",
|
|
101
|
+
targetListId: "",
|
|
100
102
|
});
|
|
101
103
|
expect(result).toBe("inside");
|
|
102
104
|
});
|
|
@@ -110,6 +112,8 @@ describe("validateDropIndicator", () => {
|
|
|
110
112
|
offsetStart: 20, // offsetStart in top third
|
|
111
113
|
elementStart: 0, // elementStart
|
|
112
114
|
elementSize: 100, // elementSize
|
|
115
|
+
sourceListId: "",
|
|
116
|
+
targetListId: "",
|
|
113
117
|
});
|
|
114
118
|
expect(result).toBe("above");
|
|
115
119
|
});
|
|
@@ -123,6 +127,8 @@ describe("validateDropIndicator", () => {
|
|
|
123
127
|
offsetStart: 80, // offsetStart in bottom third
|
|
124
128
|
elementStart: 0, // elementStart
|
|
125
129
|
elementSize: 100, // elementSize
|
|
130
|
+
sourceListId: "",
|
|
131
|
+
targetListId: "",
|
|
126
132
|
});
|
|
127
133
|
expect(result).toBe("inside");
|
|
128
134
|
});
|
|
@@ -138,6 +144,8 @@ describe("validateDropIndicator", () => {
|
|
|
138
144
|
offsetStart: 50, // offsetStart in middle third
|
|
139
145
|
elementStart: 0, // elementStart
|
|
140
146
|
elementSize: 100, // elementSize
|
|
147
|
+
sourceListId: "",
|
|
148
|
+
targetListId: "",
|
|
141
149
|
});
|
|
142
150
|
expect(result).toBe("inside");
|
|
143
151
|
});
|
|
@@ -151,6 +159,8 @@ describe("validateDropIndicator", () => {
|
|
|
151
159
|
offsetStart: 20, // offsetStart in top third
|
|
152
160
|
elementStart: 0, // elementStart
|
|
153
161
|
elementSize: 100, // elementSize
|
|
162
|
+
sourceListId: "",
|
|
163
|
+
targetListId: "",
|
|
154
164
|
});
|
|
155
165
|
expect(result).toBe("above");
|
|
156
166
|
});
|
|
@@ -164,6 +174,8 @@ describe("validateDropIndicator", () => {
|
|
|
164
174
|
offsetStart: 80, // offsetStart in bottom third
|
|
165
175
|
elementStart: 0, // elementStart
|
|
166
176
|
elementSize: 100, // elementSize
|
|
177
|
+
sourceListId: "",
|
|
178
|
+
targetListId: "",
|
|
167
179
|
});
|
|
168
180
|
expect(result).toBe("below");
|
|
169
181
|
});
|
|
@@ -177,6 +189,8 @@ describe("validateDropIndicator", () => {
|
|
|
177
189
|
offsetStart: 120, // offsetStart in middle third relative to elementStart
|
|
178
190
|
elementStart: 100, // elementStart
|
|
179
191
|
elementSize: 60, // elementSize
|
|
192
|
+
sourceListId: "",
|
|
193
|
+
targetListId: "",
|
|
180
194
|
});
|
|
181
195
|
expect(result).toBe("inside");
|
|
182
196
|
});
|
|
@@ -191,6 +205,8 @@ describe("validateDropIndicator", () => {
|
|
|
191
205
|
offsetStart: 50, // offsetStart in middle third
|
|
192
206
|
elementStart: 0, // elementStart
|
|
193
207
|
elementSize: 100, // elementSize
|
|
208
|
+
sourceListId: "",
|
|
209
|
+
targetListId: "",
|
|
194
210
|
});
|
|
195
211
|
expect(result).toBe("inside");
|
|
196
212
|
});
|
|
@@ -204,6 +220,8 @@ describe("validateDropIndicator", () => {
|
|
|
204
220
|
offsetStart: 20, // offsetStart in top half
|
|
205
221
|
elementStart: 0, // elementStart
|
|
206
222
|
elementSize: 100, // elementSize
|
|
223
|
+
sourceListId: "",
|
|
224
|
+
targetListId: "",
|
|
207
225
|
});
|
|
208
226
|
expect(result).toBe("above");
|
|
209
227
|
});
|
|
@@ -217,6 +235,8 @@ describe("validateDropIndicator", () => {
|
|
|
217
235
|
offsetStart: 70, // offsetStart in bottom half
|
|
218
236
|
elementStart: 0, // elementStart
|
|
219
237
|
elementSize: 100, // elementSize
|
|
238
|
+
sourceListId: "",
|
|
239
|
+
targetListId: "",
|
|
220
240
|
});
|
|
221
241
|
expect(result).toBe("below");
|
|
222
242
|
});
|
|
@@ -230,6 +250,8 @@ describe("validateDropIndicator", () => {
|
|
|
230
250
|
offsetStart: 20, // offsetStart in top half
|
|
231
251
|
elementStart: 0, // elementStart
|
|
232
252
|
elementSize: 100, // elementSize
|
|
253
|
+
sourceListId: "",
|
|
254
|
+
targetListId: "",
|
|
233
255
|
});
|
|
234
256
|
expect(result).toBe("inside");
|
|
235
257
|
});
|
|
@@ -244,6 +266,8 @@ describe("validateDropIndicator", () => {
|
|
|
244
266
|
offsetStart: 50,
|
|
245
267
|
elementStart: 0,
|
|
246
268
|
elementSize: 100,
|
|
269
|
+
sourceListId: "",
|
|
270
|
+
targetListId: "",
|
|
247
271
|
});
|
|
248
272
|
expect(result).toBeUndefined();
|
|
249
273
|
});
|
|
@@ -257,6 +281,8 @@ describe("validateDropIndicator", () => {
|
|
|
257
281
|
offsetStart: 50,
|
|
258
282
|
elementStart: 0,
|
|
259
283
|
elementSize: 100,
|
|
284
|
+
sourceListId: "",
|
|
285
|
+
targetListId: "",
|
|
260
286
|
});
|
|
261
287
|
expect(result).toBeUndefined();
|
|
262
288
|
});
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
ListViewRowProps,
|
|
9
9
|
} from "../components/ListView";
|
|
10
10
|
import { List } from "./List";
|
|
11
|
+
import { SharedDragProps } from "./sorting/DragRegistration";
|
|
11
12
|
|
|
12
13
|
export type CollectionViewType = "grid" | "list";
|
|
13
14
|
|
|
@@ -93,6 +94,8 @@ export interface CollectionProps<T, M extends string = string> {
|
|
|
93
94
|
/** @default false */
|
|
94
95
|
sortable?: boolean;
|
|
95
96
|
dragIndicatorStyle?: ListViewRowProps<M>["dragIndicatorStyle"];
|
|
97
|
+
sharedDragProps?: SharedDragProps;
|
|
98
|
+
sortableId?: string;
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
export const Collection = forwardRefGeneric(function Collection<
|
package/src/components/List.tsx
CHANGED
|
@@ -64,6 +64,8 @@ export const List = memoGeneric(
|
|
|
64
64
|
getPlaceholder,
|
|
65
65
|
onClickItem,
|
|
66
66
|
dragIndicatorStyle,
|
|
67
|
+
sortableId,
|
|
68
|
+
sharedDragProps,
|
|
67
69
|
} = props;
|
|
68
70
|
|
|
69
71
|
const [internalHoveredId, setHoveredId] = useState<string>();
|
|
@@ -133,6 +135,8 @@ export const List = memoGeneric(
|
|
|
133
135
|
onMoveItem={onMoveItem}
|
|
134
136
|
renderEmptyState={renderEmptyState}
|
|
135
137
|
getDropTargetParentIndex={getDropTargetParentIndex}
|
|
138
|
+
sharedDragProps={sharedDragProps}
|
|
139
|
+
sortableId={sortableId}
|
|
136
140
|
{...dropTargetProps}
|
|
137
141
|
renderItem={(
|
|
138
142
|
item: T,
|
|
@@ -31,13 +31,15 @@ import { ContextMenu } from "./ContextMenu";
|
|
|
31
31
|
import { InputField } from "./InputField";
|
|
32
32
|
import { MenuItem } from "./internal/Menu";
|
|
33
33
|
import { ScrollArea } from "./ScrollArea";
|
|
34
|
+
import { SharedDragProps } from "./sorting/DragRegistration";
|
|
35
|
+
import { Sortable } from "./sorting/Sortable";
|
|
34
36
|
import {
|
|
35
37
|
DropValidator,
|
|
38
|
+
MoveDragItemHandler,
|
|
36
39
|
normalizeListTargetIndex,
|
|
37
40
|
RelativeDropPosition,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
} from "./Sortable";
|
|
41
|
+
SortableItemContextProps,
|
|
42
|
+
} from "./sorting/sorting";
|
|
41
43
|
import { Spacer } from "./Spacer";
|
|
42
44
|
|
|
43
45
|
export type ListRowMarginType = "none" | "top" | "bottom" | "vertical";
|
|
@@ -776,11 +778,7 @@ export type ListViewRootProps = {
|
|
|
776
778
|
onPress?: () => void;
|
|
777
779
|
scrollable?: boolean;
|
|
778
780
|
expandable?: boolean;
|
|
779
|
-
onMoveItem?:
|
|
780
|
-
sourceIndex: number,
|
|
781
|
-
targetIndex: number,
|
|
782
|
-
position: RelativeDropPosition
|
|
783
|
-
) => void;
|
|
781
|
+
onMoveItem?: MoveDragItemHandler;
|
|
784
782
|
indentation?: number;
|
|
785
783
|
acceptsDrop?: DropValidator;
|
|
786
784
|
pressEventName?: PressEventName;
|
|
@@ -796,6 +794,8 @@ export type ListViewRootProps = {
|
|
|
796
794
|
onDrop?: React.DragEventHandler<HTMLDivElement>;
|
|
797
795
|
renderEmptyState?: () => ReactNode;
|
|
798
796
|
getDropTargetParentIndex?: SortableItemContextProps["getDropTargetParentIndex"];
|
|
797
|
+
sharedDragProps?: SharedDragProps;
|
|
798
|
+
sortableId?: string;
|
|
799
799
|
};
|
|
800
800
|
|
|
801
801
|
const ListViewRootInner = forwardRefGeneric(function ListViewRootInner<T>(
|
|
@@ -831,6 +831,8 @@ const ListViewRootInner = forwardRefGeneric(function ListViewRootInner<T>(
|
|
|
831
831
|
onDrop,
|
|
832
832
|
renderEmptyState,
|
|
833
833
|
getDropTargetParentIndex,
|
|
834
|
+
sharedDragProps,
|
|
835
|
+
sortableId,
|
|
834
836
|
}: RenderProps<T> & ListViewRootProps,
|
|
835
837
|
forwardedRef: ForwardedRef<IVirtualizedList>
|
|
836
838
|
) {
|
|
@@ -1003,11 +1005,13 @@ const ListViewRootInner = forwardRefGeneric(function ListViewRootInner<T>(
|
|
|
1003
1005
|
const withSortable = (children: ReactNode) =>
|
|
1004
1006
|
sortable ? (
|
|
1005
1007
|
<Sortable.Root
|
|
1008
|
+
id={sortableId}
|
|
1006
1009
|
onMoveItem={onMoveItem}
|
|
1007
1010
|
keys={ids}
|
|
1008
1011
|
renderOverlay={renderOverlay}
|
|
1009
1012
|
acceptsDrop={acceptsDrop}
|
|
1010
1013
|
getDropTargetParentIndex={getDropTargetParentIndex}
|
|
1014
|
+
sharedDragProps={sharedDragProps}
|
|
1011
1015
|
>
|
|
1012
1016
|
{children}
|
|
1013
1017
|
</Sortable.Root>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useKeyboardShortcuts } from "@noya-app/noya-keymap";
|
|
2
|
-
import React from "react";
|
|
2
|
+
import React, { ComponentProps } from "react";
|
|
3
3
|
|
|
4
4
|
import { memoGeneric } from "@noya-app/react-utils";
|
|
5
5
|
import { BaseToolbar } from "./BaseToolbar";
|
|
@@ -60,16 +60,20 @@ export const ToolbarMenuButton = memoGeneric(function ToolbarMenuButton<
|
|
|
60
60
|
>({
|
|
61
61
|
item,
|
|
62
62
|
onSelectMenuItem,
|
|
63
|
+
as,
|
|
63
64
|
}: {
|
|
64
65
|
item: SelectableMenuItem<T>;
|
|
65
66
|
onSelectMenuItem?: (value: T) => void;
|
|
67
|
+
as?: ComponentProps<typeof Button>["as"];
|
|
66
68
|
}) {
|
|
67
69
|
const content = (
|
|
68
70
|
<Button
|
|
71
|
+
as={as}
|
|
69
72
|
disabled={item.disabled}
|
|
70
73
|
active={item.checked}
|
|
71
74
|
icon={item.icon}
|
|
72
75
|
style={item.icon && !item.title ? iconButtonStyle : undefined}
|
|
76
|
+
{...(as === "a" && { href: item.value })}
|
|
73
77
|
onClick={() => {
|
|
74
78
|
if (onSelectMenuItem && item.value) {
|
|
75
79
|
onSelectMenuItem(item.value);
|
|
@@ -94,9 +98,11 @@ export const ToolbarMenuItem = memoGeneric(function ToolbarMenuItem<
|
|
|
94
98
|
>({
|
|
95
99
|
item,
|
|
96
100
|
onSelectMenuItem,
|
|
101
|
+
buttonAs,
|
|
97
102
|
}: {
|
|
98
103
|
item: MenuItem<T>;
|
|
99
104
|
onSelectMenuItem?: (value: T) => void;
|
|
105
|
+
buttonAs?: ComponentProps<typeof Button>["as"];
|
|
100
106
|
}) {
|
|
101
107
|
if (item.type === "sectionHeader") return null;
|
|
102
108
|
|
|
@@ -106,7 +112,11 @@ export const ToolbarMenuItem = memoGeneric(function ToolbarMenuItem<
|
|
|
106
112
|
|
|
107
113
|
if (isSelectableMenuItem(item)) {
|
|
108
114
|
return (
|
|
109
|
-
<ToolbarMenuButton
|
|
115
|
+
<ToolbarMenuButton
|
|
116
|
+
item={item}
|
|
117
|
+
onSelectMenuItem={onSelectMenuItem}
|
|
118
|
+
as={buttonAs}
|
|
119
|
+
/>
|
|
110
120
|
);
|
|
111
121
|
}
|
|
112
122
|
|
|
@@ -118,9 +128,11 @@ export const ToolbarMenuItem = memoGeneric(function ToolbarMenuItem<
|
|
|
118
128
|
export const ToolbarMenu = memoGeneric(function ToolbarMenu<T extends string>({
|
|
119
129
|
items,
|
|
120
130
|
onSelectMenuItem,
|
|
131
|
+
buttonAs,
|
|
121
132
|
}: {
|
|
122
133
|
items: MenuItem<T>[];
|
|
123
134
|
onSelectMenuItem?: (value: T) => void;
|
|
135
|
+
buttonAs?: ComponentProps<typeof Button>["as"];
|
|
124
136
|
}) {
|
|
125
137
|
return (
|
|
126
138
|
<>
|
|
@@ -130,6 +142,7 @@ export const ToolbarMenu = memoGeneric(function ToolbarMenu<T extends string>({
|
|
|
130
142
|
key={i}
|
|
131
143
|
item={item}
|
|
132
144
|
onSelectMenuItem={onSelectMenuItem}
|
|
145
|
+
buttonAs={buttonAs}
|
|
133
146
|
/>
|
|
134
147
|
);
|
|
135
148
|
})}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AcceptsDrop,
|
|
3
|
+
DragState,
|
|
4
|
+
MoveDragItemHandler,
|
|
5
|
+
RelativeDropPosition,
|
|
6
|
+
} from "./sorting";
|
|
7
|
+
|
|
8
|
+
import { Active, Over, UniqueIdentifier } from "@dnd-kit/core";
|
|
9
|
+
import { AcceptsFromList } from "./sorting";
|
|
10
|
+
|
|
11
|
+
import { Point } from "@noya-app/noya-geometry";
|
|
12
|
+
import React from "react";
|
|
13
|
+
|
|
14
|
+
export type GetDropIndicatorParameters = {
|
|
15
|
+
absolutePosition: Point;
|
|
16
|
+
active: Active;
|
|
17
|
+
over: Over;
|
|
18
|
+
sourceListId: string;
|
|
19
|
+
targetListId: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export interface RegisteredList {
|
|
23
|
+
id: string;
|
|
24
|
+
keys: UniqueIdentifier[];
|
|
25
|
+
onMoveItem: MoveDragItemHandler;
|
|
26
|
+
renderOverlay?: (id: UniqueIdentifier) => React.ReactNode;
|
|
27
|
+
acceptsFromList: AcceptsFromList;
|
|
28
|
+
acceptsDrop: AcceptsDrop;
|
|
29
|
+
getDropIndicator: (
|
|
30
|
+
parameters: GetDropIndicatorParameters
|
|
31
|
+
) => RelativeDropPosition | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface DragRegistrationContextType {
|
|
35
|
+
registerList: (list: RegisteredList) => void;
|
|
36
|
+
unregisterList: (listId: string) => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const DragRegistrationContext =
|
|
40
|
+
React.createContext<DragRegistrationContextType | null>(null);
|
|
41
|
+
|
|
42
|
+
export function useDragRegistration(
|
|
43
|
+
context: React.Context<DragRegistrationContextType | null>
|
|
44
|
+
): DragRegistrationContextType {
|
|
45
|
+
const value = React.useContext(context);
|
|
46
|
+
|
|
47
|
+
if (!value) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"useDragRegistration must be used within a DragRegistrationProvider"
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function useDragRegistrationManager() {
|
|
57
|
+
const [registeredLists, setRegisteredLists] = React.useState<
|
|
58
|
+
Map<string, RegisteredList>
|
|
59
|
+
>(new Map());
|
|
60
|
+
|
|
61
|
+
const registerList = React.useCallback((list: RegisteredList) => {
|
|
62
|
+
setRegisteredLists((prev) => new Map(prev).set(list.id, list));
|
|
63
|
+
}, []);
|
|
64
|
+
|
|
65
|
+
const unregisterList = React.useCallback((listId: string) => {
|
|
66
|
+
setRegisteredLists((prev) => {
|
|
67
|
+
const newMap = new Map(prev);
|
|
68
|
+
newMap.delete(listId);
|
|
69
|
+
return newMap;
|
|
70
|
+
});
|
|
71
|
+
}, []);
|
|
72
|
+
|
|
73
|
+
return { registerList, unregisterList, registeredLists };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type ActiveDragContextType = DragState;
|
|
77
|
+
|
|
78
|
+
export const ActiveDragContext =
|
|
79
|
+
React.createContext<ActiveDragContextType | null>(null);
|
|
80
|
+
|
|
81
|
+
export function useActiveDrag(
|
|
82
|
+
context: React.Context<ActiveDragContextType | null>
|
|
83
|
+
): ActiveDragContextType {
|
|
84
|
+
const value = React.useContext(context);
|
|
85
|
+
|
|
86
|
+
if (!value) {
|
|
87
|
+
throw new Error("useActiveDrag must be used within a ActiveDragProvider");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* We use this context to automatically create a drag context if not exists.
|
|
95
|
+
* We do this separately from the contexts that actually store data since for those
|
|
96
|
+
* we want to allow a custom context to be passed and not get overwritten by nested contexts.
|
|
97
|
+
*/
|
|
98
|
+
export const AnyDragContext = React.createContext<boolean>(false);
|
|
99
|
+
|
|
100
|
+
export function useAnyDragContext() {
|
|
101
|
+
return React.useContext(AnyDragContext);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export type SharedDragProps = {
|
|
105
|
+
dragRegistrationContext: React.Context<DragRegistrationContextType | null>;
|
|
106
|
+
activeDragContext: React.Context<ActiveDragContextType | null>;
|
|
107
|
+
};
|