-
Notifications
You must be signed in to change notification settings - Fork 955
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mobile): implement scroll-to-top functionality (#2831)
* refactor(mobile): wrap entry list components to forward refs * feat(mobile): implement scroll-to-top functionality * fix: cast ref types in TimelineSelectorMasonryList * chore: ensure proper cleanup in useScrollToTopRef hook
- Loading branch information
Showing
8 changed files
with
92 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { jotaiStore } from "@follow/utils" | ||
import type { FlashList } from "@shopify/flash-list" | ||
import { atom } from "jotai" | ||
import { useEffect, useRef } from "react" | ||
|
||
const defaultScrollToTop = { scrollToTop: () => {} } | ||
const scrollToTopAtom = atom<{ scrollToTop: () => void }>(defaultScrollToTop) | ||
|
||
export const scrollToTop = () => { | ||
const { scrollToTop } = jotaiStore.get(scrollToTopAtom) | ||
scrollToTop() | ||
} | ||
|
||
export const useScrollToTopRef = <T extends FlashList<unknown>>(enabled = true) => { | ||
const ref = useRef<T>(null) | ||
|
||
useEffect(() => { | ||
if (!enabled) return | ||
const scrollToTop = () => { | ||
ref.current?.scrollToOffset({ animated: true, offset: 0 }) | ||
} | ||
jotaiStore.set(scrollToTopAtom, { scrollToTop }) | ||
return () => { | ||
if (jotaiStore.get(scrollToTopAtom).scrollToTop !== scrollToTop) return | ||
jotaiStore.set(scrollToTopAtom, defaultScrollToTop) | ||
} | ||
}, [enabled, ref]) | ||
return ref | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters