@istmx/skills 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/CODE_OF_CONDUCT.md +47 -0
  2. package/LICENSE +30 -0
  3. package/README.md +48 -22
  4. package/bin/cli.js +130 -119
  5. package/istm-animate/SKILL.md +28 -58
  6. package/istm-animate/core/motion-principles/SKILL.md +284 -0
  7. package/istm-animate/core/motion-principles/references/designers.md +127 -0
  8. package/istm-animate/core/motion-principles/references/easing-guide.md +231 -0
  9. package/istm-animate/core/motion-principles/references/enter-exit-recipes.md +466 -0
  10. package/istm-animate/frameworks/canvas-generative/SKILL.md +325 -0
  11. package/istm-animate/frameworks/canvas-generative/references/algorithms.md +925 -0
  12. package/istm-animate/frameworks/css-native/SKILL.md +319 -0
  13. package/istm-animate/frameworks/css-native/references/modern-css.md +465 -0
  14. package/istm-animate/frameworks/framer-motion/SKILL.md +187 -0
  15. package/istm-animate/frameworks/framer-motion/references/api.md +312 -0
  16. package/istm-animate/frameworks/gsap/SKILL.md +188 -0
  17. package/istm-animate/frameworks/gsap/references/core.md +218 -0
  18. package/istm-animate/frameworks/gsap/references/plugins.md +371 -0
  19. package/istm-animate/frameworks/gsap/references/scrolltrigger.md +325 -0
  20. package/istm-animate/frameworks/gsap/references/timeline.md +218 -0
  21. package/istm-animate/frameworks/threejs-r3f/SKILL.md +243 -0
  22. package/istm-animate/frameworks/threejs-r3f/references/scene-setup.md +392 -0
  23. package/istm-animate/frameworks/threejs-r3f/references/shaders.md +446 -0
  24. package/istm-animate/mobile/android/compose-graphics/SKILL.md +503 -0
  25. package/istm-animate/mobile/android/compose-graphics/references/agsl-recipes.md +356 -0
  26. package/istm-animate/mobile/android/compose-graphics/references/canvas-generative.md +282 -0
  27. package/istm-animate/mobile/android/compose-graphics/references/m3-expressive-deep.md +303 -0
  28. package/istm-animate/mobile/android/compose-motion/SKILL.md +359 -0
  29. package/istm-animate/mobile/android/compose-motion/references/gestures-compose.md +273 -0
  30. package/istm-animate/mobile/android/compose-motion/references/recomposition-and-anim.md +196 -0
  31. package/istm-animate/mobile/android/compose-motion/references/shared-transitions.md +281 -0
  32. package/istm-animate/mobile/android/compose-multiplatform/SKILL.md +311 -0
  33. package/istm-animate/mobile/android/compose-multiplatform/references/cmp-interop.md +288 -0
  34. package/istm-animate/mobile/android/compose-multiplatform/references/cmp-platform-quirks.md +156 -0
  35. package/istm-animate/mobile/apple/swiftui-graphics/SKILL.md +408 -0
  36. package/istm-animate/mobile/apple/swiftui-graphics/references/canvas-swiftui.md +239 -0
  37. package/istm-animate/mobile/apple/swiftui-graphics/references/liquid-glass-deep.md +252 -0
  38. package/istm-animate/mobile/apple/swiftui-graphics/references/metal-recipes.md +382 -0
  39. package/istm-animate/mobile/apple/swiftui-motion/SKILL.md +379 -0
  40. package/istm-animate/mobile/apple/swiftui-motion/references/gestures-swiftui.md +336 -0
  41. package/istm-animate/mobile/apple/swiftui-motion/references/phase-keyframe-deep.md +231 -0
  42. package/istm-animate/mobile/apple/swiftui-motion/references/springs-cheatsheet.md +129 -0
  43. package/istm-animate/references/animate.md +199 -0
  44. package/istm-animate/references/audit.md +115 -0
  45. package/istm-animate/references/design-engineering.md +674 -0
  46. package/istm-animate/references/improve.md +101 -0
  47. package/istm-animate/references/opportunities.md +132 -0
  48. package/istm-animate/references/plan-template.md +73 -0
  49. package/istm-animate/references/recipes.md +324 -0
  50. package/istm-animate/references/review.md +112 -0
  51. package/istm-animate/references/standards.md +187 -0
  52. package/istm-animate/references/vocabulary.md +173 -0
  53. package/istm-architecture/SKILL.md +10 -1
  54. package/istm-awwward-designer/SKILL.md +28 -20
  55. package/istm-design/SKILL.md +31 -58
  56. package/istm-design/core/design-audit/SKILL.md +248 -0
  57. package/istm-design/core/desktop-principles/SKILL.md +363 -0
  58. package/istm-design/core/desktop-principles/references/keyboard-patterns.md +137 -0
  59. package/istm-design/core/desktop-principles/references/multi-window.md +105 -0
  60. package/istm-design/core/mobile-principles/SKILL.md +289 -0
  61. package/istm-design/core/mobile-principles/references/accessibility-mobile.md +175 -0
  62. package/istm-design/core/mobile-principles/references/gestures-deep.md +220 -0
  63. package/istm-design/core/ui-ux-pro-max/SKILL.md +392 -0
  64. package/istm-design/core/ui-ux-pro-max/UPSTREAM.md +23 -0
  65. package/istm-design/core/ui-ux-pro-max/data/app-interface.csv +31 -0
  66. package/istm-design/core/ui-ux-pro-max/data/charts.csv +26 -0
  67. package/istm-design/core/ui-ux-pro-max/data/colors.csv +193 -0
  68. package/istm-design/core/ui-ux-pro-max/data/google-fonts.csv +1924 -0
  69. package/istm-design/core/ui-ux-pro-max/data/icons.csv +106 -0
  70. package/istm-design/core/ui-ux-pro-max/data/landing.csv +35 -0
  71. package/istm-design/core/ui-ux-pro-max/data/motion.csv +17 -0
  72. package/istm-design/core/ui-ux-pro-max/data/products.csv +193 -0
  73. package/istm-design/core/ui-ux-pro-max/data/react-performance.csv +45 -0
  74. package/istm-design/core/ui-ux-pro-max/data/stacks/angular.csv +51 -0
  75. package/istm-design/core/ui-ux-pro-max/data/stacks/astro.csv +54 -0
  76. package/istm-design/core/ui-ux-pro-max/data/stacks/avalonia.csv +57 -0
  77. package/istm-design/core/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
  78. package/istm-design/core/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
  79. package/istm-design/core/ui-ux-pro-max/data/stacks/javafx.csv +76 -0
  80. package/istm-design/core/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
  81. package/istm-design/core/ui-ux-pro-max/data/stacks/laravel.csv +51 -0
  82. package/istm-design/core/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
  83. package/istm-design/core/ui-ux-pro-max/data/stacks/nuxt-ui.csv +71 -0
  84. package/istm-design/core/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
  85. package/istm-design/core/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
  86. package/istm-design/core/ui-ux-pro-max/data/stacks/react.csv +54 -0
  87. package/istm-design/core/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
  88. package/istm-design/core/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
  89. package/istm-design/core/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
  90. package/istm-design/core/ui-ux-pro-max/data/stacks/threejs.csv +54 -0
  91. package/istm-design/core/ui-ux-pro-max/data/stacks/uno.csv +60 -0
  92. package/istm-design/core/ui-ux-pro-max/data/stacks/uwp.csv +56 -0
  93. package/istm-design/core/ui-ux-pro-max/data/stacks/vue.csv +50 -0
  94. package/istm-design/core/ui-ux-pro-max/data/stacks/winui.csv +60 -0
  95. package/istm-design/core/ui-ux-pro-max/data/stacks/wpf.csv +57 -0
  96. package/istm-design/core/ui-ux-pro-max/data/styles.csv +85 -0
  97. package/istm-design/core/ui-ux-pro-max/data/typography.csv +75 -0
  98. package/istm-design/core/ui-ux-pro-max/data/ui-reasoning.csv +162 -0
  99. package/istm-design/core/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
  100. package/istm-design/core/ui-ux-pro-max/references/pro-rules.md +109 -0
  101. package/istm-design/core/ui-ux-pro-max/references/quick-reference.md +240 -0
  102. package/istm-design/core/ui-ux-pro-max/scripts/core.py +464 -0
  103. package/istm-design/core/ui-ux-pro-max/scripts/design_system.py +1371 -0
  104. package/istm-design/core/ui-ux-pro-max/scripts/search.py +162 -0
  105. package/istm-design/core/ui-ux-pro-max/scripts/tests/test_core.py +134 -0
  106. package/istm-design/core/ui-ux-pro-max/scripts/validate_data.py +114 -0
  107. package/istm-design/references/apple-design.md +282 -0
  108. package/istm-design/references/generation-guide.md +209 -0
  109. package/istm-design/references/pick-ui-library.md +77 -0
  110. package/istm-design/references/picker.md +197 -0
  111. package/istm-design/references/prototype.md +90 -0
  112. package/istm-design/references/schema.md +366 -0
  113. package/istm-workflow/istm-audit/SKILL.md +7 -7
  114. package/istm-workflow/istm-audit/agent-prompt.md +20 -20
  115. package/istm-workflow/istm-audit/modes/area.md +1 -1
  116. package/istm-workflow/istm-audit/modes/greenfield.md +4 -4
  117. package/istm-workflow/istm-audit/modes/tool-skills.md +1 -1
  118. package/istm-workflow/istm-check/SKILL.md +10 -10
  119. package/istm-workflow/istm-check/modes/review.md +9 -9
  120. package/istm-workflow/istm-check/modes/verify.md +19 -19
  121. package/istm-workflow/istm-check/review-agent-prompt.md +2 -2
  122. package/istm-workflow/istm-check/review-guide.md +4 -4
  123. package/istm-workflow/istm-craft/SKILL.md +20 -11
  124. package/istm-workflow/istm-craft/agent-modes/architecture.md +1 -1
  125. package/istm-workflow/istm-craft/agent-prompt.md +8 -8
  126. package/istm-workflow/istm-craft/internal/after-subagent.md +11 -11
  127. package/istm-workflow/istm-craft/internal/design-conversation.md +9 -9
  128. package/istm-workflow/istm-craft/internal/tool-discovery.md +2 -2
  129. package/istm-workflow/istm-craft/spec-template.md +18 -18
  130. package/istm-workflow/istm-debug/SKILL.md +6 -6
  131. package/istm-workflow/istm-develop/SKILL.md +31 -16
  132. package/istm-workflow/istm-develop/checklist.md +1 -1
  133. package/istm-workflow/istm-develop/flow/build.md +21 -21
  134. package/istm-workflow/istm-develop/flow/git.md +4 -4
  135. package/istm-workflow/istm-develop/logical-guide.md +5 -5
  136. package/istm-workflow/istm-develop/ui/implementation.md +2 -2
  137. package/istm-workflow/istm-develop/ui-guide.md +3 -3
  138. package/istm-workflow/istm-document/SKILL.md +6 -6
  139. package/istm-workflow/istm-document/templates/postmortem.md +1 -1
  140. package/istm-workflow/istm-scope/SKILL.md +20 -20
  141. package/istm-workflow/istm-scope/modes/add.md +1 -1
  142. package/istm-workflow/istm-scope/modes/plan-brownfield.md +2 -2
  143. package/istm-workflow/istm-scope/modes/plan-greenfield.md +4 -4
  144. package/istm-workflow/istm-scope/modes/plan-monorepo.md +3 -3
  145. package/istm-workflow/istm-scope/modes/plan.md +16 -16
  146. package/istm-workflow/istm-scope/modes/replan.md +2 -2
  147. package/istm-workflow/istm-scope/scope-template.md +29 -29
  148. package/istm-workflow/istm-sync/SKILL.md +37 -25
  149. package/istm-workflow/istm-sync/agent-prompt.md +37 -19
  150. package/istm-workflow/istm-test/SKILL.md +12 -12
  151. package/istm-workflow/istm-test/modes/setup.md +6 -6
  152. package/istm-workflow/istm-test/writing-guide.md +1 -1
  153. package/package.json +1 -1
@@ -0,0 +1,273 @@
1
+ # Gestures in Compose
2
+
3
+ Compose ships a layered gesture API: high-level modifiers for the common 90% (`clickable`, `draggable`, `transformable`, `anchoredDraggable`), and `pointerInput` for everything else. Climb up only when the abstraction breaks - don't reach for `awaitPointerEventScope` to handle a tap.
4
+
5
+ ---
6
+
7
+ ## Modifier-Based Gestures (the simple path)
8
+
9
+ ```kotlin
10
+ // Tap
11
+ Modifier.clickable(onClick = onPress)
12
+
13
+ // Tap + long-press + double-tap
14
+ Modifier.combinedClickable(
15
+ onClick = onPress,
16
+ onLongClick = onHold,
17
+ onDoubleClick = onDoubleTap,
18
+ )
19
+
20
+ // Single-axis drag
21
+ val dragState = rememberDraggableState { delta -> offsetX += delta }
22
+ Modifier.draggable(
23
+ state = dragState,
24
+ orientation = Orientation.Horizontal,
25
+ onDragStopped = { velocity -> /* fling here */ },
26
+ )
27
+
28
+ // Scrollable content (when you can't use ScrollState/LazyListState directly)
29
+ val scrollState = rememberScrollableState { delta ->
30
+ offset = (offset - delta).coerceIn(0f, max)
31
+ delta
32
+ }
33
+ Modifier.scrollable(state = scrollState, orientation = Orientation.Vertical)
34
+
35
+ // Pinch + pan + rotate
36
+ val tState = rememberTransformableState { zoomChange, panChange, rotationChange ->
37
+ scale *= zoomChange
38
+ offset += panChange
39
+ rotation += rotationChange
40
+ }
41
+ Modifier.transformable(state = tState)
42
+
43
+ // Swipeable / snap-points (Compose 1.6+, replaces deprecated swipeable)
44
+ val anchored = remember {
45
+ AnchoredDraggableState(
46
+ initialValue = DragValue.Center,
47
+ anchors = DraggableAnchors {
48
+ DragValue.Start at -1000f
49
+ DragValue.Center at 0f
50
+ DragValue.End at 1000f
51
+ },
52
+ positionalThreshold = { distance -> distance * 0.5f },
53
+ velocityThreshold = { with(density) { 100.dp.toPx() } },
54
+ snapAnimationSpec = spring(),
55
+ decayAnimationSpec = splineBasedDecay(density),
56
+ )
57
+ }
58
+ Modifier.anchoredDraggable(state = anchored, orientation = Orientation.Horizontal)
59
+ ```
60
+
61
+ `AnchoredDraggableState` is the modern primitive behind swipe-to-dismiss, bottom sheets, and side drawers. The state exposes `currentValue`, `targetValue`, `progress`, and `offset` - everything you need to drive UI off the drag.
62
+
63
+ ---
64
+
65
+ ## `pointerInput` (low-level)
66
+
67
+ When the modifier API can't express your intent (multi-stage gestures, custom hit testing, fling chained with another animation), drop down to `pointerInput`.
68
+
69
+ ```kotlin
70
+ Modifier.pointerInput(Unit) {
71
+ detectDragGestures(
72
+ onDragStart = { offset -> /* claim drag */ },
73
+ onDrag = { change, dragAmount ->
74
+ change.consume()
75
+ offsetX += dragAmount.x
76
+ offsetY += dragAmount.y
77
+ },
78
+ onDragEnd = { /* fling here */ },
79
+ onDragCancel = { /* reset */ },
80
+ )
81
+ }
82
+ ```
83
+
84
+ Common detectors: `detectTapGestures`, `detectDragGestures`, `detectDragGesturesAfterLongPress`, `detectTransformGestures` (zoom + pan + rotate). All call `change.consume()` when they decide the event is theirs - omit it and parents get a second crack at the event.
85
+
86
+ The `key` parameter on `pointerInput(key)` is critical: when the key changes, the coroutine restarts. Use `Unit` for static handlers; pass the lambdas / state you depend on as keys when they can change.
87
+
88
+ ---
89
+
90
+ ## Custom Gesture Detector with `awaitPointerEventScope`
91
+
92
+ For detectors not provided by the standard library:
93
+
94
+ ```kotlin
95
+ Modifier.pointerInput(Unit) {
96
+ awaitEachGesture {
97
+ val down = awaitFirstDown(requireUnconsumed = true)
98
+ val up = withTimeoutOrNull(viewConfiguration.longPressTimeoutMillis) {
99
+ waitForUpOrCancellation()
100
+ }
101
+ if (up != null) {
102
+ up.consume()
103
+ onShortTap(down.position)
104
+ } else {
105
+ // pointer still down - either long-press or drag
106
+ val drag = awaitTouchSlopOrCancellation(down.id) { change, _ ->
107
+ change.consume()
108
+ }
109
+ if (drag != null) {
110
+ onDrag(drag.position)
111
+ } else {
112
+ onLongPress(down.position)
113
+ }
114
+ }
115
+ }
116
+ }
117
+ ```
118
+
119
+ `awaitEachGesture` (Compose 1.6+, replaces `forEachGesture`) restarts cleanly per gesture and handles cancellation correctly. Always consume events you claim - unconsumed pointer events bubble to ancestors.
120
+
121
+ ---
122
+
123
+ ## `NestedScrollConnection`
124
+
125
+ When a scrolling container needs to coordinate with a parent (collapsing top-app-bar over a `LazyColumn`, swipe-to-refresh that intercepts the first vertical drag), implement `NestedScrollConnection`:
126
+
127
+ ```kotlin
128
+ val maxHeader = with(LocalDensity.current) { 120.dp.toPx() }
129
+ var headerOffset by remember { mutableFloatStateOf(0f) }
130
+
131
+ val nestedScrollConnection = remember {
132
+ object : NestedScrollConnection {
133
+ override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
134
+ // collapse header BEFORE the inner LazyColumn consumes scroll
135
+ val delta = available.y
136
+ val previous = headerOffset
137
+ headerOffset = (headerOffset + delta).coerceIn(-maxHeader, 0f)
138
+ val consumed = headerOffset - previous
139
+ return Offset(0f, consumed)
140
+ }
141
+
142
+ override fun onPostScroll(
143
+ consumed: Offset,
144
+ available: Offset,
145
+ source: NestedScrollSource,
146
+ ): Offset {
147
+ // re-expand header AFTER the inner reaches the top
148
+ if (available.y > 0 && headerOffset < 0f) {
149
+ val previous = headerOffset
150
+ headerOffset = (headerOffset + available.y).coerceAtMost(0f)
151
+ return Offset(0f, headerOffset - previous)
152
+ }
153
+ return Offset.Zero
154
+ }
155
+ }
156
+ }
157
+
158
+ Box(modifier = Modifier.nestedScroll(nestedScrollConnection)) {
159
+ LazyColumn(
160
+ contentPadding = PaddingValues(top = 120.dp),
161
+ modifier = Modifier.fillMaxSize(),
162
+ ) { /* items */ }
163
+
164
+ Header(modifier = Modifier.offset { IntOffset(0, headerOffset.roundToInt()) })
165
+ }
166
+ ```
167
+
168
+ Use case: collapsing toolbar with inner `LazyColumn`, swipe-to-refresh, parallax header. `onPreFling` and `onPostFling` exist for fling coordination too.
169
+
170
+ ---
171
+
172
+ ## Conflict Resolution Patterns
173
+
174
+ ### Vertical scroll vs horizontal swipe
175
+
176
+ Compose's gesture system handles native nested scroll fine when you declare orientation explicitly. A `LazyColumn` (vertical) inside a `HorizontalPager` works out of the box: the pager only claims horizontal drags, the column only vertical. Don't reinvent it with `pointerInput` unless you have a reason.
177
+
178
+ ### Tap vs drag
179
+
180
+ Use `awaitTouchSlopOrCancellation` to delay the "this is a drag" decision until the user has moved past the system slop threshold. Below the threshold, treat as a tap. The detectors above already handle this; if you write your own, mirror the pattern.
181
+
182
+ ### System back gesture (Android 13+) vs custom horizontal pan
183
+
184
+ Android's predictive back gesture lives on the screen edges. If your custom horizontal pan starts from the edge, users will trigger back instead of your interaction half the time. Either:
185
+
186
+ - Avoid edge-starting horizontal pans (move the affordance inward by 24dp+).
187
+ - Opt into predictive back via `BackHandler { ... }` and skip your pan when back is in flight.
188
+ - Accept the conflict and document it (rare; usually a UX bug).
189
+
190
+ ```kotlin
191
+ BackHandler(enabled = drawerOpen) { drawerOpen = false }
192
+ ```
193
+
194
+ ---
195
+
196
+ ## Fling and Decay
197
+
198
+ Two paths:
199
+
200
+ **With `Animatable`:**
201
+ ```kotlin
202
+ val offsetX = remember { Animatable(0f) }
203
+ val decay = splineBasedDecay<Float>(LocalDensity.current)
204
+
205
+ Modifier.draggable(
206
+ state = rememberDraggableState { delta ->
207
+ scope.launch { offsetX.snapTo(offsetX.value + delta) }
208
+ },
209
+ orientation = Orientation.Horizontal,
210
+ onDragStopped = { velocity ->
211
+ offsetX.animateDecay(velocity, decay)
212
+ },
213
+ )
214
+ ```
215
+
216
+ `splineBasedDecay` uses Android's standard fling friction (matches scrollers and `Scroller` exactly). For custom curves, `exponentialDecay(frictionMultiplier, absVelocityThreshold)`.
217
+
218
+ **With `AnchoredDraggableState`:**
219
+ The fling is wired automatically: when the drag stops, the state animates to the closest anchor honoring `velocityThreshold` and `positionalThreshold`. No manual decay needed.
220
+
221
+ ---
222
+
223
+ ## Common Patterns
224
+
225
+ ### Pull-to-refresh (Material 3 1.3+)
226
+ ```kotlin
227
+ PullToRefreshBox(isRefreshing = loading, onRefresh = ::refetch) {
228
+ LazyColumn { items(list) { Row(it) } }
229
+ }
230
+ ```
231
+
232
+ ### Swipe-to-dismiss row (Material 3 1.2+)
233
+ ```kotlin
234
+ val state = rememberSwipeToDismissBoxState()
235
+ SwipeToDismissBox(
236
+ state = state,
237
+ backgroundContent = { DismissBackground(state.dismissDirection) },
238
+ ) { ItemRow(item) }
239
+
240
+ LaunchedEffect(state.currentValue) {
241
+ if (state.currentValue == SwipeToDismissBoxValue.EndToStart) onDelete(item)
242
+ }
243
+ ```
244
+
245
+ ### Pinch-to-zoom image
246
+ ```kotlin
247
+ var scale by remember { mutableFloatStateOf(1f) }
248
+ var offset by remember { mutableStateOf(Offset.Zero) }
249
+ val tState = rememberTransformableState { zoom, pan, _ ->
250
+ scale = (scale * zoom).coerceIn(1f, 5f)
251
+ offset += pan
252
+ }
253
+ Image(
254
+ painter = painter,
255
+ contentDescription = null,
256
+ modifier = Modifier
257
+ .graphicsLayer {
258
+ scaleX = scale
259
+ scaleY = scale
260
+ translationX = offset.x
261
+ translationY = offset.y
262
+ }
263
+ .transformable(tState),
264
+ )
265
+ ```
266
+
267
+ ---
268
+
269
+ ## Sources
270
+
271
+ - [Android touch and input docs](https://developer.android.com/develop/ui/compose/touch-input)
272
+ - [Android nested scroll](https://developer.android.com/develop/ui/compose/touch-input/pointer-input/scroll)
273
+ - [AnchoredDraggable migration](https://developer.android.com/jetpack/androidx/releases/compose-foundation)
@@ -0,0 +1,196 @@
1
+ # Recomposition and Animation
2
+
3
+ Recomposition is your friend until it's not. Animations change state every frame; if a frame's worth of state change recomposes a tree of 200 nodes, you ship a slideshow. This file covers when animations cause unnecessary recompositions and how to detect / fix them.
4
+
5
+ ---
6
+
7
+ ## Why Recomposition Matters for Animation
8
+
9
+ Compose recomposes any composable function that reads `MutableState` whose value changed. Animations change state every frame (16.67ms at 60fps, 8.33ms at 120fps). If a frame-rate state read happens at the composition phase, every reader recomposes every frame and you have jank.
10
+
11
+ The fix is to confine the state read to a phase that doesn't trigger recomposition:
12
+
13
+ - **Composition phase:** reading state here recomposes the function.
14
+ - **Layout phase:** reading state here re-runs measure/place but not composition.
15
+ - **Draw phase:** reading state here re-runs only drawing - the cheapest of the three.
16
+
17
+ The lambda-based modifiers (`Modifier.offset { ... }`, `Modifier.graphicsLayer { ... }`, `Modifier.drawBehind { ... }`) defer their state reads to layout or draw. The non-lambda variants read in composition and trigger recomposition.
18
+
19
+ ---
20
+
21
+ ## Diff: State Read in Composition vs Layout/Draw
22
+
23
+ ```kotlin
24
+ // BAD - Modifier.offset(x) reads animatedX in composition.
25
+ // Every frame: full composition for this Box.
26
+ val animatedX by animateFloatAsState(targetX, label = "x")
27
+ Box(Modifier.offset(x = animatedX.dp))
28
+ ```
29
+
30
+ ```kotlin
31
+ // GOOD - lambda defers read to placement (layout phase).
32
+ // Composition runs once; placement re-runs each frame.
33
+ val animatedX by animateFloatAsState(targetX, label = "x")
34
+ Box(Modifier.offset { IntOffset(animatedX.roundToInt(), 0) })
35
+ ```
36
+
37
+ Same idea for transforms:
38
+
39
+ ```kotlin
40
+ // BAD
41
+ Box(Modifier.scale(animatedScale))
42
+ ```
43
+
44
+ ```kotlin
45
+ // GOOD - graphicsLayer reads in draw phase
46
+ Box(Modifier.graphicsLayer { scaleX = animatedScale; scaleY = animatedScale })
47
+ ```
48
+
49
+ `graphicsLayer { ... }` is the right choice for any transform-only animation (translate, scale, rotate, alpha): composite-only, no layout pass, GPU-accelerated.
50
+
51
+ ---
52
+
53
+ ## `derivedStateOf` for Derived Values
54
+
55
+ When you compute a `Boolean` (or any cheap value) from a frequently-changing `State`, wrap it in `derivedStateOf` so readers only recompose when the derived value flips, not every time the source changes.
56
+
57
+ ```kotlin
58
+ val lazyListState = rememberLazyListState()
59
+
60
+ // BAD - recomposes on every scroll position change (every pixel)
61
+ val isScrolled = lazyListState.firstVisibleItemIndex > 0 ||
62
+ lazyListState.firstVisibleItemScrollOffset > 0
63
+
64
+ // GOOD - recomposes only when the boolean actually flips
65
+ val isScrolled by remember {
66
+ derivedStateOf {
67
+ lazyListState.firstVisibleItemIndex > 0 ||
68
+ lazyListState.firstVisibleItemScrollOffset > 0
69
+ }
70
+ }
71
+
72
+ if (isScrolled) Shadow()
73
+ ```
74
+
75
+ Rule of thumb: if the predicate result changes far less often than its inputs, use `derivedStateOf`. If the predicate is expensive AND deterministic in its inputs, also use it (caches the result until inputs change).
76
+
77
+ ---
78
+
79
+ ## `key()` in Lists with Animation
80
+
81
+ ```kotlin
82
+ // BAD - no keys; on insert/remove/reorder, items animate the WRONG slot
83
+ LazyColumn {
84
+ items(list) { item ->
85
+ AnimatedVisibility(item.expanded) { Row(item) }
86
+ }
87
+ }
88
+ ```
89
+
90
+ ```kotlin
91
+ // GOOD - stable keys + Modifier.animateItem for reorder/insert/remove anim
92
+ LazyColumn {
93
+ items(list, key = { it.id }) { item ->
94
+ Row(modifier = Modifier.animateItem()) {
95
+ AnimatedVisibility(item.expanded) { ItemRow(item) }
96
+ }
97
+ }
98
+ }
99
+ ```
100
+
101
+ Without keys, Compose matches items by position - reorder the list, and item 0's visibility state slides into position 5's row. `Modifier.animateItem()` (Compose 1.7+, replaces the deprecated `Modifier.animateItemPlacement`) animates appearance, removal, and movement automatically when keys are stable.
102
+
103
+ ---
104
+
105
+ ## Layout Inspector (Recomposition Counts)
106
+
107
+ Android Studio's Layout Inspector exposes "Show recomposition counts" - it draws each composable with a counter and a "skipped" counter.
108
+
109
+ What to look for:
110
+
111
+ - A **static** component (header, footer, button you didn't touch) recomposing on every list scroll = somewhere up the tree, a state read leaks to the parent. Hoist the read.
112
+ - A node that recomposes 60 times per second during an animation = the animation state is being read at composition phase. Switch to lambda-based modifiers (`offset { }`, `graphicsLayer { }`).
113
+ - Skipped count high relative to recompositions = recomposition is happening but Compose is correctly detecting unchanged inputs and skipping the body. That's healthy.
114
+
115
+ Open it via Tools -> Layout Inspector while running on a connected device. Compose nodes show in the same tree as Android views.
116
+
117
+ ---
118
+
119
+ ## `Modifier.recomposeHighlighter()` (dev only)
120
+
121
+ Compose 1.6+ ships an opt-in `Modifier.recomposeHighlighter()` (you also find Cyril Pierre's standalone version on GitHub) that flashes a colored border on every recomposition. Drop it on suspicious nodes during dev:
122
+
123
+ ```kotlin
124
+ @Composable
125
+ fun Header(title: String) {
126
+ Row(
127
+ modifier = Modifier
128
+ .fillMaxWidth()
129
+ .recomposeHighlighter() // dev only
130
+ .padding(16.dp),
131
+ ) { Text(title) }
132
+ }
133
+ ```
134
+
135
+ If the border flashes when you scroll a list further down the tree, you have a leak. Remove for prod.
136
+
137
+ ---
138
+
139
+ ## Macrobenchmark for Frame Timing
140
+
141
+ `androidx.benchmark:benchmark-macro` runs your app on a device or emulator and measures real frame timings via `FrameTimingMetric`.
142
+
143
+ ```kotlin
144
+ @RunWith(AndroidJUnit4::class)
145
+ class ScrollBenchmark {
146
+ @get:Rule val benchmarkRule = MacrobenchmarkRule()
147
+
148
+ @Test fun scrollHomeFeed() = benchmarkRule.measureRepeated(
149
+ packageName = "com.example.app",
150
+ metrics = listOf(FrameTimingMetric()),
151
+ iterations = 10,
152
+ startupMode = StartupMode.WARM,
153
+ ) {
154
+ startActivityAndWait()
155
+ val list = device.findObject(By.res("home-feed"))
156
+ list.fling(Direction.DOWN)
157
+ device.waitForIdle()
158
+ }
159
+ }
160
+ ```
161
+
162
+ Targets:
163
+ - **60Hz device:** p50 < 16.67ms, p99 < 30ms.
164
+ - **120Hz / ProMotion:** p50 < 8.33ms, p99 < 16ms.
165
+ - A single dropped frame is fine. A pattern of dropped frames during scroll = a fix needed.
166
+
167
+ ---
168
+
169
+ ## Baseline Profiles
170
+
171
+ Compose recomposition logic itself goes through JIT before AOT compilation kicks in - first launch and first scroll are the worst. Baseline Profiles bake hot paths into AOT at install time.
172
+
173
+ ```kotlin
174
+ @RunWith(AndroidJUnit4::class)
175
+ class GenerateBaselineProfile {
176
+ @get:Rule val rule = BaselineProfileRule()
177
+
178
+ @Test fun generate() = rule.collect(packageName = "com.example.app") {
179
+ startActivityAndWait()
180
+ val list = device.findObject(By.res("home-feed"))
181
+ list.fling(Direction.DOWN)
182
+ device.waitForIdle()
183
+ }
184
+ }
185
+ ```
186
+
187
+ Run via the AGP `:app:generateBaselineProfile` task; commit the generated `baseline-prof.txt`. Typical wins: 20-40% reduction in jank on cold-start scrolls. Combine with Macrobenchmark to measure delta.
188
+
189
+ ---
190
+
191
+ ## Sources
192
+
193
+ - [Compose performance docs](https://developer.android.com/develop/ui/compose/performance)
194
+ - [Stability and recomposition](https://developer.android.com/develop/ui/compose/performance/stability)
195
+ - [Macrobenchmark](https://developer.android.com/topic/performance/benchmarking/macrobenchmark-overview)
196
+ - [Baseline Profiles](https://developer.android.com/topic/performance/baselineprofiles/overview)