@dxos/react-ui-masonry 0.8.4-main.c85a9c8dae → 0.8.4-main.cb12b3f963

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-masonry",
3
- "version": "0.8.4-main.c85a9c8dae",
3
+ "version": "0.8.4-main.cb12b3f963",
4
4
  "description": "A masonry component.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -26,39 +26,42 @@
26
26
  "src"
27
27
  ],
28
28
  "dependencies": {
29
+ "@fluentui/react-tabster": "9.26.11",
29
30
  "@radix-ui/react-compose-refs": "1.1.1",
30
- "masonic": "^4.1.0",
31
+ "@radix-ui/react-context": "1.1.1",
32
+ "@virtuoso.dev/masonry": "^1.4.2",
31
33
  "react-resize-detector": "^11.0.1",
32
- "@dxos/react-hooks": "0.8.4-main.c85a9c8dae",
33
- "@dxos/types": "0.8.4-main.c85a9c8dae",
34
- "@dxos/react-ui-mosaic": "0.8.4-main.c85a9c8dae"
34
+ "@dxos/react-hooks": "0.8.4-main.cb12b3f963",
35
+ "@dxos/react-ui-mosaic": "0.8.4-main.cb12b3f963",
36
+ "@dxos/ui-types": "0.8.4-main.cb12b3f963",
37
+ "@dxos/types": "0.8.4-main.cb12b3f963"
35
38
  },
36
39
  "devDependencies": {
37
40
  "@types/react": "~19.2.7",
38
41
  "@types/react-dom": "~19.2.3",
39
- "effect": "3.19.16",
42
+ "effect": "3.20.0",
40
43
  "react": "~19.2.3",
41
44
  "react-dom": "~19.2.3",
42
- "vite": "^7.1.11",
43
- "@dxos/client": "0.8.4-main.c85a9c8dae",
44
- "@dxos/echo": "0.8.4-main.c85a9c8dae",
45
- "@dxos/react-client": "0.8.4-main.c85a9c8dae",
46
- "@dxos/random": "0.8.4-main.c85a9c8dae",
47
- "@dxos/react-ui-form": "0.8.4-main.c85a9c8dae",
48
- "@dxos/react-ui": "0.8.4-main.c85a9c8dae",
49
- "@dxos/react-ui-stack": "0.8.4-main.c85a9c8dae",
50
- "@dxos/schema": "0.8.4-main.c85a9c8dae",
51
- "@dxos/ui-theme": "0.8.4-main.c85a9c8dae",
52
- "@dxos/test-utils": "0.8.4-main.c85a9c8dae",
53
- "@dxos/storybook-utils": "0.8.4-main.c85a9c8dae"
45
+ "vite": "^8.0.10",
46
+ "@dxos/echo": "0.8.4-main.cb12b3f963",
47
+ "@dxos/client": "0.8.4-main.cb12b3f963",
48
+ "@dxos/random": "0.8.4-main.cb12b3f963",
49
+ "@dxos/react-ui-form": "0.8.4-main.cb12b3f963",
50
+ "@dxos/react-client": "0.8.4-main.cb12b3f963",
51
+ "@dxos/react-ui-stack": "0.8.4-main.cb12b3f963",
52
+ "@dxos/schema": "0.8.4-main.cb12b3f963",
53
+ "@dxos/react-ui": "0.8.4-main.cb12b3f963",
54
+ "@dxos/ui-theme": "0.8.4-main.cb12b3f963",
55
+ "@dxos/storybook-utils": "0.8.4-main.cb12b3f963",
56
+ "@dxos/test-utils": "0.8.4-main.cb12b3f963"
54
57
  },
55
58
  "peerDependencies": {
56
- "effect": "3.19.16",
59
+ "effect": "3.20.0",
57
60
  "react": "~19.2.3",
58
61
  "react-dom": "~19.2.3",
59
- "@dxos/react-ui": "0.8.4-main.c85a9c8dae",
60
- "@dxos/random": "0.8.4-main.c85a9c8dae",
61
- "@dxos/ui-theme": "0.8.4-main.c85a9c8dae"
62
+ "@dxos/random": "0.8.4-main.cb12b3f963",
63
+ "@dxos/react-ui": "0.8.4-main.cb12b3f963",
64
+ "@dxos/ui-theme": "0.8.4-main.cb12b3f963"
62
65
  },
63
66
  "publishConfig": {
64
67
  "access": "public"
@@ -6,7 +6,7 @@ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
6
  import React from 'react';
7
7
 
8
8
  import { Filter } from '@dxos/client/echo';
9
- import { faker } from '@dxos/random';
9
+ import { random } from '@dxos/random';
10
10
  import { useQuery } from '@dxos/react-client/echo';
11
11
  import { useClientStory, withClientProvider } from '@dxos/react-client/testing';
12
12
  import { Card } from '@dxos/react-ui';
@@ -14,33 +14,41 @@ import { withLayout, withTheme } from '@dxos/react-ui/testing';
14
14
  import { createObjectFactory } from '@dxos/schema/testing';
15
15
  import { Organization } from '@dxos/types';
16
16
 
17
- import { Masonry } from './Masonry';
17
+ import { Masonry, MasonryRootProps } from './Masonry';
18
18
 
19
19
  const StoryItem = ({ data: { image, name, description } }: { data: Organization.Organization }) => {
20
20
  return (
21
21
  <Card.Root>
22
+ <Card.Toolbar>
23
+ <Card.Icon icon='ph--building-office--regular' />
24
+ <Card.Title>{name}</Card.Title>
25
+ </Card.Toolbar>
22
26
  <Card.Poster alt={name!} {...(image ? { image } : { icon: 'ph--building-office--regular' })} />
23
- <Card.Heading>{name}</Card.Heading>
24
- {description && <Card.Text variant='description'>{description}</Card.Text>}
27
+ {description && (
28
+ <Card.Section classNames='px-2 pb-2'>
29
+ <Card.Text variant='description'>{description}</Card.Text>
30
+ </Card.Section>
31
+ )}
25
32
  </Card.Root>
26
33
  );
27
34
  };
28
35
 
29
- const DefaultStory = () => {
36
+ const DefaultStory = (props: MasonryRootProps) => {
30
37
  const { space } = useClientStory();
31
38
  const organizations = useQuery(space?.db, Filter.type(Organization.Organization));
32
39
 
33
40
  return (
34
- <Masonry.Root<Organization.Organization>
35
- items={organizations}
36
- render={StoryItem}
37
- classNames='w-full max-w-full h-full max-h-full overflow-y-auto p-4'
38
- />
41
+ <Masonry.Root {...props} Tile={StoryItem}>
42
+ <Masonry.Content>
43
+ <Masonry.Viewport items={organizations} />
44
+ </Masonry.Content>
45
+ </Masonry.Root>
39
46
  );
40
47
  };
41
48
 
42
49
  const meta = {
43
50
  title: 'ui/react-ui-masonry/Masonry',
51
+ render: DefaultStory,
44
52
  decorators: [
45
53
  withTheme(),
46
54
  withLayout({ layout: 'fullscreen' }),
@@ -49,7 +57,7 @@ const meta = {
49
57
  createIdentity: true,
50
58
  createSpace: true,
51
59
  onCreateSpace: async ({ space }) => {
52
- const factory = createObjectFactory(space.db, faker as any);
60
+ const factory = createObjectFactory(space.db, random as any);
53
61
  await factory([{ type: Organization.Organization, count: 36 }]);
54
62
  },
55
63
  }),
@@ -57,12 +65,10 @@ const meta = {
57
65
  parameters: {
58
66
  layout: 'fullscreen',
59
67
  },
60
- } satisfies Meta<typeof Masonry>;
68
+ } satisfies Meta;
61
69
 
62
70
  export default meta;
63
71
 
64
72
  type Story = StoryObj<typeof meta>;
65
73
 
66
- export const Default: Story = {
67
- render: DefaultStory,
68
- };
74
+ export const Default: Story = {};
package/src/Masonry.tsx CHANGED
@@ -2,133 +2,246 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ import { useArrowNavigationGroup } from '@fluentui/react-tabster';
5
6
  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';
7
+ import { createContext } from '@radix-ui/react-context';
8
+ import { VirtuosoMasonry, type VirtuosoMasonryProps } from '@virtuoso.dev/masonry';
9
+ import React, { type ComponentType, type JSX, type PropsWithChildren, type Ref, useMemo, useRef } from 'react';
22
10
  import { useResizeDetector } from 'react-resize-detector';
23
11
 
24
- import { useScroller } from '@dxos/react-hooks';
25
- import { type ThemedClassName, usePx } from '@dxos/react-ui';
26
- import { cardMaxInlineSize, cardMinInlineSize } from '@dxos/ui-theme';
27
- import { mx } from '@dxos/ui-theme';
28
-
29
- type MasonryRootProps<Item> = ThemedClassName<ComponentPropsWithRef<'div'>> &
30
- Omit<UsePositionerOptions, 'width'> &
31
- Omit<
32
- UseMasonryOptions<Item>,
33
- | 'height'
34
- | 'scrollTop'
35
- | 'isScrolling'
36
- | 'resizeObserver'
37
- | 'positioner'
38
- | 'as'
39
- | 'id'
40
- | 'itemAs'
41
- | 'itemStyle'
42
- | 'className'
43
- | 'style'
44
- | 'role'
45
- | 'tabIndex'
46
- | 'containerRef'
47
- > & { intrinsicHeight?: boolean };
48
-
49
- const usePxProps = (remProps: Omit<UsePositionerOptions, 'width' | 'columnCount' | 'maxColumnCount'>) => {
50
- const remInPx = usePx(1);
51
- return useMemo(() => {
52
- return Object.fromEntries(
53
- Object.entries(remProps)
54
- .filter(([_, value]) => Number.isFinite(value))
55
- .map(([key, value]) => {
56
- return [key, value * remInPx];
57
- }),
58
- );
59
- }, [remProps, remInPx]);
12
+ import { ScrollArea, ScrollAreaRootProps, ThemedClassName, usePx } from '@dxos/react-ui';
13
+ import { cardMaxInlineSize, cardMinInlineSize, composable, composableProps, scrollbar } from '@dxos/ui-theme';
14
+
15
+ //
16
+ // Context
17
+ //
18
+
19
+ type MasonryContextValue = {
20
+ /** Render component for each masonry item. */
21
+ Tile: ComponentType<{ data: any; index: number }>;
22
+ /** Override auto-calculated column count. */
23
+ columns: number | undefined;
24
+ /** Upper bound on number of columns. */
25
+ maxColumns: number | undefined;
26
+ /** Minimum column width in rem. */
27
+ minColumnWidth: number;
28
+ /** Maximum column width in rem. */
29
+ maxColumnWidth: number;
30
+ /** Space between columns and rows in rem. */
31
+ gutter: number;
60
32
  };
61
33
 
62
- const MasonryRootImpl = <Item,>(
63
- {
64
- columnCount,
65
- maxColumnCount,
66
- columnWidth = cardMinInlineSize,
67
- maxColumnWidth = cardMaxInlineSize,
68
- columnGutter = 1,
69
- rowGutter,
70
- items,
71
- itemHeightEstimate = 256,
72
- itemKey,
73
- overscanBy = 6,
74
- render,
75
- onRender,
76
- classNames,
77
- intrinsicHeight,
78
- ...props
79
- }: MasonryRootProps<Item>,
80
- forwardedRef: ForwardedRef<HTMLDivElement>,
81
- ) => {
82
- const rootRef = useRef<HTMLDivElement | null>(null);
83
- const ref = useComposedRefs(rootRef, forwardedRef);
84
- const { width = 0, height = 0 } = useResizeDetector({ targetRef: rootRef });
85
- const { scrollTop, isScrolling } = useScroller(rootRef);
86
- const remProps = useMemo(
87
- () => ({ columnWidth, maxColumnWidth, columnGutter, rowGutter }),
88
- [columnWidth, maxColumnWidth, columnGutter, rowGutter],
89
- );
90
- const pxProps = usePxProps(remProps);
91
- const numItems = items.length;
92
- const positionerProps = useMemo<UsePositionerOptions>(
93
- () => ({
94
- width,
95
- columnCount,
96
- maxColumnCount: Math.max(maxColumnCount ?? 1, numItems),
97
- ...pxProps,
98
- }),
99
- [width, columnCount, maxColumnCount, numItems, pxProps],
100
- );
101
- const positioner = usePositioner(positionerProps, [positionerProps]);
102
- const resizeObserver = useResizeObserver(positioner);
103
- const children = useMasonry({
104
- height: intrinsicHeight ? Infinity : height,
105
- scrollTop,
106
- isScrolling,
107
- resizeObserver,
108
- positioner,
109
- items,
110
- itemHeightEstimate,
111
- itemKey,
112
- overscanBy,
113
- render,
114
- onRender,
115
- });
116
-
117
- return (
118
- <div className={mx(classNames)} {...props} ref={ref}>
119
- {children}
120
- </div>
121
- );
34
+ const MASONRY_NAME = 'Masonry';
35
+
36
+ const [MasonryProvider, useMasonryContext] = createContext<MasonryContextValue>(MASONRY_NAME);
37
+
38
+ /** Content-scoped context: measured width of the ScrollArea.Root, shared with Viewport. */
39
+ type MasonryContentContextValue = {
40
+ width: number;
122
41
  };
123
42
 
124
- const MasonryRoot = forwardRef(MasonryRootImpl) as <Item>(
125
- props: MasonryRootProps<Item> & {
43
+ const [MasonryContentProvider, useMasonryContentContext] = createContext<MasonryContentContextValue>('Masonry.Content');
44
+
45
+ //
46
+ // Root
47
+ //
48
+
49
+ type MasonryRootProps = PropsWithChildren<Partial<MasonryContextValue>>;
50
+
51
+ const MasonryRoot = ({
52
+ children,
53
+ Tile,
54
+ columns = undefined,
55
+ maxColumns = undefined,
56
+ minColumnWidth = cardMinInlineSize,
57
+ maxColumnWidth = cardMaxInlineSize,
58
+ gutter = 0.75,
59
+ }: MasonryRootProps) => (
60
+ <MasonryProvider
61
+ Tile={Tile!}
62
+ columns={columns}
63
+ maxColumns={maxColumns}
64
+ minColumnWidth={minColumnWidth}
65
+ maxColumnWidth={maxColumnWidth}
66
+ gutter={gutter}
67
+ >
68
+ {children}
69
+ </MasonryProvider>
70
+ );
71
+
72
+ MasonryRoot.displayName = 'Masonry.Root';
73
+
74
+ //
75
+ // Content
76
+ //
77
+ // The outer wrapper: renders the ScrollArea.Root, measures available width,
78
+ // and publishes that width via context for Masonry.Viewport to consume.
79
+ // Style this layer (centered/thin/padding) to control the scroll container.
80
+ //
81
+
82
+ type MasonryContentProps = ThemedClassName<
83
+ PropsWithChildren<Pick<ScrollAreaRootProps, 'centered' | 'thin' | 'padding'>>
84
+ >;
85
+
86
+ const MasonryContentInner = composable<HTMLDivElement, MasonryContentProps>(
87
+ ({ children, centered, thin = true, padding = true, ...props }, forwardedRef) => {
88
+ const rootRef = useRef<HTMLDivElement | null>(null);
89
+ const composedRef = useComposedRefs(rootRef, forwardedRef);
90
+ const { width = 0 } = useResizeDetector({ targetRef: rootRef });
91
+
92
+ // NOTE: Masonry currently doesn't support an external scroller.
93
+ // https://github.com/petyosi/react-virtuoso/issues/1305
94
+ return (
95
+ <ScrollArea.Root {...composableProps(props)} centered={centered} thin={thin} padding={padding} ref={composedRef}>
96
+ <MasonryContentProvider width={width}>{children}</MasonryContentProvider>
97
+ </ScrollArea.Root>
98
+ );
99
+ },
100
+ );
101
+
102
+ MasonryContentInner.displayName = 'Masonry.Content';
103
+
104
+ const MasonryContent = MasonryContentInner as (
105
+ props: MasonryContentProps & {
106
+ ref?: Ref<HTMLDivElement | null>;
107
+ },
108
+ ) => JSX.Element;
109
+
110
+ //
111
+ // Viewport
112
+ //
113
+ // The inner render layer: renders the ScrollArea.Viewport wrapped around
114
+ // VirtuosoMasonry. Style this layer separately from Content to control
115
+ // the tile grid (e.g. inner padding, alignment).
116
+ //
117
+
118
+ type MasonryViewportProps<Item> = ThemedClassName<{
119
+ /** Items to render in the masonry grid. */
120
+ items: Item[];
121
+ /** Extract a stable key from an item, aligned with react-ui-mosaic's getId. */
122
+ getId?: (data: Item) => string;
123
+ }>;
124
+
125
+ const MasonryViewportInner = composable<HTMLDivElement, MasonryViewportProps<any>>(
126
+ ({ items, getId, ...props }, forwardedRef) => {
127
+ const { Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gutter } = useMasonryContext('Masonry.Viewport');
128
+ const { width } = useMasonryContentContext('Masonry.Viewport');
129
+ const columnCount = useColumnCount(
130
+ width - (scrollbar.thin.size + scrollbar.thin.padding),
131
+ columns,
132
+ maxColumns,
133
+ minColumnWidth,
134
+ maxColumnWidth,
135
+ gutter,
136
+ );
137
+
138
+ // Arrow-key navigation across tiles. Uses Tabster's `both` axis so all
139
+ // four arrows move focus through the items. True 2D (grid) axis doesn't
140
+ // work inside a masonry because VirtuosoMasonry lays items out column-
141
+ // first in the DOM — tabster's Grid direction requires the next DOM
142
+ // element to be to the right of the current for ArrowRight, which is
143
+ // never the case between columns here. `both` treats the keys as flat
144
+ // next/previous-focusable, which gives predictable wrap-around in DOM
145
+ // order for all four directions.
146
+ const arrowNavigationAttrs = useArrowNavigationGroup({
147
+ axis: 'both',
148
+ memorizeCurrent: true,
149
+ tabbable: true,
150
+ });
151
+
152
+ const TileAdapter = useMemo(() => {
153
+ const Adapter = ({ data, index }: { data: any; index: number }) => (
154
+ <div role='listitem' style={{ paddingBottom: `${gutter}rem` }}>
155
+ <Tile index={index} data={data} />
156
+ </div>
157
+ );
158
+ Adapter.displayName = 'Masonry.TileAdapter';
159
+ return Adapter;
160
+ }, [Tile, gutter]);
161
+
162
+ if (width <= 0) {
163
+ return null;
164
+ }
165
+
166
+ return (
167
+ <ScrollArea.Viewport asChild>
168
+ <ComposableVirtuosoMasonry
169
+ {...arrowNavigationAttrs}
170
+ {...composableProps(props)}
171
+ ref={forwardedRef}
172
+ style={{ gap: `${gutter}rem` }}
173
+ data={items}
174
+ columnCount={columnCount}
175
+ ItemContent={TileAdapter}
176
+ />
177
+ </ScrollArea.Viewport>
178
+ );
179
+ },
180
+ );
181
+
182
+ const ComposableVirtuosoMasonry = composable<HTMLDivElement, VirtuosoMasonryProps<any, any>>(
183
+ ({ ...props }, _forwardedRef) => {
184
+ return <VirtuosoMasonry {...props} />;
185
+ },
186
+ );
187
+
188
+ MasonryViewportInner.displayName = 'Masonry.Viewport';
189
+
190
+ const MasonryViewport = MasonryViewportInner as <Item>(
191
+ props: MasonryViewportProps<Item> & {
126
192
  ref?: Ref<HTMLDivElement | null>;
127
193
  },
128
194
  ) => JSX.Element;
129
195
 
196
+ /** Compute column count from container width and column constraints. */
197
+ const useColumnCount = (
198
+ width: number,
199
+ columns: number | undefined,
200
+ maxColumns: number | undefined,
201
+ minColumnWidth: number,
202
+ maxColumnWidth: number,
203
+ gutter: number,
204
+ ) => {
205
+ const remInPx = usePx(1);
206
+ return useMemo(() => {
207
+ if (columns != null) {
208
+ return columns;
209
+ }
210
+
211
+ const minColumnWidthPx = minColumnWidth * remInPx;
212
+ const maxColumnWidthPx = maxColumnWidth * remInPx;
213
+ const gutterPx = gutter * remInPx;
214
+ if (width <= 0 || minColumnWidthPx <= 0) {
215
+ return 1;
216
+ }
217
+
218
+ // Each tile has paddingRight: gutter, so every slot (including the last) occupies
219
+ // (colWidth + gutterPx). The container therefore fits floor(containerWidth / (colWidth + gutterPx)) columns.
220
+ let cols = Math.floor(width / (minColumnWidthPx + gutterPx));
221
+ if (maxColumnWidthPx > 0) {
222
+ const effectiveColWidth = width / cols - gutterPx;
223
+ if (effectiveColWidth > maxColumnWidthPx) {
224
+ // Try to add columns to keep cards below maxColumnWidth, but never violate minColumnWidth.
225
+ const maxCols = Math.ceil(width / (maxColumnWidthPx + gutterPx));
226
+ if (width / maxCols - gutterPx >= minColumnWidthPx) {
227
+ cols = maxCols;
228
+ }
229
+ }
230
+ }
231
+
232
+ const clamped = maxColumns != null ? Math.min(cols, maxColumns) : cols;
233
+ return Math.max(1, clamped);
234
+ }, [remInPx, width, columns, maxColumns, minColumnWidth, maxColumnWidth, gutter]);
235
+ };
236
+
237
+ //
238
+ // Masonry
239
+ //
240
+
130
241
  export const Masonry = {
131
242
  Root: MasonryRoot,
243
+ Content: MasonryContent,
244
+ Viewport: MasonryViewport,
132
245
  };
133
246
 
134
- export type { MasonryRootProps };
247
+ export type { MasonryRootProps, MasonryContentProps, MasonryViewportProps };