shadcn-templ 2.0 beta. Switch to templUI v1
1.7k

Carousel

A carousel with motion and swipe built using vanilla JavaScript.

1
2
3
4
5
package examples import (

About

The carousel component is built using native templ and vanilla JavaScript, no external dependencies.

Installation

shadcn-templ add carousel

Usage

import "github.com/axadrn/shadcn-templ/v2/components/carousel"
@carousel.Carousel() {	@carousel.Content() {		@carousel.Item() {			...		}		@carousel.Item() {			...		}		@carousel.Item() {			...		}	}	@carousel.Previous()	@carousel.Next()}

Composition

Use the following composition to build a Carousel:

carousel.Carousel├── carousel.Content│   ├── carousel.Item│   └── carousel.Item├── carousel.Previous└── carousel.Next

Sizes

To set the size of the items, you can use the basis utility class on the carousel.Item.

1
2
3
4
5
package examples import (
// 33% of the carousel width.@carousel.Carousel() {	@carousel.Content() {		@carousel.Item(carousel.ItemProps{Class: "basis-1/3"}) { ... }		@carousel.Item(carousel.ItemProps{Class: "basis-1/3"}) { ... }		@carousel.Item(carousel.ItemProps{Class: "basis-1/3"}) { ... }	}}
// 50% on small screens and 33% on larger screens.@carousel.Carousel() {	@carousel.Content() {		@carousel.Item(carousel.ItemProps{Class: "md:basis-1/2 lg:basis-1/3"}) { ... }		@carousel.Item(carousel.ItemProps{Class: "md:basis-1/2 lg:basis-1/3"}) { ... }		@carousel.Item(carousel.ItemProps{Class: "md:basis-1/2 lg:basis-1/3"}) { ... }	}}

Spacing

To set the spacing between the items, we use a pl-[VALUE] utility on the carousel.Item and a negative -ml-[VALUE] on the carousel.Content.

1
2
3
4
5
package examples import (
@carousel.Carousel() {	@carousel.Content(carousel.ContentProps{Class: "-ml-4"}) {		@carousel.Item(carousel.ItemProps{Class: "pl-4"}) { ... }		@carousel.Item(carousel.ItemProps{Class: "pl-4"}) { ... }		@carousel.Item(carousel.ItemProps{Class: "pl-4"}) { ... }	}}
@carousel.Carousel() {	@carousel.Content(carousel.ContentProps{Class: "-ml-2 md:-ml-4"}) {		@carousel.Item(carousel.ItemProps{Class: "pl-2 md:pl-4"}) { ... }		@carousel.Item(carousel.ItemProps{Class: "pl-2 md:pl-4"}) { ... }		@carousel.Item(carousel.ItemProps{Class: "pl-2 md:pl-4"}) { ... }	}}

Orientation

Use the Orientation prop to set the orientation of the carousel.

1
2
3
4
5
package examples import (
@carousel.Carousel(carousel.Props{Orientation: carousel.OrientationVertical}) {	@carousel.Content() {		@carousel.Item() { ... }		@carousel.Item() { ... }		@carousel.Item() { ... }	}}

Options

You can configure the carousel using the Align, Loop, Autoplay and Interval props.

@carousel.Carousel(carousel.Props{	Align: carousel.AlignStart,	Loop:  true,}) {	@carousel.Content() {		@carousel.Item() { ... }		@carousel.Item() { ... }		@carousel.Item() { ... }	}}

API

The carousel exposes its selection state on the root element as data-tui-carousel-selected and data-tui-carousel-count.

1
2
3
4
5
package examples import (
<script>	const carousel = document.getElementById("my-carousel");	carousel.addEventListener("carousel-select", (e) => {		const current = e.detail.selected + 1;		const count = e.detail.count;	});</script>

Events

You can listen to events using the bubbling carousel-select event.

<script>	document.addEventListener("carousel-select", (e) => {		// Do something on select.		console.log("Slide " + e.detail.selected + " of " + e.detail.count);	});</script>

Plugins

Use the Autoplay prop with an optional Interval to advance the slides automatically. Autoplay pauses while hovered.

@carousel.Carousel(carousel.Props{	Autoplay: true,	Interval: 2000,}) {	// ...}
1
2
3
4
5
package examples import (

API Reference

The Carousel component is the root container that manages scrolling, keyboard navigation and autoplay.

Prop Type Default
Orientation OrientationHorizontal | OrientationVertical OrientationHorizontal
Align AlignStart | AlignCenter | AlignEnd AlignCenter
Loop bool false
Autoplay bool false
Interval int 5000
Class string -

Content

The carousel.Content component is the sliding track that holds the items.

Prop Type Default
Class string -

Item

The carousel.Item component is a single slide.

Prop Type Default
Class string -

Previous

The carousel.Previous component scrolls to the previous slide.

Prop Type Default
Class string -

Next

The carousel.Next component scrolls to the next slide.

Prop Type Default
Class string -