Carousel
Touch dragging (displacement / velocity dual thresholds), autoplay (paused during hover and drag), looping wrap and boundary clamping, dots indicator and arrows. Instance methods prev / next / goTo are exposed as a handle.
Spec source:
lh-ui-core/specs/carousel.json· Golden cases:lh-ui-core/cases/carousel.json(10 cases, translated into hypium tests)
Device-side implementation note
The spec's behaviors explicitly allow mobile ends to wrap the native swiper — page turning is carried by the native ArkUI Swiper (value → index, loop → loop; gestures and wrap handled natively), while autoplay is driven by the component's own setTimeout to precisely control "pause during drag and hover" and "stop at the last page when not looping". The device-side counterparts of the spec's default / item slots are itemCount (page count) + itemBuilder (render each page by index):
@Builder
carouselPage(index: number) {
Stack() {
Text(`Page ${index + 1}`)
}
.width('100%').height('100%').backgroundColor(this.colors.primary)
}
build() {
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, lhHeight: 160 })
}The page math (normalizePageIndex / nextPageIndex / prevPageIndex / resolveSwipeTarget / stepAutoplay) is the cross-platform pure-function source of truth (LhCarouselTokens) and matches the drag dual thresholds (displacement over 20% of page width or velocity over 0.5px/ms). The spec's height prop is renamed lhHeight per the D26 common-attribute-name aversion.
Basic usage
itemCount sets the page count and itemBuilder renders each page; looping wrap and dots indicator are on by default (indicatorPosition = bottom occupies the document flow).
The buttons call prev / next / goTo through LhCarouselController (golden case next-emits-change)
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, lhHeight: 160 })Indicator and position
indicator takes dots / none; indicatorPosition takes bottom (document flow, below the content) or overlay (over the content, carousel.indicator.overlayBottom from the bottom). The active dot is scaled up by carousel.indicator.dotActiveScale (takes color.primary.default; inactive takes color.text.disabled).
Top: overlay indicator (over the content); bottom: indicator=none
LhCarousel({
itemCount: 3,
itemBuilder: this.carouselPage,
indicatorPosition: LhCarouselIndicatorPosition.Overlay,
lhHeight: 160,
})Autoplay and loop
autoplay is the interval in ms (0 or absent = no autoplay); it pauses during drag and hover. With loop = true the page index wraps modulo (negative wraps too); with loop = false the index clamps at the boundary and autoplay stops at the last page (golden case autoplay-stops-at-last-nonloop).
Drag thresholds: displacement over 20% of page width or velocity over 0.5px/ms (golden cases drag-over-threshold-flips / flick-velocity-flips)
// Autoplay + wrap
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, autoplay: 2000 })
// Non-loop: stops at the last page (prev/next clamp at the boundary)
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, autoplay: 1000, loop: false })Arrows and methods
showArrow = true shows left / right arrows (desktop form; clicking calls prev / next); disabled = true greys and disables autoplay and gestures (golden case disabled-blocks-gesture).
disabled greys and disables autoplay and gestures
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, showArrow: true, controller: this.carouselCtl })
// Handle methods
this.carouselCtl.prev() // previous page (first wraps to last when looping)
this.carouselCtl.next() // next page
this.carouselCtl.goTo(2) // jump to page 3 (0-based)API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
value | Current page index (controlled; 0-based; absent means uncontrolled self-running) | number | — |
autoplay | Autoplay interval ms (0 or absent = no autoplay); paused during hover and drag | number | 0 |
loop | Loop wrap; false clamps at the boundary (next/prev do not switch) | boolean | true |
indicator | Indicator form | LhCarouselIndicator | Dots |
indicatorPosition | Indicator position: bottom document flow / overlay over the content | LhCarouselIndicatorPosition | Bottom |
lhHeight | Height (vp; spec prop height, renamed per D26) | number | 200 |
showArrow | Show left / right arrows (desktop) | boolean | false |
disabled | Disabled: greys and disables autoplay and gestures | boolean | false |
itemCount | Page count (device-side counterpart of the spec slots default/item) | number | 0 |
itemBuilder | Page content builder (device-side counterpart of the item slot) | (index: number) => void | — |
ariaLabel | Accessibility name (falls back to the page announcement text) | string | — |
onChange | Page switch (including uncontrolled self-running and controlled re-writes) | (value: number) => void | — |
controller | Host handle (plain property, not @Prop) | LhCarouselController | — |
LhCarouselIndicator
| Value | Description |
|---|---|
Dots | Dots indicator (default) |
None | No indicator |
LhCarouselIndicatorPosition
| Value | Description |
|---|---|
Bottom | Document flow (below the content) |
Overlay | Over the content (carousel.indicator.overlayBottom from the bottom) |
LhCarouselController
| Method | Description |
|---|---|
prev() | Previous page (first wraps to last when looping) |
next() | Next page (last wraps to first when looping) |
goTo(index) | Jump to the given page (0-based; out-of-range normalized per loop semantics) |
Tokens
| Token | Usage |
|---|---|
carousel.height.base | Default height (200vp) |
carousel.indicator.dotSize | Indicator dot diameter |
carousel.indicator.dotGap | Indicator dot gap |
carousel.indicator.dotActiveScale | Active dot scale increment (fraction 0.25) |
carousel.indicator.overlayBottom | Indicator bottom offset in overlay form |
color.primary.default | Active dot / arrow color |
color.text.disabled | Inactive dot / disabled arrow color |
color.background.default | Page background color |
Golden cases
The 10 assertions of cases/carousel.json mapped to hypium tests (entry/src/test/Carousel.test.ets):
| case id | Assertion | Local test |
|---|---|---|
renders-pages-and-dots | Renders pages and dots by the page count | renders_pages_and_dots |
next-emits-change | next switches to the next page and emits | next_emits_change |
nonloop-stops-at-last | Non-loop at the last page does not switch | nonloop_stops_at_last |
loop-wraps-negative | Looping wraps the first page's prev to the last | loop_wraps_negative |
drag-over-threshold-flips | Drag past the threshold (20% of page width) flips the page | drag_over_threshold_flips |
flick-velocity-flips | A fast flick (over the velocity threshold) flips even with small displacement | flick_velocity_flips |
small-drag-keeps-page | Small displacement and slow velocity keep the current page | small_drag_keeps_page |
autoplay-stops-at-last-nonloop | Autoplay stops at the last page (non-loop) | autoplay_stops_at_last_nonloop |
disabled-blocks-gesture | Disabled state does not flip on gesture | disabled_blocks_gesture |
aria-region-announcement | role=region + carousel announcement + page number | aria_region_announcement (accessibility-tree assertion verified on device) |
Additional dark-mode cases: dark_colors_come_from_dark_tokens / dimensions_are_identical_in_dark_mode, plus indicator_enums_match_spec / invalid_values_converge. Run: DevEco Studio, right-click entry/src/test → Run Tests.

