skydoves/compose-performance-skills/migrating-to-modifier-node
Claude Code and agent skill by skydoves/compose-performance-skills
- community
- ext:[{"type":"hook","description":"Pre/post lifecycle hooks"}]
Checking that the SKILL.md file is still in the repository.
Use this skill to author new custom Jetpack Compose modifiers and migrate legacy ones from Modifier.composed { } to Modifier.Node + ModifierNodeElement<T>. Covers the persistent-node lifecycle (onAttach, onDetach, onReset, coroutineScope), the specialized node interfaces (DrawModifierNode, LayoutModifierNode, SemanticsModifierNode, PointerInputModifierNode, CompositionLocalConsumerModifierNode, LayoutAwareModifierNode, GlobalPositionAwareModifierNode, ObserverModifierNode, DelegatingNode, TraversableNode), why ModifierNodeElement MUST be a data class for diffing, and the manual-invalidation knobs (invalidateDraw, invalidateMeasurement, invalidatePlacement, shouldAutoInvalidate). Use when the developer mentions Modifier.composed, custom modifier, ModifierNodeElement, Modifier.Node, "rewriting our drawBehind helper", node lifecycle, or sees Modifier.composed { } in a code review.
Install
npx vskill@latest install skydoves/compose-performance-skills/migrating-to-modifier-nodeSemi-extensibleMentions customization, but needs skill-specific knowledge. Read the extensible skills standard
SKILL.md
The same SKILL.md was published earlier as skydoves/compose-performance-skills/migrating-to-modifier-node.
Migrating to Modifier.Node — Persistent Nodes Over composed { }
Modifier.composed { } allocates a fresh composable scope per modifier per composition: it cannot be skipped, cannot be hoisted, and forces the parent to run on every recomposition. Modifier.Node is a persistent node diffed by ModifierNodeElement.equals() — created once on first apply, updated in place on subsequent applies. There is no per-recomposition allocation, no fresh composable scope, and no parent invalidation chain, which is why Android Developers describes the system as "designed from the ground up to be far more performant" than the legacy composed { } factory (see developer.android.com/develop/ui/compose/custom-modifiers). This skill teaches Claude how to author new modifiers as Modifier.Node and migrate legacy composed { } factories.
When to use this skill
- A custom modifier currently uses
Modifier.composed { }(search the module forModifier.composed). - Authoring a new custom modifier from scratch — never start with
composed { }. - A code review surfaces a
composed { }factory. - The custom modifier needs a
CoroutineScope(animation loop, debouncer), reads aCompositionLocal, participates in layout / drawing / pointer input, or tracks layout coordinates. - A
@TraceRecompositionlog shows the parent composable recomposing on every frame because acomposed { }modifier is in the chain.
When NOT to use this skill
- The "modifier" is actually a one-line composable wrapper that can stay a
@Composablefunction — leave it alone. - The built-in modifier composition (
Modifier.padding(...).clickable(...)) is sufficient, no custom node behavior needed. - The fix the developer needs is reordering an existing chain, not authoring a new node — see
../ordering-modifier-chains/SKILL.md. - The custom modifier reads a hot animation value via
Modifier.composed { }only to feed a value-form modifier underneath — the underlying issue is a wrong-phase state read; see../../recomposition/deferring-state-reads/SKILL.md.
Prerequisites
- Compose UI 1.5+ —
Modifier.Nodeand the specialized node interfaces are stable here. - Kotlin 2.0+ with
org.jetbrains.kotlin.plugin.composeapplied. Strong Skipping is on by default; non-skippable modifiers compound at scroll velocity. - A release build for any final measurement (skydoves hot take #5: debug builds run interpreted and lie).
- For the full per-interface override surface and lifecycle diagram, read
references/modifier-node-anatomy.mdbefore authoring anything beyond aDrawModifierNode.
Workflow
- [ ] 1. Identify every `Modifier.composed { }` factory in the module. Grep for
Modifier.composed. Each match is one migration target. Note what the body does —remember,drawBehind,LaunchedEffect, aCompositionLocalread, a pointer handler — because that decides which specialized node interface(s) you need.
- [ ] 2. Sketch the three pieces every Modifier.Node migration produces.
- (a) The public extension —
fun Modifier.foo(...): Modifier = this then FooElement(...). Same name and signature as the oldcomposed { }factory. - (b) The `ModifierNodeElement<T>` `data class` — holds the parameters; implements
create()(called once on first apply) andupdate(node: T)(called on subsequent applies). - (c) The `Modifier.Node` subclass — holds mutable state (
varfields), implements one or more specialized node interfaces, and runs lifecycle hooks (onAttach,onDetach,onReset).
- [ ] 3. Make the Element a `data class`. The compiler-synthesized
equals()/hashCode()are how Compose decides whether to callupdate()vs leave the node alone. MUST bedata class. A plainclassfalls back to referential equality, the diff thinks every apply is a new modifier, andupdate()is never called — your node holds stale parameters silently.
- [ ] 4. Pick the right specialized node interface(s). A
Modifier.Nodeis empty by itself; behavior comes from interfaces it implements. The common ones:
| Interface | Use when the modifier needs to … | |
|---|---|---|
DrawModifierNode | draw (replaces drawBehind/drawWithCache) | |
LayoutModifierNode | measure/place (replaces layout { } and custom Layout) | |
SemanticsModifierNode | contribute to accessibility | |
PointerInputModifierNode | handle pointer / gesture input (replaces pointerInput) | |
CompositionLocalConsumerModifierNode | read a CompositionLocal from inside the node | |
LayoutAwareModifierNode | get notified when this node's size/coordinates change | |
GlobalPositionAwareModifierNode | get notified about position in the window/root | |
ObserverModifierNode | observe arbitrary state reads with a custom observer (observeReads { ... }) | |
DelegatingNode | compose multiple node behaviors by delegating to child nodes | |
TraversableNode | walk the modifier chain (parent/child traversal) |
A node MAY implement several interfaces at once — e.g. DrawModifierNode + CompositionLocalConsumerModifierNode + LayoutAwareModifierNode. For complex multi-behavior modifiers, PREFERRED: compose smaller DelegatingNode children rather than one mega-node implementing five interfaces.
- [ ] 5. Use the built-in `coroutineScope` for async work. Every
Modifier.Nodeexposes acoroutineScope: CoroutineScopelazily tied to the node's attach/detach lifecycle. Launch animations, observers, debouncers there fromonAttach(). MUST NOT create your ownCoroutineScopeinsideonAttach— you will leak it pastonDetach.
- [ ] 6. Trigger re-runs explicitly when needed. When you mutate node state from inside
update()or a coroutine and need a redraw / re-measure / re-place, callinvalidateDraw(),invalidateMeasurement(), orinvalidatePlacement(). By default,update()triggers an auto-invalidation; for fine control, overrideshouldAutoInvalidate = falseand invalidate manually.
- [ ] 7. Implement `onAttach`/`onDetach`/`onReset` for resource lifecycle.
onAttachruns when the node joins the tree;onDetachwhen it leaves;onResetwhen the node is reused (only relevant inside lazy layouts). MUST release listeners, observers, and external subscriptions inonDetach.
- [ ] 8. Verify migration: no `Modifier.composed { }` remains. Re-grep the module. If any
composed { }calls survive, list them with rationale; otherwise the migration is complete.
Patterns
Pattern: migrate composed { drawBehind } to DrawModifierNode
The data class Element gives equals() / hashCode() for free. When the caller passes the same color, equals() returns true and the node is left alone. When the color changes, update() mutates node.color in place — no allocation, no Composition invalidation in the parent.
Pattern: coroutine work in a node (replace composed { LaunchedEffect })
The node's built-in coroutineScope is cancelled automatically on onDetach. No leak, no manual DisposableEffect.
Pattern: read a CompositionLocal inside a node
CompositionLocalConsumerModifierNode exposes currentValueOf(local) from inside any node callback. Reads are tracked by the Draw invalidation list, not by a Composition restart scope, so changing LocalThemeTokens redraws the node without recomposing the parent.
Pattern: forgetting data class (the silent-stale-node bug)
Pattern: holding a reference to the calling composable
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingmodifiers/migrating-to-modifier-node/SKILL.md
Popularity
- Repository stars
- 444
- Repository forks
- 15
- 7-day trend score
- 30
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Extension points
hookPre/post lifecycle hooks
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/skydoves/compose-performance-skills/migrating-to-modifier-node)