@dxos/react-ui-masonry 0.8.4-main.03d5cd7b56

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 ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@dxos/react-ui-masonry",
3
+ "version": "0.8.4-main.03d5cd7b56",
4
+ "description": "A masonry component.",
5
+ "homepage": "https://dxos.org",
6
+ "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
11
+ "license": "MIT",
12
+ "author": "DXOS.org",
13
+ "sideEffects": false,
14
+ "type": "module",
15
+ "exports": {
16
+ ".": {
17
+ "source": "./src/index.ts",
18
+ "types": "./dist/types/src/index.d.ts",
19
+ "browser": "./dist/lib/browser/index.mjs",
20
+ "node": "./dist/lib/node-esm/index.mjs"
21
+ }
22
+ },
23
+ "types": "dist/types/src/index.d.ts",
24
+ "files": [
25
+ "dist",
26
+ "src"
27
+ ],
28
+ "dependencies": {
29
+ "@fluentui/react-tabster": "9.26.11",
30
+ "@radix-ui/react-compose-refs": "1.1.1",
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.03d5cd7b56",
35
+ "@dxos/react-ui-mosaic": "0.8.4-main.03d5cd7b56",
36
+ "@dxos/types": "0.8.4-main.03d5cd7b56",
37
+ "@dxos/ui-types": "0.8.4-main.03d5cd7b56"
38
+ },
39
+ "devDependencies": {
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.10",
46
+ "@dxos/echo": "0.8.4-main.03d5cd7b56",
47
+ "@dxos/random": "0.8.4-main.03d5cd7b56",
48
+ "@dxos/react-client": "0.8.4-main.03d5cd7b56",
49
+ "@dxos/react-ui": "0.8.4-main.03d5cd7b56",
50
+ "@dxos/react-ui-form": "0.8.4-main.03d5cd7b56",
51
+ "@dxos/storybook-utils": "0.8.4-main.03d5cd7b56",
52
+ "@dxos/react-ui-stack": "0.8.4-main.03d5cd7b56",
53
+ "@dxos/schema": "0.8.4-main.03d5cd7b56",
54
+ "@dxos/test-utils": "0.8.4-main.03d5cd7b56",
55
+ "@dxos/ui-theme": "0.8.4-main.03d5cd7b56",
56
+ "@dxos/client": "0.8.4-main.03d5cd7b56"
57
+ },
58
+ "peerDependencies": {
59
+ "effect": "3.20.0",
60
+ "react": "~19.2.3",
61
+ "react-dom": "~19.2.3",
62
+ "@dxos/react-ui": "0.8.4-main.03d5cd7b56",
63
+ "@dxos/ui-theme": "0.8.4-main.03d5cd7b56",
64
+ "@dxos/random": "0.8.4-main.03d5cd7b56"
65
+ },
66
+ "publishConfig": {
67
+ "access": "public"
68
+ }
69
+ }
@@ -0,0 +1,74 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
+ import React from 'react';
7
+
8
+ import { Filter } from '@dxos/client/echo';
9
+ import { random } from '@dxos/random';
10
+ import { useQuery } from '@dxos/react-client/echo';
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';
14
+ import { createObjectFactory } from '@dxos/schema/testing';
15
+ import { Organization } from '@dxos/types';
16
+
17
+ import { Masonry, MasonryRootProps } from './Masonry';
18
+
19
+ const StoryItem = ({ data: { image, name, description } }: { data: Organization.Organization }) => {
20
+ return (
21
+ <Card.Root>
22
+ <Card.Toolbar>
23
+ <Card.Icon icon='ph--building-office--regular' />
24
+ <Card.Title>{name}</Card.Title>
25
+ </Card.Toolbar>
26
+ <Card.Poster alt={name!} {...(image ? { image } : { icon: 'ph--building-office--regular' })} />
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>
33
+ );
34
+ };
35
+
36
+ const DefaultStory = (props: MasonryRootProps) => {
37
+ const { space } = useClientStory();
38
+ const organizations = useQuery(space?.db, Filter.type(Organization.Organization));
39
+
40
+ return (
41
+ <Masonry.Root {...props} Tile={StoryItem}>
42
+ <Masonry.Content>
43
+ <Masonry.Viewport items={organizations} />
44
+ </Masonry.Content>
45
+ </Masonry.Root>
46
+ );
47
+ };
48
+
49
+ const meta = {
50
+ title: 'ui/react-ui-masonry/Masonry',
51
+ render: DefaultStory,
52
+ decorators: [
53
+ withTheme(),
54
+ withLayout({ layout: 'fullscreen' }),
55
+ withClientProvider({
56
+ types: [Organization.Organization],
57
+ createIdentity: true,
58
+ createSpace: true,
59
+ onCreateSpace: async ({ space }) => {
60
+ const factory = createObjectFactory(space.db, random as any);
61
+ await factory([{ type: Organization.Organization, count: 36 }]);
62
+ },
63
+ }),
64
+ ],
65
+ parameters: {
66
+ layout: 'fullscreen',
67
+ },
68
+ } satisfies Meta;
69
+
70
+ export default meta;
71
+
72
+ type Story = StoryObj<typeof meta>;
73
+
74
+ export const Default: Story = {};
@@ -0,0 +1,247 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { useArrowNavigationGroup } from '@fluentui/react-tabster';
6
+ import { useComposedRefs } from '@radix-ui/react-compose-refs';
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;
32
+ };
33
+
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;
41
+ };
42
+
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> & {
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
+
241
+ export const Masonry = {
242
+ Root: MasonryRoot,
243
+ Content: MasonryContent,
244
+ Viewport: MasonryViewport,
245
+ };
246
+
247
+ export type { MasonryRootProps, MasonryContentProps, MasonryViewportProps };
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './Masonry';