From 1b1770ace825282e8032f9aab6be5363d77a327d Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sat, 9 Dec 2023 14:24:25 +0000 Subject: [PATCH] Dynamically change search bar sticky offset depending on window width --- src/pages/parts/home/HeroPart.tsx | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/pages/parts/home/HeroPart.tsx b/src/pages/parts/home/HeroPart.tsx index e40a1aa0..d62549de 100644 --- a/src/pages/parts/home/HeroPart.tsx +++ b/src/pages/parts/home/HeroPart.tsx @@ -1,11 +1,13 @@ -import { useCallback, useRef, useState } from "react"; +import { useCallback, useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import Sticky from "react-sticky-el"; +import { useWindowSize } from "react-use"; import { SearchBarInput } from "@/components/form/SearchBar"; import { ThinContainer } from "@/components/layout/ThinContainer"; import { useSlashFocus } from "@/components/player/hooks/useSlashFocus"; import { HeroTitle } from "@/components/text/HeroTitle"; +import { useIsMobile } from "@/hooks/useIsMobile"; import { useRandomTranslation } from "@/hooks/useRandomTranslation"; import { useSearchQuery } from "@/hooks/useSearchQuery"; import { useBannerSize } from "@/stores/banner"; @@ -29,6 +31,20 @@ export function HeroPart({ setIsSticky, searchParams }: HeroPartProps) { [setShowBg, setIsSticky] ); + const { width: windowWidth } = useWindowSize(); + + const topSpacing = 16; + const [stickyOffset, setStickyOffset] = useState(topSpacing); + useEffect(() => { + if (windowWidth > 1200) { + // On large screens the bar goes inline with the nav elements + setStickyOffset(topSpacing); + } else { + // On smaller screens the bar goes below the nav elements + setStickyOffset(topSpacing + 60); + } + }, [windowWidth]); + let time = "night"; const hour = new Date().getHours(); if (hour < 12) time = "morning"; @@ -47,9 +63,9 @@ export function HeroPart({ setIsSticky, searchParams }: HeroPartProps) {