From c9585b9242616b63ef2bad356152c1cf1e1b0f19 Mon Sep 17 00:00:00 2001 From: haninekkoub Date: Tue, 22 Oct 2024 17:41:15 +0100 Subject: [PATCH] Filter dropdown mobile/ desktop --- .../product-refinement/filters/drop-down.tsx | 5 +- .../filters/filter-select.tsx | 117 +++++++++++++----- .../filters/mobile/index.tsx | 71 ++++++++++- 3 files changed, 156 insertions(+), 37 deletions(-) diff --git a/storefront/components/products/product-refinement/filters/drop-down.tsx b/storefront/components/products/product-refinement/filters/drop-down.tsx index 34eb8b4..b7ce916 100644 --- a/storefront/components/products/product-refinement/filters/drop-down.tsx +++ b/storefront/components/products/product-refinement/filters/drop-down.tsx @@ -59,14 +59,11 @@ export default function DropDown({
{children}
diff --git a/storefront/components/products/product-refinement/filters/filter-select.tsx b/storefront/components/products/product-refinement/filters/filter-select.tsx index 6dd3ec6..7a42a79 100644 --- a/storefront/components/products/product-refinement/filters/filter-select.tsx +++ b/storefront/components/products/product-refinement/filters/filter-select.tsx @@ -5,7 +5,7 @@ import Icon from "@/components/shared/icon"; import Body from "@/components/shared/typography/body"; import {cx} from "cva"; import {parseAsArrayOf, parseAsString, useQueryState} from "nuqs"; -import {type ComponentProps, useState} from "react"; +import {type ComponentProps, useEffect, useRef, useState} from "react"; import DropDown from "./drop-down"; @@ -37,39 +37,96 @@ export default function FilterSelect( const {filter, setFilter} = useMultiFilter(props.name); const [isOpen, setOpen] = useState(false); + const [showTopArrow, setShowTopArrow] = useState(false); + const [showBottomArrow, setShowBottomArrow] = useState(false); + const scrollContainerRef = useRef(null); + + const handleScroll = () => { + const container = scrollContainerRef.current; + if (container) { + setShowTopArrow(container.scrollTop > 0); + setShowBottomArrow( + container.scrollHeight - container.clientHeight > + container.scrollTop + 1, + ); + } + }; + + useEffect(() => { + handleScroll(); + }, [isOpen]); + + const scrollBy = (amount: number) => { + const container = scrollContainerRef.current; + if (container) { + container.scrollBy({behavior: "smooth", top: amount}); + } + }; return ( -
- {props.options.map((option) => { - const selected = filter?.includes(option.value); - return ( - - ); - })} +
+ +
+ + {option.label} + + + ); + })} +
+
+
scrollBy(250)} + > + +
+
); diff --git a/storefront/components/products/product-refinement/filters/mobile/index.tsx b/storefront/components/products/product-refinement/filters/mobile/index.tsx index f5f5851..1ced8de 100644 --- a/storefront/components/products/product-refinement/filters/mobile/index.tsx +++ b/storefront/components/products/product-refinement/filters/mobile/index.tsx @@ -1,8 +1,16 @@ "use client"; import {Cta} from "@/components/shared/button"; +import Icon from "@/components/shared/icon"; import Body from "@/components/shared/typography/body"; +import {cx} from "cva"; import {usePathname, useRouter} from "next/navigation"; -import {type PropsWithChildren, useCallback, useState} from "react"; +import { + type PropsWithChildren, + useCallback, + useEffect, + useRef, + useState, +} from "react"; import DropDown from "../drop-down"; @@ -17,11 +25,68 @@ export default function MobileFilterDropdown({children}: Props) { router.push(pathname); setOpen(false); }, [router, pathname]); + const [showTopArrow, setShowTopArrow] = useState(false); + const [showBottomArrow, setShowBottomArrow] = useState(false); + const scrollContainerRef = useRef(null); + const handleScroll = () => { + const container = scrollContainerRef.current; + if (container) { + setShowTopArrow(container.scrollTop > 0); + setShowBottomArrow( + container.scrollHeight - container.clientHeight > + container.scrollTop + 1, + ); + } + }; + + useEffect(() => { + handleScroll(); + }, [isOpen]); + + const scrollBy = (amount: number) => { + const container = scrollContainerRef.current; + if (container) { + container.scrollBy({behavior: "smooth", top: amount}); + } + }; return ( - {children} -
+
+
+
scrollBy(-250)} + > + +
+
+ {children} +
+
scrollBy(250)} + > + +
+
+
+ +
setOpen(false)} size="md"> Show Results