Carousel
Touch-drag page turning (dual thresholds: distance / velocity), autoplay (paused while dragging or disabled), loop wrap-around with boundary clamping, plus dot indicators and desktop arrows. The instance methods prev / next / goTo are exposed via a LhCarouselHandle handle.
Spec source:
lh-ui-core/specs/carousel.json· Golden cases:lh-ui-core/cases/carousel.json(10 entries, translated into XCTest, runnable on the macOS host)
Paging slot
SwiftUI cannot count direct child nodes, so this end takes the spec's item named-slot form: count + (Int) -> Content, rendering one page per index.
Basic usage
Defaults are loop: true (loop wrap-around), indicator: .dots, and indicatorPosition: .bottom. The height defaults to the carousel.height.base token (200pt).
LhCarousel(count: 3) { index in
ZStack {
Rectangle().fill(Color.gray.opacity(0.15))
Text("Page \(index + 1)")
}
}Drag and autoplay
Drag page turning uses dual thresholds: distance exceeds 20% of the page width, or speed exceeds 0.5px/ms (a fast flick turns the page even when the distance falls short); swiping left goes to the next page, swiping right goes to the previous page; when the distance falls short and the speed is slow, it springs back to the current page. autoplay is the autoplay interval in milliseconds (0 = off); autoplay pauses while dragging and while disabled. With loop: false, boundaries clamp (next/prev do not switch, and autoplay self-stops at the last page).
LhCarousel(count: 4, autoplay: 2500, loop: true) { index in
Text("Page \(index + 1)")
}Indicator
indicator: dots (default) / none (not drawn); indicatorPosition: bottom (in the normal flow) / overlay (overlaid on the content, with the container background taking color.background.default). showArrow: true shows left/right arrows (desktop; the icons go through LhIconMap's arrow-left / arrow-right).
LhCarousel(count: 3, indicatorPosition: .overlay, showArrow: true) { index in
Text("Page \(index + 1)")
}States
disabled grays the component out and stops autoplay and gestures: the indicator dots take color.text.disabled, and neither dragging nor the arrows turn the page. Passing value makes it controlled (the page index is 0-based): page changes are written back through onChange; without it, the component runs itself.
@State private var current = 0
LhCarousel(count: 3, value: current, onChange: { current = $0 }) { index in
Text("Page \(index + 1)")
}API
Props
| Prop | Description | Type | Default |
|---|---|---|---|
count | Page count (item slot: index → page) | Int | — |
value | Current page index (controlled; without it the component runs itself) | Int? | nil |
autoplay | Autoplay interval in ms (0 = off) | Int | 0 |
loop | Loop wrap-around; with false, boundaries clamp | Bool | true |
indicator | Indicator form | LhCarouselIndicator | .dots |
indicatorPosition | Indicator position | LhCarouselIndicatorPosition | .bottom |
height | Height (pt; takes the carousel.height.base token) | CGFloat | 200 |
showArrow | Show left/right arrows (desktop) | Bool | false |
disabled | Disabled: grays out and stops autoplay and gestures | Bool | false |
ariaLabel | Accessibility name (falls back to the "current page / total pages" announcement when unset) | String? | nil |
handle | Handle (prev / next / goTo) | LhCarouselHandle? | nil |
onChange | Page change (covers both uncontrolled self-advance and controlled write-back) | ((Int) -> Void)? | nil |
content | Page content (item slot: (Int) -> Content) | @ViewBuilder | — |
LhCarouselIndicator
| Value | Description |
|---|---|
.dots | Dot indicator (the active dot scales up per carousel.indicator.dotActiveScale) |
.none | No indicator drawn |
LhCarouselIndicatorPosition
| Value | Description |
|---|---|
.bottom | In the normal flow, at the bottom |
.overlay | Overlaid on the content (bottom offset per carousel.indicator.overlayBottom) |
LhCarouselHandle
| Method | Description |
|---|---|
next() | Next page (loop wraps / non-loop does not switch at the last page) |
previous() | Previous page (loop wraps / non-loop does not switch at the first page) |
goTo(_:) | Jump to the given page (out-of-range values wrap or clamp per loop semantics) |
currentIndex | Current page index (0-based, @Published) |
Drag thresholds
| Parameter | Threshold |
|---|---|
| Distance | Exceeds 20% of the page width (abs(deltaX) > pageWidth * 0.2, strictly greater) |
| Speed | Exceeds 0.5 px/ms (abs(velocityX) > 0.5) |
| Direction | Swipe left for the next page, swipe right for the previous page |
Token Reference
| Token | Purpose |
|---|---|
carousel.height.base | Height |
carousel.indicator.dotSize | Indicator dot diameter |
carousel.indicator.dotGap | Indicator dot spacing |
carousel.indicator.dotActiveScale | Active dot scale-up increment (converted to a scale with +1 on device) |
carousel.indicator.overlayBottom | Bottom spacing of the indicator in overlay form |
color.primary.default | Active indicator dot color |
color.text.disabled | Inactive indicator dot color / disabled state |
color.background.default | Overlay indicator container background |
Golden Case Mapping
| case id | Assertion | Local test |
|---|---|---|
renders-pages-and-dots | 3 pages of content: page node count = 3, indicator dot count = 3 | testCarouselInitWithThreePages (render assertions left to manual walkthrough) |
next-emits-change | next turns to the next page and fires the callback (payload=1) | testNextIndexAdvances / testHandleMethodsEmitChange |
nonloop-stops-at-last | With non-loop, next at the last page does not switch | testNonLoopStopsAtLast |
loop-wraps-negative | With loop, prev at the first page wraps to the last page | testLoopWrapsNegative / testHandlePrevWrapsInLoop |
drag-over-threshold-flips | Distance over 20% turns the page | testDragOverThresholdFlips |
flick-velocity-flips | Speed over 0.5px/ms turns the page | testFlickVelocityFlips |
small-drag-keeps-page | Insufficient distance and slow speed keep the current page | testSmallDragKeepsPage |
autoplay-stops-at-last-nonloop | Autoplay self-stops at the last page (non-loop) | testAutoplayStopsAtLastNonLoop |
disabled-blocks-gesture | In the disabled state, gestures do not turn the page (color.text.disabled) | testDisabledAppearanceUsesTokens |
aria-region-announcement | The accessibility name falls back to "current page / total pages" | testAriaLabelFallbackToPageIndex |
Plus dedicated tests: testNextIndexWrapsInLoop / testNonLoopStopsAtFirst / testSwipeWithZeroPageWidthIsNil / testAutoplayWrapsInLoop / testLayoutUsesCarouselTokens / testIndicatorNoneHidesDots / testDarkColorsComeFromDarkTokens / testDimensionsAreIdenticalInDarkMode / testHandleGoToNormalizesOutOfRange / testCarouselInitWithFullProps / testCarouselWithZeroCountNormalizes / testDefaultPropsMatchSpec.
swift test --filter LhCarouselTests # Runs on the macOS host, no simulator required
