Skip to content

Commit

Permalink
feat: new onResize event
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed May 27, 2021
1 parent 71b89d0 commit 919144d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-frogs-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-virtual": patch
---

feat: new `onResize` event
5 changes: 5 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export interface OnScroll {
}): void;
}

export interface OnResize {
(event: { width: number; height: number }): void;
}

export interface Item {
readonly key?: string;
readonly index: number;
Expand Down Expand Up @@ -99,6 +103,7 @@ export interface Options {
isItemLoaded?: IsItemLoaded;
loadMore?: LoadMore;
onScroll?: OnScroll;
onResize?: OnResize;
}

export interface Return<O, I> {
Expand Down
10 changes: 10 additions & 0 deletions src/types/react-cool-virtual.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ declare module "react-cool-virtual" {
(event: OnScrollEvent): void;
}

export interface OnResizeEvent {
width: number;
height: number;
}

export interface OnResize {
(event: OnResizeEvent): void;
}

export interface MeasureRef {
(el: HTMLElement | null): void;
}
Expand Down Expand Up @@ -99,6 +108,7 @@ declare module "react-cool-virtual" {
isItemLoaded?: IsItemLoaded;
loadMore?: LoadMore;
onScroll?: OnScroll;
onResize?: OnResize;
}

export interface Return<
Expand Down
9 changes: 7 additions & 2 deletions src/useVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
LoadMore,
Measure,
OnScroll,
OnResize,
Options,
Return,
ScrollEasingFunction,
Expand Down Expand Up @@ -73,10 +74,11 @@ export default <
scrollDuration = 500,
scrollEasingFunction = easeInOutCubic,
keyExtractor,
onScroll,
loadMoreThreshold = 15,
isItemLoaded,
loadMore,
onScroll,
onResize,
}: Options): Return<O, I> => {
const [items, setItems] = useState<Item[]>(() =>
getInitItems(ssrItemCount, keyExtractor)
Expand All @@ -96,9 +98,10 @@ export default <
const easingFnRef = useLatest<ScrollEasingFunction>(scrollEasingFunction);
const keyExtractorRef = useLatest<KeyExtractor | undefined>(keyExtractor);
const itemSizeRef = useLatest<ItemSize>(itemSize);
const onScrollRef = useLatest<OnScroll | undefined>(onScroll);
const isItemLoadedRef = useRef<IsItemLoaded | undefined>(isItemLoaded);
const loadMoreRef = useLatest<LoadMore | undefined>(loadMore);
const onScrollRef = useLatest<OnScroll | undefined>(onScroll);
const onResizeRef = useLatest<OnResize | undefined>(onResize);
const sizeKey = !horizontal ? "height" : "width";
const itemSizeKey = !horizontal ? "blockSize" : "inlineSize";
const marginKey = !horizontal ? "marginTop" : "marginLeft";
Expand Down Expand Up @@ -431,6 +434,8 @@ export default <

handleScroll(scrollOffsetRef.current);

if (onResizeRef.current) onResizeRef.current(rect);

const { current: msData } = msDataRef;
const ratio =
!isSameWidth &&
Expand Down

0 comments on commit 919144d

Please sign in to comment.