fix: improved slide change using onscreen buttons

This commit is contained in:
ner committed 2024-10-11 17:16:32 +03:00
1 parent 9291898490
commit b7a28e3ec9
2 files changed
+17 -6

No files matched your search

@@ -5,6 +5,7 @@ import { Virtuoso, VirtuosoProps } from 'react-virtuoso';
import ScrollNavigation from '@/components/ScrollNavigation'; import ScrollNavigation from '@/components/ScrollNavigation';
import Card from '@/components/Show'; import Card from '@/components/Show';
import { cn } from '@/lib/utils';
const components: VirtuosoProps<Show, (index: number) => void>['components'] = { const components: VirtuosoProps<Show, (index: number) => void>['components'] = {
Item: ({ item, children, context: onIndexChange, ...props }) => { Item: ({ item, children, context: onIndexChange, ...props }) => {
@@ -43,6 +44,8 @@ const components: VirtuosoProps<Show, (index: number) => void>['components'] = {
return <div {...props} ref={ref} style={{ ...props.style, display: 'flex' }} className='gap-2' />; return <div {...props} ref={ref} style={{ ...props.style, display: 'flex' }} className='gap-2' />;
}), }),
Scroller: forwardRef(function Scroller({ context, ...props }, ref) { Scroller: forwardRef(function Scroller({ context, ...props }, ref) {
const GAP_BETWEEN_SLIDES = 8; // gap-2 = 0.5rem = 8px
const handleRef = const handleRef =
(ref: ForwardedRef<HTMLDivElement>, setRef: (element: HTMLDivElement) => void) => (ref: ForwardedRef<HTMLDivElement>, setRef: (element: HTMLDivElement) => void) =>
(element: HTMLDivElement | null) => { (element: HTMLDivElement | null) => {
@@ -59,12 +62,15 @@ const components: VirtuosoProps<Show, (index: number) => void>['components'] = {
arrowsClassName='h-20 bg-secondary' arrowsClassName='h-20 bg-secondary'
backClassName='rounded-none rounded-r-full' backClassName='rounded-none rounded-r-full'
nextClassName='rounded-none rounded-l-full' nextClassName='rounded-none rounded-l-full'
gap={GAP_BETWEEN_SLIDES}
> >
{(setRef) => ( {(setRef, isArrowHovered) => (
<div <div
{...props} {...props}
ref={handleRef(ref, setRef)} ref={handleRef(ref, setRef)}
className='no-scrollbar h-full snap-x snap-mandatory overflow-hidden rounded-lg' className={cn('no-scrollbar h-full overflow-hidden rounded-lg', {
'snap-x snap-mandatory': !isArrowHovered
})}
/> />
)} )}
</ScrollNavigation> </ScrollNavigation>
+9 -4
View File
@@ -9,7 +9,8 @@ interface Props<E extends HTMLElement> {
backClassName?: string; backClassName?: string;
nextClassName?: string; nextClassName?: string;
iconClassName?: string; iconClassName?: string;
children: (setScrollElement: (element: E) => void) => ReactNode; gap?: number;
children: (setScrollElement: (element: E) => void, isArrowHovered: boolean) => ReactNode;
} }
function ScrollNavigation<E extends HTMLElement>({ function ScrollNavigation<E extends HTMLElement>({
@@ -18,12 +19,14 @@ function ScrollNavigation<E extends HTMLElement>({
backClassName, backClassName,
nextClassName, nextClassName,
iconClassName, iconClassName,
gap = 0,
children children
}: Props<E>) { }: Props<E>) {
const [scrollElement, setScrollElement] = useState<E | null>(null); const [scrollElement, setScrollElement] = useState<E | null>(null);
const [isShowBack, setIsShowBack] = useState(true); const [isShowBack, setIsShowBack] = useState(true);
const [isShowNext, setIsShowNext] = useState(true); const [isShowNext, setIsShowNext] = useState(true);
const [childWidth, setChildWidth] = useState(0); const [childWidth, setChildWidth] = useState(0);
const [isArrowHovered, setIsArrowHovered] = useState(false);
useEffect(() => { useEffect(() => {
if (!scrollElement) return; if (!scrollElement) return;
@@ -31,7 +34,7 @@ function ScrollNavigation<E extends HTMLElement>({
let timeout: NodeJS.Timeout; let timeout: NodeJS.Timeout;
if (scrollElement.firstChild) { if (scrollElement.firstChild) {
setChildWidth((scrollElement.firstChild as HTMLElement).clientWidth); setChildWidth((scrollElement.firstChild as HTMLElement).clientWidth + gap);
} }
const handleScroll = () => { const handleScroll = () => {
@@ -52,7 +55,7 @@ function ScrollNavigation<E extends HTMLElement>({
return () => { return () => {
scrollElement.removeEventListener('scroll', handleScroll); scrollElement.removeEventListener('scroll', handleScroll);
}; };
}, [scrollElement]); }, [scrollElement, gap]);
const handleScrollNext = (direction: 1 | -1) => () => { const handleScrollNext = (direction: 1 | -1) => () => {
if (!scrollElement) return; if (!scrollElement) return;
@@ -62,7 +65,7 @@ function ScrollNavigation<E extends HTMLElement>({
return ( return (
<div className={cn('relative group', className)}> <div className={cn('relative group', className)}>
{children(setScrollElement)} {children(setScrollElement, isArrowHovered)}
{[ {[
{ {
Arrow: ChevronLeft, Arrow: ChevronLeft,
@@ -86,6 +89,8 @@ function ScrollNavigation<E extends HTMLElement>({
<div <div
key={className} key={className}
onClick={handleScroll} onClick={handleScroll}
onMouseEnter={() => setIsArrowHovered(true)}
onMouseLeave={() => setIsArrowHovered(false)}
className={cn( 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', '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',
{ {