--- name: compose-design-system-component description: 'Use when creating or changing a reusable Compose Multiplatform design-system component — button, text field, chip, card, badge, avatar, dialog — or theme tokens: variant and size enums, enabled/disabled/loading/error states, CompositionLocal theming, light/dark previews, accessibility semantics, testTag hooks, component gallery. Also for hardcoded colors or dp in components, or a component that ignores dark mode.' license: Apache-2.0 compatibility: Kotlin Multiplatform project with Compose Multiplatform 1.8+ (commonMain UI). Component UI tests need an iOS simulator or an Android device/emulator. metadata: category: ui platforms: android, ios --- # Compose Multiplatform design-system component A design-system component takes **every** visual value from theme tokens, exposes a small fixed set of variants and sizes, handles each interaction state explicitly, is accessible without extra work at the call site, and is visible in previews and the component gallery before any screen uses it. ## When to use - Adding a reusable UI primitive to the shared design-system module (or `ui/components` package). - Adding a variant, size or state (loading, error) to an existing component. - Adding or restructuring theme tokens (colours, typography, spacing, shapes) and the theme entry. - Symptoms: hex colours or raw `dp` inside components, dark mode wrong in one component, a component unreadable at large font sizes, QA cannot locate an inner element. ## When not to use - A layout used by one screen only → build it inside that screen (`compose-multiplatform-screen`). - Implementing a Figma frame → `figma-to-mobile-ui` (it calls back here when a component is missing). - SwiftUI components for a native iOS UI → SwiftUI skills in Related; the token rules still apply. ## Step 0 — Read the project first 1. **Where components live.** A design-system module (`:designsystem`, `:core:ui`, …) or a package in `:composeApp`/`:shared`. Write only there. 2. **Theme entry and tokens.** Find the theme composable (`AppTheme { }` or similar) and how tokens are read: `AppTheme.colors.*`, `MaterialTheme.colorScheme.*` plus extensions, plain `object`s. List the colour/typography/spacing/shape token names available. **Never invent token names.** 3. **Material 3 or custom.** Do components wrap Material 3 (`Button`, `OutlinedTextField`) or draw from foundation primitives? Match the existing ones. 4. **Existing components.** Search for the component and its synonyms (`Button`, `Cta`, `Action`). If one exists, extend it with a variant instead of adding a sibling. 5. **Conventions.** Name prefix (`App`, a brand prefix, none), parameter order, `Defaults`/`Colors` objects, preview import (`androidx.compose.ui.tooling.preview.Preview` on Compose Multiplatform 1.10+, `org.jetbrains.compose.ui.tooling.preview.Preview` before), gallery/showcase app and how entries are registered. 6. **Resources.** Is the component allowed to own strings (`composeResources` in the design-system module, with `publicResClass` if consumers need them)? With no design system at all, create the theme first: [references/theme-and-tokens.md](references/theme-and-tokens.md). ## Workflow ### 1. Decide the API before writing pixels ```kotlin @Composable fun AppButton( text: String, // required content first onClick: () -> Unit, modifier: Modifier = Modifier, // first optional parameter variant: AppButtonVariant = AppButtonVariant.Primary, size: AppButtonSize = AppButtonSize.Medium, enabled: Boolean = true, loading: Boolean = false, leadingIcon: (@Composable () -> Unit)? = null, // slot, not an icon enum colors: AppButtonColors = AppButtonDefaults.colors(variant), interactionSource: MutableInteractionSource? = null, ) ``` - Variants and sizes are `enum`s with the values design actually uses — not booleans (`isPrimary`, `isLarge`) that multiply into invalid combinations. - Content that varies in shape is a **slot** (`@Composable () -> Unit`); content that is just text is a `String`. - No domain types (`Note`, `Order`) and no ViewModels: the component must be usable from any feature. - Colour sets resolved per variant live in an `@Immutable` `XColors` class, built by `XDefaults`. Done when the signature reads well at three call sites you write down first. ### 2. Resolve every visual from tokens | Must use | Must not use | |---|---| | `AppTheme.colors.` (or the project's colour accessor) | `Color(0xFF…)`, `Color.Red`, `Color.White` | | `AppTheme.spacing.` | `8.dp`, `16.dp` padding/gaps | | `AppTheme.shapes.` | `RoundedCornerShape(12.dp)` inline | | `AppTheme.typography.