From 19fbe50d46b9090b93a4fc9bb42282081beb2742 Mon Sep 17 00:00:00 2001 From: Pouria Delfanazari Date: Sun, 14 Jan 2024 16:43:55 -0800 Subject: [PATCH] Translate in and out nav and buttons smoothly Remove useHideOnScroll which only detected direction. The new hook will update translation and opacity based on scroll amount. --- src/app/providers/scroll.tsx | 8 ++-- .../actions/composeButton/ComposeButton.tsx | 12 ++++-- src/components/actions/refetch/Refetch.tsx | 12 ++++-- src/components/navigational/appBar/AppBar.tsx | 12 ++++-- .../navigational/feedTabs/FeedTabs.tsx | 11 +++-- src/components/navigational/topBar/TopBar.tsx | 12 ++++-- src/lib/hooks/useHideOnScroll.tsx | 38 ----------------- src/lib/hooks/useScrollTranslation.tsx | 42 +++++++++++++++++++ 8 files changed, 85 insertions(+), 62 deletions(-) delete mode 100644 src/lib/hooks/useHideOnScroll.tsx create mode 100644 src/lib/hooks/useScrollTranslation.tsx diff --git a/src/app/providers/scroll.tsx b/src/app/providers/scroll.tsx index 9458fba4..efa16b7f 100644 --- a/src/app/providers/scroll.tsx +++ b/src/app/providers/scroll.tsx @@ -1,9 +1,9 @@ "use client"; -import useHideOnScroll from "@/lib/hooks/useHideOnScroll"; +import useScrollTranslation from "@/lib/hooks/useScrollTranslation"; import { ReactNode, createContext, useContext } from "react"; -const ScrollContext = createContext(undefined); +const ScrollContext = createContext(0); export const useScrollContext = () => { const context = useContext(ScrollContext); @@ -18,9 +18,9 @@ interface ScrollProviderProps { } export const ScrollProvider = (props: ScrollProviderProps) => { const { children } = props; - const show = useHideOnScroll(); + const val = useScrollTranslation(); return ( - {children} + {children} ); }; diff --git a/src/components/actions/composeButton/ComposeButton.tsx b/src/components/actions/composeButton/ComposeButton.tsx index 20274c13..421bade6 100644 --- a/src/components/actions/composeButton/ComposeButton.tsx +++ b/src/components/actions/composeButton/ComposeButton.tsx @@ -11,7 +11,7 @@ interface Props { export default function ComposeButton(props: Props) { const { mode } = props; - const show = useScrollContext(); + const val = useScrollContext(); const pathname = usePathname(); const userHandle = pathname.includes("/user/") && !pathname.includes("/post/") @@ -19,14 +19,18 @@ export default function ComposeButton(props: Props) { : ""; const { openComposer } = useComposerControls(); + const canUpdate = typeof window !== "undefined"; + return ( <> {mode === "float" && ( diff --git a/src/components/actions/refetch/Refetch.tsx b/src/components/actions/refetch/Refetch.tsx index 584e0929..99c7cd79 100644 --- a/src/components/actions/refetch/Refetch.tsx +++ b/src/components/actions/refetch/Refetch.tsx @@ -11,15 +11,19 @@ interface Props { export default function Refetch(props: Props) { const { onRefetch } = props; - const show = useScrollContext(); + const val = useScrollContext(); const debouncedRefetch = useDebouncedCallback(onRefetch, 300); + const canUpdate = typeof window !== "undefined"; + return ( diff --git a/src/components/navigational/appBar/AppBar.tsx b/src/components/navigational/appBar/AppBar.tsx index c1547346..4a2d9824 100644 --- a/src/components/navigational/appBar/AppBar.tsx +++ b/src/components/navigational/appBar/AppBar.tsx @@ -13,7 +13,7 @@ import { FaBell } from "react-icons/fa"; export default function AppBar() { const pathname = usePathname(); - const show = useScrollContext(); + const val = useScrollContext(); const agent = useAgent(); const { data: notificationsCount, @@ -25,11 +25,15 @@ export default function AppBar() { refetchInterval: 10000, }); + const canUpdate = typeof window !== "undefined"; + return (