Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(component): new pauseOnHover prop to <Carousel>, provide quick information with chance to pause #918

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/docs/components/carousel/carousel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ Add custom indicators or disable them by passing the `indicators` prop to the `<
</Carousel>
</CodePreview>

## Pause On Hover

To conditionally pause the carousel on mouse hover (desktop), or touch and hold (mobile), you can use the `pauseOnHover` property on the `<Carousel>` component. Default value is `false`.

<CodePreview title="Pause on hovering" className="h-56 sm:h-64 xl:h-80 2xl:h-96">
<Carousel pauseOnHover>
<img src="https://flowbite.com/docs/images/carousel/carousel-1.svg" alt="..." />
<img src="https://flowbite.com/docs/images/carousel/carousel-2.svg" alt="..." />
<img src="https://flowbite.com/docs/images/carousel/carousel-3.svg" alt="..." />
<img src="https://flowbite.com/docs/images/carousel/carousel-4.svg" alt="..." />
<img src="https://flowbite.com/docs/images/carousel/carousel-5.svg" alt="..." />
</Carousel>
</CodePreview>

## Slider content

Instead of images you can also use any type of markup and content inside the carousel such as simple text.
Expand Down
20 changes: 17 additions & 3 deletions src/components/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface CarouselProps extends PropsWithChildren<ComponentProps<'div'>>
slideInterval?: number;
theme?: DeepPartial<FlowbiteCarouselTheme>;
onSlideChange?: (slide: number) => void;
pauseOnHover?: boolean;
}

export const Carousel: FC<CarouselProps> = ({
Expand All @@ -63,6 +64,7 @@ export const Carousel: FC<CarouselProps> = ({
className,
theme: customTheme = {},
onSlideChange = null,
pauseOnHover = false,
...props
}) => {
const theme = mergeDeep(useTheme().theme.carousel, customTheme);
Expand All @@ -71,6 +73,7 @@ export const Carousel: FC<CarouselProps> = ({
const carouselContainer = useRef<HTMLDivElement>(null);
const [activeItem, setActiveItem] = useState(0);
const [isDragging, setIsDragging] = useState(false);
const [isHovering, setIsHovering] = useState(false);

const didMountRef = useRef(false);

Expand Down Expand Up @@ -103,12 +106,12 @@ export const Carousel: FC<CarouselProps> = ({
}, [isDragging]);

useEffect(() => {
if (slide) {
if (slide && !(pauseOnHover && isHovering)) {
const intervalId = setInterval(() => !isDragging && navigateTo(activeItem + 1)(), slideInterval ?? 3000);

return () => clearInterval(intervalId);
}
}, [activeItem, isDragging, navigateTo, slide, slideInterval]);
}, [activeItem, isDragging, navigateTo, slide, slideInterval, pauseOnHover, isHovering]);

useEffect(() => {
if (didMountRef.current) {
Expand All @@ -120,8 +123,19 @@ export const Carousel: FC<CarouselProps> = ({

const handleDragging = (dragging: boolean) => () => setIsDragging(dragging);

const setHoveringTrue = useCallback(() => setIsHovering(true), [setIsHovering]);
const setHoveringFalse = useCallback(() => setIsHovering(false), [setIsHovering]);

return (
<div className={twMerge(theme.root.base, className)} data-testid="carousel" {...props}>
<div
className={twMerge(theme.root.base, className)}
data-testid="carousel"
onMouseEnter={setHoveringTrue}
onMouseLeave={setHoveringFalse}
onTouchStart={setHoveringTrue}
onTouchEnd={setHoveringFalse}
{...props}
>
<ScrollContainer
className={twMerge(theme.scrollContainer.base, (isDeviceMobile || !isDragging) && theme.scrollContainer.snap)}
draggingClassName="cursor-grab"
Expand Down