Skip to content

Commit

Permalink
Merge pull request #213 from inokawa/align-center
Browse files Browse the repository at this point in the history
Add `align: center` option to scrollToIndex
  • Loading branch information
inokawa authored Oct 20, 2023
2 parents be107b5 + 9bb5ea9 commit b993e28
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/core/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ export const createScroller = (
store._getItemOffset(index) +
store._getItemSize(index) -
store._getViewportSize()
: align === "center"
? () =>
store._getItemOffset(index) +
(store._getItemSize(index) - store._getViewportSize()) / 2
: () => store._getItemOffset(index)
);
},
Expand Down
3 changes: 2 additions & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface CacheSnapshot {
* Alignment of item when calling scrollToIndex method.
*
* - `start`(default): Align the item to the start of the list.
* - `center`: Align the item to the center of the list.
* - `end`: Align the item to the end of the list.
*/
export type ScrollToIndexAlign = "start" | "end";
export type ScrollToIndexAlign = "start" | "center" | "end";
11 changes: 11 additions & 0 deletions stories/basics/VList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ export const ScrollTo: StoryObj = {
/>
start
</label>
<label style={{ marginLeft: 4 }}>
<input
type="radio"
style={{ marginLeft: 4 }}
checked={scrollIndexAlign === "center"}
onChange={() => {
setScrollToIndexAlign("center");
}}
/>
center
</label>
<label style={{ marginLeft: 4 }}>
<input
type="radio"
Expand Down

0 comments on commit b993e28

Please sign in to comment.