Skip to main content

CamoSwitch

Concept​

1. Why​

A switch looks like a checkbox in code (a boolean in, a callback out) but means something different. A switch applies immediately (Wi-Fi on), while a checkbox selects for later (in a form that is submitted). It's also where skins differ most visibly in motion: Material's thumb grows, and an iOS switch slides a white knob. Like the checkbox, it takes an optional label value and then builds the whole settings row.

2. Shape​

Material mapping preview

StateTrackThumb
offsurfaceContainerHighest fill + outline borderoutline, small (16)
onprimaryonPrimary, large (24)
pressedthe samegrows to 28
disabledonSurface at the disabled alphasthe same, dimmed

With a label, it's a settings row: the label (bodyLarge, onSurface) at the start and the switch at the end. See Material Skin.

3. API​

Contract​

fun CamoSwitch(
checked: Boolean,
onCheckedChange: ((Boolean) -> Unit)?, // null → display-only
label: String? = null, // non-null → the whole row is the target
labelSpans: List<CamoSpan>? = null, // rich label (links, bold); use instead of label
description: String? = null, // a second line under the label ("Uses more battery")
error: String? = null, // shown below the row (used by CamoSwitchFormField)
enabled: Boolean = true,
)

State → renderer: SwitchState(checked, label: String or List<CamoSpan>?, description?, error?, enabled, interaction). Slots: none.

Tokens used​

  • colors.primary, onPrimary, surfaceContainerHighest, outline, onSurface (label and disabled)
  • shapes.full for the track and thumb
  • track 52 × 32, inside a 48-high target
  • typography.bodyLarge (label), spacing.l between the label and the switch
  • motion.durationShort, easingStandard

Component theme​

theme.components.switch: SwitchTheme?. Every field is nullable in the theme. The skin's defaults(theme) fills whatever isn't set (Material defaults shown). See Theme §3.10.

FieldTypeMaterial defaultWhat it's for
showThumbIconsBooleanfalsean icon inside the thumb (a check when on, optionally a cross when off). Any skin can draw it (ADR-28); the Material skin defaults it to true.
thumbIconsThumbIcons(checked: IconSource, unchecked: IconSource?)the skin's check / nonethe brand's own icons for the thumb
trackWidth / trackHeightDp52 / 32track size
thumbSizeOff / thumbSizeOn / thumbSizePressedDp16 / 24 / 28thumb sizes per state
labelStyleCamoTextStyletypography.bodyLargethe label
labelPositionLabelPosition (Start | End)Start (the label before the switch)which side the label sits on (ADR-28)
labelGapDpspacing.l (16)space between label and switch
descriptionStyleCamoTextStyletypography.bodyMedium in onSurfaceVariantthe description line
colorsSwitchColors(trackOn, trackOff, thumbOn, thumbOff, trackOffBorder, label)primary, surfaceContainerHighest, onPrimary, outline, outline, onSurfaceevery color the switch uses

Accessibility​

  • Role switch, with on/off announced ("Wi-Fi, switch, on"), not "checked".
  • With a label: one element, and the whole row is the target.
  • Display-only: no semantics of its own.
  • Space toggles it.
  • The state must be distinguishable without color (WCAG 1.4.1): the thumb position and size do this.

4. Build steps​

  1. Core: toggleable wiring (the whole row when labelled), switch role, name = label, the 48 target, interaction tracking (including thumb drag → interaction.dragged), skin.switch.render.
  2. DebugSkin renderer: the label, then a rectangle with a square on the left or right, with no animation.
  3. Sample: a settings list with three labelled switches, one of them disabled.
  4. Tests:
    • tapping the label toggles it;
    • dragging the thumb past the middle toggles it; a short drag springs back (RTL: the directions mirror);
    • it's announced as a switch with on/off;
    • display-only has no own semantics;
    • under reduced motion the thumb moves instantly.

Done when

  • Settings rows toggle and announce as switches.
  • Under reduced motion the thumb jumps instead of sliding.

5. Edge cases​

CaseDecided behavior
Dragging the thumbSupported in v1 (settled 2026-09-27). The component tracks the drag and sets interaction.dragged; the renderer draws the thumb following the finger. Releasing past the middle toggles (calls onCheckedChange), otherwise it springs back. A tap still toggles.
RTL"On" is the end side. The thumb goes left when on in RTL, and the label moves to the right.
Change needs confirmationThe app shows a dialog from onCheckedChange and updates checked only on confirm.
Async toggles (a network call)The app keeps checked unchanged and sets enabled = false while pending.
Long label at 200% font scaleThe label wraps. The switch stays vertically centered at the end.
A description under the label ("Uses more battery")description (settled 2026-09-27). It's announced as the element's description. The switch stays vertically centered on the label + description block.

Implementation​

1. Why​

It's the same wiring as the checkbox, but with Role.Switch (so it's announced as on/off) and a thumb animation driven by the theme's motion tokens. A labelled switch is a settings row: the label at the start, the switch at the end.

2. Shape​

3. API​

@Composable
public fun CamoSwitch(
checked: Boolean,
onCheckedChange: ((Boolean) -> Unit)?,
modifier: Modifier = Modifier,
label: String? = null,
labelSpans: List<CamoSpan>? = null,
description: String? = null,
error: String? = null,
enabled: Boolean = true,
interactionSource: MutableInteractionSource? = null,
)
  • SwitchTheme / SwitchStyle follow the base table (track size, three thumb sizes, label style, gap, SwitchColors).
  • Thumb position: if (checked) trackWidth - thumbSize - inset else inset, measured from the start, so RTL flips it through Modifier.offset in a layout that respects LocalLayoutDirection (use absoluteOffset never).

Label side and description: the row reads style.labelPosition (Start default | End) and orders the label column and the switch accordingly; description renders under the label with style.descriptionStyle and is merged into the row's single node, which reads "Wi-Fi, switch, on, Uses more battery".

4. Build steps​

  1. Types, the component, and the DebugSkin renderer (no animation).
  2. commonTest:
    • Role.Switch is present, and assertIsOn() after a tap;
    • under reduced motion (a CamouflageProvider in a test that forces motion.reduced()), the thumb reaches its end position in the first frame;
    • in RTL, an "on" thumb is on the left.

Done when

  • A settings list of switches works and is announced as switches on Android and iOS.

5. Edge cases​

CaseDecided behavior
Drag the thumb (ADR-25)Modifier.draggable(orientation = Horizontal, interactionSource = source) on the thumb: the offset follows the finger (clamped to the track), DragInteraction sets interaction.dragged, and on stop the switch toggles if the thumb passed the middle, otherwise it animates back. Tap still toggles.
Thumb iconsstyle.showThumbIcons draws style.thumbIcons.checked (and unchecked, if set) inside the thumb with CamoIcon.
animateDpAsState with duration 0Snaps immediately, which is correct for reduced motion.
Rapid togglesEach tap animates from the current position (Compose animations are interruptible).