Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add viewportSize to handle #72

Merged
merged 1 commit into from
May 4, 2023
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
7 changes: 7 additions & 0 deletions src/react/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export interface ListHandle {
* Get current scrollHeight or scrollWidth.
*/
readonly scrollSize: number;
/**
* Get current offsetHeight or offsetWidth.
*/
readonly viewportSize: number;
/**
* Scroll to the item specified by index.
* @param index index of item
Expand Down Expand Up @@ -388,6 +392,9 @@ export const List = forwardRef<ListHandle, ListProps>(
get scrollSize() {
return scroller._getActualScrollSize();
},
get viewportSize() {
return store._getViewportSize();
},
scrollToIndex(index) {
scroller._scrollToIndex(index, count);
},
Expand Down
2 changes: 1 addition & 1 deletion stories/advanced/InfiniteScrolling.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const InfiniteScrolling: StoryObj = {
}}
>
{items.map((d) => (
<Row id={d.id} name={d.name} text={d.text} />
<Row key={d.id} id={d.id} name={d.name} text={d.text} />
))}
{/* Now hide spinner without unmounting because onRangeChange is called twice due to item length change */}
<Spinner hidden={!fetching} />
Expand Down
73 changes: 73 additions & 0 deletions stories/advanced/Loop.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Meta, StoryObj } from "@storybook/react";
import React, { useLayoutEffect, useMemo, useRef } from "react";
import { List, ListHandle } from "../../src";

export default {
component: List,
} as Meta;

const Row = ({ i }: { i: number }) => {
return (
<div
style={{
background: i % 2 === 0 ? "darkgray" : "white",
color: i % 2 === 0 ? "white" : "darkgray",
borderBottom: "solid 1px #ccc",
display: "flex",
alignItems: "center",
justifyContent: "center",
padding: 10,
height: 200,
fontSize: 64,
fontWeight: "bold",
}}
>
{i}
</div>
);
};

export const Loop: StoryObj = {
render: () => {
const ref = useRef<ListHandle>(null);

const ITEM_LENGTH = 10;
const items = useMemo(() => Array.from({ length: ITEM_LENGTH }), []);

useLayoutEffect(() => {
ref.current?.scrollToIndex(ITEM_LENGTH);
}, []);

return (
<div
style={{ height: "100vh", display: "flex", flexDirection: "column" }}
>
<List
ref={ref}
style={{ flex: 1 }}
itemSize={200}
onScroll={(offset) => {
if (!ref.current) return;

const MARGIN = 100;
if (offset < 0 + MARGIN) {
ref.current.scrollTo(ref.current.scrollSize / 2 + offset);
} else if (
offset >
ref.current.scrollSize - ref.current.viewportSize - MARGIN
) {
ref.current.scrollTo(offset - ref.current.scrollSize / 2);
}
}}
>
{items.map((d, i) => (
<Row key={i} i={i} />
))}
{items.map((d, i) => (
<Row key={i + "-2"} i={i} />
))}
</List>
</div>
);
},
};