Corex.Carousel
(Corex v0.1.0-beta.1)
View Source
Phoenix implementation of Zag.js Carousel.
Examples
Basic with image URLs
<.carousel id="car" items={["/images/beach.jpg", "/images/fall.jpg", "/images/sand.jpg"]} class="carousel">
<:prev_trigger>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5 8.25 12l7.5-7.5" /></svg>
</:prev_trigger>
<:next_trigger>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="m8.25 4.5 7.5 7.5-7.5 7.5" /></svg>
</:next_trigger>
</.carousel>Items can be URLs (strings) or maps with :url and optional :alt keys.
API
Imperative helpers target the carousel by DOM id on the hook root (the same id you pass to carousel/1).
- Client —
play/1,pause/1,scroll_next/1,scroll_prev/1, andscroll_next/2,scroll_prev/2with optionalinstant(JS.dispatchtocorex:carousel:*). - Server —
play/2,pause/2,scroll_next/2,scroll_prev/2, andscroll_next/3,scroll_prev/3with optionalinstant(push_eventconsumed by the hook).
Events
From the client, dispatch CustomEvents on the hook root (#your-id):
| Type | Purpose |
|---|---|
corex:carousel:play | Start or resume autoplay |
corex:carousel:pause | Pause autoplay |
corex:carousel:scroll-next | Next page; optional detail.instant boolean |
corex:carousel:scroll-prev | Previous page; optional detail.instant boolean |
From LiveView, push_event names: carousel_play, carousel_pause, carousel_scroll_next, carousel_scroll_prev with payload %{"id" => ...} and optional "instant" for scroll events.
Styling
Use data attributes to target elements:
[data-scope="carousel"][data-part="root"] {}
[data-scope="carousel"][data-part="control"] {}
[data-scope="carousel"][data-part="item-group"] {}
[data-scope="carousel"][data-part="item"] {}
[data-scope="carousel"][data-part="prev-trigger"] {}
[data-scope="carousel"][data-part="next-trigger"] {}
[data-scope="carousel"][data-part="indicator-group"] {}
[data-scope="carousel"][data-part="indicator"] {}If you wish to use the default Corex styling, you can use the class carousel on the component.
This requires to install Mix.Tasks.Corex.Design first and import the component css file.
@import "../corex/main.css";
@import "../corex/tokens/themes/neo/light.css";
@import "../corex/components/carousel.css";You can then use modifiers
<.carousel class="carousel carousel--accent carousel--lg" items={[]}>
</.carousel>
Summary
API
Pauses carousel autoplay from the client.
Starts or resumes carousel autoplay from the client. Returns a Phoenix.LiveView.JS command.
Scrolls to the next page from the client.
Scrolls to the next page from the client. Pass true for instant to skip animation.
Scrolls to the previous page from the client.
Components
Attributes
id(:string)aria_label(:string) - Defaults tonil.items(:list) (required) - List of image URLs (strings) or maps with :url and optional :alt.page(:integer) - Defaults to0.controlled(:boolean) - Defaults tofalse.dir(:string) - Defaults to"ltr". Must be one of"ltr", or"rtl".orientation(:string) - Defaults to"horizontal". Must be one of"horizontal", or"vertical".slides_per_page(:integer) - Defaults to1.slides_per_move(:any) - Number of slides to move per step, or "auto". Defaults tonil.loop(:boolean) - Defaults tofalse.autoplay(:boolean) - Defaults tofalse.autoplay_delay(:integer) - Defaults to4000.allow_mouse_drag(:boolean) - Defaults tofalse.spacing(:string) - Defaults to"0px".padding(:string) - Defaults tonil.in_view_threshold(:float) - Defaults to0.6.snap_type(:string) - Defaults to"mandatory". Must be one of"proximity", or"mandatory".auto_size(:boolean) - Defaults tofalse.on_page_change(:string) - Defaults tonil.on_page_change_client(:string) - Defaults tonil.compound(:boolean) - Enable compound mode with :let={ctx} and carousel_root, carousel_item_group, carousel_item, carousel_control, carousel_prev_trigger, carousel_next_trigger, carousel_indicator_group, carousel_indicator. Defaults tofalse.- Global attributes are accepted.
Slots
inner_block- Compound mode. Use compound and :let={ctx}.prev_trigger- Accepts attributes:class(:string)
next_trigger- Accepts attributes:class(:string)
API
Pauses carousel autoplay from the client.
Starts or resumes carousel autoplay from the client. Returns a Phoenix.LiveView.JS command.
Scrolls to the next page from the client.
Scrolls to the next page from the client. Pass true for instant to skip animation.
Scrolls to the previous page from the client.
Functions
Attributes
ctx(:map) (required)- Global attributes are accepted.
Slots
inner_block(required)
Attributes
ctx(:map) (required)index(:integer) (required)- Global attributes are accepted.
Slots
inner_block
Attributes
ctx(:map) (required)- Global attributes are accepted.
Slots
inner_block(required)
Attributes
ctx(:map) (required)index(:integer) (required)- Global attributes are accepted.
Slots
inner_block(required)
Attributes
ctx(:map) (required)- Global attributes are accepted.
Slots
inner_block(required)
Attributes
ctx(:map) (required)- Global attributes are accepted.
Slots
inner_block(required)
Attributes
ctx(:map) (required)- Global attributes are accepted.
Slots
inner_block(required)
Attributes
ctx(:map) (required)- Global attributes are accepted.
Slots
inner_block(required)