cp-toolkit 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 (196) hide show
  1. package/README.md +130 -0
  2. package/bin/cp-kit.js +72 -0
  3. package/package.json +46 -0
  4. package/src/commands/add.js +212 -0
  5. package/src/commands/doctor.js +149 -0
  6. package/src/commands/init.js +662 -0
  7. package/src/commands/list.js +128 -0
  8. package/src/index.js +13 -0
  9. package/templates/agents/backend-specialist.md +263 -0
  10. package/templates/agents/code-archaeologist.md +106 -0
  11. package/templates/agents/database-architect.md +226 -0
  12. package/templates/agents/debugger.md +225 -0
  13. package/templates/agents/devops-engineer.md +242 -0
  14. package/templates/agents/documentation-writer.md +104 -0
  15. package/templates/agents/explorer-agent.md +73 -0
  16. package/templates/agents/frontend-specialist.md +556 -0
  17. package/templates/agents/game-developer.md +162 -0
  18. package/templates/agents/mobile-developer.md +377 -0
  19. package/templates/agents/orchestrator.md +416 -0
  20. package/templates/agents/penetration-tester.md +188 -0
  21. package/templates/agents/performance-optimizer.md +187 -0
  22. package/templates/agents/product-manager.md +112 -0
  23. package/templates/agents/product-owner.md +95 -0
  24. package/templates/agents/project-planner.md +406 -0
  25. package/templates/agents/qa-automation-engineer.md +103 -0
  26. package/templates/agents/security-auditor.md +170 -0
  27. package/templates/agents/seo-specialist.md +111 -0
  28. package/templates/agents/test-engineer.md +158 -0
  29. package/templates/github/agents/backend-specialist.md +67 -0
  30. package/templates/github/agents/code-archaeologist.md +61 -0
  31. package/templates/github/agents/database-architect.md +73 -0
  32. package/templates/github/agents/debugger.md +71 -0
  33. package/templates/github/agents/devops-engineer.md +85 -0
  34. package/templates/github/agents/documentation-writer.md +107 -0
  35. package/templates/github/agents/explorer-agent.md +87 -0
  36. package/templates/github/agents/frontend-specialist.md +54 -0
  37. package/templates/github/agents/game-developer.md +94 -0
  38. package/templates/github/agents/mobile-developer.md +75 -0
  39. package/templates/github/agents/orchestrator.md +48 -0
  40. package/templates/github/agents/penetration-tester.md +87 -0
  41. package/templates/github/agents/performance-optimizer.md +70 -0
  42. package/templates/github/agents/product-manager.md +85 -0
  43. package/templates/github/agents/product-owner.md +77 -0
  44. package/templates/github/agents/project-planner.md +83 -0
  45. package/templates/github/agents/qa-automation-engineer.md +95 -0
  46. package/templates/github/agents/security-auditor.md +72 -0
  47. package/templates/github/agents/seo-specialist.md +78 -0
  48. package/templates/github/agents/test-engineer.md +79 -0
  49. package/templates/github/instructions/database.instructions.md +74 -0
  50. package/templates/github/instructions/python.instructions.md +76 -0
  51. package/templates/github/instructions/security.instructions.md +73 -0
  52. package/templates/github/instructions/typescript.instructions.md +50 -0
  53. package/templates/rules/GEMINI.md +273 -0
  54. package/templates/scripts/mcp-server.js +704 -0
  55. package/templates/skills/core/behavioral-modes/SKILL.md +242 -0
  56. package/templates/skills/core/brainstorming/SKILL.md +163 -0
  57. package/templates/skills/core/brainstorming/dynamic-questioning.md +350 -0
  58. package/templates/skills/core/clean-code/SKILL.md +201 -0
  59. package/templates/skills/core/intelligent-routing/SKILL.md +335 -0
  60. package/templates/skills/core/mcp-builder/SKILL.md +176 -0
  61. package/templates/skills/core/parallel-agents/SKILL.md +175 -0
  62. package/templates/skills/core/plan-writing/SKILL.md +152 -0
  63. package/templates/skills/optional/api-patterns/SKILL.md +81 -0
  64. package/templates/skills/optional/api-patterns/api-style.md +42 -0
  65. package/templates/skills/optional/api-patterns/auth.md +24 -0
  66. package/templates/skills/optional/api-patterns/documentation.md +26 -0
  67. package/templates/skills/optional/api-patterns/graphql.md +41 -0
  68. package/templates/skills/optional/api-patterns/rate-limiting.md +31 -0
  69. package/templates/skills/optional/api-patterns/response.md +37 -0
  70. package/templates/skills/optional/api-patterns/rest.md +40 -0
  71. package/templates/skills/optional/api-patterns/scripts/api_validator.py +211 -0
  72. package/templates/skills/optional/api-patterns/security-testing.md +122 -0
  73. package/templates/skills/optional/api-patterns/trpc.md +41 -0
  74. package/templates/skills/optional/api-patterns/versioning.md +22 -0
  75. package/templates/skills/optional/app-builder/SKILL.md +75 -0
  76. package/templates/skills/optional/app-builder/agent-coordination.md +71 -0
  77. package/templates/skills/optional/app-builder/feature-building.md +53 -0
  78. package/templates/skills/optional/app-builder/project-detection.md +34 -0
  79. package/templates/skills/optional/app-builder/scaffolding.md +118 -0
  80. package/templates/skills/optional/app-builder/tech-stack.md +40 -0
  81. package/templates/skills/optional/app-builder/templates/SKILL.md +39 -0
  82. package/templates/skills/optional/app-builder/templates/astro-static/TEMPLATE.md +76 -0
  83. package/templates/skills/optional/app-builder/templates/chrome-extension/TEMPLATE.md +92 -0
  84. package/templates/skills/optional/app-builder/templates/cli-tool/TEMPLATE.md +88 -0
  85. package/templates/skills/optional/app-builder/templates/electron-desktop/TEMPLATE.md +88 -0
  86. package/templates/skills/optional/app-builder/templates/express-api/TEMPLATE.md +83 -0
  87. package/templates/skills/optional/app-builder/templates/flutter-app/TEMPLATE.md +90 -0
  88. package/templates/skills/optional/app-builder/templates/monorepo-turborepo/TEMPLATE.md +90 -0
  89. package/templates/skills/optional/app-builder/templates/nextjs-fullstack/TEMPLATE.md +82 -0
  90. package/templates/skills/optional/app-builder/templates/nextjs-saas/TEMPLATE.md +100 -0
  91. package/templates/skills/optional/app-builder/templates/nextjs-static/TEMPLATE.md +106 -0
  92. package/templates/skills/optional/app-builder/templates/nuxt-app/TEMPLATE.md +101 -0
  93. package/templates/skills/optional/app-builder/templates/python-fastapi/TEMPLATE.md +83 -0
  94. package/templates/skills/optional/app-builder/templates/react-native-app/TEMPLATE.md +93 -0
  95. package/templates/skills/optional/architecture/SKILL.md +55 -0
  96. package/templates/skills/optional/architecture/context-discovery.md +43 -0
  97. package/templates/skills/optional/architecture/examples.md +94 -0
  98. package/templates/skills/optional/architecture/pattern-selection.md +68 -0
  99. package/templates/skills/optional/architecture/patterns-reference.md +50 -0
  100. package/templates/skills/optional/architecture/trade-off-analysis.md +77 -0
  101. package/templates/skills/optional/bash-linux/SKILL.md +199 -0
  102. package/templates/skills/optional/code-review-checklist/SKILL.md +109 -0
  103. package/templates/skills/optional/database-design/SKILL.md +52 -0
  104. package/templates/skills/optional/database-design/database-selection.md +43 -0
  105. package/templates/skills/optional/database-design/indexing.md +39 -0
  106. package/templates/skills/optional/database-design/migrations.md +48 -0
  107. package/templates/skills/optional/database-design/optimization.md +36 -0
  108. package/templates/skills/optional/database-design/orm-selection.md +30 -0
  109. package/templates/skills/optional/database-design/schema-design.md +56 -0
  110. package/templates/skills/optional/database-design/scripts/schema_validator.py +172 -0
  111. package/templates/skills/optional/deployment-procedures/SKILL.md +241 -0
  112. package/templates/skills/optional/documentation-templates/SKILL.md +194 -0
  113. package/templates/skills/optional/frontend-design/SKILL.md +418 -0
  114. package/templates/skills/optional/frontend-design/animation-guide.md +331 -0
  115. package/templates/skills/optional/frontend-design/color-system.md +311 -0
  116. package/templates/skills/optional/frontend-design/decision-trees.md +418 -0
  117. package/templates/skills/optional/frontend-design/motion-graphics.md +306 -0
  118. package/templates/skills/optional/frontend-design/scripts/accessibility_checker.py +183 -0
  119. package/templates/skills/optional/frontend-design/scripts/ux_audit.py +722 -0
  120. package/templates/skills/optional/frontend-design/typography-system.md +345 -0
  121. package/templates/skills/optional/frontend-design/ux-psychology.md +541 -0
  122. package/templates/skills/optional/frontend-design/visual-effects.md +383 -0
  123. package/templates/skills/optional/game-development/2d-games/SKILL.md +119 -0
  124. package/templates/skills/optional/game-development/3d-games/SKILL.md +135 -0
  125. package/templates/skills/optional/game-development/SKILL.md +167 -0
  126. package/templates/skills/optional/game-development/game-art/SKILL.md +185 -0
  127. package/templates/skills/optional/game-development/game-audio/SKILL.md +190 -0
  128. package/templates/skills/optional/game-development/game-design/SKILL.md +129 -0
  129. package/templates/skills/optional/game-development/mobile-games/SKILL.md +108 -0
  130. package/templates/skills/optional/game-development/multiplayer/SKILL.md +132 -0
  131. package/templates/skills/optional/game-development/pc-games/SKILL.md +144 -0
  132. package/templates/skills/optional/game-development/vr-ar/SKILL.md +123 -0
  133. package/templates/skills/optional/game-development/web-games/SKILL.md +150 -0
  134. package/templates/skills/optional/geo-fundamentals/SKILL.md +156 -0
  135. package/templates/skills/optional/geo-fundamentals/scripts/geo_checker.py +289 -0
  136. package/templates/skills/optional/i18n-localization/SKILL.md +154 -0
  137. package/templates/skills/optional/i18n-localization/scripts/i18n_checker.py +241 -0
  138. package/templates/skills/optional/lint-and-validate/SKILL.md +45 -0
  139. package/templates/skills/optional/lint-and-validate/scripts/lint_runner.py +172 -0
  140. package/templates/skills/optional/lint-and-validate/scripts/type_coverage.py +173 -0
  141. package/templates/skills/optional/mobile-design/SKILL.md +394 -0
  142. package/templates/skills/optional/mobile-design/decision-trees.md +516 -0
  143. package/templates/skills/optional/mobile-design/mobile-backend.md +491 -0
  144. package/templates/skills/optional/mobile-design/mobile-color-system.md +420 -0
  145. package/templates/skills/optional/mobile-design/mobile-debugging.md +122 -0
  146. package/templates/skills/optional/mobile-design/mobile-design-thinking.md +357 -0
  147. package/templates/skills/optional/mobile-design/mobile-navigation.md +458 -0
  148. package/templates/skills/optional/mobile-design/mobile-performance.md +767 -0
  149. package/templates/skills/optional/mobile-design/mobile-testing.md +356 -0
  150. package/templates/skills/optional/mobile-design/mobile-typography.md +433 -0
  151. package/templates/skills/optional/mobile-design/platform-android.md +666 -0
  152. package/templates/skills/optional/mobile-design/platform-ios.md +561 -0
  153. package/templates/skills/optional/mobile-design/scripts/mobile_audit.py +670 -0
  154. package/templates/skills/optional/mobile-design/touch-psychology.md +537 -0
  155. package/templates/skills/optional/nextjs-react-expert/1-async-eliminating-waterfalls.md +312 -0
  156. package/templates/skills/optional/nextjs-react-expert/2-bundle-bundle-size-optimization.md +240 -0
  157. package/templates/skills/optional/nextjs-react-expert/3-server-server-side-performance.md +490 -0
  158. package/templates/skills/optional/nextjs-react-expert/4-client-client-side-data-fetching.md +264 -0
  159. package/templates/skills/optional/nextjs-react-expert/5-rerender-re-render-optimization.md +581 -0
  160. package/templates/skills/optional/nextjs-react-expert/6-rendering-rendering-performance.md +432 -0
  161. package/templates/skills/optional/nextjs-react-expert/7-js-javascript-performance.md +684 -0
  162. package/templates/skills/optional/nextjs-react-expert/8-advanced-advanced-patterns.md +150 -0
  163. package/templates/skills/optional/nextjs-react-expert/SKILL.md +267 -0
  164. package/templates/skills/optional/nextjs-react-expert/scripts/convert_rules.py +222 -0
  165. package/templates/skills/optional/nextjs-react-expert/scripts/react_performance_checker.py +252 -0
  166. package/templates/skills/optional/nodejs-best-practices/SKILL.md +333 -0
  167. package/templates/skills/optional/performance-profiling/SKILL.md +143 -0
  168. package/templates/skills/optional/performance-profiling/scripts/lighthouse_audit.py +76 -0
  169. package/templates/skills/optional/powershell-windows/SKILL.md +167 -0
  170. package/templates/skills/optional/python-patterns/SKILL.md +441 -0
  171. package/templates/skills/optional/red-team-tactics/SKILL.md +199 -0
  172. package/templates/skills/optional/seo-fundamentals/SKILL.md +129 -0
  173. package/templates/skills/optional/seo-fundamentals/scripts/seo_checker.py +219 -0
  174. package/templates/skills/optional/server-management/SKILL.md +161 -0
  175. package/templates/skills/optional/systematic-debugging/SKILL.md +109 -0
  176. package/templates/skills/optional/tailwind-patterns/SKILL.md +269 -0
  177. package/templates/skills/optional/tdd-workflow/SKILL.md +149 -0
  178. package/templates/skills/optional/testing-patterns/SKILL.md +178 -0
  179. package/templates/skills/optional/testing-patterns/scripts/test_runner.py +219 -0
  180. package/templates/skills/optional/vulnerability-scanner/SKILL.md +276 -0
  181. package/templates/skills/optional/vulnerability-scanner/checklists.md +121 -0
  182. package/templates/skills/optional/vulnerability-scanner/scripts/security_scan.py +458 -0
  183. package/templates/skills/optional/web-design-guidelines/SKILL.md +57 -0
  184. package/templates/skills/optional/webapp-testing/SKILL.md +187 -0
  185. package/templates/skills/optional/webapp-testing/scripts/playwright_runner.py +173 -0
  186. package/templates/workflows/brainstorm.md +113 -0
  187. package/templates/workflows/create.md +59 -0
  188. package/templates/workflows/debug.md +103 -0
  189. package/templates/workflows/deploy.md +176 -0
  190. package/templates/workflows/enhance.md +63 -0
  191. package/templates/workflows/orchestrate.md +237 -0
  192. package/templates/workflows/plan.md +89 -0
  193. package/templates/workflows/preview.md +81 -0
  194. package/templates/workflows/status.md +86 -0
  195. package/templates/workflows/test.md +144 -0
  196. package/templates/workflows/ui-ux-pro-max.md +296 -0
@@ -0,0 +1,432 @@
1
+ # 6. Rendering Performance
2
+
3
+ > **Impact:** MEDIUM
4
+ > **Focus:** Optimizing the rendering process reduces the work the browser needs to do.
5
+
6
+ ---
7
+
8
+ ## Overview
9
+
10
+ This section contains **9 rules** focused on rendering performance.
11
+
12
+ ---
13
+
14
+ ## Rule 6.1: Animate SVG Wrapper Instead of SVG Element
15
+
16
+ **Impact:** LOW
17
+ **Tags:** rendering, svg, css, animation, performance
18
+
19
+ ## Animate SVG Wrapper Instead of SVG Element
20
+
21
+ Many browsers don't have hardware acceleration for CSS3 animations on SVG elements. Wrap SVG in a `<div>` and animate the wrapper instead.
22
+
23
+ **Incorrect (animating SVG directly - no hardware acceleration):**
24
+
25
+ ```tsx
26
+ function LoadingSpinner() {
27
+ return (
28
+ <svg
29
+ className="animate-spin"
30
+ width="24"
31
+ height="24"
32
+ viewBox="0 0 24 24"
33
+ >
34
+ <circle cx="12" cy="12" r="10" stroke="currentColor" />
35
+ </svg>
36
+ )
37
+ }
38
+ ```
39
+
40
+ **Correct (animating wrapper div - hardware accelerated):**
41
+
42
+ ```tsx
43
+ function LoadingSpinner() {
44
+ return (
45
+ <div className="animate-spin">
46
+ <svg
47
+ width="24"
48
+ height="24"
49
+ viewBox="0 0 24 24"
50
+ >
51
+ <circle cx="12" cy="12" r="10" stroke="currentColor" />
52
+ </svg>
53
+ </div>
54
+ )
55
+ }
56
+ ```
57
+
58
+ This applies to all CSS transforms and transitions (`transform`, `opacity`, `translate`, `scale`, `rotate`). The wrapper div allows browsers to use GPU acceleration for smoother animations.
59
+
60
+ ---
61
+
62
+ ## Rule 6.2: CSS content-visibility for Long Lists
63
+
64
+ **Impact:** HIGH
65
+ **Tags:** rendering, css, content-visibility, long-lists
66
+
67
+ ## CSS content-visibility for Long Lists
68
+
69
+ Apply `content-visibility: auto` to defer off-screen rendering.
70
+
71
+ **CSS:**
72
+
73
+ ```css
74
+ .message-item {
75
+ content-visibility: auto;
76
+ contain-intrinsic-size: 0 80px;
77
+ }
78
+ ```
79
+
80
+ **Example:**
81
+
82
+ ```tsx
83
+ function MessageList({ messages }: { messages: Message[] }) {
84
+ return (
85
+ <div className="overflow-y-auto h-screen">
86
+ {messages.map(msg => (
87
+ <div key={msg.id} className="message-item">
88
+ <Avatar user={msg.author} />
89
+ <div>{msg.content}</div>
90
+ </div>
91
+ ))}
92
+ </div>
93
+ )
94
+ }
95
+ ```
96
+
97
+ For 1000 messages, browser skips layout/paint for ~990 off-screen items (10× faster initial render).
98
+
99
+ ---
100
+
101
+ ## Rule 6.3: Hoist Static JSX Elements
102
+
103
+ **Impact:** LOW
104
+ **Tags:** rendering, jsx, static, optimization
105
+
106
+ ## Hoist Static JSX Elements
107
+
108
+ Extract static JSX outside components to avoid re-creation.
109
+
110
+ **Incorrect (recreates element every render):**
111
+
112
+ ```tsx
113
+ function LoadingSkeleton() {
114
+ return <div className="animate-pulse h-20 bg-gray-200" />
115
+ }
116
+
117
+ function Container() {
118
+ return (
119
+ <div>
120
+ {loading && <LoadingSkeleton />}
121
+ </div>
122
+ )
123
+ }
124
+ ```
125
+
126
+ **Correct (reuses same element):**
127
+
128
+ ```tsx
129
+ const loadingSkeleton = (
130
+ <div className="animate-pulse h-20 bg-gray-200" />
131
+ )
132
+
133
+ function Container() {
134
+ return (
135
+ <div>
136
+ {loading && loadingSkeleton}
137
+ </div>
138
+ )
139
+ }
140
+ ```
141
+
142
+ This is especially helpful for large and static SVG nodes, which can be expensive to recreate on every render.
143
+
144
+ **Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler automatically hoists static JSX elements and optimizes component re-renders, making manual hoisting unnecessary.
145
+
146
+ ---
147
+
148
+ ## Rule 6.4: Optimize SVG Precision
149
+
150
+ **Impact:** LOW
151
+ **Tags:** rendering, svg, optimization, svgo
152
+
153
+ ## Optimize SVG Precision
154
+
155
+ Reduce SVG coordinate precision to decrease file size. The optimal precision depends on the viewBox size, but in general reducing precision should be considered.
156
+
157
+ **Incorrect (excessive precision):**
158
+
159
+ ```svg
160
+ <path d="M 10.293847 20.847362 L 30.938472 40.192837" />
161
+ ```
162
+
163
+ **Correct (1 decimal place):**
164
+
165
+ ```svg
166
+ <path d="M 10.3 20.8 L 30.9 40.2" />
167
+ ```
168
+
169
+ **Automate with SVGO:**
170
+
171
+ ```bash
172
+ npx svgo --precision=1 --multipass icon.svg
173
+ ```
174
+
175
+ ---
176
+
177
+ ## Rule 6.5: Prevent Hydration Mismatch Without Flickering
178
+
179
+ **Impact:** MEDIUM
180
+ **Tags:** rendering, ssr, hydration, localStorage, flicker
181
+
182
+ ## Prevent Hydration Mismatch Without Flickering
183
+
184
+ When rendering content that depends on client-side storage (localStorage, cookies), avoid both SSR breakage and post-hydration flickering by injecting a synchronous script that updates the DOM before React hydrates.
185
+
186
+ **Incorrect (breaks SSR):**
187
+
188
+ ```tsx
189
+ function ThemeWrapper({ children }: { children: ReactNode }) {
190
+ // localStorage is not available on server - throws error
191
+ const theme = localStorage.getItem('theme') || 'light'
192
+
193
+ return (
194
+ <div className={theme}>
195
+ {children}
196
+ </div>
197
+ )
198
+ }
199
+ ```
200
+
201
+ Server-side rendering will fail because `localStorage` is undefined.
202
+
203
+ **Incorrect (visual flickering):**
204
+
205
+ ```tsx
206
+ function ThemeWrapper({ children }: { children: ReactNode }) {
207
+ const [theme, setTheme] = useState('light')
208
+
209
+ useEffect(() => {
210
+ // Runs after hydration - causes visible flash
211
+ const stored = localStorage.getItem('theme')
212
+ if (stored) {
213
+ setTheme(stored)
214
+ }
215
+ }, [])
216
+
217
+ return (
218
+ <div className={theme}>
219
+ {children}
220
+ </div>
221
+ )
222
+ }
223
+ ```
224
+
225
+ Component first renders with default value (`light`), then updates after hydration, causing a visible flash of incorrect content.
226
+
227
+ **Correct (no flicker, no hydration mismatch):**
228
+
229
+ ```tsx
230
+ function ThemeWrapper({ children }: { children: ReactNode }) {
231
+ return (
232
+ <>
233
+ <div id="theme-wrapper">
234
+ {children}
235
+ </div>
236
+ <script
237
+ dangerouslySetInnerHTML={{
238
+ __html: `
239
+ (function() {
240
+ try {
241
+ var theme = localStorage.getItem('theme') || 'light';
242
+ var el = document.getElementById('theme-wrapper');
243
+ if (el) el.className = theme;
244
+ } catch (e) {}
245
+ })();
246
+ `,
247
+ }}
248
+ />
249
+ </>
250
+ )
251
+ }
252
+ ```
253
+
254
+ The inline script executes synchronously before showing the element, ensuring the DOM already has the correct value. No flickering, no hydration mismatch.
255
+
256
+ This pattern is especially useful for theme toggles, user preferences, authentication states, and any client-only data that should render immediately without flashing default values.
257
+
258
+ ---
259
+
260
+ ## Rule 6.6: Suppress Expected Hydration Mismatches
261
+
262
+ **Impact:** LOW-MEDIUM
263
+ **Tags:** rendering, hydration, ssr, nextjs
264
+
265
+ ## Suppress Expected Hydration Mismatches
266
+
267
+ In SSR frameworks (e.g., Next.js), some values are intentionally different on server vs client (random IDs, dates, locale/timezone formatting). For these *expected* mismatches, wrap the dynamic text in an element with `suppressHydrationWarning` to prevent noisy warnings. Do not use this to hide real bugs. Don’t overuse it.
268
+
269
+ **Incorrect (known mismatch warnings):**
270
+
271
+ ```tsx
272
+ function Timestamp() {
273
+ return <span>{new Date().toLocaleString()}</span>
274
+ }
275
+ ```
276
+
277
+ **Correct (suppress expected mismatch only):**
278
+
279
+ ```tsx
280
+ function Timestamp() {
281
+ return (
282
+ <span suppressHydrationWarning>
283
+ {new Date().toLocaleString()}
284
+ </span>
285
+ )
286
+ }
287
+ ```
288
+
289
+ ---
290
+
291
+ ## Rule 6.7: Use Activity Component for Show/Hide
292
+
293
+ **Impact:** MEDIUM
294
+ **Tags:** rendering, activity, visibility, state-preservation
295
+
296
+ ## Use Activity Component for Show/Hide
297
+
298
+ Use React's `<Activity>` to preserve state/DOM for expensive components that frequently toggle visibility.
299
+
300
+ **Usage:**
301
+
302
+ ```tsx
303
+ import { Activity } from 'react'
304
+
305
+ function Dropdown({ isOpen }: Props) {
306
+ return (
307
+ <Activity mode={isOpen ? 'visible' : 'hidden'}>
308
+ <ExpensiveMenu />
309
+ </Activity>
310
+ )
311
+ }
312
+ ```
313
+
314
+ Avoids expensive re-renders and state loss.
315
+
316
+ ---
317
+
318
+ ## Rule 6.8: Use Explicit Conditional Rendering
319
+
320
+ **Impact:** LOW
321
+ **Tags:** rendering, conditional, jsx, falsy-values
322
+
323
+ ## Use Explicit Conditional Rendering
324
+
325
+ Use explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be `0`, `NaN`, or other falsy values that render.
326
+
327
+ **Incorrect (renders "0" when count is 0):**
328
+
329
+ ```tsx
330
+ function Badge({ count }: { count: number }) {
331
+ return (
332
+ <div>
333
+ {count && <span className="badge">{count}</span>}
334
+ </div>
335
+ )
336
+ }
337
+
338
+ // When count = 0, renders: <div>0</div>
339
+ // When count = 5, renders: <div><span class="badge">5</span></div>
340
+ ```
341
+
342
+ **Correct (renders nothing when count is 0):**
343
+
344
+ ```tsx
345
+ function Badge({ count }: { count: number }) {
346
+ return (
347
+ <div>
348
+ {count > 0 ? <span className="badge">{count}</span> : null}
349
+ </div>
350
+ )
351
+ }
352
+
353
+ // When count = 0, renders: <div></div>
354
+ // When count = 5, renders: <div><span class="badge">5</span></div>
355
+ ```
356
+
357
+ ---
358
+
359
+ ## Rule 6.9: Use useTransition Over Manual Loading States
360
+
361
+ **Impact:** LOW
362
+ **Tags:** rendering, transitions, useTransition, loading, state
363
+
364
+ ## Use useTransition Over Manual Loading States
365
+
366
+ Use `useTransition` instead of manual `useState` for loading states. This provides built-in `isPending` state and automatically manages transitions.
367
+
368
+ **Incorrect (manual loading state):**
369
+
370
+ ```tsx
371
+ function SearchResults() {
372
+ const [query, setQuery] = useState('')
373
+ const [results, setResults] = useState([])
374
+ const [isLoading, setIsLoading] = useState(false)
375
+
376
+ const handleSearch = async (value: string) => {
377
+ setIsLoading(true)
378
+ setQuery(value)
379
+ const data = await fetchResults(value)
380
+ setResults(data)
381
+ setIsLoading(false)
382
+ }
383
+
384
+ return (
385
+ <>
386
+ <input onChange={(e) => handleSearch(e.target.value)} />
387
+ {isLoading && <Spinner />}
388
+ <ResultsList results={results} />
389
+ </>
390
+ )
391
+ }
392
+ ```
393
+
394
+ **Correct (useTransition with built-in pending state):**
395
+
396
+ ```tsx
397
+ import { useTransition, useState } from 'react'
398
+
399
+ function SearchResults() {
400
+ const [query, setQuery] = useState('')
401
+ const [results, setResults] = useState([])
402
+ const [isPending, startTransition] = useTransition()
403
+
404
+ const handleSearch = (value: string) => {
405
+ setQuery(value) // Update input immediately
406
+
407
+ startTransition(async () => {
408
+ // Fetch and update results
409
+ const data = await fetchResults(value)
410
+ setResults(data)
411
+ })
412
+ }
413
+
414
+ return (
415
+ <>
416
+ <input onChange={(e) => handleSearch(e.target.value)} />
417
+ {isPending && <Spinner />}
418
+ <ResultsList results={results} />
419
+ </>
420
+ )
421
+ }
422
+ ```
423
+
424
+ **Benefits:**
425
+
426
+ - **Automatic pending state**: No need to manually manage `setIsLoading(true/false)`
427
+ - **Error resilience**: Pending state correctly resets even if the transition throws
428
+ - **Better responsiveness**: Keeps the UI responsive during updates
429
+ - **Interrupt handling**: New transitions automatically cancel pending ones
430
+
431
+ Reference: [useTransition](https://react.dev/reference/react/useTransition)
432
+