Skip to content
Source
Sign inSign up

useLegendListRef.ts

25 lines858 Bytes

1import type { LegendListRef } from "@legendapp/list/react";
2import { type RefObject } from "react";
3import { useStableCallback } from "./useStableCallback";
4​
5/** Refresh scroll-derived UI when layout changes without a scroll event. */
6export function useLegendListRef(listRef: RefObject<LegendListRef | null>, onChange: () => void) {
7 const notify = useStableCallback(onChange);
8 return (list: LegendListRef | null) => {
9 listRef.current = list;
10 if (!list) return;
11 const state = list.getState();
12 const stops = [
13 state.listen("totalSize", notify),
14 state.listen("headerSize", notify),
15 state.listen("footerSize", notify),
16 state.listen("isAtStart", notify),
17 state.listen("isAtEnd", notify),
18 ];
19 notify();
20 return () => {
21 for (const stop of stops) stop();
22 listRef.current = null;
23 };
24 };