Carousel
docs/carouselFreeComponent
carousel component for SolidJS
Live preview
live · rendered by the registry
Rendered by docs on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://solidcn.dev/r/carousel.jsonSource
1import { ChevronLeft, ChevronRight } from "lucide-solid";2import type { Component, JSX } from "solid-js";3import { createContext, createSignal, onCleanup, onMount, splitProps, useContext } from "solid-js";4import { cn } from "~/lib/utils";5import { Button } from "./button";67type CarouselOrientation = "horizontal" | "vertical";89interface CarouselContext {10 orientation: CarouselOrientation;11 scrollPrev: () => void;12 scrollNext: () => void;13 canScrollPrev: () => boolean;14 canScrollNext: () => boolean;15}1617const CarouselContext = createContext<CarouselContext>({18 orientation: "horizontal",19 scrollPrev: () => {},20 scrollNext: () => {},21 canScrollPrev: () => false,22 canScrollNext: () => false,23});2425export const useCarousel = () => useContext(CarouselContext);2627export type CarouselProps = {28 orientation?: CarouselOrientation;29 opts?: { loop?: boolean };30 class?: string;31 children?: JSX.Element;32};3334export const Carousel: Component<CarouselProps> = (props) => {35 const [local] = splitProps(props, ["class", "orientation", "opts", "children"]);36 let containerRef!: HTMLDivElement;37 const [canScrollPrev, setCanScrollPrev] = createSignal(false);38 const [canScrollNext, setCanScrollNext] = createSignal(true);39 const orientation = () => local.orientation ?? "horizontal";4041 const updateScrollState = () => {42 if (!containerRef) return;43 const { scrollLeft, scrollTop, scrollWidth, scrollHeight, clientWidth, clientHeight } =44 containerRef;45 if (orientation() === "horizontal") {46 setCanScrollPrev(scrollLeft > 0);47 setCanScrollNext(Math.round(scrollLeft + clientWidth) < scrollWidth);48 } else {49 setCanScrollPrev(scrollTop > 0);50 setCanScrollNext(Math.round(scrollTop + clientHeight) < scrollHeight);51 }52 };5354 const scrollPrev = () => {55 if (!containerRef) return;56 const amount =57 orientation() === "horizontal" ? -containerRef.clientWidth : -containerRef.clientHeight;58 containerRef.scrollBy({59 left: orientation() === "horizontal" ? amount : 0,60 top: orientation() === "vertical" ? amount : 0,61 behavior: "smooth",62 });63 };6465 const scrollNext = () => {66 if (!containerRef) return;67 const amount =68 orientation() === "horizontal" ? containerRef.clientWidth : containerRef.clientHeight;69 containerRef.scrollBy({70 left: orientation() === "horizontal" ? amount : 0,71 top: orientation() === "vertical" ? amount : 0,72 behavior: "smooth",73 });74 };7576 onMount(() => {77// … truncated
More from docs
All 44 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | docs | Components | Free | no deps | |
| Alert Dialogalert-dialog | docs | Components | Free | no deps | |
| Avataravatar | docs | Components | Free | no deps | |
| Badgebadge | docs | Components | Free | no deps | |
| Breadcrumbbreadcrumb | docs | Components | Free | no deps | |
| Buttonbutton | docs | Components | Free | no deps | |
| Calendarcalendar | docs | Components | Free | no deps | |
| Cardcard | docs | Components | Free | no deps |
