Skip to content

The Open Standard for Generative UI

Carousel Component

i-subhambe0dceddd436
diff --git a/frontend-sdk/packages/react-ui/package.json b/frontend-sdk/packages/react-ui/package.json
index c1b77458a..fa4ff5326 100644
--- a/frontend-sdk/packages/react-ui/package.json
+++ b/frontend-sdk/packages/react-ui/package.json
@@ -44,6 +44,7 @@
"@radix-ui/react-slider": "^1.2.2",
"@storybook/addon-styling-webpack": "^1.0.1",
"clsx": "^2.0.0",
+ "embla-carousel-react": "^8.5.2",
"lucide-react": "^0.469.0",
"sass": "^1.83.4"
},
diff --git a/frontend-sdk/packages/react-ui/src/components/Carousel/Carousel.tsx b/frontend-sdk/packages/react-ui/src/components/Carousel/Carousel.tsx
new file mode 100644
index 000000000..48b55b0b5
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Carousel/Carousel.tsx
@@ -0,0 +1,199 @@
+import clsx from "clsx";
+import { createContext, forwardRef, useContext, useEffect, useRef, useState } from "react";
+import { IconButton } from "../IconButton";
+
+interface CarouselContextType {
+ scrollDivRef: React.RefObject<HTMLDivElement | null>;
+ scroll: (direction: "left" | "right") => void;
+ itemsToScroll: number;
+ hasWrappingDivForCards?: boolean;
+ showButtons?: boolean;
+}
+
+const CarouselContext = createContext<CarouselContextType | null>(null);
+
+const useCarousel = () => {
+ const context = useContext(CarouselContext);
+ if (!context) throw new Error("useCarousel must be used within a Carousel");
+ return context;
+};
+
+export interface CarouselProps extends React.HTMLAttributes<HTMLDivElement> {
+ itemsToScroll?: number;
+ hasWrappingDivForCards?: boolean;
+ showButtons?: boolean;
+}
+
+export const Carousel = forwardRef<HTMLDivElement, CarouselProps>(
+ (
+ {
+ itemsToScroll = 1,
+ hasWrappingDivForCards,
+ showButtons = true,
+ className,
+ children,
+ ...props
+ },
+ ref,
+ ) => {
+ const scrollDivRef = useRef<HTMLDivElement>(null);
+
+ const scroll = (direction: "left" | "right") => {
+ if (scrollDivRef.current) {
+ const container = scrollDivRef.current;
+ let children = Array.from(container.children);
+
+ const spacingEl = children.splice(0, 1)[0] as HTMLElement;
+
+ if (hasWrappingDivForCards) {
+ children = Array.from(children[0]!.children);
+ }
+
+ const visibleIndex = children.findIndex((child) => {
+ const rect = child.getBoundingClientRect();
+ return rect.left >= container.getBoundingClientRect().left;
+ });
+
+ const targetIndex =
+ direction === "left"
+ ? Math.max(0, visibleIndex - itemsToScroll)
+ : Math.min(children.length - 1, visibleIndex + itemsToScroll);
+
+ const targetElement = (
+ targetIndex === 0 ? spacingEl : children[targetIndex]
+ ) as HTMLElement;
+
+ if (targetElement) {
+ container.scrollTo({
+ left: targetElement.offsetLeft,
+ behavior: "smooth",
+ });
+ }
+ }
+ };
+
+ return (
+ <CarouselContext.Provider
+ value={{ scrollDivRef, scroll, itemsToScroll, hasWrappingDivForCards, showButtons }}
+ >
+ <div className={clsx("crayon-carousel", className)} ref={ref} {...props}>
+ {children}
+ </div>
+ </CarouselContext.Provider>
+ );
+ },
+);
+
+export const CarouselContent = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
+ ({ className, children, ...props }, ref) => {
+ const { scrollDivRef } = useCarousel();
+
+ return (
+ <div ref={scrollDivRef} className={clsx("crayon-carousel-content", className)} {...props}>
+ <div className="" />
+ {children}
+ </div>
+ );
+ },
+);
+
+export const CarouselItem = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
+ ({ className, children, ...props }, ref) => (
+ <div ref={ref} className={clsx("crayon-carousel-item", className)} {...props}>
+ {children}
+ </div>
+ ),
+);
+
+export const CarouselPrevious = forwardRef<
+ HTMLButtonElement,
+ React.ComponentProps<typeof IconButton>
+>(({ className, style, ...props }, ref) => {
+ const { scrollDivRef, scroll, showButtons } = useCarousel();
+ const [show, setShow] = useState(true);
+
+ useEffect(() => {
+ if (!scrollDivRef.current) return;
+
+ const container = scrollDivRef.current;
+ const shouldShow = () => container.scrollLeft > 0;
+
+ setShow(shouldShow());
+
+ const handleScroll = () => {
+ setShow(shouldShow());
+ };
+
+ const resizeObserver = new ResizeObserver(handleScroll);
+ resizeObserver.observe(container);
+
+ container.addEventListener("scroll", handleScroll);
+
+ return () => {
+ container.removeEventListener("scroll", handleScroll);
+ resizeObserver.disconnect();
+ };
+ }, [scrollDivRef]);
+
+ if (!show || !showButtons) return null;
+
+ return (
+ <div className="crayon-carousel-button crayon-carousel-button-left">
+ <IconButton
+ ref={ref}
+ shape="circle"
+ variant="secondary"
+ size="small"
+ onClick={() => scroll("left")}
+ style={style}
+ {...props}
+ />
+ </div>
+ );
+});
+
+export const CarouselNext = forwardRef<HTMLButtonElement, React.ComponentProps<typeof IconButton>>(
+ ({ className, style, ...props }, ref) => {
+ const { scrollDivRef, scroll, showButtons } = useCarousel();
+ const [show, setShow] = useState(true);
+
+ useEffect(() => {
+ if (!scrollDivRef.current) return;
+
+ const container = scrollDivRef.current;
+ const shouldShow = () => container.scrollLeft + container.offsetWidth < container.scrollWidth;
+
+ setShow(shouldShow());
+
+ const handleScroll = () => {
+ setShow(shouldShow());
+ };
+
+ const resizeObserver = new ResizeObserver(handleScroll);
+ resizeObserver.observe(container);
+
+ container.addEventListener("scroll", handleScroll);
+
+ return () => {
+ container.removeEventListener("scroll", handleScroll);
+ resizeObserver.disconnect();
+ };
+ }, [scrollDivRef]);
+
+ if (!show || !showButtons) return null;
+
+ return (
+ <div className="crayon-carousel-button crayon-carousel-button-right">
+ <IconButton
+ ref={ref}
+ shape="circle"
+ variant="secondary"
+ size="small"
+ onClick={() => scroll("right")}
+ style={style}
+ {...props}
+ />
+ </div>
+ );
+ },
+);
diff --git a/frontend-sdk/packages/react-ui/src/components/Carousel/carousel.scss b/frontend-sdk/packages/react-ui/src/components/Carousel/carousel.scss
new file mode 100644
index 000000000..933bb75f0
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Carousel/carousel.scss
@@ -0,0 +1,53 @@
+@use "sass:map";
+@use "../../cssUtils" as cssUtils;
+
+.crayon-carousel {
+ position: relative;
+ display: flex;
+
+ &-content {
+ position: relative;
+ display: flex;
+ width: 100%;
+ gap: map.get(cssUtils.$spacing, "xl");
+ overflow-x: auto;
+ scroll-snap-type: x mandatory;
+
+ &::-webkit-scrollbar {
+ display: none;
+ }
+ -ms-overflow-x: hidden;
+ scrollbar-width: none;
+
+ & > * {
+ box-sizing: border-box;
+ scroll-snap-align: start;
+ }
+ }
+ &-item {
+ display: flex;
+ flex-direction: column;
+ gap: map.get(cssUtils.$spacing, "s");
+ min-width: 200px;
+ max-width: 200px;
+ border-radius: map.get(cssUtils.$radius, "m");
+ background-color: map.get(cssUtils.$colors, "fills", "primary");
+ padding: map.get(cssUtils.$spacing, "m");
+ border: 1px solid map.get(cssUtils.$colors, "strokes", "default");
+ }
+
+ &-button {
+ position: absolute;
+ top: 50%;
+ z-index: 10;
+ transform: translateY(-50%);
+
+ &-left {
+ left: 0;
+ }
+
+ &-right {
+ right: 0;
+ }
+ }
+}
diff --git a/frontend-sdk/packages/react-ui/src/components/Carousel/index.ts b/frontend-sdk/packages/react-ui/src/components/Carousel/index.ts
new file mode 100644
index 000000000..2aa930336
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Carousel/index.ts
@@ -0,0 +1 @@
+export * from "./Carousel";
diff --git a/frontend-sdk/packages/react-ui/src/components/Carousel/stories/carousel.stories.tsx b/frontend-sdk/packages/react-ui/src/components/Carousel/stories/carousel.stories.tsx
new file mode 100644
index 000000000..3b1e93f01
--- /dev/null
+++ b/frontend-sdk/packages/react-ui/src/components/Carousel/stories/carousel.stories.tsx
@@ -0,0 +1,155 @@
+import type { Meta, StoryObj } from "@storybook/react";
+import { ChevronLeft, ChevronRight } from "lucide-react";
+import "../../IconButton/iconButton.scss";
+import {
+ Carousel,
+ CarouselContent,
+ CarouselItem,
+ CarouselNext,
+ CarouselPrevious,
+} from "../Carousel";
+import "../carousel.scss";
+
+const meta: Meta<typeof Carousel> = {
+ title: "Components/Carousel",
+ component: Carousel,
+ parameters: {
+ layout: "centered",
+ },
+ tags: ["autodocs"],
+};
+
+export default meta;
+type Story = StoryObj<typeof Carousel>;
+
+export const Default: Story = {
+ render: () => (
+ <Carousel style={{ width: "100%", maxWidth: "20rem" }}>
+ <CarouselContent>
+ {Array.from({ length: 10 }).map((_, index) => (
+ <CarouselItem key={index}>
+ <div style={{ padding: "0.25rem" }}>
+ <div
+ style={{
+ display: "flex",
+ aspectRatio: "1/1",
+ alignItems: "center",
+ justifyContent: "center",
+ borderRadius: "0.5rem",
+ border: "1px solid rgb(229, 231, 235)",
+ backgroundColor: "white",
+ padding: "0.5rem",
+ }}
+ >
+ <span style={{ fontSize: "1.875rem", fontWeight: 600 }}>{index + 1}</span>
+ </div>
+ </div>
+ </CarouselItem>
+ ))}
+ </CarouselContent>
+ <CarouselPrevious icon={<ChevronLeft />} />
+ <CarouselNext icon={<ChevronRight />} />
+ </Carousel>
+ ),
+};
+
+export const MultipleItems: Story = {
+ render: () => (
+ <Carousel style={{ width: "100%", maxWidth: "28rem" }} itemsToScroll={2}>
+ <CarouselContent>
+ {Array.from({ length: 10 }).map((_, index) => (
+ <CarouselItem key={index}>
+ <div style={{ padding: "0.25rem" }}>
+ <div
+ style={{
+ display: "flex",
+ aspectRatio: "1/1",
+ alignItems: "center",
+ justifyContent: "center",
+ borderRadius: "0.5rem",
+ border: "1px solid rgb(229, 231, 235)",
+ backgroundColor: "white",
+ padding: "0.5rem",
+ }}
+ >
+ <span style={{ fontSize: "1.875rem", fontWeight: 600 }}>{index + 1}</span>
+ </div>
+ </div>
+ </CarouselItem>
+ ))}
+ </CarouselContent>
+ <CarouselPrevious icon={<ChevronLeft />} />
+ <CarouselNext icon={<ChevronRight />} />
+ </Carousel>
+ ),
+};
+
+export const WithWrappingDiv: Story = {
+ render: () => (
+ <Carousel
+ style={{ width: "100%", maxWidth: "28rem" }}
+ hasWrappingDivForCards
+ showButtons={false}
+ >
+ <CarouselContent>
+ <div style={{ display: "flex" }}>
+ {Array.from({ length: 20 }).map((_, index) => (
+ <CarouselItem key={index}>
+ <div style={{ padding: "0.25rem" }}>
+ <div
+ style={{
+ display: "flex",
+ aspectRatio: "1/1",
+ alignItems: "center",
+ justifyContent: "center",
+ borderRadius: "0.5rem",
+ border: "1px solid rgb(229, 231, 235)",
+ backgroundColor: "white",
+ padding: "0.5rem",
+ }}
+ >
+ <span style={{ fontSize: "1.875rem", fontWeight: 600 }}>{index + 1}</span>
+ </div>
+ </div>
+ </CarouselItem>
+ ))}
+ </div>
+ </CarouselContent>
+ <CarouselPrevious icon={<ChevronLeft />} />
+ <CarouselNext icon={<ChevronRight />} />
+ </Carousel>
+ ),
+};
+
+export const CustomStyling: Story = {
+ render: () => (
+ <Carousel style={{ width: "100%", maxWidth: "28rem" }}>
+ <CarouselContent style={{ gap: "1rem" }}>
+ {Array.from({ length: 10 }).map((_, index) => (
+ <CarouselItem key={index}>
+ <div style={{ padding: "0.25rem" }}>
+ <div
+ style={{
+ display: "flex",
+ aspectRatio: "16/9",
+ alignItems: "center",
+ justifyContent: "center",
+ borderRadius: "0.75rem",
+ border: "1px solid rgb(229, 231, 235)",
+ background: "linear-gradient(to right, rgb(59, 130, 246), rgb(168, 85, 247))",
+ padding: "1.5rem",
+ }}
+ >
+ <span style={{ fontSize: "2.25rem", fontWeight: 700, color: "white" }}>
+ {index + 1}
+ </span>
+ </div>
+ </div>
+ </CarouselItem>
+ ))}
+ </CarouselContent>
+ <CarouselPrevious icon={<ChevronLeft />} />
+ <CarouselNext icon={<ChevronRight />} />
+ </Carousel>
+ ),
+};