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

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 (52) hide show
  1. package/dist/lib/browser/index.mjs +513 -41
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/index.d.ts +1 -0
  5. package/dist/types/src/index.d.ts.map +1 -1
  6. package/dist/types/src/mosaic/Container.d.ts +9 -1
  7. package/dist/types/src/mosaic/Container.d.ts.map +1 -1
  8. package/dist/types/src/mosaic/Mosaic.d.ts +3 -2
  9. package/dist/types/src/mosaic/Mosaic.d.ts.map +1 -1
  10. package/dist/types/src/mosaic/Root.d.ts.map +1 -1
  11. package/dist/types/src/mosaic/Tile.d.ts +6 -0
  12. package/dist/types/src/mosaic/Tile.d.ts.map +1 -1
  13. package/dist/types/src/mosaic/hooks/index.d.ts +0 -1
  14. package/dist/types/src/mosaic/hooks/index.d.ts.map +1 -1
  15. package/dist/types/src/mosaic/hooks/useItemsWithPreview.d.ts +2 -1
  16. package/dist/types/src/mosaic/hooks/useItemsWithPreview.d.ts.map +1 -1
  17. package/dist/types/src/stories/Home.stories.d.ts +11 -0
  18. package/dist/types/src/stories/Home.stories.d.ts.map +1 -0
  19. package/dist/types/src/testing/decorators.d.ts.map +1 -1
  20. package/dist/types/src/testing/generator.d.ts +4 -4
  21. package/dist/types/src/testing/generator.d.ts.map +1 -1
  22. package/dist/types/src/testing/next-fractional-index.d.ts +1 -1
  23. package/dist/types/src/testing/next-fractional-index.d.ts.map +1 -1
  24. package/dist/types/src/views/Grid/Grid.d.ts +3 -3
  25. package/dist/types/src/views/Grid/Grid.d.ts.map +1 -1
  26. package/dist/types/src/views/Grid/Grid.stories.d.ts +1 -1
  27. package/dist/types/src/views/Stack/Columns.stories.d.ts +1 -1
  28. package/dist/types/src/views/Stack/Columns.stories.d.ts.map +1 -1
  29. package/dist/types/src/views/Tree/testing.d.ts +1 -1
  30. package/dist/types/src/views/Tree/testing.d.ts.map +1 -1
  31. package/package.json +13 -13
  32. package/src/index.ts +3 -0
  33. package/src/mosaic/Container.tsx +10 -2
  34. package/src/mosaic/Mosaic.ts +6 -2
  35. package/src/mosaic/Root.tsx +1 -2
  36. package/src/mosaic/Tile.tsx +4 -0
  37. package/src/mosaic/hooks/index.ts +0 -1
  38. package/src/mosaic/hooks/useItemsWithPreview.ts +56 -20
  39. package/src/stories/Home.stories.tsx +87 -0
  40. package/src/testing/decorators.tsx +1 -0
  41. package/src/testing/generator.ts +9 -9
  42. package/src/testing/next-fractional-index.ts +1 -1
  43. package/src/views/Grid/Grid.tsx +27 -14
  44. package/src/views/Grid/testing.tsx +2 -2
  45. package/src/views/Kanban/Kanban.tsx +1 -1
  46. package/src/views/Stack/Columns.stories.tsx +1 -1
  47. package/src/views/Stack/testing.tsx +1 -1
  48. package/src/views/Tree/Tree.tsx +17 -11
  49. package/src/views/Tree/testing.tsx +11 -9
  50. package/dist/types/src/mosaic/hooks/useSortedItems.d.ts +0 -6
  51. package/dist/types/src/mosaic/hooks/useSortedItems.d.ts.map +0 -1
  52. package/src/mosaic/hooks/useSortedItems.ts +0 -14
@@ -2,8 +2,6 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import '@dxosTheme';
6
-
7
5
  import { useDroppable } from '@dnd-kit/core';
8
6
  import { PlusCircle } from '@phosphor-icons/react';
9
7
  import defaultsDeep from 'lodash.defaultsdeep';
@@ -23,7 +21,14 @@ import {
23
21
  type Position,
24
22
  type Size,
25
23
  } from './layout';
26
- import { type MosaicContainerProps, type MosaicDataItem, Mosaic, Path, useMosaic } from '../../mosaic';
24
+ import {
25
+ type MosaicContainerProps,
26
+ type MosaicDataItem,
27
+ type MosaicTileAction,
28
+ Mosaic,
29
+ Path,
30
+ useMosaic,
31
+ } from '../../mosaic';
27
32
 
28
33
  //
29
34
  // Selection
@@ -47,8 +52,8 @@ type GridOptions = {
47
52
 
48
53
  const defaultGridOptions: GridOptions = {
49
54
  size: { x: 8, y: 8 },
50
- cellBounds: { width: 280, height: 280 },
51
- spacing: 8,
55
+ cellBounds: { width: 272, height: 272 },
56
+ spacing: 6,
52
57
  };
53
58
 
54
59
  //
@@ -66,6 +71,7 @@ export type GridProps<TData extends MosaicDataItem = MosaicDataItem> = MosaicCon
66
71
  square?: boolean;
67
72
  debug?: boolean;
68
73
  onCreate?: (position: Position) => void;
74
+ onAction?: (action: MosaicTileAction) => void; // TODO(burdon): Standardize across mosaics?
69
75
  };
70
76
 
71
77
  /**
@@ -84,8 +90,10 @@ export const Grid = ({
84
90
  Component = Mosaic.DefaultComponent,
85
91
  className,
86
92
  onDrop,
93
+ onOver,
87
94
  onSelect,
88
95
  onCreate,
96
+ onAction,
89
97
  }: GridProps) => {
90
98
  const { ref: containerRef, width, height } = useResizeDetector({ refreshRate: 200 });
91
99
  const options = defaultsDeep({}, opts, defaultGridOptions);
@@ -148,11 +156,12 @@ export const Grid = ({
148
156
  getOverlayProps: () => ({ grow: true }),
149
157
  getOverlayStyle: () => getDimension(cellBounds, options.spacing),
150
158
  onDrop,
159
+ onOver,
151
160
  }}
152
161
  >
153
162
  <div className={mx('flex grow overflow-auto', className)}>
154
- <div ref={containerRef} className={mx('grow overflow-auto snap-x snap-mandatory md:snap-none bg-neutral-600')}>
155
- <div className='group block relative bg-neutral-500' style={{ ...bounds, margin: marginSize }}>
163
+ <div ref={containerRef} className={mx('grow overflow-auto snap-x snap-mandatory md:snap-none')}>
164
+ <div className='group block relative' style={{ ...bounds, margin: marginSize }}>
156
165
  {matrix && (
157
166
  <div style={{ padding: options.spacing }}>
158
167
  <div className='relative'>
@@ -172,7 +181,6 @@ export const Grid = ({
172
181
  </div>
173
182
  )}
174
183
 
175
- {/* TODO(burdon): Events: onDoubleClick={() => handleSelect(id)} */}
176
184
  <div>
177
185
  {items.map((item) => {
178
186
  const position = layout[item.id] ?? { x: 0, y: 0 };
@@ -188,6 +196,7 @@ export const Grid = ({
188
196
  ...getBounds(position, cellBounds, options.spacing),
189
197
  }}
190
198
  onSelect={() => handleSelect(item.id)}
199
+ onAction={onAction}
191
200
  // debug={debug}
192
201
  />
193
202
  );
@@ -227,14 +236,18 @@ const GridCell: FC<{
227
236
  >
228
237
  <div
229
238
  className={mx(
230
- 'group/cell hidden group-hover:flex w-full h-full items-center justify-center',
231
- isOverContainer && 'flex',
232
- 'box-border border-dashed border-4 border-neutral-600/50 rounded-lg',
233
- 'transition ease-in-out duration-200 bg-neutral-500',
234
- isOver && 'flex bg-neutral-600',
239
+ 'group/cell flex group-hover:flex w-full h-full items-center justify-center',
240
+ 'box-border border-dashed border-4 border-neutral-100 rounded-lg',
241
+ 'transition ease-in-out duration-300',
242
+ isOver && 'flex bg-neutral-200 border-neutral-400',
235
243
  )}
236
244
  >
237
- <div className={mx('hidden group-hover/cell:flex', isOverContainer && 'hidden')}>
245
+ <div
246
+ className={mx(
247
+ 'opacity-10 transition ease-in-out duration-300 group-hover/cell:opacity-100',
248
+ isOverContainer && 'hidden',
249
+ )}
250
+ >
238
251
  {onCreate && (
239
252
  // TODO(burdon): Style button.
240
253
  <Button variant='ghost' onClick={() => onCreate(position)}>
@@ -11,8 +11,8 @@ import { type MosaicDataItem, type MosaicDropEvent } from '../../mosaic';
11
11
  import { ComplexCard, TestObjectGenerator } from '../../testing';
12
12
 
13
13
  export type DemoGridProps = GridProps & {
14
- initialItems?: MosaicDataItem[];
15
- initialLayout?: GridLayout;
14
+ initialItems?: MosaicDataItem[]; // TODO(burdon): Remove or convert to controlled.
15
+ initialLayout?: GridLayout; // TODO(burdon): Remove or convert to controlled.
16
16
  types?: string[];
17
17
  debug?: boolean;
18
18
  };
@@ -51,7 +51,7 @@ export const Kanban = ({
51
51
  // Restrict to objects from other columns.
52
52
  // TODO(burdon): Consider objects from other containers.
53
53
  // TODO(wittjosiah): Allow override.
54
- onOver: ({ active, over }) => (Path.length(active.path) >= Path.length(over.path) ? 'adopt' : 'reject'),
54
+ onOver: ({ active, over }) => (Path.length(active.path) >= Path.length(over.path) ? 'transfer' : 'reject'),
55
55
  onDrop,
56
56
  }}
57
57
  >
@@ -36,7 +36,7 @@ export default {
36
36
  },
37
37
  };
38
38
 
39
- export const Adopt = {
39
+ export const Transfer = {
40
40
  args: {
41
41
  Component: SimpleCard,
42
42
  debug: true,
@@ -26,7 +26,7 @@ export const DemoStack = ({
26
26
  types,
27
27
  count = 8,
28
28
  direction = 'vertical',
29
- operation = 'adopt',
29
+ operation = 'transfer',
30
30
  debug,
31
31
  }: DemoStackProps) => {
32
32
  const [items, setItems] = useState<MosaicDataItem[]>(() => {
@@ -2,7 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import React, { forwardRef } from 'react';
5
+ import React, { forwardRef, useMemo } from 'react';
6
6
 
7
7
  import { Card, Tree as TreeComponent, TreeItem as TreeItemComponent } from '@dxos/react-ui';
8
8
  import { dropRing, mx } from '@dxos/react-ui-theme';
@@ -15,7 +15,7 @@ import {
15
15
  useContainer,
16
16
  useMosaic,
17
17
  Path,
18
- useSortedItems,
18
+ useItemsWithPreview,
19
19
  type MosaicCompareDataItem,
20
20
  } from '../../mosaic';
21
21
 
@@ -55,13 +55,16 @@ export const Tree = ({ id, Component = TreeItem, onOver, onDrop, items = [], deb
55
55
  };
56
56
 
57
57
  const TreeRoot = ({ items }: { items: TreeData[] }) => {
58
- const { id, Component } = useContainer();
59
- const sortedItems = useSortedItems(items);
58
+ const { id, Component, compare } = useContainer();
59
+ const sortedItems = useMemo(() => {
60
+ return compare ? [...items].sort(compare) : items;
61
+ }, [items, compare]);
62
+ const itemsWithPreview = useItemsWithPreview({ items: sortedItems, path: id, strategy: 'layout-stable' });
60
63
 
61
64
  return (
62
65
  <TreeComponent.Root classNames='flex flex-col'>
63
- <Mosaic.SortableContext id={id} items={sortedItems} direction='vertical'>
64
- {sortedItems.map((item, index) => (
66
+ <Mosaic.SortableContext id={id} items={itemsWithPreview} direction='vertical'>
67
+ {itemsWithPreview.map((item, index) => (
65
68
  <TreeItemComponent.Root key={item.id} collapsible defaultOpen>
66
69
  <Mosaic.SortableTile item={item} path={id} position={index} Component={Component!} />
67
70
  </TreeItemComponent.Root>
@@ -101,13 +104,16 @@ const TreeItem: MosaicTileComponent<TreeData> = forwardRef(
101
104
 
102
105
  const TreeBranch = ({ path, items }: { path: string; items: TreeData[] }) => {
103
106
  const { operation, overItem } = useMosaic();
104
- const { Component } = useContainer();
105
- const sortedItems = useSortedItems(items);
107
+ const { Component, compare } = useContainer();
108
+ const sortedItems = useMemo(() => {
109
+ return compare ? [...items].sort(compare) : items;
110
+ }, [items, compare]);
111
+ const itemsWithPreview = useItemsWithPreview({ items: sortedItems, path, strategy: 'layout-stable' });
106
112
 
107
113
  return (
108
114
  <TreeItemComponent.Body className='pis-4'>
109
- <Mosaic.SortableContext id={path} items={sortedItems} direction='vertical'>
110
- {sortedItems.map((child, index) => (
115
+ <Mosaic.SortableContext id={path} items={itemsWithPreview} direction='vertical'>
116
+ {itemsWithPreview.map((child, index) => (
111
117
  <TreeComponent.Branch key={child.id}>
112
118
  <TreeItemComponent.Root collapsible defaultOpen>
113
119
  <Mosaic.SortableTile
@@ -116,7 +122,7 @@ const TreeBranch = ({ path, items }: { path: string; items: TreeData[] }) => {
116
122
  position={index}
117
123
  Component={Component!}
118
124
  isOver={
119
- overItem?.path === Path.create(path, child.id) && (operation === 'adopt' || operation === 'copy')
125
+ overItem?.path === Path.create(path, child.id) && (operation === 'transfer' || operation === 'copy')
120
126
  }
121
127
  />
122
128
  </TreeItemComponent.Root>
@@ -5,12 +5,12 @@
5
5
  import { faker } from '@faker-js/faker';
6
6
  import React, { useCallback, useState } from 'react';
7
7
 
8
- import { type Graph, GraphBuilder, type Node } from '@braneframe/plugin-graph';
9
- import { buildGraph } from '@braneframe/plugin-graph/testing';
8
+ import { type Graph, GraphBuilder, type Node } from '@dxos/app-graph';
9
+ import { buildGraph } from '@dxos/app-graph/testing';
10
10
  import { arrayMove } from '@dxos/util';
11
11
 
12
12
  import { Tree, type TreeData, type TreeProps } from './Tree';
13
- import { type MosaicDropEvent, type MosaicMoveEvent, Path } from '../../mosaic';
13
+ import { type MosaicDropEvent, type MosaicMoveEvent, Path, type MosaicOperation } from '../../mosaic';
14
14
  import { TestObjectGenerator, nextRearrangeIndex } from '../../testing';
15
15
 
16
16
  const fake = faker.helpers.fake;
@@ -47,8 +47,8 @@ export const DemoTree = ({ id = 'tree', initialItems, types, debug }: DemoTreePr
47
47
  }),
48
48
  );
49
49
 
50
- const handleOver = useCallback(({ active, over }: MosaicMoveEvent<number>) => {
51
- return !(active.path === id && over.path !== id) ? 'adopt' : 'reject';
50
+ const handleOver = useCallback(({ over }: MosaicMoveEvent<number>) => {
51
+ return over && Path.hasChild(id, over.path) ? 'transfer' : 'reject';
52
52
  }, []);
53
53
 
54
54
  // NOTE: Does not handle deep operations.
@@ -67,8 +67,8 @@ export const DemoTree = ({ id = 'tree', initialItems, types, debug }: DemoTreePr
67
67
  if (Path.last(Path.parent(active.path)) === item.id) {
68
68
  children.splice(active.position!, 1);
69
69
  }
70
- if (Path.last(Path.parent(over.path)) === item.id) {
71
- children.splice(over.position!, 0, active.item as TreeData);
70
+ if (Path.last(over.path) === item.id) {
71
+ children.push(active.item as TreeData);
72
72
  }
73
73
  return { ...item, children };
74
74
  }),
@@ -117,7 +117,6 @@ const graphNodeCompare = (a: Node, b: Node) => {
117
117
  };
118
118
 
119
119
  export const GraphTree = ({ id, graph = createGraph(), debug }: { id: string; graph?: Graph; debug: boolean }) => {
120
- // TODO(wittjosiah): This graph does not handle order currently.
121
120
  const handleDrop = ({ operation, active, over }: MosaicDropEvent<number>) => {
122
121
  // Moving within the tree.
123
122
  if (Path.hasDescendent(id, active.path) && Path.hasDescendent(id, over.path)) {
@@ -137,18 +136,21 @@ export const GraphTree = ({ id, graph = createGraph(), debug }: { id: string; gr
137
136
  // This is a rearrange operation
138
137
  const nextIndex = nextRearrangeIndex(activeParent.children.sort(graphNodeCompare), activeNode.id, overNode.id);
139
138
  activeNode.properties.index = nextIndex ?? 'a0';
140
- } else if (activeNode && activeParent && overParent && operation === 'adopt') {
139
+ } else if (activeNode && activeParent && overParent && operation === 'transfer') {
141
140
  activeParent.removeNode(active.item.id);
142
141
  overNode.addNode('tree', { ...activeNode });
143
142
  }
144
143
  }
145
144
  };
146
145
 
146
+ const handleOver = (): MosaicOperation => 'transfer';
147
+
147
148
  return (
148
149
  <Tree
149
150
  id={id}
150
151
  items={graph.root.children as TreeData[]}
151
152
  onDrop={handleDrop}
153
+ onOver={handleOver}
152
154
  debug={debug}
153
155
  compare={graphNodeCompare}
154
156
  />
@@ -1,6 +0,0 @@
1
- import { type MosaicDataItem } from '../types';
2
- /**
3
- * Returns a sorted collection of items if a compare function is provided.
4
- */
5
- export declare const useSortedItems: <T extends MosaicDataItem>(items: T[]) => T[];
6
- //# sourceMappingURL=useSortedItems.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useSortedItems.d.ts","sourceRoot":"","sources":["../../../../../src/mosaic/hooks/useSortedItems.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C;;GAEG;AACH,eAAO,MAAM,cAAc,+CAG1B,CAAC"}
@@ -1,14 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import { useContainer } from './useContainer';
6
- import { type MosaicDataItem } from '../types';
7
-
8
- /**
9
- * Returns a sorted collection of items if a compare function is provided.
10
- */
11
- export const useSortedItems = <T extends MosaicDataItem>(items: T[]): T[] => {
12
- const { compare } = useContainer();
13
- return compare ? [...items].sort(compare) : items;
14
- };