Carousel
solid-ui/carouselFreeComponent
A carousel component
Live preview
live · rendered by the registry
Rendered by solid-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.solid-ui.com/r/carousel.jsonSource
1import type { Accessor, Component, ComponentProps, VoidProps } from "solid-js"2import {3 createContext,4 createEffect,5 createMemo,6 createSignal,7 mergeProps,8 splitProps,9 useContext10} from "solid-js"1112import type { CreateEmblaCarouselType } from "embla-carousel-solid"13import createEmblaCarousel from "embla-carousel-solid"1415import { cn } from "~/lib/utils"16import type { ButtonProps } from "~/registry/ui/button"17import { Button } from "~/registry/ui/button"1819export type CarouselApi = CreateEmblaCarouselType[1]2021type UseCarouselParameters = Parameters<typeof createEmblaCarousel>22type CarouselOptions = NonNullable<UseCarouselParameters[0]>23type CarouselPlugin = NonNullable<UseCarouselParameters[1]>2425type CarouselProps = {26 opts?: ReturnType<CarouselOptions>27 plugins?: ReturnType<CarouselPlugin>28 orientation?: "horizontal" | "vertical"29 setApi?: (api: CarouselApi) => void30}3132type CarouselContextProps = {33 carouselRef: ReturnType<typeof createEmblaCarousel>[0]34 api: ReturnType<typeof createEmblaCarousel>[1]35 scrollPrev: () => void36 scrollNext: () => void37 canScrollPrev: Accessor<boolean>38 canScrollNext: Accessor<boolean>39} & CarouselProps4041const CarouselContext = createContext<Accessor<CarouselContextProps> | null>(null)4243const useCarousel = () => {44 const context = useContext(CarouselContext)4546 if (!context) {47 throw new Error("useCarousel must be used within a <Carousel />")48 }4950 return context()51}5253const Carousel: Component<CarouselProps & ComponentProps<"div">> = (rawProps) => {54 const props = mergeProps<(CarouselProps & ComponentProps<"div">)[]>(55 { orientation: "horizontal" },56 rawProps57 )5859 const [local, others] = splitProps(props, [60 "orientation",61 "opts",62 "setApi",63 "plugins",64 "class",65 "children"66 ])6768 const [carouselRef, api] = createEmblaCarousel(69 () => ({70 ...local.opts,71 axis: local.orientation === "horizontal" ? "x" : "y"72 }),73 () => (local.plugins === undefined ? [] : local.plugins)74 )75 const [canScrollPrev, setCanScrollPrev] = createSignal(false)76 const [canScrollNext, setCanScrollNext] = createSignal(false)7778 const onSelect = (api: NonNullable<ReturnType<CarouselApi>>) => {79 setCanScrollPrev(api.canScrollPrev())80 setCanScrollNext(api.canScrollNext())81 }8283 const scrollPrev = () => {84 api()?.scrollPrev()85 }8687 const scrollNext = () => {88 api()?.scrollNext()89 }9091 const handleKeyDown = (event: KeyboardEvent) => {92 if (event.key === "ArrowLeft") {93// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from solid-ui
All 55 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | solid-ui | Components | Free | 2 deps | |
| Alertalert | solid-ui | Components | Free | 3 deps | |
| Alert Dialogalert-dialog | solid-ui | Components | Free | 2 deps | |
| Aspect Ratioaspect-ratio | solid-ui | Components | Free | 1 dep | |
| Avataravatar | solid-ui | Components | Free | 2 deps | |
| Badgebadge | solid-ui | Components | Free | 2 deps | |
| Badge Deltabadge-delta | solid-ui | Components | Free | 2 deps | |
| Bar Listbar-list | solid-ui | Components | Free | 1 dep |
