Carousel
nativeui/carouselFreeComponent
A carousel component for React Native applications.
Live preview
live · rendered by the registry
Rendered by nativeui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://nativeui.ribbi.ch/r/carousel.jsonSource
1import * as React from "react";2import {3 View,4 Text,5 Pressable,6 Dimensions,7 ScrollView,8 AccessibilityInfo,9} from "react-native";10import { cn } from "@/lib/utils";11import { Ionicons } from "@expo/vector-icons";1213type CarouselContextProps = {14 scrollViewRef: React.RefObject<ScrollView | null>;15 currentIndex: number;16 scrollTo: (index: number) => void;17 canScrollPrev: boolean;18 canScrollNext: boolean;19 itemsCount: number;20 orientation?: "horizontal" | "vertical";21};2223const CarouselContext = React.createContext<CarouselContextProps | null>(null);2425function useCarousel() {26 const context = React.useContext(CarouselContext);27 if (!context) {28 throw new Error("useCarousel must be used within a <Carousel />");29 }30 return context;31}3233interface CarouselProps {34 children: React.ReactNode;35 className?: string;36 orientation?: "horizontal" | "vertical";37 showControls?: boolean;38 showIndicators?: boolean;39 autoPlay?: boolean;40 autoPlayInterval?: number;41 loop?: boolean;42 indicatorStyle?: "dots" | "lines" | "numbers";43 onIndexChange?: (index: number) => void;44}4546const Carousel = React.forwardRef<View, CarouselProps>(47 (48 {49 children,50 className,51 orientation = "horizontal",52 showControls = true,53 showIndicators = true,54 autoPlay = false,55 autoPlayInterval = 3000,56 loop = true,57 indicatorStyle = "dots",58 onIndexChange,59 ...props60 },61 ref62 ) => {63 const scrollViewRef = React.useRef<ScrollView>(null);64 const [currentIndex, setCurrentIndex] = React.useState(0);65 const [itemsCount, setItemsCount] = React.useState(0);66 const dimensions = {67 width: Dimensions.get("window").width,68 height: Dimensions.get("window").height,69 };7071 const canScrollPrev = currentIndex > 0 || loop;72 const canScrollNext = currentIndex < itemsCount - 1 || loop;7374 const scrollTo = React.useCallback(75 (index: number) => {76 if (!scrollViewRef.current) return;7778 let targetIndex = index;79 if (index < 0) {80 targetIndex = loop ? itemsCount - 1 : 0;81 } else if (index >= itemsCount) {82 targetIndex = loop ? 0 : itemsCount - 1;83 }8485 const offset =86 orientation === "horizontal"87 ? targetIndex * dimensions.width88 : targetIndex * dimensions.height;8990 scrollViewRef.current.scrollTo({91 [orientation === "horizontal" ? "x" : "y"]: offset,92 animated: true,93 });9495// … truncated
What it pulls in
npm packages
Files it writes
More from nativeui
All 29 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | nativeui | Components | Free | 2 deps | |
| Alertalert | nativeui | Components | Free | 2 deps | |
| Alert Dialogalert-dialog | nativeui | Components | Free | 1 dep | |
| Avataravatar | nativeui | Components | Free | 1 dep | |
| Badgebadge | nativeui | Components | Free | 2 deps | |
| Breadcrumbbreadcrumb | nativeui | Components | Free | 2 deps | |
| Buttonbutton | nativeui | Components | Free | 2 deps | |
| Calendarcalendar | nativeui | Components | Free | 4 deps |
