@dxos/react-ui-masonry 0.8.4-main.2c6827d
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/LICENSE +8 -0
- package/README.md +3 -0
- package/dist/lib/browser/index.mjs +86 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +88 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/Masonry.d.ts +13 -0
- package/dist/types/src/Masonry.d.ts.map +1 -0
- package/dist/types/src/Masonry.stories.d.ts +12 -0
- package/dist/types/src/Masonry.stories.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +61 -0
- package/src/Masonry.stories.tsx +70 -0
- package/src/Masonry.tsx +134 -0
- package/src/index.ts +5 -0
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/react-ui-masonry",
|
|
3
|
+
"version": "0.8.4-main.2c6827d",
|
|
4
|
+
"description": "A masonry component.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "DXOS.org",
|
|
9
|
+
"sideEffects": true,
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"source": "./src/index.ts",
|
|
14
|
+
"types": "./dist/types/src/index.d.ts",
|
|
15
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
16
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "dist/types/src/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@radix-ui/react-compose-refs": "1.1.1",
|
|
26
|
+
"masonic": "^4.1.0",
|
|
27
|
+
"react-resize-detector": "^11.0.1",
|
|
28
|
+
"@dxos/react-hooks": "0.8.4-main.2c6827d",
|
|
29
|
+
"@dxos/types": "0.8.4-main.2c6827d"
|
|
30
|
+
},
|
|
31
|
+
"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.2c6827d",
|
|
39
|
+
"@dxos/echo": "0.8.4-main.2c6827d",
|
|
40
|
+
"@dxos/react-client": "0.8.4-main.2c6827d",
|
|
41
|
+
"@dxos/react-ui": "0.8.4-main.2c6827d",
|
|
42
|
+
"@dxos/random": "0.8.4-main.2c6827d",
|
|
43
|
+
"@dxos/react-ui-stack": "0.8.4-main.2c6827d",
|
|
44
|
+
"@dxos/react-ui-form": "0.8.4-main.2c6827d",
|
|
45
|
+
"@dxos/react-ui-theme": "0.8.4-main.2c6827d",
|
|
46
|
+
"@dxos/schema": "0.8.4-main.2c6827d",
|
|
47
|
+
"@dxos/storybook-utils": "0.8.4-main.2c6827d",
|
|
48
|
+
"@dxos/test-utils": "0.8.4-main.2c6827d"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"effect": "^3.13.3",
|
|
52
|
+
"react": "^19.0.0",
|
|
53
|
+
"react-dom": "^19.0.0",
|
|
54
|
+
"@dxos/random": "0.8.4-main.2c6827d",
|
|
55
|
+
"@dxos/react-ui-theme": "0.8.4-main.2c6827d",
|
|
56
|
+
"@dxos/react-ui": "0.8.4-main.2c6827d"
|
|
57
|
+
},
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
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 { faker } from '@dxos/random';
|
|
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 { createObjectFactory } from '@dxos/schema/testing';
|
|
16
|
+
import { Organization } from '@dxos/types';
|
|
17
|
+
|
|
18
|
+
import { Masonry } from './Masonry';
|
|
19
|
+
|
|
20
|
+
const StoryItem = ({ data: { image, name, description } }: { data: Organization.Organization }) => {
|
|
21
|
+
return (
|
|
22
|
+
<Card.StaticRoot>
|
|
23
|
+
<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>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const DefaultStory = () => {
|
|
33
|
+
const { space } = useClientProvider();
|
|
34
|
+
const organizations = useQuery(space, Filter.type(Organization.Organization));
|
|
35
|
+
|
|
36
|
+
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
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const meta = {
|
|
46
|
+
title: 'ui/react-ui-masonry/Masonry',
|
|
47
|
+
decorators: [
|
|
48
|
+
withTheme,
|
|
49
|
+
withClientProvider({
|
|
50
|
+
types: [Organization.Organization],
|
|
51
|
+
createIdentity: true,
|
|
52
|
+
createSpace: true,
|
|
53
|
+
onCreateSpace: async ({ space }) => {
|
|
54
|
+
const factory = createObjectFactory(space.db, faker as any);
|
|
55
|
+
await factory([{ type: Organization.Organization, count: 36 }]);
|
|
56
|
+
},
|
|
57
|
+
}),
|
|
58
|
+
],
|
|
59
|
+
parameters: {
|
|
60
|
+
layout: 'fullscreen',
|
|
61
|
+
},
|
|
62
|
+
} satisfies Meta<typeof Masonry>;
|
|
63
|
+
|
|
64
|
+
export default meta;
|
|
65
|
+
|
|
66
|
+
type Story = StoryObj<typeof meta>;
|
|
67
|
+
|
|
68
|
+
export const Default: Story = {
|
|
69
|
+
render: DefaultStory,
|
|
70
|
+
};
|
package/src/Masonry.tsx
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
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';
|
|
22
|
+
import { useResizeDetector } from 'react-resize-detector';
|
|
23
|
+
|
|
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]);
|
|
60
|
+
};
|
|
61
|
+
|
|
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
|
+
);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const MasonryRoot = forwardRef(MasonryRootImpl) as <Item>(
|
|
125
|
+
props: MasonryRootProps<Item> & {
|
|
126
|
+
ref?: Ref<HTMLDivElement | null>;
|
|
127
|
+
},
|
|
128
|
+
) => JSX.Element;
|
|
129
|
+
|
|
130
|
+
export const Masonry = {
|
|
131
|
+
Root: MasonryRoot,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export type { MasonryRootProps };
|