JetBrains/intellij-community / .claude/skills/compose-ui
compose-ui
VerifiedWrite or audit Compose Desktop UI state, effects, and boundaries.
audit/references/modifiers/custom-modifier-node.md
View on GitHub# Custom Modifier.Node
`Modifier.Node` is the lowest-overhead way to write stateful custom modifiers. It replaces the
older `Modifier.composed { }` and `LayoutModifier` interfaces.
## When to use Modifier.Node
| Scenario | Use `Modifier.Node`? |
|----------|---------------------|
| Stateless transformation (e.g. add padding) | No — use extension function. |
| Needs `LocalDensity` or theme value once | No — use `composed { }`. |
| Observes a `State<T>` that changes frequently | Yes — `Modifier.Node` avoids recomposition. |
| Custom draw logic that depends on changing state | Yes — use `DrawModifierNode`. |
| Custom layout logic that depends on changing state | Yes — use `LayoutModifierNode`. |
| Custom pointer input | Yes — use `PointerInputModifierNode`. |
## Minimal example: DrawModifierNode
The element and the node are required together. The element is the immutable description; the
node is the mutable runtime instance that survives recompositions.
```kotlin
fun Modifier.fade(alpha: State<Float>): Modifier = this.then(FadeNodeElement(alpha))
private data class FadeNodeElement(
val alpha: State<Float>,
) : ModifierNodeElement<FadeNode>() {
override fun create() = FadeNode(alpha)
override fun update(node: FadeNode) {
node.alpha = alpha
}
}
private class FadeNode(
var alpha: State<Float>,
) : Modifier.Node(), DrawModifierNode {
override fun ContentDrawScope.draw() {
// Reading alpha.value here is a draw-phase read.
// Compose re-draws when the state changes — no recomposition.
val currentAlpha = alpha.value
if (currentAlpha >= 1f) {
drawContent()
} else {
drawContext.canvas.saveLayer(
bounds = size.toRect(),
paint = Paint().apply { this.alpha = currentAlpha },
)
drawContent()
drawContext.canvas.restore()
}
}
}
```
For simple properties (alpha, rotation, scale, translation), prefer the `graphicsLayer` shortcut
over a full custom node:
```kotlin
// Simpler for common visual properties — no node boilerplate needed
fun Modifier.fade(alpha: State<Float>): Modifier =
graphicsLayer { this.alpha = alpha.value }
```
## Key concepts
- **`ModifierNodeElement`** — the immutable description of the modifier. Compose uses `equals`
and `hashCode` to decide whether to update or replace the node. Always implement both on
data classes or explicitly.
- **`Modifier.Node()`** — the mutable runtime instance. Lives across recompositions if the
element is equal.
- **`update()`** — called when the element changes but the node is reused. Update internal state
here; do not allocate new objects.
- **`onAttach()` / `onDetach()`** — lifecycle callbacks. Use for registering/unregistering
observers.
## Node types
| Type | Use for |
|------|---------|
| `DrawModifierNode` | Custom `drawBehind`, `drawWithContent`, alpha, rotation. |
| `LayoutModifierNode` | Custom measure/place logic. |
| `PointerInputModifierNode` | Custom gesture handling. |
| `SemanticsModifierNode` | Accessibility semantics. |
## Common mistakes
- **Forgetting `hashCode`/`equals` on the element.** Compose cannot diff modifiers; it recreates
the node on every recomposition.
- **Allocating in `update()`.** `update()` should mutate the node, not create new objects.
- **Reading `State.value` in `create()`.** The state may change; store the `State` object and read
`.value` in `draw()` or `measure()`.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |