compose-modifier-and-layout-style
Compose modifier and layout style
Core principle
A composable that emits layout is a leaf the parent places — the parent decides position, size, alignment, padding. The composable's job is structure (what's inside), not placement (where it goes). Three rules follow:
- Declare a
modifierparameter and apply it to the root, so the parent can actually do its job. Hardcoding.fillMaxWidth()on a composable's root takes that decision away from every future caller. - Construct modifier chains as one fluent expression, not stepwise reassignments. Both compile to the same thing, but the chain reads as intent in one pass.
- Conditional rendering belongs where the condition applies. A layout call whose only content is one
ifexists solely to hold the condition — push theifoutside instead.
These travel together because the same composable usually triggers all three: you declare its parameters (rule 1), the caller constructs a chain to position it (rules 2), and the body has a conditional you might be tempted to wrap (rule 3).
When to use this skill
- You're writing a
@Composable funthat calls a layout (Box,Column,Row,LazyColumn,Text,Image,Surface,Card,Layout { … }, anything fromcompose.foundation.layoutorcompose.material*) and its signature has nomodifierparameter, or has one that isn't applied to the root, or has a hardcoded.fillMaxWidth()/.padding(...)on the root. - You see
var m = Modifierfollowed bym = m.padding(…),m = m.background(…), etc. - A
modifier = …argument has three or more chained calls on a single line. - A composable's body is
Layout { if (cond) Content() }— one conditional, nothing else.
More from chrisbanes/skills
compose-ui-testing-patterns
Use when writing or reviewing Jetpack Compose UI tests, screenshot tests, previews, semantics assertions, fake image loading, keyboard input, focus assertions, interaction state (hover/pressed/focused), or tests for plain state-driven UI composables.
168kotlin-coroutines-structured-concurrency
Use when writing or reviewing Kotlin code that stores CoroutineScope, launches from init/non-suspending APIs, calls runBlocking, or catches broad exceptions around suspend calls.
168compose-side-effects
Use when writing or reviewing Jetpack Compose code with LaunchedEffect, DisposableEffect, SideEffect, rememberCoroutineScope, rememberUpdatedState, snapshotFlow, snackbar, navigation, focus requests, analytics, or event Flow collection.
167compose-stability-diagnostics
Use when writing or reviewing Jetpack Compose parameter stability, compiler reports, skippability, unstable UI state classes, collection parameters, or Kotlin 2.0+ strong skipping behavior.
166compose-state-authoring
Use when writing or reviewing Jetpack Compose code with bare local var in a @Composable, remember { mutableStateOf(...) }, mutableStateListOf/mutableStateMapOf, or @ReadOnlyComposable.
165compose-state-deferred-reads
Use when Jetpack Compose code reads scroll, animation, gesture, or other frame-rate State in composition, passes changing values across composable boundaries, or uses value-form layout/draw modifiers.
165