MicroClub Carousel
mcoli-ui-registry/mc-carouselFreeComponent
A carousel component for MicroClub UI
Live preview
live · rendered by the registry
Rendered by mcoli-ui-registry on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://mcoli-ui.microclub.info/r/mc-carousel.jsonSource
1'use client';23import * as React from 'react';4import useEmblaCarousel, { type UseEmblaCarouselType } from 'embla-carousel-react';5import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react';67import { cn } from '@/lib/utils';8import { McButton } from '@/components/ui/mc-button';910type McCarouselApi = UseEmblaCarouselType[1];11type UseCarouselParameters = Parameters<typeof useEmblaCarousel>;12type CarouselOptions = UseCarouselParameters[0];13type CarouselPlugin = UseCarouselParameters[1];1415type CarouselProps = {16 opts?: CarouselOptions;17 plugins?: CarouselPlugin;18 orientation?: 'horizontal' | 'vertical';19 setApi?: (api: McCarouselApi) => void;20};2122type CarouselContextProps = {23 carouselRef: ReturnType<typeof useEmblaCarousel>[0];24 api: ReturnType<typeof useEmblaCarousel>[1];25 scrollPrev: () => void;26 scrollNext: () => void;27 canScrollPrev: boolean;28 canScrollNext: boolean;29} & CarouselProps;3031const McCarouselContext = React.createContext<CarouselContextProps | null>(null);3233function useMcCarousel() {34 const context = React.useContext(McCarouselContext);3536 if (!context) {37 throw new Error('useMcCarousel must be used within a <McCarousel />');38 }3940 return context;41}4243function McCarousel({44 orientation = 'horizontal',45 opts,46 setApi,47 plugins,48 className,49 children,50 ...props51}: React.ComponentProps<'div'> & CarouselProps) {52 const [carouselRef, api] = useEmblaCarousel(53 {54 ...opts,55 axis: orientation === 'horizontal' ? 'x' : 'y',56 },57 plugins58 );59 const [canScrollPrev, setCanScrollPrev] = React.useState(false);60 const [canScrollNext, setCanScrollNext] = React.useState(false);6162 const onSelect = React.useCallback((api: McCarouselApi) => {63 if (!api) return;64 setCanScrollPrev(api.canScrollPrev());65 setCanScrollNext(api.canScrollNext());66 }, []);6768 const scrollPrev = React.useCallback(() => {69 api?.scrollPrev();70 }, [api]);7172 const scrollNext = React.useCallback(() => {73 api?.scrollNext();74 }, [api]);7576 const handleKeyDown = React.useCallback(77 (event: React.KeyboardEvent<HTMLDivElement>) => {78 if (event.key === 'ArrowLeft') {79 event.preventDefault();80 scrollPrev();81 } else if (event.key === 'ArrowRight') {82 event.preventDefault();83 scrollNext();84 }85 },86 [scrollPrev, scrollNext]87 );8889 React.useEffect(() => {90 if (!api || !setApi) return;91 setApi(api);92 }, [api, setApi]);9394 React.useEffect(() => {95 if (!api) return;96// … truncated
What it pulls in
npm packages
Files it writes
More from mcoli-ui-registry
All 42 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| MicroClub Accordionmc-accordion | mcoli-ui-registry | Components | Free | 2 deps | |
| MicroClub Alertmc-alert | mcoli-ui-registry | Components | Free | 1 dep | |
| MicroClub Alert Dialogmc-alert-dialog | mcoli-ui-registry | Components | Free | 1 dep | |
| MicroClub Avatarmc-avatar | mcoli-ui-registry | Components | Free | 1 dep | |
| MicroClub Badgemc-badge | mcoli-ui-registry | Components | Free | 1 dep | |
| MicroClub Breadcrumbmc-breadcrumb | mcoli-ui-registry | Components | Free | 2 deps | |
| MicroClub Buttonmc-button | mcoli-ui-registry | Components | Free | 2 deps | |
| MicroClub Calendarmc-calendar | mcoli-ui-registry | Components | Free | 3 deps |
