Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add carousel thumbnails + reorganize carousel components

Tim Wilsond8a5c5942103
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page-client.tsx
index fb49c01256..0e951cd1e2 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page-client.tsx
@@ -20,11 +20,14 @@ import {
CarouselContent,
CarouselItem,
CarouselNavBar,
+ CarouselThumbnail,
+ CarouselThumbnails,
Logo,
MaxWidthWrapper,
Popover,
Tooltip,
TooltipContent,
+ useMediaQuery,
} from "@dub/ui";
import {
CircleWarning,
@@ -45,7 +48,7 @@ import {
} from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
import Link from "next/link";
-import { useState } from "react";
+import { memo, useState } from "react";
import { toast } from "sonner";
const integrationSettings = {
@@ -60,6 +63,7 @@ export default function IntegrationPageClient({
integration: InstalledIntegrationInfoProps;
}) {
const { slug, id: workspaceId } = useWorkspace();
+ const { isMobile } = useMediaQuery();
const [openPopover, setOpenPopover] = useState(false);
const { execute, isPending } = useAction(getIntegrationInstallUrl, {
@@ -218,7 +222,7 @@ export default function IntegrationPageClient({
),
},
].map(({ label, content }) => (
- <div className="flex flex-col gap-1">
+ <div key={label} className="flex flex-col gap-1">
<span className="text-xs uppercase text-neutral-500">
{label}
</span>
@@ -269,24 +273,58 @@ export default function IntegrationPageClient({
<div className="w-full rounded-lg border border-neutral-200 bg-white">
{integration.screenshots && integration.screenshots.length > 0 ? (
- <Carousel
- autoplay={{ delay: 5000 }}
- className="rounded-t-lg bg-white p-4"
- >
- <CarouselContent>
- {integration.screenshots.map((src, idx) => (
- <CarouselItem key={idx}>
- <BlurImage
- src={src}
- alt={`Screenshot of ${integration.name}`}
- width={900}
- height={580}
- className="aspect-[900/580] w-[5/6] overflow-hidden rounded-md border border-neutral-200 object-cover object-top"
- />
- </CarouselItem>
- ))}
- </CarouselContent>
- <CarouselNavBar variant="floating" />
+ <Carousel autoplay={{ delay: 5000 }}>
+ <div className="relative rounded-t-lg bg-white p-4">
+ <CarouselContent>
+ {integration.screenshots.map((src, idx) => (
+ <CarouselItem key={idx}>
+ <BlurImageMemo
+ src={src}
+ alt={`Screenshot ${idx + 1} of ${integration.name}`}
+ width={900}
+ height={580}
+ className="aspect-[900/580] w-[5/6] overflow-hidden rounded-md border border-neutral-200 object-cover object-top"
+ />
+ </CarouselItem>
+ ))}
+ </CarouselContent>
+ <CarouselNavBar
+ variant="simple"
+ className="absolute bottom-6 left-1/2 -translate-x-1/2"
+ />
+ </div>
+ {!isMobile && (
+ <div className="relative">
+ <CarouselThumbnails className="py-0.5">
+ {integration.screenshots.map((src, idx) => (
+ <CarouselThumbnail
+ key={idx}
+ index={idx}
+ className={({ active }) =>
+ cn(
+ "aspect-[900/580] h-[100px] shrink-0 select-none overflow-hidden rounded-[6px] border",
+ "border-neutral-200 ring-2 ring-transparent transition-all duration-100",
+ active
+ ? "border-neutral-300 ring-black/10"
+ : "hover:ring-black/5",
+ )
+ }
+ >
+ <BlurImageMemo
+ src={src}
+ alt={`Screenshot ${idx + 1} thumbnail`}
+ width={900}
+ height={580}
+ className="overflow-hidden rounded-[5px] object-cover object-top"
+ />
+ </CarouselThumbnail>
+ ))}
+ </CarouselThumbnails>
+
+ <div className="absolute inset-y-0 left-0 w-4 bg-gradient-to-r from-white" />
+ <div className="absolute inset-y-0 right-0 w-4 bg-gradient-to-l from-white" />
+ </div>
+ )}
</Carousel>
) : null}
@@ -297,3 +335,5 @@ export default function IntegrationPageClient({
</MaxWidthWrapper>
);
}
+
+const BlurImageMemo = memo(BlurImage);
diff --git a/packages/ui/src/carousel.tsx b/packages/ui/src/carousel.tsx
deleted file mode 100644
index 891abcb599..0000000000
--- a/packages/ui/src/carousel.tsx
+++ /dev/null
@@ -1,413 +0,0 @@
-"use client";
-
-import { cn } from "@dub/utils";
-import { VariantProps, cva } from "class-variance-authority";
-import Autoplay from "embla-carousel-autoplay";
-import useEmblaCarousel, {
- type UseEmblaCarouselType,
-} from "embla-carousel-react";
-import { motion } from "framer-motion";
-import { ArrowLeft, ArrowRight, ChevronLeft, ChevronRight } from "lucide-react";
-import * as React from "react";
-import { buttonVariants } from "./button";
-
-export const AUTOPLAY_DEFAULT_DELAY = 2000;
-
-type CarouselApi = UseEmblaCarouselType[1];
-type UseCarouselParameters = Parameters<typeof useEmblaCarousel>;
-type CarouselOptions = UseCarouselParameters[0];
-type CarouselPlugin = UseCarouselParameters[1];
-type AutoplayOptions = Parameters<typeof Autoplay>[0];
-
-type CarouselProps = {
- opts?: CarouselOptions;
- plugins?: CarouselPlugin;
- orientation?: "horizontal" | "vertical";
- autoplay?: boolean | AutoplayOptions;
- setApi?: (api: CarouselApi) => void;
-};
-
-type CarouselContextProps = {
- carouselRef: ReturnType<typeof useEmblaCarousel>[0];
- api: ReturnType<typeof useEmblaCarousel>[1];
- scrollPrev: () => void;
- scrollNext: () => void;
- canScrollPrev: boolean;
- canScrollNext: boolean;
-} & CarouselProps;
-
-const CarouselContext = React.createContext<CarouselContextProps | null>(null);
-
-export function useCarousel() {
- const context = React.useContext(CarouselContext);
-
- if (!context) {
- throw new Error("useCarousel must be used within a <Carousel />");
- }
-
- return context;
-}
-
-export function useCarouselActiveIndex() {
- const { api } = useCarousel();
-
- const [activeIndex, setActiveIndex] = React.useState(0);
-
- const onSelect = React.useCallback(
- (api: CarouselApi) => setActiveIndex(api?.selectedScrollSnap() ?? 0),
- [],
- );
-
- React.useEffect(() => {
- if (!api) return;
-
- onSelect(api);
- api.on("reInit", onSelect);
- api.on("select", onSelect);
- }, [api, onSelect]);
-
- return activeIndex;
-}
-
-const Carousel = React.forwardRef<
- HTMLDivElement,
- React.HTMLAttributes<HTMLDivElement> & CarouselProps
->(
- (
- {
- orientation = "horizontal",
- opts,
- setApi,
- autoplay,
- plugins,
- className,
- children,
- ...props
- },
- ref,
- ) => {
- const [carouselRef, api] = useEmblaCarousel(
- {
- ...opts,
- axis: orientation === "horizontal" ? "x" : "y",
- },
- [
- ...(autoplay
- ? [
- Autoplay(
- typeof autoplay === "object"
- ? autoplay
- : {
- delay: AUTOPLAY_DEFAULT_DELAY,
- },
- ),
- ]
- : []),
- ...(plugins || []),
- ],
- );
- const [canScrollPrev, setCanScrollPrev] = React.useState(false);
- const [canScrollNext, setCanScrollNext] = React.useState(false);
-
- const onSelect = React.useCallback((api: CarouselApi) => {
- if (!api) {
- return;
- }
-
- setCanScrollPrev(api.canScrollPrev());
- setCanScrollNext(api.canScrollNext());
- }, []);
-
- const scrollPrev = React.useCallback(() => {
- api?.scrollPrev();
- }, [api]);
-
- const scrollNext = React.useCallback(() => {
- api?.scrollNext();
- }, [api]);
-
- const handleKeyDown = React.useCallback(
- (event: React.KeyboardEvent<HTMLDivElement>) => {
- if (event.key === "ArrowLeft") {
- event.preventDefault();
- scrollPrev();
- } else if (event.key === "ArrowRight") {
- event.preventDefault();
- scrollNext();
- }
- },
- [scrollPrev, scrollNext],
- );
-
- React.useEffect(() => {
- if (!api || !setApi) {
- return;
- }
-
- setApi(api);
- }, [api, setApi]);
-
- React.useEffect(() => {
- if (!api) {
- return;
- }
-
- onSelect(api);
- api.on("reInit", onSelect);
- api.on("select", onSelect);
-
- return () => {
- api?.off("select", onSelect);
- };
- }, [api, onSelect]);
-
- return (
- <CarouselContext.Provider
- value={{
- carouselRef,
- api: api,
- opts,
- orientation:
- orientation || (opts?.axis === "y" ? "vertical" : "horizontal"),
- scrollPrev,
- scrollNext,
- canScrollPrev,
- canScrollNext,
- }}
- >
- <div
- ref={ref}
- onKeyDownCapture={handleKeyDown}
- className={cn("relative", className)}
- role="region"
- aria-roledescription="carousel"
- {...props}
- >
- {children}
- </div>
- </CarouselContext.Provider>
- );
- },
-);
-Carousel.displayName = "Carousel";
-
-const CarouselContent = React.forwardRef<
- HTMLDivElement,
- React.HTMLAttributes<HTMLDivElement>
->(({ className, ...props }, ref) => {
- const { carouselRef, orientation } = useCarousel();
-
- return (
- <div ref={carouselRef} className="overflow-hidden">
- <div
- ref={ref}
- className={cn(
- "flex",
- orientation === "horizontal" ? "-ml-4" : "-mt-4 flex-col",
- className,
- )}
- {...props}
- />
- </div>
- );
-});
-CarouselContent.displayName = "CarouselContent";
-
-const CarouselItem = React.forwardRef<
- HTMLDivElement,
- React.HTMLAttributes<HTMLDivElement>
->(({ className, ...props }, ref) => {
- const { orientation } = useCarousel();
-
- return (
- <div
- ref={ref}
- role="group"
- aria-roledescription="slide"
- className={cn(
- "min-w-0 shrink-0 grow-0 basis-full",
- orientation === "horizontal" ? "pl-4" : "pt-4",
- className,
- )}
- {...props}
- />
- );
-});
-CarouselItem.displayName = "CarouselItem";
-
-const CarouselPrevious = React.forwardRef<
- HTMLButtonElement,
- { className?: string }
->(({ className, ...props }, ref) => {
- const { orientation, scrollPrev, canScrollPrev } = useCarousel();
-
- return (
- <button
- ref={ref}
- className={cn(
- "absolute",
- orientation === "horizontal"
- ? "-left-12 top-1/2 -translate-y-1/2"
- : "-top-12 left-1/2 -translate-x-1/2 rotate-90",
- buttonVariants({ variant: "secondary", className: "p-2" }),
- className,
- )}
- disabled={!canScrollPrev}
- onClick={scrollPrev}
- {...props}
- >
- <ArrowLeft className="h-4 w-4" />
- <span className="sr-only">Previous slide</span>
- </button>
- );
-});
-CarouselPrevious.displayName = "CarouselPrevious";
-
-const CarouselNext = React.forwardRef<
- HTMLButtonElement,
- { className?: string }
->(({ className, ...props }, ref) => {
- const { orientation, scrollNext, canScrollNext } = useCarousel();
-
- return (
- <button
- ref={ref}
- className={cn(
- "absolute",
- orientation === "horizontal"
- ? "-right-12 top-1/2 -translate-y-1/2"
- : "-bottom-12 left-1/2 -translate-x-1/2 rotate-90",
- buttonVariants({ variant: "secondary", className: "p-2" }),
- className,
- )}
- disabled={!canScrollNext}
- onClick={scrollNext}
- {...props}
- >
- <ArrowRight className="h-4 w-4" />
- <span className="sr-only">Next slide</span>
- </button>
- );
-});
-CarouselNext.displayName = "CarouselNext";
-
-const CarouselNavBarVariants = cva(
- "flex items-center justify-center gap-3 sm:gap-6",
- {
- variants: {
- variant: {
- simple: "relative",
- floating:
- "absolute bottom-4 left-1/2 -translate-x-1/2 rounded-full border border-gray-800/10 bg-white sm:bottom-6 ",
- },
- },
- },
-);
-
-const CarouselNavBar = ({
- variant = "simple",
- className,
-}: VariantProps<typeof CarouselNavBarVariants> & { className?: string }) => {
- const { scrollNext, scrollPrev, canScrollNext, canScrollPrev, api } =
- useCarousel();
-
- const autoplay = api?.plugins()?.autoplay;
-
- const [selectedIndex, setSelectedIndex] = React.useState(0);
-
- const [isPlaying, setIsPlaying] = React.useState(false);
-
- const onSelect = React.useCallback((api: CarouselApi) => {
- setSelectedIndex(api?.selectedScrollSnap() ?? 0);
- }, []);
-
- const stopAutoplayAnd = React.useCallback(
- (fn: () => void) => () => {
- if (autoplay && autoplay.isPlaying()) autoplay.stop();
- fn();
- },
- [autoplay],
- );
-
- React.useEffect(() => {
- if (!api) return;
-
- onSelect(api);
- setIsPlaying(autoplay?.isPlaying() ?? false);
- api.on("reInit", onSelect);
- api.on("select", onSelect);
- api.on("autoplay:play", () => setIsPlaying(true));
- api.on("autoplay:stop", () => setIsPlaying(false));
- }, [api, autoplay, onSelect]);
-
- return (
- <div className={cn(CarouselNavBarVariants({ variant }), className)}>
- {variant !== "simple" && (
- <button
- className="cursor-pointer rounded-full p-2 hover:bg-gray-50 active:bg-gray-100"
- disabled={!canScrollPrev}
- onClick={stopAutoplayAnd(scrollPrev)}
- >
- <ChevronLeft className="h-4 w-4" />
- <span className="sr-only">Previous slide</span>
- </button>
- )}
-
- {api != null && (
- <div className="flex items-center gap-1">
- {api.slideNodes().map((_, idx) => (
- <button
- key={idx}
- onClick={stopAutoplayAnd(() => api.scrollTo(idx))}
- className="rounded-full p-0.5 hover:bg-gray-100 active:bg-gray-200 sm:p-1.5"
- >
- <div
- className={cn(
- "relative isolate h-1.5 w-1.5 overflow-hidden rounded-full transition-all",
- idx === selectedIndex ? "bg-black" : "bg-black/20",
- isPlaying && idx === selectedIndex && "w-6 bg-black/20",
- )}
- >
- {isPlaying && idx === selectedIndex && (
- <motion.div
- initial={{ x: "-100%" }}
- animate={{ x: 0 }}
- transition={{
- type: "tween",
- duration:
- (autoplay?.options.delay ?? AUTOPLAY_DEFAULT_DELAY) /
- 1000,
- }}
- className="animate-fill-width h-full w-full rounded-full bg-black"
- />
- )}
- </div>
- <span className="sr-only">Slide {idx + 1}</span>
- </button>
- ))}
- </div>
- )}
-
- {variant !== "simple" && (
- <button
- className="cursor-pointer rounded-full p-2 hover:bg-gray-50 active:bg-gray-100"
- disabled={!canScrollNext}
- onClick={stopAutoplayAnd(scrollNext)}
- >
- <ChevronRight className="h-4 w-4" />
- <span className="sr-only">Next slide</span>
- </button>
- )}
- </div>
- );
-};
-
-export {
- Carousel,
- CarouselContent,
- CarouselItem,
- CarouselNavBar,
- CarouselNext,
- CarouselPrevious,
- type CarouselApi,
-};
diff --git a/packages/ui/src/carousel/carousel.tsx b/packages/ui/src/carousel/carousel.tsx
new file mode 100644
index 0000000000..489429208e
--- /dev/null
+++ b/packages/ui/src/carousel/carousel.tsx
@@ -0,0 +1,303 @@
+import { cn } from "@dub/utils";
+import Autoplay from "embla-carousel-autoplay";
+import useEmblaCarousel, {
+ type UseEmblaCarouselType,
+} from "embla-carousel-react";
+import { ArrowLeft, ArrowRight } from "lucide-react";
+import {
+ createContext,
+ forwardRef,
+ HTMLAttributes,
+ KeyboardEvent,
+ useCallback,
+ useContext,
+ useEffect,
+ useState,
+} from "react";
+import { buttonVariants } from "../button";
+
+export const AUTOPLAY_DEFAULT_DELAY = 2000;
+
+type CarouselApi = UseEmblaCarouselType[1];
+type UseCarouselParameters = Parameters<typeof useEmblaCarousel>;
+type CarouselOptions = UseCarouselParameters[0];
+type CarouselPlugin = UseCarouselParameters[1];
+type AutoplayOptions = Parameters<typeof Autoplay>[0];
+
+type CarouselProps = {
+ opts?: CarouselOptions;
+ plugins?: CarouselPlugin;
+ orientation?: "horizontal" | "vertical";
+ autoplay?: boolean | AutoplayOptions;
+ setApi?: (api: CarouselApi) => void;
+};
+
+type CarouselContextProps = {
+ carouselRef: ReturnType<typeof useEmblaCarousel>[0];
+ api: ReturnType<typeof useEmblaCarousel>[1];
+ scrollPrev: () => void;
+ scrollNext: () => void;
+ canScrollPrev: boolean;
+ canScrollNext: boolean;
+} & CarouselProps;
+
+const CarouselContext = createContext<CarouselContextProps | null>(null);
+
+export function useCarousel() {
+ const context = useContext(CarouselContext);
+
+ if (!context) {
+ throw new Error("useCarousel must be used within a <Carousel />");
+ }
+
+ return context;
+}
+
+export function useCarouselActiveIndex() {
+ const { api } = useCarousel();
+
+ const [activeIndex, setActiveIndex] = useState(0);
+
+ const onSelect = useCallback(
+ (api: CarouselApi) => setActiveIndex(api?.selectedScrollSnap() ?? 0),
+ [],
+ );
+
+ useEffect(() => {
+ if (!api) return;
+
+ onSelect(api);
+ api.on("reInit", onSelect);
+ api.on("select", onSelect);
+ }, [api, onSelect]);
+
+ return activeIndex;
+}
+
+const Carousel = forwardRef<
+ HTMLDivElement,
+ HTMLAttributes<HTMLDivElement> & CarouselProps
+>(
+ (
+ {
+ orientation = "horizontal",
+ opts,
+ setApi,
+ autoplay,
+ plugins,
+ className,
+ children,
+ ...props
+ },
+ ref,
+ ) => {
+ const [carouselRef, api] = useEmblaCarousel(
+ {
+ ...opts,
+ axis: orientation === "horizontal" ? "x" : "y",
+ },
+ [
+ ...(autoplay
+ ? [
+ Autoplay(
+ typeof autoplay === "object"
+ ? autoplay
+ : {
+ delay: AUTOPLAY_DEFAULT_DELAY,
+ },
+ ),
+ ]
+ : []),
+ ...(plugins || []),
+ ],
+ );
+ const [canScrollPrev, setCanScrollPrev] = useState(false);
+ const [canScrollNext, setCanScrollNext] = useState(false);
+
+ const onSelect = useCallback((api: CarouselApi) => {
+ if (!api) {
+ return;
+ }
+
+ setCanScrollPrev(api.canScrollPrev());
+ setCanScrollNext(api.canScrollNext());
+ }, []);
+
+ const scrollPrev = useCallback(() => {
+ api?.scrollPrev();
+ }, [api]);
+
+ const scrollNext = useCallback(() => {
+ api?.scrollNext();
+ }, [api]);
+
+ const handleKeyDown = useCallback(
+ (event: KeyboardEvent<HTMLDivElement>) => {
+ if (event.key === "ArrowLeft") {
+ event.preventDefault();
+ scrollPrev();
+ } else if (event.key === "ArrowRight") {
+ event.preventDefault();
+ scrollNext();
+ }
+ },
+ [scrollPrev, scrollNext],
+ );
+
+ useEffect(() => {
+ if (!api || !setApi) {
+ return;
+ }
+
+ setApi(api);
+ }, [api, setApi]);
+
+ useEffect(() => {
+ if (!api) {
+ return;
+ }
+
+ onSelect(api);
+ api.on("reInit", onSelect);
+ api.on("select", onSelect);
+
+ return () => {
+ api?.off("select", onSelect);
+ };
+ }, [api, onSelect]);
+
+ return (
+ <CarouselContext.Provider
+ value={{
+ carouselRef,
+ api: api,
+ opts,
+ orientation:
+ orientation || (opts?.axis === "y" ? "vertical" : "horizontal"),
+ scrollPrev,
+ scrollNext,
+ canScrollPrev,
+ canScrollNext,
+ }}
+ >
+ <div
+ ref={ref}
+ onKeyDownCapture={handleKeyDown}
+ className={cn("relative", className)}
+ role="region"
+ aria-roledescription="carousel"
+ {...props}
+ >
+ {children}
+ </div>
+ </CarouselContext.Provider>
+ );
+ },
+);
+Carousel.displayName = "Carousel";
+
+const CarouselContent = forwardRef<
+ HTMLDivElement,
+ HTMLAttributes<HTMLDivElement>
+>(({ className, ...props }, ref) => {
+ const { carouselRef, orientation } = useCarousel();
+
+ return (
+ <div ref={carouselRef} className="overflow-hidden">
+ <div
+ ref={ref}
+ className={cn(
+ "flex",
+ orientation === "horizontal" ? "-ml-4" : "-mt-4 flex-col",
+ className,
+ )}
+ {...props}
+ />
+ </div>
+ );
+});
+CarouselContent.displayName = "CarouselContent";
+
+const CarouselItem = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
+ ({ className, ...props }, ref) => {
+ const { orientation } = useCarousel();
+
+ return (
+ <div
+ ref={ref}
+ role="group"
+ aria-roledescription="slide"
+ className={cn(
+ "min-w-0 shrink-0 grow-0 basis-full",
+ orientation === "horizontal" ? "pl-4" : "pt-4",
+ className,
+ )}
+ {...props}
+ />
+ );
+ },
+);
+CarouselItem.displayName = "CarouselItem";
+
+const CarouselPrevious = forwardRef<HTMLButtonElement, { className?: string }>(
+ ({ className, ...props }, ref) => {
+ const { orientation, scrollPrev, canScrollPrev } = useCarousel();
+
+ return (
+ <button
+ ref={ref}
+ className={cn(
+ "absolute",
+ orientation === "horizontal"
+ ? "-left-12 top-1/2 -translate-y-1/2"
+ : "-top-12 left-1/2 -translate-x-1/2 rotate-90",
+ buttonVariants({ variant: "secondary", className: "p-2" }),
+ className,
+ )}
+ disabled={!canScrollPrev}
+ onClick={scrollPrev}
+ {...props}
+ >
+ <ArrowLeft className="h-4 w-4" />
+ <span className="sr-only">Previous slide</span>
+ </button>
+ );
+ },
+);
+CarouselPrevious.displayName = "CarouselPrevious";
+
+const CarouselNext = forwardRef<HTMLButtonElement, { className?: string }>(
+ ({ className, ...props }, ref) => {
+ const { orientation, scrollNext, canScrollNext } = useCarousel();
+
+ return (
+ <button
+ ref={ref}
+ className={cn(
+ "absolute",
+ orientation === "horizontal"
+ ? "-right-12 top-1/2 -translate-y-1/2"
+ : "-bottom-12 left-1/2 -translate-x-1/2 rotate-90",
+ buttonVariants({ variant: "secondary", className: "p-2" }),
+ className,
+ )}
+ disabled={!canScrollNext}
+ onClick={scrollNext}
+ {...props}
+ >
+ <ArrowRight className="h-4 w-4" />
+ <span className="sr-only">Next slide</span>
+ </button>
+ );
+ },
+);
+CarouselNext.displayName = "CarouselNext";
+
+export {
+ Carousel,
+ CarouselContent,
+ CarouselItem,
+ CarouselNext,
+ CarouselPrevious,
+ type CarouselApi,
+};
diff --git a/packages/ui/src/carousel/index.ts b/packages/ui/src/carousel/index.ts
new file mode 100644
index 0000000000..12b91b6b0b
--- /dev/null
+++ b/packages/ui/src/carousel/index.ts
@@ -0,0 +1,3 @@
+export * from "./carousel";
+export * from "./nav-bar";
+export * from "./thumbnails";
diff --git a/packages/ui/src/carousel/nav-bar.tsx b/packages/ui/src/carousel/nav-bar.tsx
new file mode 100644
index 0000000000..079f5e2ceb
--- /dev/null
+++ b/packages/ui/src/carousel/nav-bar.tsx
@@ -0,0 +1,117 @@
+import { cn } from "@dub/utils";
+import { VariantProps, cva } from "class-variance-authority";
+import { motion } from "framer-motion";
+import { ChevronLeft, ChevronRight } from "lucide-react";
+import * as React from "react";
+import { AUTOPLAY_DEFAULT_DELAY, CarouselApi, useCarousel } from "./carousel";
+
+const CarouselNavBarVariants = cva(
+ "flex items-center justify-center gap-3 sm:gap-6",
+ {
+ variants: {
+ variant: {
+ simple: "relative",
+ floating:
+ "absolute bottom-4 left-1/2 -translate-x-1/2 rounded-full border border-gray-800/10 bg-white sm:bottom-6 ",
+ },
+ },
+ },
+);
+
+export const CarouselNavBar = ({
+ variant = "simple",
+ className,
+}: VariantProps<typeof CarouselNavBarVariants> & { className?: string }) => {
+ const { scrollNext, scrollPrev, canScrollNext, canScrollPrev, api } =
+ useCarousel();
+
+ const autoplay = api?.plugins()?.autoplay;
+
+ const [selectedIndex, setSelectedIndex] = React.useState(0);
+
+ const [isPlaying, setIsPlaying] = React.useState(false);
+
+ const onSelect = React.useCallback((api: CarouselApi) => {
+ setSelectedIndex(api?.selectedScrollSnap() ?? 0);
+ }, []);
+
+ const stopAutoplayAnd = React.useCallback(
+ (fn: () => void) => () => {
+ if (autoplay && autoplay.isPlaying()) autoplay.stop();
+ fn();
+ },
+ [autoplay],
+ );
+
+ React.useEffect(() => {
+ if (!api) return;
+
+ onSelect(api);
+ setIsPlaying(autoplay?.isPlaying() ?? false);
+ api.on("reInit", onSelect);
+ api.on("select", onSelect);
+ api.on("autoplay:play", () => setIsPlaying(true));
+ api.on("autoplay:stop", () => setIsPlaying(false));
+ }, [api, autoplay, onSelect]);
+
+ return (
+ <div className={cn(CarouselNavBarVariants({ variant }), className)}>
+ {variant !== "simple" && (
+ <button
+ className="cursor-pointer rounded-full p-2 hover:bg-gray-50 active:bg-gray-100"
+ disabled={!canScrollPrev}
+ onClick={stopAutoplayAnd(scrollPrev)}
+ >
+ <ChevronLeft className="h-4 w-4" />
+ <span className="sr-only">Previous slide</span>
+ </button>
+ )}
+
+ {api != null && (
+ <div className="flex items-center gap-1">
+ {api.slideNodes().map((_, idx) => (
+ <button
+ key={idx}
+ onClick={stopAutoplayAnd(() => api.scrollTo(idx))}
+ className="rounded-full p-0.5 hover:bg-gray-100 active:bg-gray-200 sm:p-1.5"
+ >
+ <div
+ className={cn(
+ "relative isolate h-1.5 w-1.5 overflow-hidden rounded-full transition-all",
+ idx === selectedIndex ? "bg-black" : "bg-black/20",
+ isPlaying && idx === selectedIndex && "w-6 bg-black/20",
+ )}
+ >
+ {isPlaying && idx === selectedIndex && (
+ <motion.div
+ initial={{ x: "-100%" }}
+ animate={{ x: 0 }}
+ transition={{
+ type: "tween",
+ duration:
+ (autoplay?.options.delay ?? AUTOPLAY_DEFAULT_DELAY) /
+ 1000,
+ }}
+ className="animate-fill-width h-full w-full rounded-full bg-black"
+ />
+ )}
+ </div>
+ <span className="sr-only">Slide {idx + 1}</span>
+ </button>
+ ))}
+ </div>
+ )}
+
+ {variant !== "simple" && (
+ <button
+ className="cursor-pointer rounded-full p-2 hover:bg-gray-50 active:bg-gray-100"
+ disabled={!canScrollNext}
+ onClick={stopAutoplayAnd(scrollNext)}
+ >
+ <ChevronRight className="h-4 w-4" />
+ <span className="sr-only">Next slide</span>
+ </button>
+ )}
+ </div>
+ );
+};
diff --git a/packages/ui/src/carousel/thumbnails.tsx b/packages/ui/src/carousel/thumbnails.tsx
new file mode 100644
index 0000000000..72c585f3e5
--- /dev/null
+++ b/packages/ui/src/carousel/thumbnails.tsx
@@ -0,0 +1,84 @@
+import { cn } from "@dub/utils";
+import useEmblaCarousel from "embla-carousel-react";
+import {
+ ButtonHTMLAttributes,
+ forwardRef,
+ HTMLAttributes,
+ useCallback,
+ useEffect,
+} from "react";
+import { CarouselApi, useCarousel, useCarouselActiveIndex } from "./carousel";
+
+export const CarouselThumbnails = ({
+ className,
+ ...rest
+}: HTMLAttributes<HTMLDivElement>) => {
+ const { api: mainApi } = useCarousel();
+ const [thumbnailsRef, thumbnailsApi] = useEmblaCarousel({
+ dragFree: true,
+ });
+
+ const onSelect = useCallback(
+ (api: CarouselApi) => {
+ if (!api || !thumbnailsApi) return;
+
+ thumbnailsApi?.scrollTo(api.selectedScrollSnap());
+ },
+ [thumbnailsApi],
+ );
+
+ useEffect(() => {
+ if (!mainApi || !thumbnailsApi) return;
+
+ onSelect(mainApi);
+
+ mainApi.on("reInit", onSelect);
+ mainApi.on("select", onSelect);
+
+ return () => {
+ mainApi?.off("reInit", onSelect);
+ mainApi?.off("select", onSelect);
+ };
+ }, [mainApi, thumbnailsApi, onSelect]);
+
+ return (
+ <div className="overflow-hidden" ref={thumbnailsRef}>
+ <div className={cn("mx-4 flex gap-4", className)} {...rest} />
+ </div>
+ );
+};
+
+type CarouselThumbnailProps = {
+ index: number;
+ className?: string | ((d: { active: boolean }) => string);
+} & Omit<ButtonHTMLAttributes<HTMLButtonElement>, "className">;
+
+export const CarouselThumbnail = forwardRef<
+ HTMLDivElement,
+ CarouselThumbnailProps
+>(({ index, className, ...rest }: CarouselThumbnailProps, ref) => {
+ const { api: mainApi } = useCarousel();
+
+ const activeIndex = useCarouselActiveIndex();
+
+ return (
+ <div ref={ref}>
+ <button
+ type="button"
+ onClick={() => {
+ if (!mainApi) return;
+ const autoplay = mainApi.plugins()?.autoplay;
+ if (autoplay && autoplay.isPlaying()) autoplay.stop();
+
+ mainApi.scrollTo(index);
+ }}
+ className={
+ typeof className === "function"
+ ? className({ active: index === activeIndex })
+ : className
+ }
+ {...rest}
+ />
+ </div>
+ );
+});