Display

Carousel

Shows a set of slides one view at a time; swipe, scroll or use the buttons.

Installation

npx sinaui add carousel

Examples

Basic

Swipe or scroll with a trackpad and it snaps to each slide; the indicator glides with it.

<script lang="ts">
	import Carousel from '#lib/ui/Carousel.svelte';

	const treasures = [
		{ title: 'The Canon of Medicine', by: 'Ibn Sīnā', year: 1025, hue: 160 },
		{ title: 'Book of Optics', by: 'Ibn al-Haytham', year: 1021, hue: 200 },
		{ title: 'Al-Jabr', by: 'Al-Khwārizmī', year: 820, hue: 30 },
		{ title: 'Tabula Rogeriana', by: 'Al-Idrīsī', year: 1154, hue: 280 },
		{ title: 'Book of Ingenious Devices', by: 'Al-Jazarī', year: 1206, hue: 340 }
	];
</script>

<div class="wrap">
	<Carousel label="Treasures of the House of Wisdom" slides={treasures}>
		{#snippet slide(t)}
			<article class="card" style:--hue={t.hue}>
				<span class="year">{t.year}</span>
				<h4>{t.title}</h4>
				<p>{t.by}</p>
				<a href="#basic">View manuscript</a>
			</article>
		{/snippet}
	</Carousel>
</div>

<style>
	.wrap {
		inline-size: min(100%, 30rem);
	}
	.card {
		display: grid;
		align-content: end;
		gap: 0.125rem;
		aspect-ratio: 16 / 10;
		padding: 1.25rem;
		box-sizing: border-box;
		border-radius: calc(var(--ui-radius) * 1.5);
		background: linear-gradient(
			145deg,
			hsl(var(--hue) 45% 38%),
			hsl(calc(var(--hue) + 40) 50% 24%)
		);
		color: #fff;
	}
	.year {
		font-variant-numeric: tabular-nums;
		opacity: 0.8;
	}
	h4 {
		margin: 0;
		font-size: 1.25rem;
	}
	p {
		margin: 0 0 0.5rem;
		opacity: 0.85;
	}
	a {
		color: #fff;
		font-weight: 500;
	}
</style>

Several in view, playing

Moves on by itself every four seconds, and waits while it's hovered, focused or the tab is hidden. The pause button stops it.

<script lang="ts">
	import Carousel from '#lib/ui/Carousel.svelte';

	const rooms = ['Baghdad', 'Cairo', 'Córdoba', 'Fez', 'Samarkand', 'Bukhara', 'Damascus'];
</script>

<div class="wrap">
	<Carousel label="Reading rooms" slides={rooms} perView={3} autoplay={4000}>
		{#snippet slide(room, i)}
			<div class="room">
				<span class="n">{String(i + 1).padStart(2, '0')}</span>
				{room}
			</div>
		{/snippet}
	</Carousel>
</div>

<style>
	.wrap {
		inline-size: 100%;
	}
	.room {
		display: grid;
		gap: 0.25rem;
		padding: 1rem;
		border-radius: var(--ui-radius);
		background: var(--ui-subtle);
		font-weight: 500;
	}
	.n {
		color: var(--ui-muted);
		font-size: 0.8125rem;
		font-variant-numeric: tabular-nums;
	}
</style>

Props

PropTypeDefault
labelstring (required)
slidesT[]
slideSnippet<[T, index]>
perViewnumber1
autoplaynumber (ms)off

Accessibility

  • Announced as a carousel, and each slide as “slide, 3 of 5”; changing slide is announced (not while playing).
  • Slides out of view can't be reached by Tab or screen readers until they're shown.
  • Playing by itself always has a pause button, and waits while hovered, focused or hidden (WCAG 2.2.2).
  • The slides can be focused and scrolled with the arrow keys.
Introduction
Theming
Changelog
Button
Action button
Confirm button
Copy button
Hold to confirm
Split button
Calendar
Checkbox
Color picker
Combobox
Date picker
Date range picker
File drop
Form
Input
Mention input
Multi select
Number field
OTP input
Password input
Phone input
Radio cards
Radio group
Rich text editor
Search field
Segmented control
Select
Signature pad
Slider
Switch
Tag input
Upload
Wheel picker
Textarea
Time field
Toggle group
Toolbar
Alert dialog
Command palette
Context menu
Dialog
Drawer
Dropdown
Hover card
Popover
Tooltip
Announcement bar
Alert
Empty state
Meter
Progress
Rolling number
Skeleton
Spinner
Toast
Avatar
Avatar group
Badge
Card
Carousel
Chart
Chat
Code block
Data table
Folder
Kbd
Mind map
Resizable panels
Scroll area
Separator
Sortable
Table
Tag group
Virtual list
Audio player
Image
Image compare
Video
Visualizer
Breadcrumb
File tree
Menubar
Navigation menu
Pagination
Sidebar layout
Stepper
Tabs
Alarm clock
Clock
Dynamic island
Fluid orb
Hourglass
Stopwatch
Timer
AI chat
AI media
Auth
Board
Booking
Dashboard
File explorer
Messaging
Notifications
Settings
Team
Overview
Pricing
Agent chat
Calendar
Voice agent
Mail inbox
CMS
Blog
Marketing site
E-commerce
AI agents
SaaS landing page
SaaS app
Accordion
Dark theme
Light theme