Pantrix UI

Tri-state Checkbox

A checkbox with a third state: checked, not checked, or some of both.

import com.pantrixui.components.elements.checkbox.PantrixTriStateCheckbox

Examples

Three states: select all

PantrixTriStateCheckbox draws a third state. While some of the boxes under it are selected, "All notifications" is neither checked nor unchecked: it shows a dash, and that is how a screen reader reads it.

The caller works out the state, not the box: On if all are selected, Off if none is selected, Indeterminate in between. onClick only reports that it was pressed; here it selects them all, and when all are selected it clears them all.

val notifications = listOf("Shipping status", "Promotions", "Account security")
var chosen by remember { mutableStateOf(setOf(notifications.first())) }
val all = when (chosen.size) {
    0 -> ToggleableState.Off
    notifications.size -> ToggleableState.On
    else -> ToggleableState.Indeterminate
}
Column(verticalArrangement = Arrangement.spacedBy(PantrixTheme.spacing.xs)) {
    PantrixTriStateCheckbox(
        state = all,
        onClick = { chosen = if (all == ToggleableState.On) emptySet() else notifications.toSet() },
        label = "All notifications",
        tagKey = "all_notifications"
    )
    Column(modifier = Modifier.padding(start = PantrixTheme.spacing.xl)) {
        notifications.forEachIndexed { index, name ->
            PantrixCheckbox(
                checked = name in chosen,
                onCheckedChange = { on -> chosen = if (on) chosen + name else chosen - name },
                label = name,
                tagKey = "notification_$index"
            )
        }
    }
}

API

state

A checkbox with a third state: checked, not checked, or some of both.

ParameterTypeDefaultWhat it is
stateToggleableStatechecked, not checked, or indeterminate.
onClick(() -> Unit)?called when the user presses the checkbox. Null makes it read-only.
modifierModifierModifierthe row's modifier.
enabledBooleantruewhether the checkbox can be pressed. One that cannot is drawn in the disabled colours and says so.
labelString?nullthe text beside the box: part of the target, and the checkbox's name for a screen reader.
supportingTextString?nullhelp under the label, in a smaller type: the message, while error is true.
errorBooleanfalsewhether the value is wrong: the box and the supporting text turn to the danger role, and a screen reader is told the input is invalid.
styleCheckboxStyle?nullthis call's own style: over every enclosing one and under the density. Only the fields it sets change, on every platform.
colorsCheckboxColors?nullthis checkbox's own colours: the ones it sets, with the theme's for the rest. A disabled box and one in error keep the theme's.
contentDescriptionString?nullwhat a screen reader says for the checkbox where it has no label, or in place of it.
tagKeyString?nullthe checkbox's automation key: the row is Checkbox_<tagKey>.
interactionSourceMutableInteractionSource?nullthe checkbox's interactions, hoisted. It keeps a source of its own when this is null.
controlPositionControlPositionControlPosition.Startwhere the box stands: before the label, as a list of choices has it, or after it. After it, the box is at the end of the width the row is given, Modifier.fillMaxWidth() lining it up with the others, or right after the label where the row is given none; the label wraps in what the box leaves.

CheckboxStyle

PantrixCheckbox's box: its size, its corners and its edge.

ParameterTypeDefaultWhat it is
sizeDpDp.UnspecifiedThe box's width and height, its edge included.
shapeShapeSpecShapeSpec.UnspecifiedThe box's corners: a step of the brand's shape scale, so a brand with square corners has square boxes.
strokeWidthDpDp.UnspecifiedHow thick the box's edge is, and the tick's and the dash's line.

CheckboxColors

What PantrixCheckbox is painted with, for a checkbox that does not take the theme's: a colour that is set is used, and one that is left out stays the theme's role. A product gives the colours it changes and no others:

ParameterTypeDefaultWhat it is
checkedColorColor.UnspecifiedThe box's fill when it is checked or indeterminate. The theme's primary.
checkmarkColorColor.UnspecifiedThe tick and the dash on the fill: the colour that reads on checked. The primary role's onColor.
borderColorColor.UnspecifiedThe box's edge when it is not checked. The theme's borderStrong: an empty box is nothing but its edge.

Accessibility