--- title: "carousel" description: "Horizontally scrolling slides with snap points and arrow controls." category: "Data Display" --- # carousel Horizontally scrolling slides with snap points and arrow controls. ## Installation ```bash php artisan ui:add carousel ``` ## Dependencies - composer: `gehrisandro/tailwind-merge-laravel` ## Usage ```blade @foreach ($slides as $slide) {{-- basis-full by default; use class="basis-1/2" for a multi-up row. --}} {{ $slide }} @endforeach ``` ## Example The demo rendered on the docs page. ```blade @foreach (range(1, 5) as $i) {{ $i }} @endforeach ``` ## Source Copy each file to the path shown above it. `resources/views/components/ui/carousel/index.blade.php` ```blade @props([ // false drops the two built-in arrow buttons so you can place your own // in the controls slot. Any element inside the root carrying // data-ui-carousel-prev or data-ui-carousel-next works as an arrow. 'arrows' => true, // Extra classes for the scrolling track, merged over its defaults: a // different gap, or padding plus matching scroll-padding for slides that // peek past the container edges. 'track' => null, ]) @php $classes = \TailwindMerge\Laravel\Facades\TailwindMerge::merge( 'relative', $attributes->get('class'), ); $trackClasses = \TailwindMerge\Laravel\Facades\TailwindMerge::merge( 'flex snap-x snap-mandatory gap-4 overflow-x-auto scroll-smooth [scrollbar-width:none] [&::-webkit-scrollbar]:hidden', $track, ); $arrow = 'absolute top-1/2 z-10 flex size-8 -translate-y-1/2 items-center justify-center rounded-full border border-input bg-background shadow-xs outline-none transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] disabled:pointer-events-none disabled:opacity-50 dark:bg-input/30 dark:hover:bg-input/50'; @endphp {{-- Scroll-snap carries the whole interaction: swipe, trackpad, scrollbar dragging and keyboard scrolling are the browser's, not ours. Script is only needed for the arrow buttons and the optional dots, which have no declarative equivalent. Strip the script and everything else still works. The default slot holds the slides only. Anything else (your own arrows, dots, a caption) goes in the controls slot, which renders after the track inside the same positioned root. Dots: put an empty element with data-ui-carousel-dots in the controls slot. The script fills it with one button per slide and marks the current one with aria-current and data-active, so style it with data-active: utilities. Its data-label attribute sets the accessible name prefix (default "Go to slide"). --}}
except('class')->merge(['class' => $classes]) }}>
{{ $slot }}
{{ $controls ?? '' }} @if ($arrows) @endif
@once @endonce ``` `resources/views/components/ui/carousel/item.blade.php` ```blade @props([]) @php // basis-full shows one slide at a time. Override it through class for a // multi-up carousel, e.g. class="basis-1/2 md:basis-1/3". $classes = \TailwindMerge\Laravel\Facades\TailwindMerge::merge( 'min-w-0 shrink-0 grow-0 basis-full snap-start', $attributes->get('class'), ); @endphp
except('class')->merge(['class' => $classes]) }}> {{ $slot }}
``` ## Theme tokens `--background`, `--accent`, `--input`, `--ring`, `--foreground`, `--muted-foreground` CSS scroll-snap does the scrolling; the inline