Skip to documentation
Design system / Sources & standards
Current repository source
contentCarousel.tsx
src/components/carousel/contentCarousel.tsx
This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.
"use client";
import { useId, type ReactNode } from "react";
import { Carousel, CarouselContent, CarouselItem, CarouselMedia, CarouselOverlayControls, CarouselIndicators, useCarousel, type CarouselApi } from "@/components/ui/carousel";
import { Card, CardAction, CardContent, CardHeader } from "@/components/ui/card";
import { Text } from "@/components/ui/text";
import { cn } from "@/lib/utils";
export type ContentCarouselItem = { id: string; content: ReactNode; footer?: ReactNode };
export type ContentCarouselProps = {
title: string;
showTitle?: boolean;
items: readonly ContentCarouselItem[];
emptyMessage?: string;
className?: string;
variant?: "plain" | "card" | "expanded";
headerAction?: ReactNode;
startIndex?: number;
setApi?: (api: CarouselApi) => void;
};
/** Select supplied footer content using the existing compound-carousel state. */
function ContentCarouselFooter({ items, expanded = false }: { items: readonly ContentCarouselItem[]; expanded?: boolean }) {
const { selectedIndex } = useCarousel();
return <div data-slot="carousel-footer" className={cn("px-3 pt-3", expanded && "shrink-0 px-4 pb-4")}>
{items[selectedIndex]?.footer ?? <CarouselIndicators />}
</div>;
}
function CarouselPosition() {
const { selectedIndex, snapCount } = useCarousel();
return <span className="font-normal text-muted-foreground">{selectedIndex + 1} of {snapCount || 1}</span>;
}
/** Titled composition of the standard shadcn carousel, with caller-owned slides. */
export function ContentCarousel({ title, showTitle = true, items, emptyMessage = "No items to display.", className, variant = "plain", headerAction, startIndex = 0, setApi }: ContentCarouselProps) {
const id = useId();
const heading = showTitle ? <h2 id={`${id}-title`} className={cn("font-semibold", variant === "card" ? "text-label" : "text-lg")}>{title} <span className="font-normal text-muted-foreground">({items.length})</span></h2> : null;
const accessibleName = { "aria-labelledby": showTitle ? `${id}-title` : undefined, "aria-label": showTitle ? undefined : `${title} (${items.length})` };
const expanded = variant === "expanded";
const slides = <CarouselContent viewportClassName={expanded ? "min-h-0 flex-1" : undefined} className={cn(variant !== "plain" && "ml-0", expanded && "h-full")}>
{items.map((item, index) => <CarouselItem key={item.id} className={cn(variant !== "plain" && "pl-0", expanded && "h-full")} aria-label={`${index + 1} of ${items.length}`}>
<CarouselMedia className={expanded ? "h-full" : undefined}>{item.content}{!expanded ? <CarouselOverlayControls slideIndex={index} /> : null}</CarouselMedia>
</CarouselItem>)}
</CarouselContent>;
if (expanded && items.length) return <Carousel {...accessibleName} tabIndex={0} opts={{ startIndex }} setApi={setApi} className={cn("group/carousel-media flex h-full min-h-0 flex-col gap-3", className)}>
<div className="flex shrink-0 items-center justify-between gap-4 px-4 py-3">
{showTitle ? <h2 id={`${id}-title`} className="min-w-0 text-label font-semibold">{title} <CarouselPosition /></h2> : <CarouselPosition />}
{headerAction}
</div>
{slides}
<ContentCarouselFooter items={items} expanded />
<CarouselOverlayControls />
</Carousel>;
if (variant === "card") {
const content = <>
{showTitle || headerAction ? <CardHeader className="px-4">
{heading}
{headerAction ? <CardAction>{headerAction}</CardAction> : null}
</CardHeader> : null}
<CardContent className={items.length ? "px-0" : "px-4"}>
{items.length ? <>{slides}<ContentCarouselFooter items={items} /></> : <Text variant="supporting" className="text-muted-foreground">{emptyMessage}</Text>}
</CardContent>
</>;
return <section data-slot="content-carousel" aria-label={!showTitle && !items.length ? `${title} (0)` : undefined} className={cn("min-w-0", className)}>
<Card className="gap-3 py-4">
{items.length ? <Carousel className="space-y-3" {...accessibleName} tabIndex={0} opts={{ startIndex }} setApi={setApi}>{content}</Carousel> : content}
</Card>
</section>;
}
return (
<section data-slot="content-carousel" aria-label={!showTitle && !items.length ? `${title} (0)` : undefined} className={cn("min-w-0 space-y-4", className)}>
{heading}
{items.length === 0 ? <Text variant="supporting" className="text-muted-foreground">{emptyMessage}</Text> : (
<Carousel {...accessibleName} tabIndex={0} opts={{ startIndex }} setApi={setApi}>
{slides}
<ContentCarouselFooter items={items} />
</Carousel>
)}
</section>
);
}
All sources and standards