aboutsummaryrefslogtreecommitdiffstats
path: root/src/components
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-11-15 16:37:16 +0100
committerArmand Philippot <git@armandphilippot.com>2023-11-15 17:26:17 +0100
commitc826ad66df066b90b09009f2f4b83b56d018436e (patch)
tree6320b85c7a4fdedf8e4384eaca290c02ea2a71e2 /src/components
parent0f38aee374029213a47ef7c29bd164093fe63c85 (diff)
refactor(hooks): rewrite useScrollPosition hook
* return the scroll position (both X and Y) * no longer accepts arguments * add tests
Diffstat (limited to 'src/components')
-rw-r--r--src/components/templates/layout/layout.tsx24
1 files changed, 10 insertions, 14 deletions
diff --git a/src/components/templates/layout/layout.tsx b/src/components/templates/layout/layout.tsx
index c7b8dbd..5dad8a2 100644
--- a/src/components/templates/layout/layout.tsx
+++ b/src/components/templates/layout/layout.tsx
@@ -7,7 +7,6 @@ import {
type ReactElement,
type ReactNode,
useRef,
- useState,
type CSSProperties,
type FormEvent,
useCallback,
@@ -367,19 +366,16 @@ export const Layout: FC<LayoutProps> = ({
subjectOf: { '@id': `${url}` },
};
- const [backToTopClassName, setBackToTopClassName] = useState<string>(
- `${styles['back-to-top']} ${styles['back-to-top--hidden']}`
- );
- const updateBackToTopClassName = () => {
- const visibleBreakpoint = 300;
- setBackToTopClassName(
- window.scrollY > visibleBreakpoint
- ? `${styles['back-to-top']} ${styles['back-to-top--visible']}`
- : `${styles['back-to-top']} ${styles['back-to-top--hidden']}`
- );
- };
-
- useScrollPosition(updateBackToTopClassName);
+ const scrollPos = useScrollPosition();
+ const backToTopBreakpoint = 300;
+ const backToTopClassName = [
+ styles['back-to-top'],
+ styles[
+ scrollPos.y > backToTopBreakpoint
+ ? 'back-to-top--visible'
+ : 'back-to-top--hidden'
+ ],
+ ].join(' ');
const topRef = useRef<HTMLSpanElement>(null);
const giveFocusToTopRef = () => {