@dxos/react-ui-masonry 0.8.4-main.ae835ea → 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.
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-masonry",
3
- "version": "0.8.4-main.ae835ea",
3
+ "version": "0.8.4-main.bcb3aa67d6",
4
4
  "description": "A masonry component.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
- "sideEffects": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
@@ -23,35 +27,40 @@
23
27
  ],
24
28
  "dependencies": {
25
29
  "@radix-ui/react-compose-refs": "1.1.1",
26
- "masonic": "^4.1.0",
27
- "@dxos/react-hooks": "0.8.4-main.ae835ea"
30
+ "@radix-ui/react-context": "1.1.1",
31
+ "@virtuoso.dev/masonry": "^1.4.2",
32
+ "react-resize-detector": "^11.0.1",
33
+ "@dxos/react-hooks": "0.8.4-main.bcb3aa67d6",
34
+ "@dxos/react-ui-mosaic": "0.8.4-main.bcb3aa67d6",
35
+ "@dxos/types": "0.8.4-main.bcb3aa67d6",
36
+ "@dxos/ui-types": "0.8.4-main.bcb3aa67d6"
28
37
  },
29
38
  "devDependencies": {
30
- "@types/react": "~19.2.2",
31
- "@types/react-dom": "~19.2.2",
32
- "effect": "3.18.3",
33
- "react": "~19.2.0",
34
- "react-dom": "~19.2.0",
35
- "vite": "7.1.9",
36
- "@dxos/client": "0.8.4-main.ae835ea",
37
- "@dxos/echo": "0.8.4-main.ae835ea",
38
- "@dxos/random": "0.8.4-main.ae835ea",
39
- "@dxos/react-ui": "0.8.4-main.ae835ea",
40
- "@dxos/react-client": "0.8.4-main.ae835ea",
41
- "@dxos/react-ui-stack": "0.8.4-main.ae835ea",
42
- "@dxos/react-ui-theme": "0.8.4-main.ae835ea",
43
- "@dxos/schema": "0.8.4-main.ae835ea",
44
- "@dxos/storybook-utils": "0.8.4-main.ae835ea",
45
- "@dxos/react-ui-form": "0.8.4-main.ae835ea",
46
- "@dxos/test-utils": "0.8.4-main.ae835ea"
39
+ "@types/react": "~19.2.7",
40
+ "@types/react-dom": "~19.2.3",
41
+ "effect": "3.20.0",
42
+ "react": "~19.2.3",
43
+ "react-dom": "~19.2.3",
44
+ "vite": "^7.1.11",
45
+ "@dxos/random": "0.8.4-main.bcb3aa67d6",
46
+ "@dxos/client": "0.8.4-main.bcb3aa67d6",
47
+ "@dxos/echo": "0.8.4-main.bcb3aa67d6",
48
+ "@dxos/react-ui": "0.8.4-main.bcb3aa67d6",
49
+ "@dxos/react-ui-stack": "0.8.4-main.bcb3aa67d6",
50
+ "@dxos/react-client": "0.8.4-main.bcb3aa67d6",
51
+ "@dxos/schema": "0.8.4-main.bcb3aa67d6",
52
+ "@dxos/storybook-utils": "0.8.4-main.bcb3aa67d6",
53
+ "@dxos/test-utils": "0.8.4-main.bcb3aa67d6",
54
+ "@dxos/ui-theme": "0.8.4-main.bcb3aa67d6",
55
+ "@dxos/react-ui-form": "0.8.4-main.bcb3aa67d6"
47
56
  },
48
57
  "peerDependencies": {
49
- "effect": "^3.13.3",
50
- "react": "^19.0.0",
51
- "react-dom": "^19.0.0",
52
- "@dxos/random": "0.8.4-main.ae835ea",
53
- "@dxos/react-ui-theme": "0.8.4-main.ae835ea",
54
- "@dxos/react-ui": "0.8.4-main.ae835ea"
58
+ "effect": "3.20.0",
59
+ "react": "~19.2.3",
60
+ "react-dom": "~19.2.3",
61
+ "@dxos/random": "0.8.4-main.bcb3aa67d6",
62
+ "@dxos/react-ui": "0.8.4-main.bcb3aa67d6",
63
+ "@dxos/ui-theme": "0.8.4-main.bcb3aa67d6"
55
64
  },
56
65
  "publishConfig": {
57
66
  "access": "public"
@@ -8,63 +8,85 @@ import React from 'react';
8
8
  import { Filter } from '@dxos/client/echo';
9
9
  import { faker } from '@dxos/random';
10
10
  import { useQuery } from '@dxos/react-client/echo';
11
- import { useClientProvider, withClientProvider } from '@dxos/react-client/testing';
12
- import { withTheme } from '@dxos/react-ui/testing';
13
- import { Card, cardNoSpacing, cardSpacing } from '@dxos/react-ui-stack';
14
- import { mx } from '@dxos/react-ui-theme';
15
- import { DataType } from '@dxos/schema';
11
+ import { useClientStory, withClientProvider } from '@dxos/react-client/testing';
12
+ import { Card, ScrollArea } from '@dxos/react-ui';
13
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
16
14
  import { createObjectFactory } from '@dxos/schema/testing';
15
+ import { Organization } from '@dxos/types';
17
16
 
18
- import { Masonry } from './Masonry';
17
+ import { Masonry, MasonryRootProps } from './Masonry';
19
18
 
20
- const StoryItem = ({ data: { image, name, description } }: { data: DataType.Organization }) => {
19
+ const StoryItem = ({ data: { image, name, description } }: { data: Organization.Organization }) => {
21
20
  return (
22
- <Card.StaticRoot>
21
+ <Card.Root>
22
+ <Card.Toolbar>
23
+ <Card.Icon icon='ph--building-office--regular' />
24
+ <Card.Title>{name}</Card.Title>
25
+ </Card.Toolbar>
23
26
  <Card.Poster alt={name!} {...(image ? { image } : { icon: 'ph--building-office--regular' })} />
24
- <div role='none' className={mx('flex items-center gap-2', cardSpacing)}>
25
- <Card.Heading classNames={cardNoSpacing}>{name}</Card.Heading>
26
- </div>
27
- {description && <Card.Text classNames='line-clamp-2'>{description}</Card.Text>}
28
- </Card.StaticRoot>
27
+ {description && (
28
+ <Card.Section classNames='px-2 pb-2'>
29
+ <Card.Text variant='description'>{description}</Card.Text>
30
+ </Card.Section>
31
+ )}
32
+ </Card.Root>
29
33
  );
30
34
  };
31
35
 
32
- const DefaultStory = () => {
33
- const { space } = useClientProvider();
34
- const organizations = useQuery(space, Filter.type(DataType.Organization));
36
+ type DefaultStoryProps = MasonryRootProps;
37
+
38
+ const DefaultStory = (props: DefaultStoryProps) => {
39
+ const { space } = useClientStory();
40
+ const organizations = useQuery(space?.db, Filter.type(Organization.Organization));
35
41
 
36
42
  return (
37
- <Masonry.Root<DataType.Organization>
38
- items={organizations}
39
- render={StoryItem}
40
- classNames='is-full max-is-full bs-full max-bs-full overflow-y-auto p-4'
41
- />
43
+ <Masonry.Root {...props} Tile={StoryItem}>
44
+ <Masonry.Content items={organizations} />
45
+ </Masonry.Root>
42
46
  );
43
47
  };
44
48
 
45
49
  const meta = {
46
50
  title: 'ui/react-ui-masonry/Masonry',
51
+ render: DefaultStory,
47
52
  decorators: [
48
- withTheme,
53
+ withTheme(),
54
+ withLayout({ layout: 'fullscreen' }),
49
55
  withClientProvider({
50
- types: [DataType.Organization],
56
+ types: [Organization.Organization],
51
57
  createIdentity: true,
52
58
  createSpace: true,
53
59
  onCreateSpace: async ({ space }) => {
54
60
  const factory = createObjectFactory(space.db, faker as any);
55
- await factory([{ type: DataType.Organization, count: 36 }]);
61
+ await factory([{ type: Organization.Organization, count: 36 }]);
56
62
  },
57
63
  }),
58
64
  ],
59
65
  parameters: {
60
66
  layout: 'fullscreen',
61
67
  },
62
- } satisfies Meta<typeof Masonry>;
68
+ } satisfies Meta;
63
69
 
64
70
  export default meta;
65
71
 
66
72
  type Story = StoryObj<typeof meta>;
67
73
 
68
- export const Default: Story = {
69
- render: DefaultStory,
74
+ export const Default: Story = {};
75
+
76
+ // TODO(burdon): Masonry currently doesn't support an external scroller.
77
+ export const Single: Story = {
78
+ render: (props) => {
79
+ return (
80
+ <div className='dx-container flex justify-center'>
81
+ <ScrollArea.Root className='dx-card-max-width' thin padding>
82
+ <ScrollArea.Viewport>
83
+ <DefaultStory {...props} />
84
+ </ScrollArea.Viewport>
85
+ </ScrollArea.Root>
86
+ </div>
87
+ );
88
+ },
89
+ args: {
90
+ columns: 1,
91
+ },
70
92
  };
package/src/Masonry.tsx CHANGED
@@ -3,130 +3,189 @@
3
3
  //
4
4
 
5
5
  import { useComposedRefs } from '@radix-ui/react-compose-refs';
6
- import {
7
- type UseMasonryOptions,
8
- type UsePositionerOptions,
9
- useMasonry,
10
- usePositioner,
11
- useResizeObserver,
12
- } from 'masonic';
13
- import React, {
14
- type ComponentPropsWithRef,
15
- type ForwardedRef,
16
- type JSX,
17
- type Ref,
18
- forwardRef,
19
- useMemo,
20
- useRef,
21
- } from 'react';
22
-
23
- import { useScroller, useSize } from '@dxos/react-hooks';
24
- import { type ThemedClassName, usePx } from '@dxos/react-ui';
25
- import { mx } from '@dxos/react-ui-theme';
26
-
27
- type MasonryRootProps<Item> = ThemedClassName<ComponentPropsWithRef<'div'>> &
28
- Omit<UsePositionerOptions, 'width'> &
29
- Omit<
30
- UseMasonryOptions<Item>,
31
- | 'height'
32
- | 'scrollTop'
33
- | 'isScrolling'
34
- | 'resizeObserver'
35
- | 'positioner'
36
- | 'as'
37
- | 'id'
38
- | 'itemAs'
39
- | 'itemStyle'
40
- | 'className'
41
- | 'style'
42
- | 'role'
43
- | 'tabIndex'
44
- | 'containerRef'
45
- > & { intrinsicHeight?: boolean };
46
-
47
- const usePxProps = (remProps: Omit<UsePositionerOptions, 'width' | 'columnCount' | 'maxColumnCount'>) => {
48
- const remInPx = usePx(1);
49
- return useMemo(() => {
50
- return Object.fromEntries(
51
- Object.entries(remProps)
52
- .filter(([_, value]) => Number.isFinite(value))
53
- .map(([key, value]) => {
54
- return [key, value * remInPx];
55
- }),
56
- );
57
- }, [remProps, remInPx]);
6
+ import { createContext } from '@radix-ui/react-context';
7
+ import { VirtuosoMasonry, type VirtuosoMasonryProps } from '@virtuoso.dev/masonry';
8
+ import React, { type ComponentType, type JSX, type PropsWithChildren, type Ref, useMemo, useRef } from 'react';
9
+ import { useResizeDetector } from 'react-resize-detector';
10
+
11
+ import { ScrollArea, usePx } from '@dxos/react-ui';
12
+ import { cardMaxInlineSize, cardMinInlineSize, composable, composableProps, scrollbar } from '@dxos/ui-theme';
13
+
14
+ //
15
+ // Context
16
+ //
17
+
18
+ type MasonryContextValue = {
19
+ /** Render component for each masonry item. */
20
+ Tile: ComponentType<{ data: any; index: number }>;
21
+ /** Override auto-calculated column count. */
22
+ columns: number | undefined;
23
+ /** Upper bound on number of columns. */
24
+ maxColumns: number | undefined;
25
+ /** Minimum column width in rem. */
26
+ minColumnWidth: number;
27
+ /** Maximum column width in rem. */
28
+ maxColumnWidth: number;
29
+ /** Space between columns and rows in rem. */
30
+ gutter: number;
58
31
  };
59
32
 
60
- // TODO(burdon): Currently not responsive to width.
61
- const MasonryRootImpl = <Item,>(
62
- {
63
- columnCount,
64
- maxColumnCount,
65
- columnWidth = 18, // cardMaxWidth
66
- maxColumnWidth = 22,
67
- columnGutter = 1,
68
- rowGutter,
69
- items,
70
- itemHeightEstimate = 256,
71
- itemKey,
72
- overscanBy = 6,
73
- render,
74
- onRender,
75
- classNames,
76
- intrinsicHeight,
77
- ...props
78
- }: MasonryRootProps<Item>,
79
- forwardedRef: ForwardedRef<HTMLDivElement>,
80
- ) => {
81
- const rootRef = useRef<HTMLDivElement | null>(null);
82
- const ref = useComposedRefs(rootRef, forwardedRef);
83
- const { width, height } = useSize(rootRef);
84
- const { scrollTop, isScrolling } = useScroller(rootRef);
85
- const remProps = useMemo(
86
- () => ({ columnWidth, maxColumnWidth, columnGutter, rowGutter }),
87
- [columnWidth, maxColumnWidth, columnGutter, rowGutter],
88
- );
89
- const pxProps = usePxProps(remProps);
90
- const positionerProps = useMemo<UsePositionerOptions>(
91
- () => ({
92
- width,
93
- columnCount,
94
- maxColumnCount,
95
- ...pxProps,
96
- }),
97
- [width, pxProps, columnCount, maxColumnCount],
98
- );
99
- const positioner = usePositioner(positionerProps, [positionerProps]);
100
- const resizeObserver = useResizeObserver(positioner);
101
- const children = useMasonry({
102
- height: intrinsicHeight ? Infinity : height,
103
- scrollTop,
104
- isScrolling,
105
- resizeObserver,
106
- positioner,
107
- items,
108
- itemHeightEstimate,
109
- itemKey,
110
- overscanBy,
111
- render,
112
- onRender,
113
- });
114
-
115
- return (
116
- <div className={mx(classNames)} {...props} ref={ref}>
117
- {children}
118
- </div>
119
- );
33
+ const MASONRY_NAME = 'Masonry';
34
+
35
+ const [MasonryProvider, useMasonryContext] = createContext<MasonryContextValue>(MASONRY_NAME);
36
+
37
+ //
38
+ // Root
39
+ //
40
+
41
+ type MasonryRootProps = PropsWithChildren<Partial<MasonryContextValue>>;
42
+
43
+ const MasonryRoot = ({
44
+ children,
45
+ Tile,
46
+ columns = undefined,
47
+ maxColumns = undefined,
48
+ minColumnWidth = cardMinInlineSize,
49
+ maxColumnWidth = cardMaxInlineSize,
50
+ gutter = 0.75,
51
+ }: MasonryRootProps) => (
52
+ <MasonryProvider
53
+ Tile={Tile!}
54
+ columns={columns}
55
+ maxColumns={maxColumns}
56
+ minColumnWidth={minColumnWidth}
57
+ maxColumnWidth={maxColumnWidth}
58
+ gutter={gutter}
59
+ >
60
+ {children}
61
+ </MasonryProvider>
62
+ );
63
+
64
+ MasonryRoot.displayName = 'Masonry.Root';
65
+
66
+ //
67
+ // Content
68
+ //
69
+
70
+ type MasonryContentProps<Item> = {
71
+ /** Items to render in the masonry grid. */
72
+ items: Item[];
73
+ /** Extract a stable key from an item, aligned with react-ui-mosaic's getId. */
74
+ getId?: (data: Item) => string;
120
75
  };
121
76
 
122
- const MasonryRoot = forwardRef(MasonryRootImpl) as <Item>(
123
- props: MasonryRootProps<Item> & {
77
+ const MasonryContentInner = composable<HTMLDivElement, MasonryContentProps<any>>(
78
+ ({ items, getId, ...props }, forwardedRef) => {
79
+ const rootRef = useRef<HTMLDivElement | null>(null);
80
+ const composedRef = useComposedRefs(rootRef, forwardedRef);
81
+ const { width = 0 } = useResizeDetector({ targetRef: rootRef });
82
+
83
+ const { Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gutter } = useMasonryContext('Masonry.Content');
84
+ const columnCount = useColumnCount(
85
+ width - (scrollbar.thin.size + scrollbar.thin.padding),
86
+ columns,
87
+ maxColumns,
88
+ minColumnWidth,
89
+ maxColumnWidth,
90
+ gutter,
91
+ );
92
+
93
+ const TileAdapter = useMemo(() => {
94
+ const Adapter = ({ data, index }: { data: any; index: number }) => {
95
+ return (
96
+ <div role='listitem' style={{ paddingBottom: `${gutter}rem` }}>
97
+ <Tile index={index} data={data} />
98
+ </div>
99
+ );
100
+ };
101
+ Adapter.displayName = 'Masonry.TileAdapter';
102
+ return Adapter;
103
+ }, [Tile, gutter]);
104
+
105
+ // TODO(burdon): Masonry currently doesn't support an external scroller.
106
+ // https://github.com/petyosi/react-virtuoso/issues/1305
107
+ if (columns === 1) {
108
+ return <VirtuosoMasonry data={items} columnCount={1} ItemContent={TileAdapter} useWindowScroll />;
109
+ }
110
+
111
+ return (
112
+ <ScrollArea.Root {...composableProps(props, { classNames: 'w-' })} thin padding ref={composedRef}>
113
+ <ScrollArea.Viewport asChild>
114
+ {width > 0 && (
115
+ <ComposableVirtuosoMasonry
116
+ style={{ gap: `${gutter}rem` }}
117
+ data={items}
118
+ columnCount={columnCount}
119
+ ItemContent={TileAdapter}
120
+ />
121
+ )}
122
+ </ScrollArea.Viewport>
123
+ </ScrollArea.Root>
124
+ );
125
+ },
126
+ );
127
+
128
+ const ComposableVirtuosoMasonry = composable<HTMLDivElement, VirtuosoMasonryProps<any, any>>(({ ...props }) => {
129
+ return <VirtuosoMasonry {...props} />;
130
+ });
131
+
132
+ MasonryContentInner.displayName = 'Masonry.Content';
133
+
134
+ const MasonryContent = MasonryContentInner as <Item>(
135
+ props: MasonryContentProps<Item> & {
124
136
  ref?: Ref<HTMLDivElement | null>;
125
137
  },
126
138
  ) => JSX.Element;
127
139
 
140
+ /** Compute column count from container width and column constraints. */
141
+ const useColumnCount = (
142
+ width: number,
143
+ columns: number | undefined,
144
+ maxColumns: number | undefined,
145
+ minColumnWidth: number,
146
+ maxColumnWidth: number,
147
+ gutter: number,
148
+ ) => {
149
+ const remInPx = usePx(1);
150
+ return useMemo(() => {
151
+ if (columns != null) {
152
+ return columns;
153
+ }
154
+
155
+ const minColumnWidthPx = minColumnWidth * remInPx;
156
+ const maxColumnWidthPx = maxColumnWidth * remInPx;
157
+ const gutterPx = gutter * remInPx;
158
+
159
+ if (width <= 0 || minColumnWidthPx <= 0) {
160
+ return 1;
161
+ }
162
+
163
+ // Each tile has paddingRight: gutter, so every slot (including the last) occupies
164
+ // (colWidth + gutterPx). The container therefore fits floor(containerWidth / (colWidth + gutterPx)) columns.
165
+ let cols = Math.floor(width / (minColumnWidthPx + gutterPx));
166
+ if (maxColumnWidthPx > 0) {
167
+ const effectiveColWidth = width / cols - gutterPx;
168
+ if (effectiveColWidth > maxColumnWidthPx) {
169
+ // Try to add columns to keep cards below maxColumnWidth, but never violate minColumnWidth.
170
+ const maxCols = Math.ceil(width / (maxColumnWidthPx + gutterPx));
171
+ if (width / maxCols - gutterPx >= minColumnWidthPx) {
172
+ cols = maxCols;
173
+ }
174
+ }
175
+ }
176
+
177
+ const clamped = maxColumns != null ? Math.min(cols, maxColumns) : cols;
178
+ return Math.max(1, clamped);
179
+ }, [remInPx, width, columns, maxColumns, minColumnWidth, maxColumnWidth, gutter]);
180
+ };
181
+
182
+ //
183
+ // Masonry
184
+ //
185
+
128
186
  export const Masonry = {
129
187
  Root: MasonryRoot,
188
+ Content: MasonryContent,
130
189
  };
131
190
 
132
- export type { MasonryRootProps };
191
+ export type { MasonryRootProps, MasonryContentProps };