@dxos/react-ui-masonry 0.8.4-main.70d3990 → 0.8.4-main.74a063c4e0

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.70d3990",
3
+ "version": "0.8.4-main.74a063c4e0",
4
4
  "description": "A masonry component.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
- "sideEffects": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
@@ -23,37 +27,40 @@
23
27
  ],
24
28
  "dependencies": {
25
29
  "@radix-ui/react-compose-refs": "1.1.1",
26
- "masonic": "^4.1.0",
30
+ "@radix-ui/react-context": "1.1.1",
31
+ "@virtuoso.dev/masonry": "^1.4.2",
27
32
  "react-resize-detector": "^11.0.1",
28
- "@dxos/react-hooks": "0.8.4-main.70d3990",
29
- "@dxos/types": "0.8.4-main.70d3990"
33
+ "@dxos/react-ui-mosaic": "0.8.4-main.74a063c4e0",
34
+ "@dxos/types": "0.8.4-main.74a063c4e0",
35
+ "@dxos/react-hooks": "0.8.4-main.74a063c4e0",
36
+ "@dxos/ui-types": "0.8.4-main.74a063c4e0"
30
37
  },
31
38
  "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.70d3990",
39
- "@dxos/echo": "0.8.4-main.70d3990",
40
- "@dxos/react-client": "0.8.4-main.70d3990",
41
- "@dxos/random": "0.8.4-main.70d3990",
42
- "@dxos/react-ui": "0.8.4-main.70d3990",
43
- "@dxos/react-ui-form": "0.8.4-main.70d3990",
44
- "@dxos/react-ui-stack": "0.8.4-main.70d3990",
45
- "@dxos/schema": "0.8.4-main.70d3990",
46
- "@dxos/storybook-utils": "0.8.4-main.70d3990",
47
- "@dxos/react-ui-theme": "0.8.4-main.70d3990",
48
- "@dxos/test-utils": "0.8.4-main.70d3990"
39
+ "@types/react": "~19.2.7",
40
+ "@types/react-dom": "~19.2.3",
41
+ "effect": "3.20.0",
42
+ "react": "~19.2.3",
43
+ "react-dom": "~19.2.3",
44
+ "vite": "^7.1.11",
45
+ "@dxos/client": "0.8.4-main.74a063c4e0",
46
+ "@dxos/echo": "0.8.4-main.74a063c4e0",
47
+ "@dxos/random": "0.8.4-main.74a063c4e0",
48
+ "@dxos/react-client": "0.8.4-main.74a063c4e0",
49
+ "@dxos/react-ui": "0.8.4-main.74a063c4e0",
50
+ "@dxos/react-ui-stack": "0.8.4-main.74a063c4e0",
51
+ "@dxos/react-ui-form": "0.8.4-main.74a063c4e0",
52
+ "@dxos/schema": "0.8.4-main.74a063c4e0",
53
+ "@dxos/storybook-utils": "0.8.4-main.74a063c4e0",
54
+ "@dxos/test-utils": "0.8.4-main.74a063c4e0",
55
+ "@dxos/ui-theme": "0.8.4-main.74a063c4e0"
49
56
  },
50
57
  "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.70d3990",
55
- "@dxos/random": "0.8.4-main.70d3990",
56
- "@dxos/react-ui-theme": "0.8.4-main.70d3990"
58
+ "effect": "3.20.0",
59
+ "react": "~19.2.3",
60
+ "react-dom": "~19.2.3",
61
+ "@dxos/random": "0.8.4-main.74a063c4e0",
62
+ "@dxos/react-ui": "0.8.4-main.74a063c4e0",
63
+ "@dxos/ui-theme": "0.8.4-main.74a063c4e0"
57
64
  },
58
65
  "publishConfig": {
59
66
  "access": "public"
@@ -6,52 +6,56 @@ 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
- 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.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(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 items={organizations} />
43
+ </Masonry.Root>
42
44
  );
43
45
  };
44
46
 
45
47
  const meta = {
46
48
  title: 'ui/react-ui-masonry/Masonry',
49
+ render: DefaultStory,
47
50
  decorators: [
48
- withTheme,
51
+ withTheme(),
52
+ withLayout({ layout: 'fullscreen' }),
49
53
  withClientProvider({
50
54
  types: [Organization.Organization],
51
55
  createIdentity: true,
52
56
  createSpace: true,
53
57
  onCreateSpace: async ({ space }) => {
54
- const factory = createObjectFactory(space.db, faker as any);
58
+ const factory = createObjectFactory(space.db, random as any);
55
59
  await factory([{ type: Organization.Organization, count: 36 }]);
56
60
  },
57
61
  }),
@@ -59,12 +63,10 @@ const meta = {
59
63
  parameters: {
60
64
  layout: 'fullscreen',
61
65
  },
62
- } satisfies Meta<typeof Masonry>;
66
+ } satisfies Meta;
63
67
 
64
68
  export default meta;
65
69
 
66
70
  type Story = StoryObj<typeof meta>;
67
71
 
68
- export const Default: Story = {
69
- render: DefaultStory,
70
- };
72
+ export const Default: Story = {};
package/src/Masonry.tsx CHANGED
@@ -3,132 +3,187 @@
3
3
  //
4
4
 
5
5
  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';
6
+ import { createContext } from '@radix-ui/react-context';
7
+ import { VirtuosoMasonry, type VirtuosoMasonryProps } from '@virtuoso.dev/masonry';
8
+ import React, { type ComponentType, type JSX, type PropsWithChildren, type Ref, useMemo, useRef } from 'react';
22
9
  import { useResizeDetector } from 'react-resize-detector';
23
10
 
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]);
11
+ import { ScrollArea, ScrollAreaRootProps, ThemedClassName, usePx } from '@dxos/react-ui';
12
+ import { cardMaxInlineSize, cardMinInlineSize, composable, composableProps, scrollbar } from '@dxos/ui-theme';
13
+
14
+ //
15
+ // Context
16
+ //
17
+
18
+ type MasonryContextValue = {
19
+ /** Render component for each masonry item. */
20
+ Tile: ComponentType<{ data: any; index: number }>;
21
+ /** Override auto-calculated column count. */
22
+ columns: number | undefined;
23
+ /** Upper bound on number of columns. */
24
+ maxColumns: number | undefined;
25
+ /** Minimum column width in rem. */
26
+ minColumnWidth: number;
27
+ /** Maximum column width in rem. */
28
+ maxColumnWidth: number;
29
+ /** Space between columns and rows in rem. */
30
+ gutter: number;
60
31
  };
61
32
 
62
- const MasonryRootImpl = <Item,>(
33
+ const MASONRY_NAME = 'Masonry';
34
+
35
+ const [MasonryProvider, useMasonryContext] = createContext<MasonryContextValue>(MASONRY_NAME);
36
+
37
+ //
38
+ // Root
39
+ //
40
+
41
+ type MasonryRootProps = PropsWithChildren<Partial<MasonryContextValue>>;
42
+
43
+ const MasonryRoot = ({
44
+ children,
45
+ Tile,
46
+ columns = undefined,
47
+ maxColumns = undefined,
48
+ minColumnWidth = cardMinInlineSize,
49
+ maxColumnWidth = cardMaxInlineSize,
50
+ gutter = 0.75,
51
+ }: MasonryRootProps) => (
52
+ <MasonryProvider
53
+ Tile={Tile!}
54
+ columns={columns}
55
+ maxColumns={maxColumns}
56
+ minColumnWidth={minColumnWidth}
57
+ maxColumnWidth={maxColumnWidth}
58
+ gutter={gutter}
59
+ >
60
+ {children}
61
+ </MasonryProvider>
62
+ );
63
+
64
+ MasonryRoot.displayName = 'Masonry.Root';
65
+
66
+ //
67
+ // Content
68
+ //
69
+
70
+ type MasonryContentProps<Item> = ThemedClassName<
63
71
  {
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
- );
122
- };
72
+ /** Items to render in the masonry grid. */
73
+ items: Item[];
74
+ /** Extract a stable key from an item, aligned with react-ui-mosaic's getId. */
75
+ getId?: (data: Item) => string;
76
+ } & Pick<ScrollAreaRootProps, 'centered' | 'thin' | 'padding'>
77
+ >;
78
+
79
+ const MasonryContentInner = composable<HTMLDivElement, MasonryContentProps<any>>(
80
+ ({ items, getId, centered, thin = true, padding = true, ...props }, forwardedRef) => {
81
+ const rootRef = useRef<HTMLDivElement | null>(null);
82
+ const composedRef = useComposedRefs(rootRef, forwardedRef);
83
+ const { width = 0 } = useResizeDetector({ targetRef: rootRef });
123
84
 
124
- const MasonryRoot = forwardRef(MasonryRootImpl) as <Item>(
125
- props: MasonryRootProps<Item> & {
85
+ const { Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gutter } = useMasonryContext('Masonry.Content');
86
+ const columnCount = useColumnCount(
87
+ width - (scrollbar.thin.size + scrollbar.thin.padding),
88
+ columns,
89
+ maxColumns,
90
+ minColumnWidth,
91
+ maxColumnWidth,
92
+ gutter,
93
+ );
94
+
95
+ const TileAdapter = useMemo(() => {
96
+ const Adapter = ({ data, index }: { data: any; index: number }) => {
97
+ return (
98
+ <div role='listitem' style={{ paddingBottom: `${gutter}rem` }}>
99
+ <Tile index={index} data={data} />
100
+ </div>
101
+ );
102
+ };
103
+ Adapter.displayName = 'Masonry.TileAdapter';
104
+ return Adapter;
105
+ }, [Tile, gutter]);
106
+
107
+ // NOTE: Masonry currently doesn't support an external scroller.
108
+ // https://github.com/petyosi/react-virtuoso/issues/1305
109
+ return (
110
+ <ScrollArea.Root {...composableProps(props)} centered={centered} thin={thin} padding={padding} ref={composedRef}>
111
+ {width > 0 && (
112
+ <ScrollArea.Viewport asChild>
113
+ <ComposableVirtuosoMasonry
114
+ style={{ gap: `${gutter}rem` }}
115
+ data={items}
116
+ columnCount={columnCount}
117
+ ItemContent={TileAdapter}
118
+ />
119
+ </ScrollArea.Viewport>
120
+ )}
121
+ </ScrollArea.Root>
122
+ );
123
+ },
124
+ );
125
+
126
+ const ComposableVirtuosoMasonry = composable<HTMLDivElement, VirtuosoMasonryProps<any, any>>(({ ...props }) => {
127
+ return <VirtuosoMasonry {...props} />;
128
+ });
129
+
130
+ MasonryContentInner.displayName = 'Masonry.Content';
131
+
132
+ const MasonryContent = MasonryContentInner as <Item>(
133
+ props: MasonryContentProps<Item> & {
126
134
  ref?: Ref<HTMLDivElement | null>;
127
135
  },
128
136
  ) => JSX.Element;
129
137
 
138
+ /** Compute column count from container width and column constraints. */
139
+ const useColumnCount = (
140
+ width: number,
141
+ columns: number | undefined,
142
+ maxColumns: number | undefined,
143
+ minColumnWidth: number,
144
+ maxColumnWidth: number,
145
+ gutter: number,
146
+ ) => {
147
+ const remInPx = usePx(1);
148
+ return useMemo(() => {
149
+ if (columns != null) {
150
+ return columns;
151
+ }
152
+
153
+ const minColumnWidthPx = minColumnWidth * remInPx;
154
+ const maxColumnWidthPx = maxColumnWidth * remInPx;
155
+ const gutterPx = gutter * remInPx;
156
+
157
+ if (width <= 0 || minColumnWidthPx <= 0) {
158
+ return 1;
159
+ }
160
+
161
+ // Each tile has paddingRight: gutter, so every slot (including the last) occupies
162
+ // (colWidth + gutterPx). The container therefore fits floor(containerWidth / (colWidth + gutterPx)) columns.
163
+ let cols = Math.floor(width / (minColumnWidthPx + gutterPx));
164
+ if (maxColumnWidthPx > 0) {
165
+ const effectiveColWidth = width / cols - gutterPx;
166
+ if (effectiveColWidth > maxColumnWidthPx) {
167
+ // Try to add columns to keep cards below maxColumnWidth, but never violate minColumnWidth.
168
+ const maxCols = Math.ceil(width / (maxColumnWidthPx + gutterPx));
169
+ if (width / maxCols - gutterPx >= minColumnWidthPx) {
170
+ cols = maxCols;
171
+ }
172
+ }
173
+ }
174
+
175
+ const clamped = maxColumns != null ? Math.min(cols, maxColumns) : cols;
176
+ return Math.max(1, clamped);
177
+ }, [remInPx, width, columns, maxColumns, minColumnWidth, maxColumnWidth, gutter]);
178
+ };
179
+
180
+ //
181
+ // Masonry
182
+ //
183
+
130
184
  export const Masonry = {
131
185
  Root: MasonryRoot,
186
+ Content: MasonryContent,
132
187
  };
133
188
 
134
- export type { MasonryRootProps };
189
+ export type { MasonryRootProps, MasonryContentProps };