@dxos/react-ui-masonry 0.8.4-main.406dc2a → 0.8.4-main.43cb759274

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.406dc2a",
3
+ "version": "0.8.4-main.43cb759274",
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.406dc2a"
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.43cb759274",
35
+ "@dxos/react-ui-mosaic": "0.8.4-main.43cb759274",
36
+ "@dxos/types": "0.8.4-main.43cb759274",
37
+ "@dxos/ui-types": "0.8.4-main.43cb759274"
28
38
  },
29
39
  "devDependencies": {
30
- "@types/react": "~19.2.2",
31
- "@types/react-dom": "~19.2.1",
32
- "effect": "3.18.3",
33
- "react": "~19.2.0",
34
- "react-dom": "~19.2.0",
35
- "vite": "7.1.9",
36
- "@dxos/echo": "0.8.4-main.406dc2a",
37
- "@dxos/client": "0.8.4-main.406dc2a",
38
- "@dxos/random": "0.8.4-main.406dc2a",
39
- "@dxos/react-client": "0.8.4-main.406dc2a",
40
- "@dxos/react-ui-form": "0.8.4-main.406dc2a",
41
- "@dxos/react-ui-theme": "0.8.4-main.406dc2a",
42
- "@dxos/react-ui-stack": "0.8.4-main.406dc2a",
43
- "@dxos/storybook-utils": "0.8.4-main.406dc2a",
44
- "@dxos/schema": "0.8.4-main.406dc2a",
45
- "@dxos/react-ui": "0.8.4-main.406dc2a",
46
- "@dxos/test-utils": "0.8.4-main.406dc2a"
40
+ "@types/react": "~19.2.7",
41
+ "@types/react-dom": "~19.2.3",
42
+ "effect": "3.21.2",
43
+ "react": "~19.2.3",
44
+ "react-dom": "~19.2.3",
45
+ "vite": "^8.0.14",
46
+ "@dxos/client": "0.8.4-main.43cb759274",
47
+ "@dxos/echo": "0.8.4-main.43cb759274",
48
+ "@dxos/random": "0.8.4-main.43cb759274",
49
+ "@dxos/react-client": "0.8.4-main.43cb759274",
50
+ "@dxos/react-ui-form": "0.8.4-main.43cb759274",
51
+ "@dxos/react-ui": "0.8.4-main.43cb759274",
52
+ "@dxos/react-ui-stack": "0.8.4-main.43cb759274",
53
+ "@dxos/schema": "0.8.4-main.43cb759274",
54
+ "@dxos/storybook-utils": "0.8.4-main.43cb759274",
55
+ "@dxos/ui-theme": "0.8.4-main.43cb759274",
56
+ "@dxos/test-utils": "0.8.4-main.43cb759274"
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.406dc2a",
53
- "@dxos/react-ui": "0.8.4-main.406dc2a",
54
- "@dxos/react-ui-theme": "0.8.4-main.406dc2a"
59
+ "effect": "3.21.2",
60
+ "react": "~19.2.3",
61
+ "react-dom": "~19.2.3",
62
+ "@dxos/ui-theme": "0.8.4-main.43cb759274",
63
+ "@dxos/random": "0.8.4-main.43cb759274",
64
+ "@dxos/react-ui": "0.8.4-main.43cb759274"
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.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(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,129 +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';
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 { 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;
58
33
  };
59
34
 
60
- const MasonryRootImpl = <Item,>(
61
- {
62
- columnCount,
63
- maxColumnCount,
64
- columnWidth = 18,
65
- maxColumnWidth,
66
- columnGutter = 1,
67
- rowGutter,
68
- items,
69
- itemHeightEstimate = 256,
70
- itemKey,
71
- overscanBy = 6,
72
- render,
73
- onRender,
74
- classNames,
75
- intrinsicHeight,
76
- ...props
77
- }: MasonryRootProps<Item>,
78
- forwardedRef: ForwardedRef<HTMLDivElement>,
79
- ) => {
80
- const rootRef = useRef<HTMLDivElement | null>(null);
81
- const ref = useComposedRefs(rootRef, forwardedRef);
82
- const { width, height } = useSize(rootRef);
83
- const { scrollTop, isScrolling } = useScroller(rootRef);
84
- const remProps = useMemo(
85
- () => ({ columnWidth, maxColumnWidth, columnGutter, rowGutter }),
86
- [columnWidth, maxColumnWidth, columnGutter, rowGutter],
87
- );
88
- const pxProps = usePxProps(remProps);
89
- const positionerProps = useMemo(
90
- () => ({
91
- width,
92
- columnCount,
93
- maxColumnCount,
94
- ...pxProps,
95
- }),
96
- [width, pxProps, columnCount, maxColumnCount],
97
- );
98
- const positioner = usePositioner(positionerProps, [positionerProps]);
99
- const resizeObserver = useResizeObserver(positioner);
100
- const children = useMasonry({
101
- height: intrinsicHeight ? Infinity : height,
102
- scrollTop,
103
- isScrolling,
104
- resizeObserver,
105
- positioner,
106
- items,
107
- itemHeightEstimate,
108
- itemKey,
109
- overscanBy,
110
- render,
111
- onRender,
112
- });
113
- return (
114
- <div className={mx(classNames)} {...props} ref={ref}>
115
- {children}
116
- </div>
117
- );
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;
118
42
  };
119
43
 
120
- const MasonryRoot = forwardRef(MasonryRootImpl) as <Item>(
121
- 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 & {
122
107
  ref?: Ref<HTMLDivElement | null>;
123
108
  },
124
109
  ) => JSX.Element;
125
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: 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}
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> & {
193
+ ref?: Ref<HTMLDivElement | null>;
194
+ },
195
+ ) => JSX.Element;
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
+
126
242
  export const Masonry = {
127
243
  Root: MasonryRoot,
244
+ Content: MasonryContent,
245
+ Viewport: MasonryViewport,
128
246
  };
129
247
 
130
- export type { MasonryRootProps };
248
+ export type { MasonryRootProps, MasonryContentProps, MasonryViewportProps };