fix: improved slide change using onscreen buttons
This commit is contained in:
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 Card from '@/components/Show';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
const components: VirtuosoProps<Show, (index: number) => void>['components'] = {
|
||||
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' />;
|
||||
}),
|
||||
Scroller: forwardRef(function Scroller({ context, ...props }, ref) {
|
||||
const GAP_BETWEEN_SLIDES = 8; // gap-2 = 0.5rem = 8px
|
||||
|
||||
const handleRef =
|
||||
(ref: ForwardedRef<HTMLDivElement>, setRef: (element: HTMLDivElement) => void) =>
|
||||
(element: HTMLDivElement | null) => {
|
||||
@@ -59,12 +62,15 @@ const components: VirtuosoProps<Show, (index: number) => 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) => (
|
||||
<div
|
||||
{...props}
|
||||
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>
|
||||
|
||||
@@ -9,7 +9,8 @@ interface Props<E extends HTMLElement> {
|
||||
backClassName?: string;
|
||||
nextClassName?: string;
|
||||
iconClassName?: string;
|
||||
children: (setScrollElement: (element: E) => void) => ReactNode;
|
||||
gap?: number;
|
||||
children: (setScrollElement: (element: E) => void, isArrowHovered: boolean) => ReactNode;
|
||||
}
|
||||
|
||||
function ScrollNavigation<E extends HTMLElement>({
|
||||
@@ -18,12 +19,14 @@ function ScrollNavigation<E extends HTMLElement>({
|
||||
backClassName,
|
||||
nextClassName,
|
||||
iconClassName,
|
||||
gap = 0,
|
||||
children
|
||||
}: Props<E>) {
|
||||
const [scrollElement, setScrollElement] = useState<E | null>(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<E extends HTMLElement>({
|
||||
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<E extends HTMLElement>({
|
||||
return () => {
|
||||
scrollElement.removeEventListener('scroll', handleScroll);
|
||||
};
|
||||
}, [scrollElement]);
|
||||
}, [scrollElement, gap]);
|
||||
|
||||
const handleScrollNext = (direction: 1 | -1) => () => {
|
||||
if (!scrollElement) return;
|
||||
@@ -62,7 +65,7 @@ function ScrollNavigation<E extends HTMLElement>({
|
||||
|
||||
return (
|
||||
<div className={cn('relative group', className)}>
|
||||
{children(setScrollElement)}
|
||||
{children(setScrollElement, isArrowHovered)}
|
||||
{[
|
||||
{
|
||||
Arrow: ChevronLeft,
|
||||
@@ -86,6 +89,8 @@ function ScrollNavigation<E extends HTMLElement>({
|
||||
<div
|
||||
key={className}
|
||||
onClick={handleScroll}
|
||||
onMouseEnter={() => 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',
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user