@dxos/react-ui-mosaic 0.8.4-main.bc674ce → 0.8.4-main.bcb3aa67d6

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 (190) hide show
  1. package/dist/lib/browser/chunk-UTD5AWW5.mjs +1009 -0
  2. package/dist/lib/browser/chunk-UTD5AWW5.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +604 -71
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/playwright/index.mjs +148 -0
  7. package/dist/lib/browser/playwright/index.mjs.map +7 -0
  8. package/dist/lib/browser/testing/index.mjs +69 -236
  9. package/dist/lib/browser/testing/index.mjs.map +4 -4
  10. package/dist/lib/node-esm/chunk-5K56GSJL.mjs +1011 -0
  11. package/dist/lib/node-esm/chunk-5K56GSJL.mjs.map +7 -0
  12. package/dist/lib/node-esm/index.mjs +604 -71
  13. package/dist/lib/node-esm/index.mjs.map +4 -4
  14. package/dist/lib/node-esm/meta.json +1 -1
  15. package/dist/lib/node-esm/playwright/index.mjs +150 -0
  16. package/dist/lib/node-esm/playwright/index.mjs.map +7 -0
  17. package/dist/lib/node-esm/testing/index.mjs +69 -236
  18. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  19. package/dist/types/src/components/Board/Board.d.ts +88 -0
  20. package/dist/types/src/components/Board/Board.d.ts.map +1 -0
  21. package/dist/types/src/components/Board/Board.stories.d.ts +41 -0
  22. package/dist/types/src/components/Board/Board.stories.d.ts.map +1 -0
  23. package/dist/types/src/components/Board/Column.d.ts +48 -0
  24. package/dist/types/src/components/Board/Column.d.ts.map +1 -0
  25. package/dist/types/src/components/Board/Item.d.ts +12 -0
  26. package/dist/types/src/components/Board/Item.d.ts.map +1 -0
  27. package/dist/types/src/components/Board/index.d.ts +2 -0
  28. package/dist/types/src/components/Board/index.d.ts.map +1 -0
  29. package/dist/types/src/components/Focus/Focus.d.ts +1 -24
  30. package/dist/types/src/components/Focus/Focus.d.ts.map +1 -1
  31. package/dist/types/src/components/Focus/index.d.ts +0 -1
  32. package/dist/types/src/components/Focus/index.d.ts.map +1 -1
  33. package/dist/types/src/components/Mosaic/Container.d.ts +49 -0
  34. package/dist/types/src/components/Mosaic/Container.d.ts.map +1 -0
  35. package/dist/types/src/components/Mosaic/Mosaic.d.ts +36 -126
  36. package/dist/types/src/components/Mosaic/Mosaic.d.ts.map +1 -1
  37. package/dist/types/src/components/Mosaic/Placeholder.d.ts +21 -0
  38. package/dist/types/src/components/Mosaic/Placeholder.d.ts.map +1 -0
  39. package/dist/types/src/components/Mosaic/Root.d.ts +36 -0
  40. package/dist/types/src/components/Mosaic/Root.d.ts.map +1 -0
  41. package/dist/types/src/components/Mosaic/Stack.d.ts +23 -0
  42. package/dist/types/src/components/Mosaic/Stack.d.ts.map +1 -0
  43. package/dist/types/src/components/Mosaic/Stack.stories.d.ts +20 -0
  44. package/dist/types/src/components/Mosaic/Stack.stories.d.ts.map +1 -0
  45. package/dist/types/src/components/Mosaic/Tile.d.ts +57 -0
  46. package/dist/types/src/components/Mosaic/Tile.d.ts.map +1 -0
  47. package/dist/types/src/components/Mosaic/styles.d.ts.map +1 -1
  48. package/dist/types/src/components/Mosaic/types.d.ts +9 -2
  49. package/dist/types/src/components/Mosaic/types.d.ts.map +1 -1
  50. package/dist/types/src/components/index.d.ts +1 -4
  51. package/dist/types/src/components/index.d.ts.map +1 -1
  52. package/dist/types/src/hooks/index.d.ts +1 -0
  53. package/dist/types/src/hooks/index.d.ts.map +1 -1
  54. package/dist/types/src/hooks/useDefaultColumnEventHandler.d.ts +15 -0
  55. package/dist/types/src/hooks/useDefaultColumnEventHandler.d.ts.map +1 -0
  56. package/dist/types/src/hooks/useEventHandlerAdapter.d.ts +1 -1
  57. package/dist/types/src/hooks/useEventHandlerAdapter.d.ts.map +1 -1
  58. package/dist/types/src/hooks/useVisibleItems.d.ts +2 -2
  59. package/dist/types/src/hooks/useVisibleItems.d.ts.map +1 -1
  60. package/dist/types/src/playwright/board-manager.d.ts +45 -0
  61. package/dist/types/src/playwright/board-manager.d.ts.map +1 -0
  62. package/dist/types/src/playwright/index.d.ts +9 -0
  63. package/dist/types/src/playwright/index.d.ts.map +1 -0
  64. package/dist/types/src/playwright/playwright.config.d.ts +3 -0
  65. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -0
  66. package/dist/types/src/playwright/smoke.spec.d.ts +2 -0
  67. package/dist/types/src/playwright/smoke.spec.d.ts.map +1 -0
  68. package/dist/types/src/testing/CardContainer.d.ts +5 -4
  69. package/dist/types/src/testing/CardContainer.d.ts.map +1 -1
  70. package/dist/types/src/testing/DefaultStackTile.d.ts +2 -2
  71. package/dist/types/src/testing/DefaultStackTile.d.ts.map +1 -1
  72. package/dist/types/src/testing/index.d.ts +1 -1
  73. package/dist/types/src/testing/index.d.ts.map +1 -1
  74. package/dist/types/src/testing/types.d.ts +21 -0
  75. package/dist/types/src/testing/types.d.ts.map +1 -0
  76. package/dist/types/src/translations.d.ts +5 -1
  77. package/dist/types/src/translations.d.ts.map +1 -1
  78. package/dist/types/tsconfig.tsbuildinfo +1 -1
  79. package/package.json +43 -28
  80. package/src/AUDIT.md +106 -0
  81. package/src/components/Board/Board.stories.tsx +273 -0
  82. package/src/components/Board/Board.tsx +191 -0
  83. package/src/components/Board/Column.tsx +302 -0
  84. package/src/components/Board/Item.tsx +116 -0
  85. package/src/components/{Layout → Board}/index.ts +1 -1
  86. package/src/components/Focus/Focus.tsx +2 -93
  87. package/src/components/Focus/index.ts +0 -2
  88. package/src/components/Mosaic/Container.tsx +318 -0
  89. package/src/components/Mosaic/Mosaic.tsx +35 -913
  90. package/src/components/Mosaic/Placeholder.tsx +133 -0
  91. package/src/components/Mosaic/Root.tsx +292 -0
  92. package/src/components/Mosaic/Stack.stories.tsx +176 -0
  93. package/src/components/Mosaic/Stack.tsx +321 -0
  94. package/src/components/Mosaic/Tile.tsx +247 -0
  95. package/src/components/Mosaic/extension.ts +1 -1
  96. package/src/components/Mosaic/styles.ts +9 -8
  97. package/src/components/Mosaic/types.ts +15 -3
  98. package/src/components/index.ts +1 -4
  99. package/src/hooks/index.ts +1 -0
  100. package/src/hooks/useContainerDebug.tsx +5 -3
  101. package/src/hooks/useDefaultColumnEventHandler.ts +35 -0
  102. package/src/hooks/useEventHandlerAdapter.ts +15 -6
  103. package/src/hooks/useVisibleItems.ts +7 -2
  104. package/src/playwright/board-manager.ts +159 -0
  105. package/src/playwright/index.ts +13 -0
  106. package/src/playwright/playwright.config.ts +17 -0
  107. package/src/playwright/smoke.spec.ts +143 -0
  108. package/src/testing/CardContainer.tsx +31 -19
  109. package/src/testing/DefaultStackTile.tsx +36 -22
  110. package/src/testing/index.ts +1 -1
  111. package/src/testing/types.ts +38 -0
  112. package/src/translations.ts +5 -1
  113. package/dist/lib/browser/chunk-UMBPEG4F.mjs +0 -1637
  114. package/dist/lib/browser/chunk-UMBPEG4F.mjs.map +0 -7
  115. package/dist/lib/browser/index.css +0 -13
  116. package/dist/lib/browser/index.css.map +0 -7
  117. package/dist/lib/browser/testing/index.css +0 -13
  118. package/dist/lib/browser/testing/index.css.map +0 -7
  119. package/dist/lib/node-esm/chunk-X6GV7SMK.mjs +0 -1639
  120. package/dist/lib/node-esm/chunk-X6GV7SMK.mjs.map +0 -7
  121. package/dist/lib/node-esm/index.css +0 -13
  122. package/dist/lib/node-esm/index.css.map +0 -7
  123. package/dist/lib/node-esm/testing/index.css +0 -13
  124. package/dist/lib/node-esm/testing/index.css.map +0 -7
  125. package/dist/types/src/components/Card/Card.d.ts +0 -121
  126. package/dist/types/src/components/Card/Card.d.ts.map +0 -1
  127. package/dist/types/src/components/Card/Card.stories.d.ts +0 -21
  128. package/dist/types/src/components/Card/Card.stories.d.ts.map +0 -1
  129. package/dist/types/src/components/Card/CardDragPreview.d.ts +0 -6
  130. package/dist/types/src/components/Card/CardDragPreview.d.ts.map +0 -1
  131. package/dist/types/src/components/Card/index.d.ts +0 -4
  132. package/dist/types/src/components/Card/index.d.ts.map +0 -1
  133. package/dist/types/src/components/Card/styles.d.ts +0 -13
  134. package/dist/types/src/components/Card/styles.d.ts.map +0 -1
  135. package/dist/types/src/components/Focus/styles.d.ts +0 -6
  136. package/dist/types/src/components/Focus/styles.d.ts.map +0 -1
  137. package/dist/types/src/components/Image/Image.d.ts +0 -14
  138. package/dist/types/src/components/Image/Image.d.ts.map +0 -1
  139. package/dist/types/src/components/Image/Image.stories.d.ts +0 -33
  140. package/dist/types/src/components/Image/Image.stories.d.ts.map +0 -1
  141. package/dist/types/src/components/Image/index.d.ts +0 -2
  142. package/dist/types/src/components/Image/index.d.ts.map +0 -1
  143. package/dist/types/src/components/Layout/Layout.d.ts +0 -33
  144. package/dist/types/src/components/Layout/Layout.d.ts.map +0 -1
  145. package/dist/types/src/components/Layout/Layout.stories.d.ts +0 -10
  146. package/dist/types/src/components/Layout/Layout.stories.d.ts.map +0 -1
  147. package/dist/types/src/components/Layout/index.d.ts +0 -2
  148. package/dist/types/src/components/Layout/index.d.ts.map +0 -1
  149. package/dist/types/src/components/Mosaic/Mosaic.stories.d.ts +0 -27
  150. package/dist/types/src/components/Mosaic/Mosaic.stories.d.ts.map +0 -1
  151. package/dist/types/src/components/Scrollable/Scrollable.d.ts +0 -24
  152. package/dist/types/src/components/Scrollable/Scrollable.d.ts.map +0 -1
  153. package/dist/types/src/components/Scrollable/Scrollable.stories.d.ts +0 -10
  154. package/dist/types/src/components/Scrollable/Scrollable.stories.d.ts.map +0 -1
  155. package/dist/types/src/components/Scrollable/index.d.ts +0 -2
  156. package/dist/types/src/components/Scrollable/index.d.ts.map +0 -1
  157. package/dist/types/src/components/Stack/Stack.d.ts +0 -22
  158. package/dist/types/src/components/Stack/Stack.d.ts.map +0 -1
  159. package/dist/types/src/components/Stack/Stack.stories.d.ts +0 -9
  160. package/dist/types/src/components/Stack/Stack.stories.d.ts.map +0 -1
  161. package/dist/types/src/components/Stack/index.d.ts +0 -2
  162. package/dist/types/src/components/Stack/index.d.ts.map +0 -1
  163. package/dist/types/src/testing/Board.d.ts +0 -45
  164. package/dist/types/src/testing/Board.d.ts.map +0 -1
  165. package/dist/types/src/testing/Slot.stories.d.ts +0 -14
  166. package/dist/types/src/testing/Slot.stories.d.ts.map +0 -1
  167. package/dist/types/src/testing/Virtualizer.stories.d.ts +0 -12
  168. package/dist/types/src/testing/Virtualizer.stories.d.ts.map +0 -1
  169. package/src/components/Card/Card.stories.tsx +0 -148
  170. package/src/components/Card/Card.tsx +0 -458
  171. package/src/components/Card/CardDragPreview.tsx +0 -23
  172. package/src/components/Card/index.ts +0 -8
  173. package/src/components/Card/styles.ts +0 -23
  174. package/src/components/Focus/styles.ts +0 -24
  175. package/src/components/Image/Image.stories.tsx +0 -86
  176. package/src/components/Image/Image.tsx +0 -223
  177. package/src/components/Image/index.ts +0 -5
  178. package/src/components/Layout/Layout.stories.tsx +0 -59
  179. package/src/components/Layout/Layout.tsx +0 -108
  180. package/src/components/Mosaic/Mosaic.stories.tsx +0 -113
  181. package/src/components/Scrollable/Scrollable.stories.tsx +0 -59
  182. package/src/components/Scrollable/Scrollable.tsx +0 -88
  183. package/src/components/Scrollable/index.ts +0 -5
  184. package/src/components/Scrollable/scrollable.css +0 -20
  185. package/src/components/Stack/Stack.stories.tsx +0 -115
  186. package/src/components/Stack/Stack.tsx +0 -210
  187. package/src/components/Stack/index.ts +0 -5
  188. package/src/testing/Board.tsx +0 -278
  189. package/src/testing/Slot.stories.tsx +0 -101
  190. package/src/testing/Virtualizer.stories.tsx +0 -133
@@ -0,0 +1,133 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { dropTargetForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
6
+ import {
7
+ DropIndicator as NaturalDropIndicator,
8
+ type DropIndicatorProps as NaturalDropIndicatorProps,
9
+ } from '@atlaskit/pragmatic-drag-and-drop-react-drop-indicator/box';
10
+ import { Primitive } from '@radix-ui/react-primitive';
11
+ import { Slot } from '@radix-ui/react-slot';
12
+ import React, { type PropsWithChildren, useLayoutEffect, useMemo, useRef } from 'react';
13
+
14
+ import { type Axis, type ThemedClassName } from '@dxos/react-ui';
15
+ import { mx } from '@dxos/ui-theme';
16
+
17
+ import { useMosaicContainerContext } from './Container';
18
+ import { useMosaicTileContext } from './Tile';
19
+ import { type LocationType, type MosaicPlaceholderData, getSourceData } from './types';
20
+
21
+ //
22
+ // Placeholder
23
+ //
24
+
25
+ const MOSAIC_PLACEHOLDER_NAME = 'Mosaic.Placeholder';
26
+
27
+ // Orientation: data-[mosaic-placeholder-orientation=vertical]
28
+ const MOSAIC_PLACEHOLDER_ORIENTATION_ATTR = 'mosaic-placeholder-orientation';
29
+
30
+ // State attribute: data-[mosaic-placeholder-state=active]
31
+ const MOSAIC_PLACEHOLDER_STATE_ATTR = 'mosaic-placeholder-state';
32
+
33
+ type MosaicPlaceholderProps<Location = LocationType> = ThemedClassName<
34
+ PropsWithChildren<{
35
+ asChild?: boolean;
36
+ orientation?: Axis;
37
+ location: Location;
38
+ }>
39
+ >;
40
+
41
+ const MosaicPlaceholder = <Location extends LocationType = LocationType>({
42
+ classNames,
43
+ children,
44
+ asChild,
45
+ orientation = 'vertical',
46
+ location,
47
+ }: MosaicPlaceholderProps<Location>) => {
48
+ const rootRef = useRef<HTMLDivElement>(null);
49
+ const Comp = asChild ? Slot : Primitive.div;
50
+ const {
51
+ id: containerId,
52
+ eventHandler,
53
+ scrolling,
54
+ activeLocation,
55
+ setActiveLocation,
56
+ } = useMosaicContainerContext(MOSAIC_PLACEHOLDER_NAME);
57
+ const data = useMemo<MosaicPlaceholderData<Location>>(
58
+ () =>
59
+ ({
60
+ type: 'placeholder',
61
+ containerId,
62
+ location,
63
+ }) satisfies MosaicPlaceholderData<Location>,
64
+ [containerId, location],
65
+ );
66
+
67
+ useLayoutEffect(() => {
68
+ const root = rootRef.current;
69
+ if (!root || scrolling) {
70
+ return;
71
+ }
72
+
73
+ return dropTargetForElements({
74
+ element: root,
75
+ getData: () => data,
76
+ canDrop: ({ source }) => {
77
+ const data = getSourceData(source);
78
+ return (data && eventHandler.canDrop?.({ source: data })) || false;
79
+ },
80
+ onDragEnter: () => {
81
+ setActiveLocation(data.location);
82
+ },
83
+ onDragLeave: () => {
84
+ setActiveLocation(undefined);
85
+ },
86
+ onDrop: () => {
87
+ setActiveLocation(undefined);
88
+ },
89
+ });
90
+ }, [rootRef, data, scrolling, setActiveLocation]);
91
+
92
+ return (
93
+ <Comp
94
+ {...{
95
+ [`data-${MOSAIC_PLACEHOLDER_ORIENTATION_ATTR}`]: orientation,
96
+ [`data-${MOSAIC_PLACEHOLDER_STATE_ATTR}`]: data.location === activeLocation ? 'active' : 'idle',
97
+ }}
98
+ role='none'
99
+ className={mx('relative', classNames)}
100
+ ref={rootRef}
101
+ >
102
+ {children}
103
+ </Comp>
104
+ );
105
+ };
106
+
107
+ MosaicPlaceholder.displayName = MOSAIC_PLACEHOLDER_NAME;
108
+
109
+ //
110
+ // DropIndicator
111
+ // TODO(burdon): Support DropIndicator or Placeholder variants.
112
+ //
113
+
114
+ const DROP_INDICATOR_NAME = 'Mosaic.DropIndicator';
115
+
116
+ type MosaicDropIndicatorProps = Omit<NaturalDropIndicatorProps, 'edge'>;
117
+
118
+ const MosaicDropIndicator = (props: MosaicDropIndicatorProps) => {
119
+ const { state } = useMosaicTileContext(DROP_INDICATOR_NAME);
120
+ return state.type === 'target' && state.closestEdge ? (
121
+ <NaturalDropIndicator {...props} edge={state.closestEdge} />
122
+ ) : null;
123
+ };
124
+
125
+ MosaicDropIndicator.displayName = DROP_INDICATOR_NAME;
126
+
127
+ //
128
+ // Exports
129
+ //
130
+
131
+ export { MosaicPlaceholder, MosaicDropIndicator };
132
+
133
+ export type { MosaicPlaceholderProps, MosaicDropIndicatorProps };
@@ -0,0 +1,292 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { combine } from '@atlaskit/pragmatic-drag-and-drop/combine';
6
+ import { type ElementDragPayload, monitorForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter';
7
+ import { type DragLocationHistory } from '@atlaskit/pragmatic-drag-and-drop/types';
8
+ import { useComposedRefs } from '@radix-ui/react-compose-refs';
9
+ import { createContext } from '@radix-ui/react-context';
10
+ import { Primitive } from '@radix-ui/react-primitive';
11
+ import { Slot } from '@radix-ui/react-slot';
12
+ import React, { type PropsWithChildren, forwardRef, useCallback, useEffect, useRef, useState } from 'react';
13
+
14
+ import { log } from '@dxos/log';
15
+ import { type ThemedClassName } from '@dxos/react-ui';
16
+ import { mx } from '@dxos/ui-theme';
17
+
18
+ import { type MosaicData, type MosaicEventHandler, type MosaicTileData } from './types';
19
+
20
+ //
21
+ // Context
22
+ //
23
+
24
+ type MosaicDraggingSource = {
25
+ data: MosaicTileData;
26
+ handler?: MosaicEventHandler;
27
+ container?: Element;
28
+ };
29
+
30
+ type MosaicDraggingTarget = {
31
+ data: MosaicData;
32
+ handler?: MosaicEventHandler;
33
+ };
34
+
35
+ type MosaicDraggingState = {
36
+ source: MosaicDraggingSource;
37
+ target?: MosaicDraggingTarget;
38
+ };
39
+
40
+ type MosaicRootContextValue = {
41
+ containers: Record<string, MosaicEventHandler>;
42
+ addContainer: (container: MosaicEventHandler) => void;
43
+ removeContainer: (id: string) => void;
44
+ dragging?: MosaicDraggingState;
45
+ };
46
+
47
+ const [MosaicRootContextProvider, useMosaicRootContext] = createContext<MosaicRootContextValue>('MosaicRoot');
48
+
49
+ //
50
+ // Root
51
+ //
52
+
53
+ const MOSAIC_ROOT_NAME = 'Mosaic.Root';
54
+
55
+ // State attribute: [&:has(>_[data-mosaic-debug=true])]
56
+ const MOSAIC_ROOT_DEBUG_ATTR = 'mosaic-debug';
57
+
58
+ type MosaicRootProps = ThemedClassName<
59
+ PropsWithChildren<{
60
+ asChild?: boolean;
61
+ debug?: boolean;
62
+ }>
63
+ >;
64
+
65
+ const MosaicRoot = forwardRef<HTMLDivElement, MosaicRootProps>(
66
+ ({ classNames, children, asChild, debug }, forwardedRef) => {
67
+ const rootRef = useRef<HTMLDivElement>(null);
68
+ const composedRef = useComposedRefs<HTMLDivElement>(rootRef, forwardedRef);
69
+ const Comp = asChild ? Slot : Primitive.div;
70
+
71
+ const [handlers, setHandlers] = useState<Record<string, MosaicEventHandler>>({});
72
+ const [dragging, setDragging] = useState<MosaicDraggingState | undefined>();
73
+
74
+ const getSourceHandler = useCallback(
75
+ (source: ElementDragPayload): { data: MosaicTileData; handler?: MosaicEventHandler } => {
76
+ const data = source.data as MosaicTileData;
77
+ return { data, handler: handlers[data.containerId] };
78
+ },
79
+ [handlers],
80
+ );
81
+
82
+ const getTargetHandler = useCallback(
83
+ (location: DragLocationHistory): { data?: MosaicData; handler?: MosaicEventHandler } => {
84
+ for (const target of location.current.dropTargets) {
85
+ const data = target.data as MosaicData;
86
+ let containerId: string;
87
+ switch (data.type) {
88
+ case 'tile':
89
+ case 'placeholder':
90
+ containerId = data.containerId;
91
+ break;
92
+ case 'container':
93
+ containerId = data.id;
94
+ break;
95
+ }
96
+
97
+ const handler = handlers[containerId];
98
+ if (handler) {
99
+ return { data, handler };
100
+ }
101
+ }
102
+
103
+ return {};
104
+ },
105
+ [handlers],
106
+ );
107
+
108
+ useEffect(() => {
109
+ const handleChange = ({ source, location }: { source: ElementDragPayload; location: DragLocationHistory }) => {
110
+ const { data: sourceData } = getSourceHandler(source);
111
+ const { data: targetData, handler } = getTargetHandler(location);
112
+ setDragging((dragging) => {
113
+ dragging?.target?.handler?.onCancel?.();
114
+ return {
115
+ source: {
116
+ data: sourceData,
117
+ handler: handlers[sourceData.containerId],
118
+ // TOOD(burdon): Check id matches.
119
+ container: location.initial.dropTargets.find((target) => target.data.type === 'container')?.element,
120
+ },
121
+ target: targetData && {
122
+ data: targetData,
123
+ handler,
124
+ },
125
+ };
126
+ });
127
+ };
128
+
129
+ const handleCancel = () => {
130
+ setDragging((dragging) => {
131
+ requestAnimationFrame(() => {
132
+ dragging?.target?.handler?.onCancel?.();
133
+ dragging?.source?.container?.dispatchEvent(new CustomEvent('dnd:cancel', { bubbles: true }));
134
+ });
135
+
136
+ return undefined;
137
+ });
138
+ };
139
+
140
+ let noTargetTimeout: NodeJS.Timeout;
141
+
142
+ // Main controller.
143
+ return combine(
144
+ monitorForElements({
145
+ /**
146
+ * Dragging started within any container.
147
+ */
148
+ onDragStart: ({ source, location }) => {
149
+ log('Root.onDragStart', {
150
+ source: source.data,
151
+ location: location.current.dropTargets.map((target) => target.data),
152
+ });
153
+
154
+ handleChange({ source, location });
155
+ },
156
+
157
+ /**
158
+ * Dragging entered a new container.
159
+ */
160
+ onDropTargetChange: ({ source, location }) => {
161
+ clearTimeout(noTargetTimeout);
162
+ log('Root.onDropTargetChange', {
163
+ source: source.data,
164
+ location: location.current.dropTargets.map((target) => target.data),
165
+ });
166
+
167
+ // Stop dragging if there are no drop targets (or we are cancelling).
168
+ if (location.current.dropTargets.length === 0) {
169
+ noTargetTimeout = setTimeout(() => setDragging(undefined), 1_000);
170
+ } else {
171
+ handleChange({ source, location });
172
+ }
173
+ },
174
+
175
+ /**
176
+ * Dragging within any container.
177
+ */
178
+ onDrag: ({ source, location }) => {
179
+ const { data } = getSourceHandler(source);
180
+ const { handler } = getTargetHandler(location);
181
+ if (handler) {
182
+ const { clientX: x, clientY: y } = location.current.input;
183
+ handler.onDrag?.({ source: data, position: { x, y } });
184
+ }
185
+ },
186
+
187
+ /**
188
+ * Dragging ended.
189
+ */
190
+ onDrop: ({ source, location }) => {
191
+ log.info('Root.onDrop', {
192
+ source: source.data,
193
+ location: location.current.dropTargets.map((target) => target.data),
194
+ });
195
+
196
+ // Get the source container.
197
+ const { data: sourceData, handler: sourceHandler } = getSourceHandler(source);
198
+ if (!sourceHandler) {
199
+ log.warn('invalid source', {
200
+ source: sourceData,
201
+ handlers: Object.keys(handlers),
202
+ });
203
+ return;
204
+ }
205
+
206
+ // NOTE: When dragging is cancelled (e.g., user presses ESC) onDrop is only called after a subsequent event.
207
+ // NOTE: pDND blocks ESC event propagation while dragging.
208
+ // - ESC only flips internal state.
209
+ // - Completion happens on the next processed input event.
210
+ // - This avoids reentrancy and keeps pointer/keyboard behavior consistent.
211
+ // - We set dragging to undefined in onDropTargetChange after a delay if there are no drop targets.
212
+ try {
213
+ if (location.current.dropTargets.length === 0) {
214
+ log.info('cancelled');
215
+ } else {
216
+ // Get the target container.
217
+ const { data: targetData, handler: targetHandler } = getTargetHandler(location);
218
+ if (!targetHandler) {
219
+ log.warn('invalid target', {
220
+ source: sourceData,
221
+ location,
222
+ handlers: Object.keys(handlers),
223
+ });
224
+ return;
225
+ }
226
+
227
+ // TODO(burdon): Check object doesn't already exist in the collection.
228
+ if (sourceHandler === targetHandler) {
229
+ targetHandler.onDrop?.({
230
+ source: sourceData,
231
+ target: targetData,
232
+ });
233
+ } else {
234
+ if (!sourceHandler.onTake) {
235
+ log.warn('invalid source', { source: sourceData });
236
+ return;
237
+ }
238
+
239
+ sourceHandler.onTake?.({ source: sourceData }, async (object) => {
240
+ targetHandler.onDrop?.({
241
+ source: { ...sourceData, data: object },
242
+ target: targetData,
243
+ });
244
+
245
+ return true;
246
+ });
247
+ }
248
+ }
249
+ } finally {
250
+ handleCancel();
251
+ }
252
+ },
253
+ }),
254
+ );
255
+ }, [handlers, getSourceHandler, getTargetHandler]);
256
+
257
+ return (
258
+ <MosaicRootContextProvider
259
+ containers={handlers}
260
+ addContainer={(container) =>
261
+ setHandlers((containers) => ({
262
+ ...containers,
263
+ [container.id]: container,
264
+ }))
265
+ }
266
+ removeContainer={(id) =>
267
+ setHandlers((containers) => {
268
+ delete containers[id];
269
+ return { ...containers };
270
+ })
271
+ }
272
+ dragging={dragging}
273
+ >
274
+ <Comp
275
+ className={mx('group', classNames)}
276
+ {...{
277
+ [`data-${MOSAIC_ROOT_DEBUG_ATTR}`]: debug,
278
+ }}
279
+ ref={composedRef}
280
+ >
281
+ {children}
282
+ </Comp>
283
+ </MosaicRootContextProvider>
284
+ );
285
+ },
286
+ );
287
+
288
+ MosaicRoot.displayName = MOSAIC_ROOT_NAME;
289
+
290
+ export { MosaicRoot, useMosaicRootContext };
291
+
292
+ export type { MosaicRootProps, MosaicDraggingState };
@@ -0,0 +1,176 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
+ import React, { useState } from 'react';
7
+ import { useMemo } from 'react';
8
+
9
+ import { Obj } from '@dxos/echo';
10
+ import { faker } from '@dxos/random';
11
+ import { Panel, ScrollArea, Toolbar } from '@dxos/react-ui';
12
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
13
+
14
+ import { useContainerDebug } from '../../hooks';
15
+ import { DefaultStackTile, TestItem } from '../../testing';
16
+
17
+ import { Mosaic, MosaicStackProps } from './Mosaic';
18
+ import { MosaicStack } from './Stack';
19
+ import { Focus } from '../Focus';
20
+
21
+ faker.seed(999);
22
+
23
+ const NUM_ITEMS = 50;
24
+
25
+ // Create test items factory (deferred to render time).
26
+ const createTestItems = (n: number) =>
27
+ Array.from({ length: n }, () =>
28
+ Obj.make(TestItem, {
29
+ name: faker.lorem.sentence(3),
30
+ description: faker.lorem.paragraph(),
31
+ }),
32
+ );
33
+
34
+ const DefaultStackStory = (props: MosaicStackProps<Obj.Any>) => {
35
+ // Create items at render time to avoid Storybook serialization issues with ECHO objects.
36
+ const items = useMemo(() => createTestItems(NUM_ITEMS), []);
37
+ const [DebugInfo, debugHandler] = useContainerDebug(props.debug);
38
+ const [viewport, setViewport] = useState<HTMLElement | null>(null);
39
+ return (
40
+ <Mosaic.Root classNames='dx-container' debug={props.debug}>
41
+ <Panel.Root>
42
+ <Panel.Toolbar asChild>
43
+ <Toolbar.Root>
44
+ <div className='flex grow justify-center'>Items: {items.length}</div>
45
+ </Toolbar.Root>
46
+ </Panel.Toolbar>
47
+ <Panel.Content asChild>
48
+ <Focus.Group asChild>
49
+ <Mosaic.Container
50
+ asChild
51
+ orientation='vertical'
52
+ autoScroll={viewport}
53
+ eventHandler={{ id: 'test', canDrop: () => true }}
54
+ debug={debugHandler}
55
+ >
56
+ <ScrollArea.Root orientation='vertical'>
57
+ <ScrollArea.Viewport ref={setViewport}>
58
+ <Mosaic.Stack {...props} items={items} />
59
+ </ScrollArea.Viewport>
60
+ </ScrollArea.Root>
61
+ </Mosaic.Container>
62
+ </Focus.Group>
63
+ </Panel.Content>
64
+ {props.debug && (
65
+ <Panel.Statusbar className='h-[40dvh]'>
66
+ <DebugInfo />
67
+ </Panel.Statusbar>
68
+ )}
69
+ </Panel.Root>
70
+ </Mosaic.Root>
71
+ );
72
+ };
73
+
74
+ const VirtualStackStory = (props: MosaicStackProps<Obj.Any>) => {
75
+ // Create items at render time to avoid Storybook serialization issues with ECHO objects.
76
+ const items = useMemo(() => createTestItems(NUM_ITEMS), []);
77
+ const [info, setInfo] = useState<any>(null);
78
+ const [DebugInfo, debugHandler] = useContainerDebug(props.debug);
79
+ const [viewport, setViewport] = useState<HTMLElement | null>(null);
80
+ return (
81
+ <Mosaic.Root classNames='dx-container' debug={props.debug}>
82
+ <Panel.Root>
83
+ <Panel.Toolbar asChild>
84
+ <Toolbar.Root>
85
+ <div className='flex grow justify-center'>{JSON.stringify(info)}</div>
86
+ </Toolbar.Root>
87
+ </Panel.Toolbar>
88
+ <Panel.Content asChild>
89
+ <Mosaic.Container
90
+ asChild
91
+ orientation='vertical'
92
+ autoScroll={viewport}
93
+ eventHandler={{ id: 'test', canDrop: () => true }}
94
+ debug={debugHandler}
95
+ >
96
+ <ScrollArea.Root orientation='vertical'>
97
+ <ScrollArea.Viewport ref={setViewport}>
98
+ <Mosaic.VirtualStack
99
+ {...props}
100
+ items={items}
101
+ getScrollElement={() => viewport}
102
+ estimateSize={() => 40}
103
+ onChange={(virtualizer) => {
104
+ setInfo({ range: virtualizer.range });
105
+ }}
106
+ />
107
+ </ScrollArea.Viewport>
108
+ </ScrollArea.Root>
109
+ </Mosaic.Container>
110
+ </Panel.Content>
111
+ {props.debug && (
112
+ <Panel.Statusbar className='h-[40dvh]'>
113
+ <DebugInfo />
114
+ </Panel.Statusbar>
115
+ )}
116
+ </Panel.Root>
117
+ </Mosaic.Root>
118
+ );
119
+ };
120
+
121
+ const meta: Meta<typeof MosaicStack<Obj.Any>> = {
122
+ title: 'ui/react-ui-mosaic/Stack',
123
+ component: MosaicStack,
124
+ decorators: [withLayout({ layout: 'column' }), withTheme()],
125
+ parameters: {
126
+ layout: 'fullscreen',
127
+ },
128
+ args: {
129
+ orientation: 'vertical',
130
+ getId: (item) => item.id,
131
+ Tile: DefaultStackTile,
132
+ draggable: false,
133
+ debug: false,
134
+ },
135
+ };
136
+
137
+ export default meta;
138
+
139
+ type Story = StoryObj<typeof meta>;
140
+
141
+ export const Default: Story = {
142
+ render: DefaultStackStory,
143
+ };
144
+
145
+ export const DefaultDraggable: Story = {
146
+ render: DefaultStackStory,
147
+ args: {
148
+ draggable: true,
149
+ },
150
+ };
151
+
152
+ export const DefaultDebug: Story = {
153
+ render: DefaultStackStory,
154
+ args: {
155
+ debug: true,
156
+ draggable: true,
157
+ },
158
+ };
159
+
160
+ export const Virtual: Story = {
161
+ render: VirtualStackStory,
162
+ };
163
+
164
+ export const VirtualDraggable: Story = {
165
+ render: VirtualStackStory,
166
+ args: {
167
+ draggable: true,
168
+ },
169
+ };
170
+
171
+ export const VirtualDebug = {
172
+ render: VirtualStackStory,
173
+ args: {
174
+ debug: true,
175
+ },
176
+ };