Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
/generated
/public

README.md
integrations/next/.next
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.2.6

- `useDynamicRowHeight` should not instantiate `ResizeObserver` when server-rendering

## 2.2.5

- Use `defaultHeight`/`defaultWidth` prop to server render initial set of rows/cells
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ Documentation for this project is available at [react-window.vercel.app](https:/
### List

<!-- List:description:begin -->

Renders data with many rows.

<!-- List:description:end -->

#### Required props
Expand Down Expand Up @@ -168,12 +166,10 @@ The default value is &quot;div&quot;, meaning that List renders an HTMLDivElemen
### Grid

<!-- Grid:description:begin -->

Renders data with many rows and columns.

ℹ️ Unlike `List` rows, `Grid` cell sizes must be known ahead of time.
Either static sizes or something that can be derived (from the data in `CellProps`) without rendering.

<!-- Grid:description:end -->

#### Required props
Expand Down
22 changes: 22 additions & 0 deletions integrations/next/app/list-dynamic/components/List.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client";

import { List as ListExternal, useDynamicRowHeight } from "react-window";
import { RowComponent } from "./RowComponent";

export function List() {
const rowHeight = useDynamicRowHeight({
defaultRowHeight: 30
});

return (
<ListExternal
className="h-[250px]"
defaultHeight={250}
overscanCount={0}
rowComponent={RowComponent}
rowCount={100}
rowHeight={rowHeight}
rowProps={{}}
/>
);
}
15 changes: 15 additions & 0 deletions integrations/next/app/list-dynamic/components/RowComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use client";

import { type RowComponentProps } from "react-window";

export function RowComponent({
ariaAttributes,
index,
style
}: RowComponentProps<object>) {
return (
<div className="flex items-center gap-1" style={style} {...ariaAttributes}>
Row {index}
</div>
);
}
17 changes: 17 additions & 0 deletions integrations/next/app/list-dynamic/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
AnimationFrameRowCellCounter,
EnvironmentMarker,
LayoutShiftDetecter
} from "../../../tests";
import { List } from "./components/List";

export default async function Home() {
return (
<div className="p-2 flex flex-col gap-2">
<EnvironmentMarker>NextJS (server components)</EnvironmentMarker>
<AnimationFrameRowCellCounter />
<LayoutShiftDetecter />
<List />
</div>
);
}
1 change: 1 addition & 0 deletions integrations/next/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default async function Home() {
return (
<div className="p-2 flex flex-col gap-2">
<Link href="/list">List</Link>
<Link href="/list-dynamic">List + useDynamicRowHeight</Link>
<Link href="/grid">Grid</Link>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions integrations/vike/pages/index/+Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default function Page() {
return (
<div className="p-2 flex flex-col gap-2">
<a href="/list">List</a>
<a href="/list-dynamic">List + useDynamicRowHeight</a>
<a href="/grid">Grid</a>
</div>
);
Expand Down
30 changes: 30 additions & 0 deletions integrations/vike/pages/list-dynamic/+Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { List, useDynamicRowHeight } from "react-window";
import {
AnimationFrameRowCellCounter,
EnvironmentMarker,
LayoutShiftDetecter
} from "../../../tests";
import { RowComponent } from "./RowComponent";

export default function Page() {
const rowHeight = useDynamicRowHeight({
defaultRowHeight: 30
});

return (
<div className="p-2 flex flex-col gap-2">
<EnvironmentMarker>Vike (server rendering)</EnvironmentMarker>
<AnimationFrameRowCellCounter />
<LayoutShiftDetecter />
<List
className="h-[250px]"
defaultHeight={250}
overscanCount={0}
rowComponent={RowComponent}
rowCount={100}
rowHeight={rowHeight}
rowProps={{}}
/>
</div>
);
}
13 changes: 13 additions & 0 deletions integrations/vike/pages/list-dynamic/RowComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type RowComponentProps } from "react-window";

export function RowComponent({
ariaAttributes,
index,
style
}: RowComponentProps<object>) {
return (
<div className="flex items-center gap-1" style={style} {...ariaAttributes}>
Row {index}
</div>
);
}
27 changes: 17 additions & 10 deletions lib/components/list/useDynamicRowHeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,29 @@ export function useDynamicRowHeight({
}
);

const [resizeObserver] = useState(
() => new ResizeObserver(resizeObserverCallback)
);
const [resizeObserver] = useState(() => {
if (typeof ResizeObserver !== "undefined") {
return new ResizeObserver(resizeObserverCallback);
}
});

useEffect(() => {
return () => {
resizeObserver.disconnect();
};
if (resizeObserver) {
return () => {
resizeObserver.disconnect();
};
}
}, [resizeObserver]);

const observeRowElements = useCallback(
(elements: Element[] | NodeListOf<Element>) => {
elements.forEach((element) => resizeObserver.observe(element));
return () => {
elements.forEach((element) => resizeObserver.unobserve(element));
};
if (resizeObserver) {
elements.forEach((element) => resizeObserver.observe(element));
return () => {
elements.forEach((element) => resizeObserver.unobserve(element));
};
}
return () => {};
},
[resizeObserver]
);
Expand Down
34 changes: 29 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading