From b7a28e3ec95cf6be21be1be64bd082da79f1029c Mon Sep 17 00:00:00 2001 From: ner Date: Fri, 11 Oct 2024 17:16:32 +0300 Subject: [PATCH] fix: improved slide change using onscreen buttons --- components/Random/components/Carousel/index.tsx | 10 ++++++++-- components/ScrollNavigation/index.tsx | 13 +++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/components/Random/components/Carousel/index.tsx b/components/Random/components/Carousel/index.tsx index bea6887..79a9b7b 100644 --- a/components/Random/components/Carousel/index.tsx +++ b/components/Random/components/Carousel/index.tsx @@ -5,6 +5,7 @@ import { Virtuoso, VirtuosoProps } from 'react-virtuoso'; import ScrollNavigation from '@/components/ScrollNavigation'; import Card from '@/components/Show'; +import { cn } from '@/lib/utils'; const components: VirtuosoProps void>['components'] = { Item: ({ item, children, context: onIndexChange, ...props }) => { @@ -43,6 +44,8 @@ const components: VirtuosoProps void>['components'] = { return
; }), Scroller: forwardRef(function Scroller({ context, ...props }, ref) { + const GAP_BETWEEN_SLIDES = 8; // gap-2 = 0.5rem = 8px + const handleRef = (ref: ForwardedRef, setRef: (element: HTMLDivElement) => void) => (element: HTMLDivElement | null) => { @@ -59,12 +62,15 @@ const components: VirtuosoProps void>['components'] = { arrowsClassName='h-20 bg-secondary' backClassName='rounded-none rounded-r-full' nextClassName='rounded-none rounded-l-full' + gap={GAP_BETWEEN_SLIDES} > - {(setRef) => ( + {(setRef, isArrowHovered) => (
)} diff --git a/components/ScrollNavigation/index.tsx b/components/ScrollNavigation/index.tsx index 0cbdf47..18e45db 100644 --- a/components/ScrollNavigation/index.tsx +++ b/components/ScrollNavigation/index.tsx @@ -9,7 +9,8 @@ interface Props { backClassName?: string; nextClassName?: string; iconClassName?: string; - children: (setScrollElement: (element: E) => void) => ReactNode; + gap?: number; + children: (setScrollElement: (element: E) => void, isArrowHovered: boolean) => ReactNode; } function ScrollNavigation({ @@ -18,12 +19,14 @@ function ScrollNavigation({ backClassName, nextClassName, iconClassName, + gap = 0, children }: Props) { const [scrollElement, setScrollElement] = useState(null); const [isShowBack, setIsShowBack] = useState(true); const [isShowNext, setIsShowNext] = useState(true); const [childWidth, setChildWidth] = useState(0); + const [isArrowHovered, setIsArrowHovered] = useState(false); useEffect(() => { if (!scrollElement) return; @@ -31,7 +34,7 @@ function ScrollNavigation({ let timeout: NodeJS.Timeout; if (scrollElement.firstChild) { - setChildWidth((scrollElement.firstChild as HTMLElement).clientWidth); + setChildWidth((scrollElement.firstChild as HTMLElement).clientWidth + gap); } const handleScroll = () => { @@ -52,7 +55,7 @@ function ScrollNavigation({ return () => { scrollElement.removeEventListener('scroll', handleScroll); }; - }, [scrollElement]); + }, [scrollElement, gap]); const handleScrollNext = (direction: 1 | -1) => () => { if (!scrollElement) return; @@ -62,7 +65,7 @@ function ScrollNavigation({ return (
- {children(setScrollElement)} + {children(setScrollElement, isArrowHovered)} {[ { Arrow: ChevronLeft, @@ -86,6 +89,8 @@ function ScrollNavigation({
setIsArrowHovered(true)} + onMouseLeave={() => setIsArrowHovered(false)} className={cn( 'opacity-0 hover-none:hidden hover-none:pointer-events-none transition-opacity absolute top-1/2 transform -translate-y-1/2 bg-gradient-to-r h-full w-14 flex items-center cursor-pointer', {