Fragiola

API: components

Carousel

The APG carousel, basic or tabbed, with native scroll snapping a page at a time, a mouse drag, and a rotation that focus stops, in each framework.

A set of slides shown a page at a time, one slide or several side by side. It is the APG carousel in both its styles: the basic carousel (Previous, Next and a group of picker buttons) and the tabbed one (the pickers a tablist, the slides tabpanels). The slides scroll natively with CSS scroll snapping: a touch swipe or a trackpad scrolls them, a mouse drags them.

Image carousel
Gallery

Behaviour

  • Pages: a page starts at slide 0, then every slidesPerMove slides; the last page is always full (it starts at slideCount - slidesPerPage). The out-of-view slides are inert and hidden from assistive technology.
  • Changes: Previous and Next (previous-press, next-press; disabled at the edges unless loop), an Indicator (item-press), a tabbed Indicator's keys (keyboard), the rotation (autoplay), a mouse drag (drag) and any other scroll you make (swipe, once it settles).
  • Rotation: playing rotates the pages every delay, wrapping to the first. Focus entering the carousel stops it (focus-in) until the AutoplayTrigger starts it again; the pointer over it (but the AutoplayTrigger) pauses it, and leaving resumes it with the time it had left. The slides' container is aria-live="off" while rotating, "polite" otherwise.
  • Tabbed: variant="tabbed": the APG tabs' keys (the arrows select and wrap, Home and End), one slide per page.
  • Layout: the ItemGroup lays the slides out and scrolls them (a structural style: a grid, its scrolling axis, the snapping, no visible scrollbar); give it a width (and a height when vertical). --carousel-slides-per-page and --carousel-spacing are on the Root.
  • No text: name the Root, the slides ("2 of 4"), the triggers and the Indicators; the AutoplayTrigger's label follows its state.

Anatomy

partelementdata-primitives-partrole
Rootdivcarousel-rootregion, the role description "carousel"
ItemGroupdivcarousel-item-groupthe slides' scroll container and live region
Itemdivcarousel-itema slide: group with the role description "slide" (tabbed: tabpanel)
PrevTriggerbuttoncarousel-prev-triggerthe page before
NextTriggerbuttoncarousel-next-triggerthe page after
IndicatorGroupdivcarousel-indicator-groupthe pickers: group (tabbed: tablist)
Indicatorbuttoncarousel-indicatorpicks a page (tabbed: a tab)
AutoplayTriggerbuttoncarousel-autoplay-triggerstops and starts the rotation

Usage

import { Carousel } from "@fragiola/primitives-react/carousel";

<Carousel.Root aria-label="Featured" slideCount={photos.length} defaultPlaying loop>
    <Carousel.AutoplayTrigger>
        {({ playing }) => (playing ? "Stop slide rotation" : "Start slide rotation")}
    </Carousel.AutoplayTrigger>
    <Carousel.ItemGroup className="w-96">
        {photos.map((photo, index) => (
            <Carousel.Item key={photo.src} index={index} aria-label={`${index + 1} of ${photos.length}`}>
                <img src={photo.src} alt={photo.alt} />
            </Carousel.Item>
        ))}
    </Carousel.ItemGroup>
    <Carousel.PrevTrigger aria-label="Previous slide">‹</Carousel.PrevTrigger>
    <Carousel.NextTrigger aria-label="Next slide">›</Carousel.NextTrigger>
</Carousel.Root>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. Vue binds them as props (v-model:page, v-model:playing, :slide-count), and Angular as inputs ([(page)], [(playing)], [slideCount]).

Root

proptypedefaultdescription
slideCountnumberrequiredthe number of slides
pagenumberthe current page, from 0 (controlled)
defaultPagenumber0the page at first
onPageChange(page, details) => voidevery page change, with its reason; Vue @page-change (and update:page), Angular (pageChangeDetails) (and (pageChange))
slidesPerPagenumber1the slides a page shows side by side
slidesPerMovenumberslidesPerPagethe slides a page moves by
loopbooleanfalsePrevious on the first page goes to the last, Next on the last to the first
orientation"horizontal" | "vertical""horizontal"the scrolling axis
spacingstring"0px"the gap between slides, a CSS length
variant"basic" | "tabbed""basic""tabbed": the Indicators are tabs, the slides tabpanels
playingbooleanwhether the slides rotate (controlled)
defaultPlayingbooleanfalsewhether they rotate at first
onPlayingChange(playing, details) => voidevery rotation change, with its reason; Vue @playing-change (and update:playing), Angular (playingChangeDetails) (and (playingChange))
delaynumber5000the time a page shows while rotating, in ms
allowMouseDragbooleantruea mouse drag scrolls the slides

ItemGroup

No props of its own.

Item

proptypedefaultdescription
indexnumberrequiredits slide, from 0

PrevTrigger

proptypedefaultdescription
onClick (react)MouseEventHandlera click, after the trigger's own; not while disabled

NextTrigger

proptypedefaultdescription
onClick (react)MouseEventHandlera click, after the trigger's own; not while disabled

IndicatorGroup

No props of its own; name it with aria-label.

Indicator

proptypedefaultdescription
indexnumberrequiredthe page it picks, from 0
onClick (react)MouseEventHandlera click, after the Indicator's own; not on the current page's

AutoplayTrigger

Its label follows its state: React children as a node or ({ playing }) => node, Vue the default slot's { state }, Angular state() through exportAs.

proptypedefaultdescription
onClick (react)MouseEventHandlera click, after the trigger's own

State and part attributes

partattributes
Rootrole="region", aria-roledescription="carousel"; data-orientation; data-playing while rotating; data-paused while a hover pauses it
ItemGrouparia-live="off" while playing, "polite" otherwise; aria-atomic="false"; tabindex="0" while its slides hold nothing Tab reaches; data-orientation; data-dragging
Itembasic: role="group", aria-roledescription="slide"; tabbed: role="tabpanel"; out of view inert and aria-hidden="true"; data-inview; data-index
PrevTrigger, NextTriggertype="button"; aria-controls the ItemGroup; disabled and data-disabled at an edge
IndicatorGrouprole="group" (tabbed: tablist, aria-orientation); data-orientation
Indicatordata-current; basic: aria-disabled="true" on the current one; tabbed: role="tab", aria-selected, aria-controls, the roving tabindex
AutoplayTriggerdata-playing while playing

Keyboard

Tab and Shift+Tab move through the controls and the in-view slides. In the tabbed variant the IndicatorGroup is one tab stop: the arrows (swapped in RTL) select the next or previous tab and wrap, Home and End the first and last.

Exports

entryexports
@fragiola/primitives-react/carouselCarousel, CarouselAutoplayTriggerProps, CarouselAutoplayTriggerState, CarouselIndicatorGroupProps, CarouselIndicatorProps, CarouselIndicatorState, CarouselItemGroupProps, CarouselItemProps, CarouselItemState, CarouselNextTriggerProps, CarouselOrientation, CarouselPrevTriggerProps, CarouselRootProps, CarouselState, CarouselStepTriggerProps, CarouselStepTriggerState, ChangeDetails, ChangeReason, RenderProp
@fragiola/primitives-vue/carouselCarousel, CarouselAutoplayTriggerState, CarouselIndicatorProps, CarouselIndicatorState, CarouselItemProps, CarouselItemState, CarouselOrientation, CarouselRootEmits, CarouselRootProps, CarouselState, CarouselStepTriggerState, ChangeDetails, ChangeReason, RenderSlotProps
@fragiola/primitives-angular/carouselCarouselAutoplayTrigger, CarouselAutoplayTriggerState, CarouselIndicator, CarouselIndicatorGroup, CarouselIndicatorState, CarouselItem, CarouselItemGroup, CarouselItemState, CarouselNextTrigger, CarouselOrientation, CarouselPrevTrigger, CarouselRoot, CarouselState, CarouselStepTriggerState, ChangeDetails, ChangeReason, ValueChangeEvent