@dxos/react-ui-masonry 0.8.4-main.fffef41 → 0.9.0

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