-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #459 from audioverse-org/av-467
Make mobile scroll transitions much smoother
- Loading branch information
Showing
6 changed files
with
86 additions
and
94 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
37 changes: 37 additions & 0 deletions
37
src/components/organisms/mobileHeader.useScrollDirection.ts
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,37 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import useScrollTop from '~src/lib/hooks/useScrollTop'; | ||
|
||
export const SCROLL_DIRECTIONS = { | ||
UP: 'up', | ||
DOWN: 'down', | ||
}; | ||
|
||
export default function useScrollDirection( | ||
scrollRef: React.RefObject<HTMLDivElement> | ||
) { | ||
const scrollTop = useScrollTop(scrollRef); | ||
const [lastScrollTop, setLastScrollTop] = useState<number>(0); | ||
const [direction, setDirection] = useState<string>(SCROLL_DIRECTIONS.UP); | ||
|
||
useEffect(() => { | ||
setLastScrollTop(Math.max(scrollTop, 0)); | ||
}, [scrollTop]); | ||
|
||
useEffect(() => { | ||
const scrollHeight = scrollRef.current?.scrollHeight || 0; | ||
const clientHeight = scrollRef.current?.clientHeight || 0; | ||
const clientTop = scrollHeight - scrollTop; | ||
const buffer = clientTop - clientHeight; | ||
const maxHeaderHeight = 101; | ||
if (buffer < maxHeaderHeight) return; | ||
if (scrollTop > lastScrollTop) { | ||
setDirection(SCROLL_DIRECTIONS.DOWN); | ||
} | ||
if (scrollTop < lastScrollTop) { | ||
setDirection(SCROLL_DIRECTIONS.UP); | ||
} | ||
}, [scrollTop, lastScrollTop, scrollRef]); | ||
|
||
return direction; | ||
} |
55 changes: 0 additions & 55 deletions
55
src/components/organisms/mobileHeader.useTransitionProgress.ts
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
import isServerSide from '../isServerSide'; | ||
|
||
export default function useScrollTop( | ||
scrollRef: React.RefObject<HTMLDivElement> | ||
) { | ||
const [scrollTop, setScrollTop] = useState<number>(0); | ||
|
||
const listener = useCallback(() => { | ||
if (isServerSide() || !scrollRef.current) return; | ||
setScrollTop(scrollRef.current.scrollTop); | ||
}, [scrollRef]); | ||
|
||
useEffect(() => { | ||
if (isServerSide() || !scrollRef.current) return; | ||
const el = scrollRef.current; | ||
el.addEventListener('scroll', listener); | ||
return () => el.removeEventListener('scroll', listener); | ||
}, [listener, scrollRef]); | ||
|
||
return scrollTop; | ||
} |
3b0eef9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
audioverse-next – ./
audioverse.org
beta.audioverse.org
audioverse-next-audioverse.vercel.app
www.audioverse.org
audioverse-next-git-master-audioverse.vercel.app
audioverse-next.vercel.app