@dxos/react-ui-mosaic 0.3.3-main.006ca4d

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.
Files changed (153) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +45 -0
  3. package/dist/lib/browser/index.mjs +596 -0
  4. package/dist/lib/browser/index.mjs.map +7 -0
  5. package/dist/lib/browser/meta.json +1 -0
  6. package/dist/types/src/index.d.ts +2 -0
  7. package/dist/types/src/index.d.ts.map +1 -0
  8. package/dist/types/src/mosaic/Container.d.ts +65 -0
  9. package/dist/types/src/mosaic/Container.d.ts.map +1 -0
  10. package/dist/types/src/mosaic/Debug.d.ts +9 -0
  11. package/dist/types/src/mosaic/Debug.d.ts.map +1 -0
  12. package/dist/types/src/mosaic/DefaultComponent.d.ts +4 -0
  13. package/dist/types/src/mosaic/DefaultComponent.d.ts.map +1 -0
  14. package/dist/types/src/mosaic/DragOverlay.d.ts +11 -0
  15. package/dist/types/src/mosaic/DragOverlay.d.ts.map +1 -0
  16. package/dist/types/src/mosaic/Mosaic.d.ts +32 -0
  17. package/dist/types/src/mosaic/Mosaic.d.ts.map +1 -0
  18. package/dist/types/src/mosaic/Root.d.ts +24 -0
  19. package/dist/types/src/mosaic/Root.d.ts.map +1 -0
  20. package/dist/types/src/mosaic/SortableContext.d.ts +14 -0
  21. package/dist/types/src/mosaic/SortableContext.d.ts.map +1 -0
  22. package/dist/types/src/mosaic/Tile.d.ts +41 -0
  23. package/dist/types/src/mosaic/Tile.d.ts.map +1 -0
  24. package/dist/types/src/mosaic/hooks/index.d.ts +5 -0
  25. package/dist/types/src/mosaic/hooks/index.d.ts.map +1 -0
  26. package/dist/types/src/mosaic/hooks/useContainer.d.ts +4 -0
  27. package/dist/types/src/mosaic/hooks/useContainer.d.ts.map +1 -0
  28. package/dist/types/src/mosaic/hooks/useItemsWithPreview.d.ts +10 -0
  29. package/dist/types/src/mosaic/hooks/useItemsWithPreview.d.ts.map +1 -0
  30. package/dist/types/src/mosaic/hooks/useMosaic.d.ts +2 -0
  31. package/dist/types/src/mosaic/hooks/useMosaic.d.ts.map +1 -0
  32. package/dist/types/src/mosaic/hooks/useSortedItems.d.ts +6 -0
  33. package/dist/types/src/mosaic/hooks/useSortedItems.d.ts.map +1 -0
  34. package/dist/types/src/mosaic/index.d.ts +5 -0
  35. package/dist/types/src/mosaic/index.d.ts.map +1 -0
  36. package/dist/types/src/mosaic/types.d.ts +9 -0
  37. package/dist/types/src/mosaic/types.d.ts.map +1 -0
  38. package/dist/types/src/mosaic/util/data.d.ts +3 -0
  39. package/dist/types/src/mosaic/util/data.d.ts.map +1 -0
  40. package/dist/types/src/mosaic/util/index.d.ts +4 -0
  41. package/dist/types/src/mosaic/util/index.d.ts.map +1 -0
  42. package/dist/types/src/mosaic/util/path.d.ts +14 -0
  43. package/dist/types/src/mosaic/util/path.d.ts.map +1 -0
  44. package/dist/types/src/mosaic/util/path.test.d.ts +2 -0
  45. package/dist/types/src/mosaic/util/path.test.d.ts.map +1 -0
  46. package/dist/types/src/mosaic/util/styles.d.ts +3 -0
  47. package/dist/types/src/mosaic/util/styles.d.ts.map +1 -0
  48. package/dist/types/src/testing/Cards.d.ts +10 -0
  49. package/dist/types/src/testing/Cards.d.ts.map +1 -0
  50. package/dist/types/src/testing/decorators.d.ts +4 -0
  51. package/dist/types/src/testing/decorators.d.ts.map +1 -0
  52. package/dist/types/src/testing/generator.d.ts +43 -0
  53. package/dist/types/src/testing/generator.d.ts.map +1 -0
  54. package/dist/types/src/testing/index.d.ts +5 -0
  55. package/dist/types/src/testing/index.d.ts.map +1 -0
  56. package/dist/types/src/testing/next-fractional-index.d.ts +5 -0
  57. package/dist/types/src/testing/next-fractional-index.d.ts.map +1 -0
  58. package/dist/types/src/views/Demo/Demo.stories.d.ts +13 -0
  59. package/dist/types/src/views/Demo/Demo.stories.d.ts.map +1 -0
  60. package/dist/types/src/views/Demo/Echo.stories.d.ts +12 -0
  61. package/dist/types/src/views/Demo/Echo.stories.d.ts.map +1 -0
  62. package/dist/types/src/views/Grid/Grid.d.ts +30 -0
  63. package/dist/types/src/views/Grid/Grid.d.ts.map +1 -0
  64. package/dist/types/src/views/Grid/Grid.stories.d.ts +13 -0
  65. package/dist/types/src/views/Grid/Grid.stories.d.ts.map +1 -0
  66. package/dist/types/src/views/Grid/index.d.ts +3 -0
  67. package/dist/types/src/views/Grid/index.d.ts.map +1 -0
  68. package/dist/types/src/views/Grid/layout.d.ts +27 -0
  69. package/dist/types/src/views/Grid/layout.d.ts.map +1 -0
  70. package/dist/types/src/views/Grid/testing.d.ts +10 -0
  71. package/dist/types/src/views/Grid/testing.d.ts.map +1 -0
  72. package/dist/types/src/views/Kanban/Echo.stories.d.ts +18 -0
  73. package/dist/types/src/views/Kanban/Echo.stories.d.ts.map +1 -0
  74. package/dist/types/src/views/Kanban/Kanban.d.ts +11 -0
  75. package/dist/types/src/views/Kanban/Kanban.d.ts.map +1 -0
  76. package/dist/types/src/views/Kanban/Kanban.stories.d.ts +28 -0
  77. package/dist/types/src/views/Kanban/Kanban.stories.d.ts.map +1 -0
  78. package/dist/types/src/views/Kanban/index.d.ts +2 -0
  79. package/dist/types/src/views/Kanban/index.d.ts.map +1 -0
  80. package/dist/types/src/views/Kanban/testing.d.ts +20 -0
  81. package/dist/types/src/views/Kanban/testing.d.ts.map +1 -0
  82. package/dist/types/src/views/Stack/Columns.stories.d.ts +34 -0
  83. package/dist/types/src/views/Stack/Columns.stories.d.ts.map +1 -0
  84. package/dist/types/src/views/Stack/Stack.d.ts +9 -0
  85. package/dist/types/src/views/Stack/Stack.d.ts.map +1 -0
  86. package/dist/types/src/views/Stack/Stack.stories.d.ts +39 -0
  87. package/dist/types/src/views/Stack/Stack.stories.d.ts.map +1 -0
  88. package/dist/types/src/views/Stack/index.d.ts +2 -0
  89. package/dist/types/src/views/Stack/index.d.ts.map +1 -0
  90. package/dist/types/src/views/Stack/testing.d.ts +9 -0
  91. package/dist/types/src/views/Stack/testing.d.ts.map +1 -0
  92. package/dist/types/src/views/Tree/Graph.stories.d.ts +21 -0
  93. package/dist/types/src/views/Tree/Graph.stories.d.ts.map +1 -0
  94. package/dist/types/src/views/Tree/Tree.d.ts +13 -0
  95. package/dist/types/src/views/Tree/Tree.d.ts.map +1 -0
  96. package/dist/types/src/views/Tree/Tree.stories.d.ts +41 -0
  97. package/dist/types/src/views/Tree/Tree.stories.d.ts.map +1 -0
  98. package/dist/types/src/views/Tree/index.d.ts +2 -0
  99. package/dist/types/src/views/Tree/index.d.ts.map +1 -0
  100. package/dist/types/src/views/Tree/testing.d.ts +15 -0
  101. package/dist/types/src/views/Tree/testing.d.ts.map +1 -0
  102. package/dist/types/src/views/index.d.ts +5 -0
  103. package/dist/types/src/views/index.d.ts.map +1 -0
  104. package/package.json +67 -0
  105. package/src/index.ts +5 -0
  106. package/src/mosaic/Container.tsx +137 -0
  107. package/src/mosaic/Debug.tsx +24 -0
  108. package/src/mosaic/DefaultComponent.tsx +26 -0
  109. package/src/mosaic/DragOverlay.tsx +136 -0
  110. package/src/mosaic/Mosaic.ts +84 -0
  111. package/src/mosaic/Root.tsx +255 -0
  112. package/src/mosaic/SortableContext.tsx +60 -0
  113. package/src/mosaic/Tile.tsx +189 -0
  114. package/src/mosaic/hooks/index.ts +8 -0
  115. package/src/mosaic/hooks/useContainer.ts +16 -0
  116. package/src/mosaic/hooks/useItemsWithPreview.ts +64 -0
  117. package/src/mosaic/hooks/useMosaic.ts +11 -0
  118. package/src/mosaic/hooks/useSortedItems.ts +14 -0
  119. package/src/mosaic/index.ts +9 -0
  120. package/src/mosaic/types.ts +13 -0
  121. package/src/mosaic/util/data.ts +19 -0
  122. package/src/mosaic/util/index.ts +7 -0
  123. package/src/mosaic/util/path.test.ts +26 -0
  124. package/src/mosaic/util/path.ts +21 -0
  125. package/src/mosaic/util/styles.ts +10 -0
  126. package/src/testing/Cards.tsx +88 -0
  127. package/src/testing/decorators.tsx +20 -0
  128. package/src/testing/generator.ts +135 -0
  129. package/src/testing/index.ts +9 -0
  130. package/src/testing/next-fractional-index.ts +36 -0
  131. package/src/views/Demo/Demo.stories.tsx +79 -0
  132. package/src/views/Demo/Echo.stories.tsx +74 -0
  133. package/src/views/Grid/Grid.stories.tsx +48 -0
  134. package/src/views/Grid/Grid.tsx +248 -0
  135. package/src/views/Grid/index.ts +7 -0
  136. package/src/views/Grid/layout.ts +42 -0
  137. package/src/views/Grid/testing.tsx +91 -0
  138. package/src/views/Kanban/Echo.stories.tsx +67 -0
  139. package/src/views/Kanban/Kanban.stories.tsx +48 -0
  140. package/src/views/Kanban/Kanban.tsx +139 -0
  141. package/src/views/Kanban/index.ts +5 -0
  142. package/src/views/Kanban/testing.tsx +231 -0
  143. package/src/views/Stack/Columns.stories.tsx +61 -0
  144. package/src/views/Stack/Stack.stories.tsx +62 -0
  145. package/src/views/Stack/Stack.tsx +58 -0
  146. package/src/views/Stack/index.ts +5 -0
  147. package/src/views/Stack/testing.tsx +77 -0
  148. package/src/views/Tree/Graph.stories.tsx +36 -0
  149. package/src/views/Tree/Tree.stories.tsx +92 -0
  150. package/src/views/Tree/Tree.tsx +128 -0
  151. package/src/views/Tree/index.ts +5 -0
  152. package/src/views/Tree/testing.tsx +156 -0
  153. package/src/views/index.ts +8 -0
package/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ MIT License
2
+ Copyright (c) 2022 DXOS
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Mosaic
2
+
3
+ Mosaic is a drag-and-drop layout system for building responsive, multi-column layouts.
4
+ It is built on top of [dnd kit](https://dndkit.com/) and DXOS UI.
5
+
6
+
7
+ ## Taxonomy
8
+
9
+ | Item | Description |
10
+ |:-------------------|:---------------------------------------------------------------------------------------------------------------------|
11
+ | Mosaic.Provider | A Root container and React context that routes events between Containers. |
12
+ | Mosaic.Overlay | A DOM portal managed by the Mosaic context that renders a Container's Component while dragging. |
13
+ | Mosaic.Container | A complex React component that manages the layout of a collection of tiles (e.g., Grid, Kanban, Stack, Table, Tree). |
14
+ | Mosaic.Tile | A Draggable wrapper managed by its parent container. |
15
+ | Component | A pure React component that is rendered within the layout via the container's Tile or the root Mosaic's Overlay. |
16
+ | Item | A datum represented by a Tile. |
17
+
18
+
19
+ ## Design principles
20
+
21
+ - Extends `dnd-kit` without obfuscation or wrapping;
22
+ for example, mosaic provides additional hooks in the same manner as `dnd-kit`'s own useSortable`.
23
+ - Mosaics consist of containers and pure components that can be laid out and rearranged by the user
24
+ - DXOS UI components are pure tailwind-styled Radix components that expose small parts (List, ListItem, ListItem.Header, etc.)
25
+ - Mosaic Containers define their own data model and __assemble__ Radix-style DXOS UI components.
26
+ - Containers implement specific layouts of Tiles, which may reuse common components, such as DXOS UI Cards.
27
+ - Container Models facilitate pure React components, but are easily mapped to data structures (e.g., `Graph`)
28
+ and `ECHO` data sets without the need to wrap/map the underlying __reactive__ objects (e.g., via `signals`).
29
+
30
+
31
+ ## Issues
32
+
33
+ - When dragging across grid always set bounds (currently changes when between cell gaps).
34
+ - Remove padding between tiles in Sortable (causes flickering); instead selectively add padding to tiles.
35
+
36
+ - Projections/Lenses: tile props (e.g., label); position (e.g., index, grid position).
37
+ - Standardize generics (when to use any, unknown, provide default, etc.)
38
+ - Standardize events (e.g., onSelect).
39
+ - Standardize Mosaic Root components (e.g., make generic).
40
+ - https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref#58473012
41
+ - Radix-style components and theming (forwardRefs).
42
+ - Focus (e.g., show grid while dragging in grid).
43
+ - Mobile
44
+
45
+ - Future of dnd-kit: https://github.com/clauderic/dnd-kit/issues/1194#issuecomment-1696704815
@@ -0,0 +1,596 @@
1
+ // packages/ui/react-ui-mosaic/src/mosaic/hooks/useContainer.ts
2
+ import { useContext } from "react";
3
+ import { raise } from "@dxos/debug";
4
+
5
+ // packages/ui/react-ui-mosaic/src/mosaic/Container.tsx
6
+ import React, { createContext, useEffect } from "react";
7
+ var DEFAULT_TRANSITION = 200;
8
+ var MosaicContainerContext = /* @__PURE__ */ createContext(void 0);
9
+ var MosaicContainer = ({ children, id, debug, Component: Component2, transitionDuration = DEFAULT_TRANSITION, modifier, getOverlayProps, getOverlayStyle, onOver, onDrop, compare }) => {
10
+ const mosaic = useMosaic();
11
+ const container = {
12
+ id,
13
+ debug,
14
+ Component: Component2,
15
+ transitionDuration,
16
+ modifier,
17
+ getOverlayProps,
18
+ getOverlayStyle,
19
+ onOver,
20
+ onDrop,
21
+ compare
22
+ };
23
+ useEffect(() => {
24
+ mosaic.setContainer(container.id, container);
25
+ return () => {
26
+ };
27
+ }, Object.values(container));
28
+ return /* @__PURE__ */ React.createElement(MosaicContainerContext.Provider, {
29
+ value: container
30
+ }, children);
31
+ };
32
+
33
+ // packages/ui/react-ui-mosaic/src/mosaic/hooks/useContainer.ts
34
+ var useContainer = () => useContext(MosaicContainerContext) ?? raise(new Error("Missing MosaicContainerContext"));
35
+
36
+ // packages/ui/react-ui-mosaic/src/mosaic/hooks/useItemsWithPreview.ts
37
+ import { useEffect as useEffect2, useState as useState2 } from "react";
38
+
39
+ // packages/ui/react-ui-mosaic/src/mosaic/hooks/useMosaic.ts
40
+ import { useContext as useContext2 } from "react";
41
+ import { raise as raise2 } from "@dxos/debug";
42
+
43
+ // packages/ui/react-ui-mosaic/src/mosaic/Root.tsx
44
+ import { DndContext, KeyboardSensor, MouseSensor, pointerWithin, rectIntersection, TouchSensor, useSensor, useSensors } from "@dnd-kit/core";
45
+ import { sortableKeyboardCoordinates } from "@dnd-kit/sortable";
46
+ import pick from "lodash.pick";
47
+ import React4, { createContext as createContext2, useState } from "react";
48
+ import { createPortal } from "react-dom";
49
+
50
+ // packages/ui/react-ui-mosaic/src/mosaic/Debug.tsx
51
+ import React2 from "react";
52
+ import { mx } from "@dxos/react-ui-theme";
53
+ var Debug = ({ data, position }) => /* @__PURE__ */ React2.createElement("pre", {
54
+ className: mx("font-mono text-xs overflow-hidden m-2 p-1 z-[100]", "bg-neutral-50 dark:bg-neutral-700 border border-neutral-300 rounded", position === "bottom-left" && "fixed left-0 bottom-0", position === "bottom-right" && "fixed right-0 bottom-0")
55
+ }, JSON.stringify(data, void 0, 1));
56
+
57
+ // packages/ui/react-ui-mosaic/src/mosaic/DefaultComponent.tsx
58
+ import React3, { forwardRef } from "react";
59
+ import { Card } from "@dxos/react-ui";
60
+
61
+ // packages/ui/react-ui-mosaic/src/mosaic/util/data.ts
62
+ import { arrayMove } from "@dxos/util";
63
+ var swapItems = (items, from, to) => {
64
+ const fromIndex = items.findIndex((item) => item.id === from.id);
65
+ const toIndex = items.findIndex((item) => item.id === to.id);
66
+ return fromIndex !== -1 && toIndex !== -1 ? arrayMove(items, fromIndex, toIndex) : items;
67
+ };
68
+
69
+ // packages/ui/react-ui-mosaic/src/mosaic/util/path.ts
70
+ var SEPARATOR = "/";
71
+ var Path = {
72
+ create: (...args) => args.join(SEPARATOR),
73
+ parts: (path) => path.split(SEPARATOR),
74
+ length: (path) => path.split(SEPARATOR).length,
75
+ first: (path) => path.split(SEPARATOR)[0] ?? path,
76
+ last: (path) => path.split(SEPARATOR).at(-1) ?? path,
77
+ parent: (path) => path.split(SEPARATOR).slice(0, -1).join(SEPARATOR),
78
+ hasRoot: (path, id) => Path.first(path) === id,
79
+ hasChild: (path, compare) => Path.parent(compare) === path,
80
+ hasDescendent: (path, compare) => compare !== path && compare.startsWith(path),
81
+ siblings: (path, compare) => Path.parent(path) === Path.parent(compare),
82
+ onPath: (path, id) => Path.parts(path).includes(id)
83
+ };
84
+
85
+ // packages/ui/react-ui-mosaic/src/mosaic/util/styles.ts
86
+ import { CSS } from "@dnd-kit/utilities";
87
+ var getTransformCSS = (transform) => transform ? CSS.Transform.toString(Object.assign(transform, {
88
+ scaleX: 1,
89
+ scaleY: 1
90
+ })) : void 0;
91
+
92
+ // packages/ui/react-ui-mosaic/src/mosaic/DefaultComponent.tsx
93
+ var DefaultComponent = /* @__PURE__ */ forwardRef(({ draggableStyle, draggableProps, item: { id }, path }, forwardRef1) => {
94
+ return /* @__PURE__ */ React3.createElement(Card.Root, {
95
+ ref: forwardRef1,
96
+ style: draggableStyle
97
+ }, /* @__PURE__ */ React3.createElement(Card.Header, null, /* @__PURE__ */ React3.createElement(Card.DragHandle, draggableProps), /* @__PURE__ */ React3.createElement(Card.Title, {
98
+ title: Path.create(path, id),
99
+ classNames: "truncate font-mono text-xs"
100
+ })));
101
+ });
102
+ DefaultComponent.displayName = "DefaultComponent";
103
+
104
+ // packages/ui/react-ui-mosaic/src/mosaic/Root.tsx
105
+ var DEFAULT_COMPONENT_ID = "__default";
106
+ var MosaicContext = /* @__PURE__ */ createContext2(void 0);
107
+ var MosaicRoot = ({ Component: Component2 = DefaultComponent, debug, children }) => {
108
+ const [containers, setContainers] = useState({
109
+ [DEFAULT_COMPONENT_ID]: {
110
+ id: DEFAULT_COMPONENT_ID,
111
+ Component: Component2
112
+ }
113
+ });
114
+ const handleSetContainer = (id, container) => {
115
+ setContainers((containers2) => {
116
+ if (container) {
117
+ return {
118
+ ...containers2,
119
+ [id]: container
120
+ };
121
+ } else {
122
+ const { [id]: _, ...rest } = containers2;
123
+ return rest;
124
+ }
125
+ });
126
+ };
127
+ const [activeItem, setActiveItem] = useState();
128
+ const [overItem, setOverItem] = useState();
129
+ const [operation, setOperation] = useState("reject");
130
+ const collisionDetection = (args) => {
131
+ const pointerCollisions = pointerWithin(args);
132
+ if (pointerCollisions.length > 0) {
133
+ return pointerCollisions;
134
+ }
135
+ return rectIntersection(args);
136
+ };
137
+ const modifiers = (props) => {
138
+ const { transform } = props;
139
+ if (activeItem) {
140
+ const container = containers[Path.first(activeItem.path)];
141
+ return container?.modifier?.(activeItem, props) ?? transform;
142
+ } else {
143
+ return transform;
144
+ }
145
+ };
146
+ const sensors = useSensors(useSensor(MouseSensor, {
147
+ activationConstraint: {
148
+ distance: 10
149
+ }
150
+ }), useSensor(TouchSensor, {
151
+ activationConstraint: {
152
+ delay: 200,
153
+ tolerance: 5
154
+ }
155
+ }), useSensor(KeyboardSensor, {
156
+ coordinateGetter: sortableKeyboardCoordinates
157
+ }));
158
+ const handleDragStart = (event) => {
159
+ setActiveItem(pick(event.active.data.current, "path", "item", "position"));
160
+ };
161
+ const handleDragMove = (event) => {
162
+ };
163
+ const handleDragOver = (event) => {
164
+ const overItem2 = pick(event.over?.data.current, "path", "item", "position");
165
+ if (overItem2?.item?.id === activeItem?.item.id) {
166
+ return;
167
+ }
168
+ const activeContainer = activeItem && containers[Path.first(activeItem.path)];
169
+ const overContainer = overItem2?.path && containers[Path.first(overItem2.path)];
170
+ if (!event.over || !overItem2 || !overContainer || !activeItem || !activeContainer) {
171
+ setOperation("reject");
172
+ setOverItem(void 0);
173
+ return;
174
+ }
175
+ const onOver = ({ active, over }) => {
176
+ if (Path.parent(active.path) === Path.parent(over.path)) {
177
+ return "rearrange";
178
+ } else if (overContainer.onOver) {
179
+ return overContainer.onOver({
180
+ active,
181
+ over
182
+ });
183
+ } else {
184
+ return "adopt";
185
+ }
186
+ };
187
+ setOperation(onOver({
188
+ active: activeItem,
189
+ over: overItem2
190
+ }));
191
+ setOverItem(overItem2);
192
+ };
193
+ const handleDragCancel = (event) => {
194
+ setOperation("reject");
195
+ setActiveItem(void 0);
196
+ setOverItem(void 0);
197
+ };
198
+ const handleDragEnd = (event) => {
199
+ const overContainer = overItem && containers[Path.first(overItem.path)];
200
+ if (operation !== "reject" && activeItem && overItem && (activeItem.path !== overItem.path || activeItem.position !== overItem.position)) {
201
+ const activeContainer = containers[Path.first(activeItem.path)];
202
+ if (activeContainer) {
203
+ activeContainer.onDrop?.({
204
+ operation,
205
+ active: activeItem,
206
+ over: overItem
207
+ });
208
+ if (overContainer && overContainer !== activeContainer) {
209
+ overContainer.onDrop?.({
210
+ operation,
211
+ active: activeItem,
212
+ over: overItem
213
+ });
214
+ }
215
+ }
216
+ }
217
+ setTimeout(() => {
218
+ setOperation("reject");
219
+ setOverItem(void 0);
220
+ setActiveItem(void 0);
221
+ }, overContainer?.transitionDuration ?? DEFAULT_TRANSITION);
222
+ };
223
+ return /* @__PURE__ */ React4.createElement(MosaicContext.Provider, {
224
+ value: {
225
+ containers,
226
+ setContainer: handleSetContainer,
227
+ activeItem,
228
+ overItem,
229
+ operation
230
+ }
231
+ }, /* @__PURE__ */ React4.createElement(DndContext, {
232
+ collisionDetection,
233
+ modifiers: [
234
+ modifiers
235
+ ],
236
+ sensors,
237
+ // TODO(burdon): Allow container to veto drop.
238
+ cancelDrop: (event) => false,
239
+ onDragStart: handleDragStart,
240
+ onDragMove: handleDragMove,
241
+ onDragOver: handleDragOver,
242
+ onDragCancel: handleDragCancel,
243
+ onDragEnd: handleDragEnd
244
+ }, children, debug && /* @__PURE__ */ createPortal(/* @__PURE__ */ React4.createElement(MosaicDebug, {
245
+ containers,
246
+ operation,
247
+ activeItem,
248
+ overItem
249
+ }), document.body)));
250
+ };
251
+ var MosaicDebug = ({ containers, operation, activeItem, overItem }) => {
252
+ return /* @__PURE__ */ React4.createElement(Debug, {
253
+ position: "bottom-right",
254
+ data: {
255
+ containers: Object.keys(containers).map((id) => id),
256
+ operation,
257
+ active: {
258
+ id: activeItem?.item?.id.slice(0, 16),
259
+ path: activeItem?.path && Path.create(...Path.parts(activeItem.path).map((part) => part.slice(0, 16))),
260
+ position: activeItem?.position
261
+ },
262
+ over: {
263
+ id: overItem?.item?.id.slice(0, 16),
264
+ path: overItem?.path && Path.create(...Path.parts(overItem.path).map((part) => part.slice(0, 16))),
265
+ position: overItem?.position
266
+ }
267
+ }
268
+ });
269
+ };
270
+
271
+ // packages/ui/react-ui-mosaic/src/mosaic/hooks/useMosaic.ts
272
+ var useMosaic = () => useContext2(MosaicContext) ?? raise2(new Error("Missing MosaicContext"));
273
+
274
+ // packages/ui/react-ui-mosaic/src/mosaic/hooks/useItemsWithPreview.ts
275
+ var useItemsWithPreview = ({ path, items }) => {
276
+ const { operation, activeItem, overItem } = useMosaic();
277
+ const [itemsWithPreview, setItemsWithPreview] = useState2(items);
278
+ const overParent = (
279
+ // If over the sortable context itself & active item is foreign, then treat self as the parent.
280
+ // This allows items to be appended to the end of the sortable.
281
+ overItem?.path === path && activeItem && Path.parent(activeItem.path) !== overItem.path ? path : overItem && Path.parent(overItem.path)
282
+ );
283
+ const [lastOverParent, setLastOverParent] = useState2(overParent);
284
+ useEffect2(() => {
285
+ if (operation === "reject" || operation === "rearrange" || !activeItem || !overItem) {
286
+ setLastOverParent(void 0);
287
+ setItemsWithPreview(items);
288
+ return;
289
+ }
290
+ if (lastOverParent === overParent) {
291
+ return;
292
+ }
293
+ setLastOverParent(overParent);
294
+ const activeIsChild = Path.hasChild(path, activeItem.path);
295
+ const overIsChild = Path.hasChild(path, overItem.path);
296
+ const overSelf = overItem.path === path;
297
+ if (!activeIsChild && overIsChild) {
298
+ setItemsWithPreview((items2) => {
299
+ const position = overItem.position;
300
+ return [
301
+ ...items2.slice(0, position),
302
+ activeItem.item,
303
+ ...items2.slice(position)
304
+ ];
305
+ });
306
+ } else if (!activeIsChild && overSelf) {
307
+ setItemsWithPreview((items2) => [
308
+ ...items2,
309
+ activeItem.item
310
+ ]);
311
+ } else if (activeIsChild && !overIsChild) {
312
+ setItemsWithPreview((items2) => items2.filter((item) => item.id !== activeItem.item.id));
313
+ } else {
314
+ setItemsWithPreview(items);
315
+ }
316
+ }, [
317
+ operation,
318
+ activeItem,
319
+ overItem,
320
+ overParent,
321
+ lastOverParent,
322
+ path,
323
+ items
324
+ ]);
325
+ return items.length === itemsWithPreview.length ? items : itemsWithPreview;
326
+ };
327
+
328
+ // packages/ui/react-ui-mosaic/src/mosaic/hooks/useSortedItems.ts
329
+ var useSortedItems = (items) => {
330
+ const { compare } = useContainer();
331
+ return compare ? [
332
+ ...items
333
+ ].sort(compare) : items;
334
+ };
335
+
336
+ // packages/ui/react-ui-mosaic/src/mosaic/DragOverlay.tsx
337
+ import { DragOverlay } from "@dnd-kit/core";
338
+ import React5, { Component, useEffect as useEffect3, useRef, useState as useState3 } from "react";
339
+ import { DensityProvider } from "@dxos/react-ui";
340
+ var MosaicDragOverlay = ({ delay = 200, debug = false, ...overlayProps }) => {
341
+ const { containers, operation, activeItem, overItem } = useMosaic();
342
+ const [state, setState] = useState3({});
343
+ const timer = useRef();
344
+ useEffect3(() => {
345
+ if (activeItem) {
346
+ let container2;
347
+ let OverlayComponent2;
348
+ if (overItem?.path) {
349
+ container2 = containers[Path.first(overItem.path)];
350
+ OverlayComponent2 = container2?.Component;
351
+ }
352
+ if (!OverlayComponent2) {
353
+ container2 = containers[Path.first(activeItem.path)];
354
+ OverlayComponent2 = container2?.Component ?? DefaultComponent;
355
+ }
356
+ clearTimeout(timer.current);
357
+ timer.current = setTimeout(() => setState({
358
+ container: container2,
359
+ OverlayComponent: OverlayComponent2
360
+ }), timer.current ? delay : 0);
361
+ } else {
362
+ setState({});
363
+ }
364
+ }, [
365
+ activeItem,
366
+ overItem
367
+ ]);
368
+ const container = state.container ?? (activeItem && containers[Path.first(activeItem.path)]);
369
+ const OverlayComponent = state.OverlayComponent ?? container?.Component;
370
+ const FallbackComponent = (activeItem && containers[Path.first(activeItem.path)])?.Component ?? DefaultComponent;
371
+ return (
372
+ // TODO(burdon): Set custom animations (e.g., in/out/around).
373
+ /* @__PURE__ */ React5.createElement(DragOverlay, {
374
+ adjustScale: false,
375
+ ...overlayProps,
376
+ style: {
377
+ ...container?.getOverlayStyle?.()
378
+ }
379
+ }, activeItem?.path && container && OverlayComponent && /* @__PURE__ */ React5.createElement(DensityProvider, {
380
+ density: "fine"
381
+ }, /* @__PURE__ */ React5.createElement(OverlayErrorBoundary, {
382
+ fallback: /* @__PURE__ */ React5.createElement(FallbackComponent, {
383
+ ...container.getOverlayProps?.(),
384
+ item: activeItem.item,
385
+ path: activeItem.path,
386
+ operation,
387
+ active: "overlay"
388
+ })
389
+ }, /* @__PURE__ */ React5.createElement(OverlayComponent, {
390
+ ...container.getOverlayProps?.(),
391
+ item: activeItem.item,
392
+ path: activeItem.path,
393
+ operation,
394
+ active: "overlay"
395
+ }), debug && /* @__PURE__ */ React5.createElement("div", {
396
+ className: "flex flex-wrap mt-1 p-1 bg-neutral-50 dark:bg-neutral-700 text-xs border rounded overflow-hidden gap-1"
397
+ }, /* @__PURE__ */ React5.createElement("span", {
398
+ className: "truncate"
399
+ }, /* @__PURE__ */ React5.createElement("span", {
400
+ className: "text-neutral-400"
401
+ }, "container "), container.id), /* @__PURE__ */ React5.createElement("span", {
402
+ className: "truncate"
403
+ }, /* @__PURE__ */ React5.createElement("span", {
404
+ className: "text-neutral-400"
405
+ }, "item "), activeItem.item.id.slice(0, 8))))))
406
+ );
407
+ };
408
+ var OverlayErrorBoundary = class extends Component {
409
+ constructor(props) {
410
+ super(props);
411
+ this.state = {
412
+ error: null
413
+ };
414
+ this.fallback = props.fallback ?? /* @__PURE__ */ React5.createElement("p", null, "ERROR: $", String(this.state.error));
415
+ }
416
+ static getDerivedStateFromError(error) {
417
+ return {
418
+ error
419
+ };
420
+ }
421
+ componentDidCatch(error, info) {
422
+ console.warn(error, info);
423
+ }
424
+ render() {
425
+ if (this.state.error) {
426
+ return this.fallback;
427
+ }
428
+ return this.props.children;
429
+ }
430
+ };
431
+
432
+ // packages/ui/react-ui-mosaic/src/mosaic/SortableContext.tsx
433
+ import { SortableContext, horizontalListSortingStrategy, verticalListSortingStrategy } from "@dnd-kit/sortable";
434
+ import React6 from "react";
435
+ var MosaicSortableContext = ({ id, items = [], direction = "vertical", children }) => {
436
+ const { operation, activeItem, overItem } = useMosaic();
437
+ const container = useContainer();
438
+ const contextId = id ?? container.id;
439
+ const Sortable = direction === "vertical" ? Column : Row;
440
+ const ids = items.map((item) => {
441
+ const path = Path.create(contextId, item.id);
442
+ const isPreview = activeItem && activeItem.item.id === item.id && overItem && (Path.siblings(overItem.path, path) || Path.hasChild(overItem.path, path)) && operation !== "reject";
443
+ return isPreview ? activeItem.path : path;
444
+ });
445
+ return /* @__PURE__ */ React6.createElement(Sortable, {
446
+ id: contextId,
447
+ items: ids
448
+ }, children);
449
+ };
450
+ var Column = ({ children, id, items }) => /* @__PURE__ */ React6.createElement(SortableContext, {
451
+ id,
452
+ items,
453
+ strategy: verticalListSortingStrategy
454
+ }, children);
455
+ var Row = ({ children, id, items }) => /* @__PURE__ */ React6.createElement(SortableContext, {
456
+ id,
457
+ items,
458
+ strategy: horizontalListSortingStrategy
459
+ }, children);
460
+
461
+ // packages/ui/react-ui-mosaic/src/mosaic/Tile.tsx
462
+ import { useDraggable, useDroppable } from "@dnd-kit/core";
463
+ import { defaultAnimateLayoutChanges, useSortable } from "@dnd-kit/sortable";
464
+ import React7 from "react";
465
+ var DraggableTile = ({ path: parentPath, item, Component: Component2 = DefaultComponent, position, draggableStyle, ...props }) => {
466
+ const { operation } = useMosaic();
467
+ const path = Path.create(parentPath, item.id);
468
+ const {
469
+ setNodeRef,
470
+ attributes,
471
+ listeners,
472
+ /* transform, */
473
+ isDragging
474
+ } = useDraggable({
475
+ id: path,
476
+ data: {
477
+ path,
478
+ item,
479
+ position
480
+ }
481
+ });
482
+ return /* @__PURE__ */ React7.createElement(Component2, {
483
+ ref: setNodeRef,
484
+ item,
485
+ path,
486
+ position,
487
+ operation,
488
+ isDragging,
489
+ draggableStyle: {
490
+ // TODO(burdon): Override by container?
491
+ // transform: getTransformCSS(transform),
492
+ ...draggableStyle
493
+ },
494
+ draggableProps: {
495
+ ...attributes,
496
+ ...listeners
497
+ },
498
+ ...props
499
+ });
500
+ };
501
+ var DroppableTile = ({ path: parentPath, item, Component: Component2 = DefaultComponent, position, ...props }) => {
502
+ const { operation } = useMosaic();
503
+ const path = parentPath === item.id ? parentPath : Path.create(parentPath, item.id);
504
+ const { setNodeRef, isOver } = useDroppable({
505
+ id: path,
506
+ data: {
507
+ path,
508
+ item,
509
+ position
510
+ }
511
+ });
512
+ return /* @__PURE__ */ React7.createElement(Component2, {
513
+ ref: setNodeRef,
514
+ item,
515
+ path,
516
+ position,
517
+ operation,
518
+ isOver,
519
+ ...props
520
+ });
521
+ };
522
+ var SortableTile = ({ path: parentPath, item, Component: OverrideComponent, position, draggableStyle, ...props }) => {
523
+ const { operation, activeItem, overItem } = useMosaic();
524
+ const { transitionDuration, Component: ContainerComponent } = useContainer();
525
+ const path = Path.create(parentPath, item.id);
526
+ const isPreview = activeItem && activeItem.item.id === item.id && overItem && !Path.siblings(activeItem.path, overItem.path) && (Path.siblings(overItem.path, path) || Path.hasChild(overItem.path, path)) && operation !== "reject";
527
+ const { setNodeRef, attributes, listeners, transform, isDragging, isOver } = useSortable({
528
+ id: isPreview ? activeItem.path : path,
529
+ data: {
530
+ path,
531
+ item,
532
+ position
533
+ },
534
+ animateLayoutChanges: (args) => defaultAnimateLayoutChanges({
535
+ ...args,
536
+ wasDragging: item.id !== activeItem?.item.id
537
+ })
538
+ });
539
+ let active;
540
+ if (isDragging) {
541
+ if (operation === "rearrange" || operation === "reject") {
542
+ active = "rearrange";
543
+ } else if (activeItem && Path.parent(activeItem.path) !== parentPath) {
544
+ active = "destination";
545
+ } else {
546
+ active = "origin";
547
+ }
548
+ }
549
+ const Component2 = OverrideComponent ?? ContainerComponent ?? DefaultComponent;
550
+ return /* @__PURE__ */ React7.createElement(Component2, {
551
+ ref: setNodeRef,
552
+ item,
553
+ path,
554
+ position,
555
+ operation,
556
+ active,
557
+ isDragging,
558
+ isOver,
559
+ draggableStyle: {
560
+ transform: getTransformCSS(transform),
561
+ transition: transform ? `transform ${transitionDuration}ms ease` : "none",
562
+ ...draggableStyle
563
+ },
564
+ draggableProps: {
565
+ ...attributes,
566
+ ...listeners
567
+ },
568
+ ...props
569
+ });
570
+ };
571
+
572
+ // packages/ui/react-ui-mosaic/src/mosaic/Mosaic.ts
573
+ var Mosaic = {
574
+ Root: MosaicRoot,
575
+ Container: MosaicContainer,
576
+ DragOverlay: MosaicDragOverlay,
577
+ SortableContext: MosaicSortableContext,
578
+ DraggableTile,
579
+ DroppableTile,
580
+ SortableTile,
581
+ // TODO(wittjosiah): Consider factoring out or using inline styles instead depending on DXOS UI theme/tailwind.
582
+ DefaultComponent,
583
+ // TODO(burdon): Don't export; move to util.
584
+ Debug
585
+ };
586
+ export {
587
+ Mosaic,
588
+ Path,
589
+ getTransformCSS,
590
+ swapItems,
591
+ useContainer,
592
+ useItemsWithPreview,
593
+ useMosaic,
594
+ useSortedItems
595
+ };
596
+ //# sourceMappingURL=index.mjs.map