CamoSlider
Concept
1. Why
A slider picks a value from a continuous or stepped range: volume, brightness, a price filter. It's controlled like every input (value in, change out), and reports when the user finishes dragging, so apps can save once instead of on every frame.
2. Shape
Material mapping preview: the M3 slider (2024 update).
- A 16 dp tall track with rounded ends.
- The active part is
primary, and the inactive partsecondaryContainer. - The handle is a vertical 4 × 44 bar in
primarythat narrows while pressed. - A value label appears above the handle while dragging.
3. API
Contract
fun CamoSlider(
value: Float,
onValueChange: (Float) -> Unit,
range: ClosedRange<Float> = 0f..1f,
steps: Int = 0, // 0 = continuous; n = n stops between the ends
onValueChangeFinished: (() -> Unit)? = null,
valueLabel: ((Float) -> String)? = null, // text for the drag label and for screen readers
label: String? = null, // accessible name ("Volume")
enabled: Boolean = true,
)
State → renderer: SliderState(fraction: Float, steps, enabled, valueText: String?, interaction) (dragging is interaction.dragged). Slots: none.
Tokens used
colors.primary,secondaryContainer,onPrimary(stop marks),inverseSurface/inverseOnSurface(value label)shapes.full,typography.labelLarge(value label)motion.durationShort(handle width change)
Component theme
theme.components.slider: SliderTheme?. Every field is nullable in the theme. The skin's defaults(theme) fills whatever isn't set (Material defaults shown). See Theme §3.10.
| Field | Type | Material default | What it's for |
|---|---|---|---|
trackHeight | Dp | 16 | track thickness |
thumbSize | Size | 4 × 44 | the thumb (a circle when square, a bar otherwise); core maps touches on a track inset by half its width |
showStopMarks | Boolean | true | dots at each step |
showValueLabel | Boolean | true | label above the handle while dragging |
colors | SliderColors(activeTrack, inactiveTrack, handle, stopMark, valueLabel, valueLabelText) | primary, secondaryContainer, primary, onPrimary/onSecondaryContainer, inverseSurface, inverseOnSurface | every color it uses |
Accessibility
- An adjustable element with name =
labeland value =valueLabel(value)(or a percentage). - Screen-reader swipe up/down (Android), and the adjust gesture (iOS), move one step, or 5% when continuous.
- Keyboard: arrows move one step; Page Up/Down move 10%; Home/End jump to the ends.
- The touch target is the full track height, at least 48, and a tap anywhere on the track jumps to that value.
4. Build steps
- Core:
- value ↔ fraction, with snapping to steps;
- drag and tap gestures;
- keyboard;
- semantics (range info, set progress action);
onValueChangeFinishedon release and after keyboard changes.
SliderRenderer, and the DebugSkin renderer.- Tests:
- dragging changes the value and snaps to steps;
onValueChangeFinishedfires once per drag;- the arrow keys and Home/End;
- the accessibility value text;
- RTL: dragging right lowers the value.
Done when
- A volume slider (continuous) and a rating slider (steps = 3) work by touch, mouse, keyboard and screen reader.
5. Edge cases
| Case | Decided behavior |
|---|---|
value outside range | Clamped for drawing. The app's value isn't changed. |
| RTL | The minimum is on the right. |
| Range slider (two handles) | Not in v1. It's a later CamoRangeSlider with the same tokens. |
| Vertical slider | Not in v1. |
| Reduced motion | No handle-width animation. The value label appears instantly. |
Implementation
1. Why
Compose's Slider lives in Material, which v1 doesn't depend on. So core builds the slider from foundation pieces: a drag gesture, keyboard steps, progress semantics with a setProgress action, and interaction.dragged (ADR-25) reported to the renderer.
2. Shape
3. API
@Composable
public fun CamoSlider(
value: Float,
onValueChange: (Float) -> Unit,
modifier: Modifier = Modifier,
range: ClosedFloatingPointRange<Float> = 0f..1f,
steps: Int = 0,
onValueChangeFinished: (() -> Unit)? = null,
valueLabel: ((Float) -> String)? = null,
label: String? = null,
enabled: Boolean = true,
interactionSource: MutableInteractionSource? = null,
)
Gesture: Modifier.pointerInput(range, steps) with awaitEachGesture: a down event jumps the thumb to the touch point, then horizontalDrag updates it; a DragInteraction.Start/Stop is emitted on the interactionSource, so collectIsDraggedAsState() feeds InteractionState.dragged. On release, onValueChangeFinished fires once. The position → value math mirrors in RTL (LocalLayoutDirection), and stepped sliders snap with ((f * (steps + 1)).roundToInt() / (steps + 1f)).
Keyboard: focusable() + onKeyEvent: arrows move one step (or 1% when continuous), PageUp/PageDown 10%, Home/End to the ends; each key press also calls onValueChangeFinished.
Semantics:
Modifier.semantics {
contentDescription = label ?: ""
progressBarRangeInfo = ProgressBarRangeInfo(value, range, steps)
stateDescription = valueLabel?.invoke(value) ?: ""
setProgress { target -> onValueChange(target.coerceIn(range)); onValueChangeFinished?.invoke(); true }
}
TalkBack's volume keys and VoiceOver's swipe up/down go through setProgress.
4. Build steps
SliderTheme/SliderStyle,CamoSliderwith the gesture, keys and semantics; the DebugSkin renderer (a line and a square thumb).commonTest: drag and tap set values and snap to steps;onValueChangeFinishedfires once per gesture and per key; RTL inverts;setProgressworks;interaction.draggedis true only while dragging.
Done when
- A volume slider and a stepped price filter work by touch, mouse, keyboard and screen reader, and the value label shows only while dragging (Minimal).
5. Edge cases
| Case | Decided behavior |
|---|---|
| Inside a horizontally scrolling container | The slider claims horizontal drags once they pass touch slop; the pager keeps vertical and fling gestures. |
value outside range | Drawn clamped; onValueChange isn't called for the clamp. |
| Range slider (two thumbs) | Not in v1. |