@dxos/react-ui-masonry 0.8.4-main.ae835ea → 0.8.4-main.bc2380dfbc

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.bc2380dfbc",
4
4
  "description": "A masonry component.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
- "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
11
+ "license": "FSL-1.1-Apache-2.0",
8
12
  "author": "DXOS.org",
9
- "sideEffects": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
@@ -22,36 +26,42 @@
22
26
  "src"
23
27
  ],
24
28
  "dependencies": {
29
+ "@fluentui/react-tabster": "9.26.11",
25
30
  "@radix-ui/react-compose-refs": "1.1.1",
26
- "masonic": "^4.1.0",
27
- "@dxos/react-hooks": "0.8.4-main.ae835ea"
31
+ "@radix-ui/react-context": "1.1.1",
32
+ "@virtuoso.dev/masonry": "^1.4.2",
33
+ "react-resize-detector": "^11.0.1",
34
+ "@dxos/react-hooks": "0.8.4-main.bc2380dfbc",
35
+ "@dxos/types": "0.8.4-main.bc2380dfbc",
36
+ "@dxos/ui-types": "0.8.4-main.bc2380dfbc",
37
+ "@dxos/react-ui-mosaic": "0.8.4-main.bc2380dfbc"
28
38
  },
29
39
  "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"
40
+ "@types/react": "~19.2.7",
41
+ "@types/react-dom": "~19.2.3",
42
+ "effect": "3.20.0",
43
+ "react": "~19.2.3",
44
+ "react-dom": "~19.2.3",
45
+ "vite": "^8.0.13",
46
+ "@dxos/client": "0.8.4-main.bc2380dfbc",
47
+ "@dxos/echo": "0.8.4-main.bc2380dfbc",
48
+ "@dxos/react-ui": "0.8.4-main.bc2380dfbc",
49
+ "@dxos/random": "0.8.4-main.bc2380dfbc",
50
+ "@dxos/react-client": "0.8.4-main.bc2380dfbc",
51
+ "@dxos/react-ui-form": "0.8.4-main.bc2380dfbc",
52
+ "@dxos/storybook-utils": "0.8.4-main.bc2380dfbc",
53
+ "@dxos/react-ui-stack": "0.8.4-main.bc2380dfbc",
54
+ "@dxos/schema": "0.8.4-main.bc2380dfbc",
55
+ "@dxos/test-utils": "0.8.4-main.bc2380dfbc",
56
+ "@dxos/ui-theme": "0.8.4-main.bc2380dfbc"
47
57
  },
48
58
  "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"
59
+ "effect": "3.20.0",
60
+ "react": "~19.2.3",
61
+ "react-dom": "~19.2.3",
62
+ "@dxos/random": "0.8.4-main.bc2380dfbc",
63
+ "@dxos/ui-theme": "0.8.4-main.bc2380dfbc",
64
+ "@dxos/react-ui": "0.8.4-main.bc2380dfbc"
55
65
  },
56
66
  "publishConfig": {
57
67
  "access": "public"
@@ -5,66 +5,70 @@
5
5
  import { type Meta, type StoryObj } from '@storybook/react-vite';
6
6
  import React from 'react';
7
7
 
8
- import { Filter } from '@dxos/client/echo';
9
- import { faker } from '@dxos/random';
8
+ import { Filter } from '@dxos/echo';
9
+ import { random } 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 } 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
+ const DefaultStory = (props: MasonryRootProps) => {
37
+ const { space } = useClientStory();
38
+ const organizations = useQuery(space?.db, Filter.type(Organization.Organization));
35
39
 
36
40
  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
- />
41
+ <Masonry.Root {...props} Tile={StoryItem}>
42
+ <Masonry.Content>
43
+ <Masonry.Viewport items={organizations} />
44
+ </Masonry.Content>
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
- const factory = createObjectFactory(space.db, faker as any);
55
- await factory([{ type: DataType.Organization, count: 36 }]);
60
+ const factory = createObjectFactory(space.db, random as any);
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,
70
- };
74
+ export const Default: Story = {};
package/src/Masonry.tsx CHANGED
@@ -2,131 +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';
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]);
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';
10
+ import { useResizeDetector } from 'react-resize-detector';
11
+
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;
58
32
  };
59
33
 
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
- );
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;
120
41
  };
121
42
 
122
- const MasonryRoot = forwardRef(MasonryRootImpl) as <Item>(
123
- 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 & {
124
106
  ref?: Ref<HTMLDivElement | null>;
125
107
  },
126
108
  ) => JSX.Element;
127
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> & {
192
+ ref?: Ref<HTMLDivElement | null>;
193
+ },
194
+ ) => JSX.Element;
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
+
128
241
  export const Masonry = {
129
242
  Root: MasonryRoot,
243
+ Content: MasonryContent,
244
+ Viewport: MasonryViewport,
130
245
  };
131
246
 
132
- export type { MasonryRootProps };
247
+ export type { MasonryRootProps, MasonryContentProps, MasonryViewportProps };