Carousel
Mobile carousel: drag-to-turn pages with dual thresholds (distance / velocity), autoplay (paused during hover and dragging), loop wrap-around with boundary clamping, dot indicators and desktop arrows. Page-index math (normalizePageIndex / next·prevPageIndex / resolveSwipeTarget / stepAutoplay) is the cross-platform pure-function source; instance methods prev/next/goTo are exposed via a state handle.
Contract:
lh-ui-core/specs/carousel.json· Golden cases:cases/carousel.json(10 cases, translated to JVM unit tests + Compose UI Test)
Basic usage
pageCount gives the page count and content(pageIndex) renders each page (item-slot form, payload is the page index). The default dots indicator sits below the content; transition animations follow the motion tokens.
LhCarousel(pageCount = 3) { page ->
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Text("Page ${page + 1}")
}
}Loop and boundaries
loop = true (default) wraps page indices modularly: next from the last page returns to the first, and prev from the first page wraps to the last. With loop = false boundaries clamp: next/prev do not switch and do not emit onChange; boundary drags are damped (visual clamp) and snap back on release.
LhCarousel(pageCount = 3, loop = false) { page -> BannerPage(page) }Autoplay
autoplay is the interval in milliseconds (0 or unset = off); each tick advances one page (wraps with loop, self-stops at the last page without loop). Autoplay pauses while hovered or dragged and restarts its timer whenever those conditions change. disabled = true disables autoplay and gestures.
LhCarousel(pageCount = 3, autoplay = 3000L) { page -> BannerPage(page) }
LhCarousel(pageCount = 3, autoplay = 3000L, disabled = true) { page -> BannerPage(page) }Indicator and arrows
indicator is dots (default) or none; indicatorPosition is bottom (in-flow, default) or overlay (overlaid on the content bottom, offset by carousel.indicator.overlayBottom). The active dot is scaled by carousel.indicator.dotActiveScale and uses the primary color. showArrow = true shows left/right arrows (desktop); clicking at a non-loop boundary does not switch.
LhCarousel(
pageCount = 3,
indicatorPosition = LhCarouselIndicatorPosition.overlay,
indicator = LhCarouselIndicator.dots,
showArrow = true,
) { page -> BannerPage(page) }Drag and keyboard
Dual-threshold turning (drag left for the next page, right for the previous): a swipe flips when the distance exceeds 20% of the page width or the speed exceeds 0.5px/ms. Focusable targets also turn pages with the arrow keys ←/→. Accessibility: the root node carries the announcement text via contentDescription (defaults to the page announcement when ariaLabel is unset) and page changes are announced via liveRegion.
LhCarousel(pageCount = 3, ariaLabel = "Promo banner") { page -> BannerPage(page) }States
Controlled and uncontrolled: passing value makes the component controlled — the page index is driven from outside and turning / dragging / autoplay only write the target page back through onChange; without it the component runs itself. The LhCarouselState handle exposes prev() / next() / goTo(index); goTo applies the same normalization as the controlled value (loop wraps, non-loop clamps).
var page by remember { mutableIntStateOf(0) }
LhCarousel(value = page, pageCount = 3, loop = false, onChange = { page = it }) { p -> BannerPage(p) }
val state = remember { LhCarouselState() }
LhCarousel(pageCount = 3, state = state) { p -> BannerPage(p) }
// state.next() / state.prev() / state.goTo(2)API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
modifier | Modifier | Modifier | Modifier |
value | Current page index (controlled; unset means uncontrolled) | Int? | null |
pageCount | Number of pages | Int | 1 |
autoplay | Autoplay interval ms (0 = off); paused on hover and drag | Long | 0 |
loop | Wrap-around; false clamps at boundaries | Boolean | true |
indicator | Indicator style | LhCarouselIndicator | Dots |
indicatorPosition | Indicator position (bottom in-flow / overlay) | LhCarouselIndicatorPosition | Bottom |
height | Height | Dp | carousel.height.base (200dp) |
showArrow | Show left/right arrows (desktop) | Boolean | false |
disabled | Disabled: greys and disables autoplay and gestures | Boolean | false |
ariaLabel | Accessibility name (falls back to the page announcement) | String? | null |
state | Method handle (prev/next/goTo/currentPage) | LhCarouselState? | null |
onChange | Page change callback (payload is the 0-based index) | ((Int) -> Unit)? | null |
content | Page content (payload is the page index) | @Composable (Int) -> Unit | none |
LhCarouselState methods
| Method | Description | Parameter |
|---|---|---|
next() | Next page; no switch at the last page without loop | none |
prev() | Previous page; wraps from the first to the last with loop | none |
goTo(index) | Jump: wraps modularly with loop (negatives too), clamps without | index: Int |
currentPage | Current page index (read-only) | property |
Token mapping
Tokens consumed by the carousel (aligned with tokenRefs of specs/carousel.json):
| Token | Usage |
|---|---|
carousel.height.base | Default content height |
carousel.indicator.dotSize | Indicator dot diameter |
carousel.indicator.dotGap | Indicator dot gap |
carousel.indicator.dotActiveScale | Active dot scale increment (fraction) |
carousel.indicator.overlayBottom | Bottom offset of the overlay indicator |
color.primary.default | Active indicator dot color |
color.text.disabled | Inactive dot color / disabled state |
color.background.default | Arrow button background |
motion.duration.base / motion.easing.easeInOut | Transition animation |
Golden case mapping
The 10 assertions of cases/carousel.json and their local tests:
| case id | Assertion | Local test |
|---|---|---|
renders-pages-and-dots | 3 pages of content: page nodes = 3 and dots = 3 | LhCarouselTest.renders_pages_and_dots (Compose UI Test) |
next-emits-change | next switches the page and emits onChange(1) | LhCarouselTest.next_emits_change |
nonloop-stops-at-last | Without loop, next at the last page does not switch | LhCarouselTest.nonloop_stops_at_last + LhCarouselDefaultsTest.nonloop_stops_at_last |
loop-wraps-negative | With loop, prev from the first page wraps to the last, onChange(2) | LhCarouselTest.loop_wraps_negative + LhCarouselDefaultsTest.loop_wraps_negative |
drag-over-threshold-flips | A drag past 20% of the page width flips | LhCarouselTest.drag_over_threshold_flips_page + LhCarouselDefaultsTest.drag_over_threshold_flips |
flick-velocity-flips | A fast flick flips even without enough distance | LhCarouselTest.flick_velocity_flips_page + LhCarouselDefaultsTest.flick_velocity_flips |
small-drag-keeps-page | Insufficient distance and slow speed keep the page | LhCarouselTest.small_drag_keeps_page + LhCarouselDefaultsTest.small_drag_keeps_page |
autoplay-stops-at-last-nonloop | Autoplay self-stops at the last page (no loop), 2 emissions | LhCarouselTest.autoplay_stops_at_last_nonloop + LhCarouselDefaultsTest.autoplay_steps_like_next |
disabled-blocks-gesture | Disabled state blocks gestures (text.disabled applied) | LhCarouselTest.disabled_blocks_gesture + LhCarouselDefaultsTest.disabled_blocks_gesture |
aria-region-announcement | Accessibility announcement (carousel + page number) | LhCarouselTest.aria_announcement_on_root / aria_defaults_to_page_announcement |
./gradlew :components:carousel:test # JVM part
./gradlew :components:carousel:connectedAndroidTest # Interaction and rendering (device required)
