Skip to content

Commit 1773182

Browse files
committed
useDynamicRowHeight: Don't call ResizeObserver when server-rendering
1 parent 69330f4 commit 1773182

File tree

10 files changed

+149
-15
lines changed

10 files changed

+149
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.2.6
4+
5+
- `useDynamicRowHeight` should not instantiate `ResizeObserver` when server-rendering
6+
37
## 2.2.5
48

59
- Use `defaultHeight`/`defaultWidth` prop to server render initial set of rows/cells
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use client";
2+
3+
import { List as ListExternal, useDynamicRowHeight } from "react-window";
4+
import { RowComponent } from "./RowComponent";
5+
6+
export function List() {
7+
const rowHeight = useDynamicRowHeight({
8+
defaultRowHeight: 30
9+
});
10+
11+
return (
12+
<ListExternal
13+
className="h-[250px]"
14+
defaultHeight={250}
15+
overscanCount={0}
16+
rowComponent={RowComponent}
17+
rowCount={100}
18+
rowHeight={rowHeight}
19+
rowProps={{}}
20+
/>
21+
);
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use client";
2+
3+
import { type RowComponentProps } from "react-window";
4+
5+
export function RowComponent({
6+
ariaAttributes,
7+
index,
8+
style
9+
}: RowComponentProps<object>) {
10+
return (
11+
<div className="flex items-center gap-1" style={style} {...ariaAttributes}>
12+
Row {index}
13+
</div>
14+
);
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {
2+
AnimationFrameRowCellCounter,
3+
EnvironmentMarker,
4+
LayoutShiftDetecter
5+
} from "../../../tests";
6+
import { List } from "./components/List";
7+
8+
export default async function Home() {
9+
return (
10+
<div className="p-2 flex flex-col gap-2">
11+
<EnvironmentMarker>NextJS (server components)</EnvironmentMarker>
12+
<AnimationFrameRowCellCounter />
13+
<LayoutShiftDetecter />
14+
<List />
15+
</div>
16+
);
17+
}

integrations/next/app/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default async function Home() {
44
return (
55
<div className="p-2 flex flex-col gap-2">
66
<Link href="/list">List</Link>
7+
<Link href="/list-dynamic">List + useDynamicRowHeight</Link>
78
<Link href="/grid">Grid</Link>
89
</div>
910
);

integrations/vike/pages/index/+Page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export default function Page() {
22
return (
33
<div className="p-2 flex flex-col gap-2">
44
<a href="/list">List</a>
5+
<a href="/list-dynamic">List + useDynamicRowHeight</a>
56
<a href="/grid">Grid</a>
67
</div>
78
);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { List, useDynamicRowHeight } from "react-window";
2+
import {
3+
AnimationFrameRowCellCounter,
4+
EnvironmentMarker,
5+
LayoutShiftDetecter
6+
} from "../../../tests";
7+
import { RowComponent } from "./RowComponent";
8+
9+
export default function Page() {
10+
const rowHeight = useDynamicRowHeight({
11+
defaultRowHeight: 30
12+
});
13+
14+
return (
15+
<div className="p-2 flex flex-col gap-2">
16+
<EnvironmentMarker>Vike (server rendering)</EnvironmentMarker>
17+
<AnimationFrameRowCellCounter />
18+
<LayoutShiftDetecter />
19+
<List
20+
className="h-[250px]"
21+
defaultHeight={250}
22+
overscanCount={0}
23+
rowComponent={RowComponent}
24+
rowCount={100}
25+
rowHeight={rowHeight}
26+
rowProps={{}}
27+
/>
28+
</div>
29+
);
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { type RowComponentProps } from "react-window";
2+
3+
export function RowComponent({
4+
ariaAttributes,
5+
index,
6+
style
7+
}: RowComponentProps<object>) {
8+
return (
9+
<div className="flex items-center gap-1" style={style} {...ariaAttributes}>
10+
Row {index}
11+
</div>
12+
);
13+
}

lib/components/list/useDynamicRowHeight.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,29 @@ export function useDynamicRowHeight({
103103
}
104104
);
105105

106-
const [resizeObserver] = useState(
107-
() => new ResizeObserver(resizeObserverCallback)
108-
);
106+
const [resizeObserver] = useState(() => {
107+
if (typeof ResizeObserver !== "undefined") {
108+
return new ResizeObserver(resizeObserverCallback);
109+
}
110+
});
109111

110112
useEffect(() => {
111-
return () => {
112-
resizeObserver.disconnect();
113-
};
113+
if (resizeObserver) {
114+
return () => {
115+
resizeObserver.disconnect();
116+
};
117+
}
114118
}, [resizeObserver]);
115119

116120
const observeRowElements = useCallback(
117121
(elements: Element[] | NodeListOf<Element>) => {
118-
elements.forEach((element) => resizeObserver.observe(element));
119-
return () => {
120-
elements.forEach((element) => resizeObserver.unobserve(element));
121-
};
122+
if (resizeObserver) {
123+
elements.forEach((element) => resizeObserver.observe(element));
124+
return () => {
125+
elements.forEach((element) => resizeObserver.unobserve(element));
126+
};
127+
}
128+
return () => {};
122129
},
123130
[resizeObserver]
124131
);

pnpm-lock.yaml

Lines changed: 29 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)