@mandujs/mcp 0.13.0 → 0.17.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 (136) hide show
  1. package/README.md +102 -7
  2. package/package.json +3 -2
  3. package/src/adapters/index.ts +20 -20
  4. package/src/adapters/monitor-adapter.ts +100 -100
  5. package/src/adapters/tool-adapter.ts +88 -88
  6. package/src/executor/error-handler.ts +250 -250
  7. package/src/executor/index.ts +22 -22
  8. package/src/executor/tool-executor.ts +148 -148
  9. package/src/hooks/config-watcher.ts +174 -174
  10. package/src/hooks/index.ts +23 -23
  11. package/src/hooks/mcp-hooks.ts +227 -227
  12. package/src/logging/index.ts +15 -15
  13. package/src/logging/mcp-transport.ts +134 -134
  14. package/src/registry/index.ts +13 -13
  15. package/src/registry/mcp-tool-registry.ts +298 -298
  16. package/src/resources/skills/guides.ts +1136 -1136
  17. package/src/resources/skills/index.ts +12 -12
  18. package/src/resources/skills/loader.ts +218 -218
  19. package/src/resources/skills/mandu-composition/SKILL.md +91 -91
  20. package/src/resources/skills/mandu-composition/metadata.json +13 -13
  21. package/src/resources/skills/mandu-composition/rules/_sections.md +26 -26
  22. package/src/resources/skills/mandu-composition/rules/_template.md +77 -77
  23. package/src/resources/skills/mandu-composition/rules/comp-arch-avoid-boolean-props.md +146 -146
  24. package/src/resources/skills/mandu-composition/rules/comp-arch-compound-components.md +164 -164
  25. package/src/resources/skills/mandu-composition/rules/comp-island-event.md +161 -161
  26. package/src/resources/skills/mandu-composition/rules/comp-island-slot-split.md +167 -167
  27. package/src/resources/skills/mandu-composition/rules/comp-pattern-children.md +149 -149
  28. package/src/resources/skills/mandu-composition/rules/comp-state-context-interface.md +148 -148
  29. package/src/resources/skills/mandu-composition/rules/comp-state-lift-state.md +150 -150
  30. package/src/resources/skills/mandu-deployment/SKILL.md +92 -92
  31. package/src/resources/skills/mandu-deployment/_sections.md +41 -41
  32. package/src/resources/skills/mandu-deployment/_template.md +38 -38
  33. package/src/resources/skills/mandu-deployment/metadata.json +13 -13
  34. package/src/resources/skills/mandu-deployment/rules/deploy-build-bun.md +109 -109
  35. package/src/resources/skills/mandu-deployment/rules/deploy-build-output.md +115 -115
  36. package/src/resources/skills/mandu-deployment/rules/deploy-cicd-github.md +219 -219
  37. package/src/resources/skills/mandu-deployment/rules/deploy-docker-bun.md +150 -150
  38. package/src/resources/skills/mandu-deployment/rules/deploy-docker-compose.md +223 -223
  39. package/src/resources/skills/mandu-deployment/rules/deploy-platform-fly.md +152 -152
  40. package/src/resources/skills/mandu-deployment/rules/deploy-platform-render.md +179 -179
  41. package/src/resources/skills/mandu-deployment/rules/deploy-platform-supabase.md +323 -323
  42. package/src/resources/skills/mandu-deployment/rules/deploy-platform-vercel.md +140 -140
  43. package/src/resources/skills/mandu-fs-routes/SKILL.md +82 -82
  44. package/src/resources/skills/mandu-fs-routes/metadata.json +12 -12
  45. package/src/resources/skills/mandu-fs-routes/rules/_sections.md +36 -36
  46. package/src/resources/skills/mandu-fs-routes/rules/_template.md +69 -69
  47. package/src/resources/skills/mandu-fs-routes/rules/routes-api-methods.md +65 -65
  48. package/src/resources/skills/mandu-fs-routes/rules/routes-dynamic-param.md +93 -93
  49. package/src/resources/skills/mandu-fs-routes/rules/routes-naming-page.md +55 -55
  50. package/src/resources/skills/mandu-guard/SKILL.md +129 -129
  51. package/src/resources/skills/mandu-guard/metadata.json +12 -12
  52. package/src/resources/skills/mandu-guard/rules/_sections.md +36 -36
  53. package/src/resources/skills/mandu-guard/rules/_template.md +82 -82
  54. package/src/resources/skills/mandu-guard/rules/guard-config-rules.md +100 -100
  55. package/src/resources/skills/mandu-guard/rules/guard-layer-direction.md +76 -76
  56. package/src/resources/skills/mandu-guard/rules/guard-preset-mandu.md +81 -81
  57. package/src/resources/skills/mandu-guard/rules/guard-validate-import.md +80 -80
  58. package/src/resources/skills/mandu-hydration/SKILL.md +91 -91
  59. package/src/resources/skills/mandu-hydration/metadata.json +12 -12
  60. package/src/resources/skills/mandu-hydration/rules/_sections.md +31 -31
  61. package/src/resources/skills/mandu-hydration/rules/_template.md +72 -72
  62. package/src/resources/skills/mandu-hydration/rules/hydration-data-event.md +109 -109
  63. package/src/resources/skills/mandu-hydration/rules/hydration-directive-use-client.md +55 -55
  64. package/src/resources/skills/mandu-hydration/rules/hydration-island-setup.md +113 -113
  65. package/src/resources/skills/mandu-hydration/rules/hydration-priority-visible.md +68 -68
  66. package/src/resources/skills/mandu-performance/SKILL.md +85 -85
  67. package/src/resources/skills/mandu-performance/metadata.json +14 -14
  68. package/src/resources/skills/mandu-performance/rules/_sections.md +31 -31
  69. package/src/resources/skills/mandu-performance/rules/_template.md +64 -64
  70. package/src/resources/skills/mandu-performance/rules/perf-async-defer-await.md +103 -103
  71. package/src/resources/skills/mandu-performance/rules/perf-async-parallel.md +95 -95
  72. package/src/resources/skills/mandu-performance/rules/perf-bun-file.md +124 -124
  73. package/src/resources/skills/mandu-performance/rules/perf-bun-serve.md +125 -125
  74. package/src/resources/skills/mandu-performance/rules/perf-bundle-imports.md +80 -80
  75. package/src/resources/skills/mandu-performance/rules/perf-bundle-island-lazy.md +145 -145
  76. package/src/resources/skills/mandu-performance/rules/perf-cache-react.md +98 -98
  77. package/src/resources/skills/mandu-performance/rules/perf-render-transitions.md +154 -154
  78. package/src/resources/skills/mandu-security/SKILL.md +87 -87
  79. package/src/resources/skills/mandu-security/metadata.json +13 -13
  80. package/src/resources/skills/mandu-security/rules/_sections.md +31 -31
  81. package/src/resources/skills/mandu-security/rules/_template.md +74 -74
  82. package/src/resources/skills/mandu-security/rules/sec-auth-guard.md +127 -127
  83. package/src/resources/skills/mandu-security/rules/sec-env-management.md +133 -133
  84. package/src/resources/skills/mandu-security/rules/sec-input-validate.md +148 -148
  85. package/src/resources/skills/mandu-security/rules/sec-protect-csrf.md +146 -146
  86. package/src/resources/skills/mandu-security/rules/sec-protect-headers.md +138 -138
  87. package/src/resources/skills/mandu-slot/SKILL.md +85 -85
  88. package/src/resources/skills/mandu-slot/metadata.json +12 -12
  89. package/src/resources/skills/mandu-slot/rules/_sections.md +36 -36
  90. package/src/resources/skills/mandu-slot/rules/_template.md +63 -63
  91. package/src/resources/skills/mandu-slot/rules/slot-basic-structure.md +38 -38
  92. package/src/resources/skills/mandu-slot/rules/slot-ctx-response.md +56 -56
  93. package/src/resources/skills/mandu-slot/rules/slot-guard-auth.md +59 -59
  94. package/src/resources/skills/mandu-slot/rules/slot-http-methods.md +64 -64
  95. package/src/resources/skills/mandu-styling/SKILL.md +154 -154
  96. package/src/resources/skills/mandu-styling/_sections.md +43 -43
  97. package/src/resources/skills/mandu-styling/_template.md +32 -32
  98. package/src/resources/skills/mandu-styling/metadata.json +15 -15
  99. package/src/resources/skills/mandu-styling/rules/style-component-compound.md +235 -235
  100. package/src/resources/skills/mandu-styling/rules/style-component-slots.md +255 -255
  101. package/src/resources/skills/mandu-styling/rules/style-component-tokens.md +205 -205
  102. package/src/resources/skills/mandu-styling/rules/style-island-animations.md +272 -272
  103. package/src/resources/skills/mandu-styling/rules/style-island-scoping.md +167 -167
  104. package/src/resources/skills/mandu-styling/rules/style-island-variants.md +221 -221
  105. package/src/resources/skills/mandu-styling/rules/style-perf-critical.md +209 -209
  106. package/src/resources/skills/mandu-styling/rules/style-perf-purge.md +192 -192
  107. package/src/resources/skills/mandu-styling/rules/style-setup-modules.md +162 -162
  108. package/src/resources/skills/mandu-styling/rules/style-setup-panda.md +164 -164
  109. package/src/resources/skills/mandu-styling/rules/style-setup-tailwind.md +170 -170
  110. package/src/resources/skills/mandu-styling/rules/style-tailwind-v4-gotchas.md +179 -179
  111. package/src/resources/skills/mandu-styling/rules/style-theme-darkmode.md +229 -229
  112. package/src/resources/skills/mandu-testing/SKILL.md +99 -99
  113. package/src/resources/skills/mandu-testing/metadata.json +13 -13
  114. package/src/resources/skills/mandu-testing/rules/_sections.md +26 -26
  115. package/src/resources/skills/mandu-testing/rules/_template.md +65 -65
  116. package/src/resources/skills/mandu-testing/rules/test-component-island.md +195 -195
  117. package/src/resources/skills/mandu-testing/rules/test-e2e-playwright.md +196 -196
  118. package/src/resources/skills/mandu-testing/rules/test-mock-fetch.md +219 -219
  119. package/src/resources/skills/mandu-testing/rules/test-slot-unit.md +192 -192
  120. package/src/resources/skills/mandu-ui/SKILL.md +117 -117
  121. package/src/resources/skills/mandu-ui/_sections.md +23 -23
  122. package/src/resources/skills/mandu-ui/_template.md +32 -32
  123. package/src/resources/skills/mandu-ui/metadata.json +13 -13
  124. package/src/resources/skills/mandu-ui/rules/ui-accessibility-aria.md +232 -232
  125. package/src/resources/skills/mandu-ui/rules/ui-accessibility-focus.md +238 -238
  126. package/src/resources/skills/mandu-ui/rules/ui-composition-patterns.md +259 -259
  127. package/src/resources/skills/mandu-ui/rules/ui-island-integration.md +258 -258
  128. package/src/resources/skills/mandu-ui/rules/ui-radix-patterns.md +213 -213
  129. package/src/resources/skills/mandu-ui/rules/ui-shadcn-setup.md +209 -209
  130. package/src/resources/skills/recipes.ts +932 -932
  131. package/src/tools/ate.ts +219 -0
  132. package/src/tools/index.ts +4 -1
  133. package/src/tools/project.ts +334 -334
  134. package/src/tools/runtime.ts +497 -497
  135. package/src/tools/seo.ts +417 -417
  136. package/src/utils/withWarnings.ts +83 -83
@@ -1,72 +1,72 @@
1
- # Rule Template
2
-
3
- Use this template when creating new rules for mandu-hydration.
4
-
5
- ---
6
-
7
- ```markdown
8
- ---
9
- title: Rule Title Here
10
- impact: CRITICAL | HIGH | MEDIUM | LOW
11
- impactDescription: 영향 설명
12
- tags: hydration, tag1, tag2
13
- ---
14
-
15
- ## Rule Title Here
16
-
17
- **Impact: {LEVEL} ({impactDescription})**
18
-
19
- 규칙의 목적과 중요성을 설명합니다.
20
-
21
- **Incorrect (문제점 설명):**
22
-
23
- \`\`\`tsx
24
- // 잘못된 예시
25
- import { useState } from "react";
26
-
27
- export default function Counter() {
28
- const [count, setCount] = useState(0); // ❌ "use client" 없음
29
- return <button>{count}</button>;
30
- }
31
- \`\`\`
32
-
33
- **Correct (올바른 방법):**
34
-
35
- \`\`\`tsx
36
- // 올바른 예시
37
- "use client";
38
-
39
- import { useState } from "react";
40
-
41
- export default function Counter() {
42
- const [count, setCount] = useState(0); // ✅ 작동함
43
- return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
44
- }
45
- \`\`\`
46
-
47
- Reference: [관련 문서 링크](https://example.com)
48
- ```
49
-
50
- ---
51
-
52
- ## Naming Convention
53
-
54
- - 파일명: `{section}-{rule-name}.md`
55
- - 예시: `hydration-directive-use-client.md`, `hydration-island-setup.md`
56
-
57
- ## Hydration Strategies Quick Reference
58
-
59
- | Strategy | JavaScript | Use Case |
60
- |----------|------------|----------|
61
- | `none` | 없음 | 순수 정적 페이지 |
62
- | `island` | 부분 | 정적 + 인터랙티브 혼합 (기본값) |
63
- | `full` | 전체 | SPA 스타일 페이지 |
64
-
65
- ## Priority Quick Reference
66
-
67
- | Priority | Load Time | Use Case |
68
- |----------|-----------|----------|
69
- | `immediate` | 페이지 로드 | 중요한 인터랙션 |
70
- | `visible` | 뷰포트 진입 | 스크롤 아래 콘텐츠 (기본값) |
71
- | `idle` | 브라우저 유휴 | 비중요 기능 |
72
- | `interaction` | 사용자 액션 | 클릭해야 활성화 |
1
+ # Rule Template
2
+
3
+ Use this template when creating new rules for mandu-hydration.
4
+
5
+ ---
6
+
7
+ ```markdown
8
+ ---
9
+ title: Rule Title Here
10
+ impact: CRITICAL | HIGH | MEDIUM | LOW
11
+ impactDescription: 영향 설명
12
+ tags: hydration, tag1, tag2
13
+ ---
14
+
15
+ ## Rule Title Here
16
+
17
+ **Impact: {LEVEL} ({impactDescription})**
18
+
19
+ 규칙의 목적과 중요성을 설명합니다.
20
+
21
+ **Incorrect (문제점 설명):**
22
+
23
+ \`\`\`tsx
24
+ // 잘못된 예시
25
+ import { useState } from "react";
26
+
27
+ export default function Counter() {
28
+ const [count, setCount] = useState(0); // ❌ "use client" 없음
29
+ return <button>{count}</button>;
30
+ }
31
+ \`\`\`
32
+
33
+ **Correct (올바른 방법):**
34
+
35
+ \`\`\`tsx
36
+ // 올바른 예시
37
+ "use client";
38
+
39
+ import { useState } from "react";
40
+
41
+ export default function Counter() {
42
+ const [count, setCount] = useState(0); // ✅ 작동함
43
+ return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
44
+ }
45
+ \`\`\`
46
+
47
+ Reference: [관련 문서 링크](https://example.com)
48
+ ```
49
+
50
+ ---
51
+
52
+ ## Naming Convention
53
+
54
+ - 파일명: `{section}-{rule-name}.md`
55
+ - 예시: `hydration-directive-use-client.md`, `hydration-island-setup.md`
56
+
57
+ ## Hydration Strategies Quick Reference
58
+
59
+ | Strategy | JavaScript | Use Case |
60
+ |----------|------------|----------|
61
+ | `none` | 없음 | 순수 정적 페이지 |
62
+ | `island` | 부분 | 정적 + 인터랙티브 혼합 (기본값) |
63
+ | `full` | 전체 | SPA 스타일 페이지 |
64
+
65
+ ## Priority Quick Reference
66
+
67
+ | Priority | Load Time | Use Case |
68
+ |----------|-----------|----------|
69
+ | `immediate` | 페이지 로드 | 중요한 인터랙션 |
70
+ | `visible` | 뷰포트 진입 | 스크롤 아래 콘텐츠 (기본값) |
71
+ | `idle` | 브라우저 유휴 | 비중요 기능 |
72
+ | `interaction` | 사용자 액션 | 클릭해야 활성화 |
@@ -1,109 +1,109 @@
1
- ---
2
- title: Use useIslandEvent for Island Communication
3
- impact: MEDIUM
4
- impactDescription: Enables Island-to-Island data flow
5
- tags: hydration, event, communication
6
- ---
7
-
8
- ## Use useIslandEvent for Island Communication
9
-
10
- Islands are isolated by default. Use `useIslandEvent` to communicate between them.
11
-
12
- **Incorrect (shared global state):**
13
-
14
- ```tsx
15
- // ❌ Don't use global variables for Island communication
16
- let globalCount = 0;
17
-
18
- // Island A
19
- export function CounterIsland() {
20
- const [count, setCount] = useState(globalCount);
21
- // This won't sync with other Islands
22
- }
23
- ```
24
-
25
- **Correct (useIslandEvent):**
26
-
27
- ```tsx
28
- // Island A: Counter (emits updates)
29
- "use client";
30
-
31
- import { useState } from "react";
32
- import { useIslandEvent } from "@mandujs/core/client";
33
-
34
- export function CounterIsland() {
35
- const [count, setCount] = useState(0);
36
- const { emit } = useIslandEvent<{ count: number }>("counter-update");
37
-
38
- const increment = () => {
39
- const newCount = count + 1;
40
- setCount(newCount);
41
- emit({ count: newCount }); // Notify other Islands
42
- };
43
-
44
- return <button onClick={increment}>Count: {count}</button>;
45
- }
46
- ```
47
-
48
- ```tsx
49
- // Island B: Display (receives updates)
50
- "use client";
51
-
52
- import { useState } from "react";
53
- import { useIslandEvent } from "@mandujs/core/client";
54
-
55
- export function DisplayIsland() {
56
- const [lastCount, setLastCount] = useState(0);
57
-
58
- useIslandEvent<{ count: number }>("counter-update", (data) => {
59
- setLastCount(data.count); // React to counter updates
60
- });
61
-
62
- return <p>Last count received: {lastCount}</p>;
63
- }
64
- ```
65
-
66
- ## API Reference
67
-
68
- ```typescript
69
- // Emit events
70
- const { emit } = useIslandEvent<T>(eventName);
71
- emit(data);
72
-
73
- // Listen to events
74
- useIslandEvent<T>(eventName, (data) => {
75
- // Handle received data
76
- });
77
-
78
- // Both emit and listen
79
- const { emit } = useIslandEvent<T>(eventName, (data) => {
80
- // Handle received data
81
- });
82
- emit(otherData);
83
- ```
84
-
85
- ## Common Patterns
86
-
87
- ### Cart Updates
88
-
89
- ```tsx
90
- // Product Island
91
- emit({ action: "add", productId: 123 });
92
-
93
- // Cart Island
94
- useIslandEvent("cart-update", ({ action, productId }) => {
95
- if (action === "add") addToCart(productId);
96
- });
97
- ```
98
-
99
- ### Form Validation
100
-
101
- ```tsx
102
- // Form Field Island
103
- emit({ field: "email", valid: true, value: "user@example.com" });
104
-
105
- // Submit Button Island
106
- useIslandEvent("field-change", ({ field, valid }) => {
107
- updateFieldStatus(field, valid);
108
- });
109
- ```
1
+ ---
2
+ title: Use useIslandEvent for Island Communication
3
+ impact: MEDIUM
4
+ impactDescription: Enables Island-to-Island data flow
5
+ tags: hydration, event, communication
6
+ ---
7
+
8
+ ## Use useIslandEvent for Island Communication
9
+
10
+ Islands are isolated by default. Use `useIslandEvent` to communicate between them.
11
+
12
+ **Incorrect (shared global state):**
13
+
14
+ ```tsx
15
+ // ❌ Don't use global variables for Island communication
16
+ let globalCount = 0;
17
+
18
+ // Island A
19
+ export function CounterIsland() {
20
+ const [count, setCount] = useState(globalCount);
21
+ // This won't sync with other Islands
22
+ }
23
+ ```
24
+
25
+ **Correct (useIslandEvent):**
26
+
27
+ ```tsx
28
+ // Island A: Counter (emits updates)
29
+ "use client";
30
+
31
+ import { useState } from "react";
32
+ import { useIslandEvent } from "@mandujs/core/client";
33
+
34
+ export function CounterIsland() {
35
+ const [count, setCount] = useState(0);
36
+ const { emit } = useIslandEvent<{ count: number }>("counter-update");
37
+
38
+ const increment = () => {
39
+ const newCount = count + 1;
40
+ setCount(newCount);
41
+ emit({ count: newCount }); // Notify other Islands
42
+ };
43
+
44
+ return <button onClick={increment}>Count: {count}</button>;
45
+ }
46
+ ```
47
+
48
+ ```tsx
49
+ // Island B: Display (receives updates)
50
+ "use client";
51
+
52
+ import { useState } from "react";
53
+ import { useIslandEvent } from "@mandujs/core/client";
54
+
55
+ export function DisplayIsland() {
56
+ const [lastCount, setLastCount] = useState(0);
57
+
58
+ useIslandEvent<{ count: number }>("counter-update", (data) => {
59
+ setLastCount(data.count); // React to counter updates
60
+ });
61
+
62
+ return <p>Last count received: {lastCount}</p>;
63
+ }
64
+ ```
65
+
66
+ ## API Reference
67
+
68
+ ```typescript
69
+ // Emit events
70
+ const { emit } = useIslandEvent<T>(eventName);
71
+ emit(data);
72
+
73
+ // Listen to events
74
+ useIslandEvent<T>(eventName, (data) => {
75
+ // Handle received data
76
+ });
77
+
78
+ // Both emit and listen
79
+ const { emit } = useIslandEvent<T>(eventName, (data) => {
80
+ // Handle received data
81
+ });
82
+ emit(otherData);
83
+ ```
84
+
85
+ ## Common Patterns
86
+
87
+ ### Cart Updates
88
+
89
+ ```tsx
90
+ // Product Island
91
+ emit({ action: "add", productId: 123 });
92
+
93
+ // Cart Island
94
+ useIslandEvent("cart-update", ({ action, productId }) => {
95
+ if (action === "add") addToCart(productId);
96
+ });
97
+ ```
98
+
99
+ ### Form Validation
100
+
101
+ ```tsx
102
+ // Form Field Island
103
+ emit({ field: "email", valid: true, value: "user@example.com" });
104
+
105
+ // Submit Button Island
106
+ useIslandEvent("field-change", ({ field, valid }) => {
107
+ updateFieldStatus(field, valid);
108
+ });
109
+ ```
@@ -1,55 +1,55 @@
1
- ---
2
- title: Add "use client" Directive for Client Components
3
- impact: CRITICAL
4
- impactDescription: Required for client-side interactivity
5
- tags: hydration, client, directive
6
- ---
7
-
8
- ## Add "use client" Directive for Client Components
9
-
10
- Client components must have `"use client"` directive at the top of the file.
11
-
12
- **Incorrect (missing directive):**
13
-
14
- ```tsx
15
- // app/counter/client.tsx
16
-
17
- import { useState } from "react";
18
-
19
- export default function Counter() {
20
- const [count, setCount] = useState(0); // ❌ Will error
21
- return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
22
- }
23
- ```
24
-
25
- **Correct (with directive):**
26
-
27
- ```tsx
28
- // app/counter/client.tsx
29
-
30
- "use client";
31
-
32
- import { useState } from "react";
33
-
34
- export default function Counter() {
35
- const [count, setCount] = useState(0); // ✅ Works
36
- return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
37
- }
38
- ```
39
-
40
- ## When to Use "use client"
41
-
42
- Use the directive when your component:
43
- - Uses React hooks (useState, useEffect, useRef, etc.)
44
- - Has event handlers (onClick, onChange, etc.)
45
- - Uses browser-only APIs (window, document, localStorage)
46
- - Needs client-side interactivity
47
-
48
- ## File Naming Convention
49
-
50
- ```
51
- app/
52
- ├── counter/
53
- │ ├── page.tsx # Server component (no directive needed)
54
- │ └── client.tsx # Client component ("use client" required)
55
- ```
1
+ ---
2
+ title: Add "use client" Directive for Client Components
3
+ impact: CRITICAL
4
+ impactDescription: Required for client-side interactivity
5
+ tags: hydration, client, directive
6
+ ---
7
+
8
+ ## Add "use client" Directive for Client Components
9
+
10
+ Client components must have `"use client"` directive at the top of the file.
11
+
12
+ **Incorrect (missing directive):**
13
+
14
+ ```tsx
15
+ // app/counter/client.tsx
16
+
17
+ import { useState } from "react";
18
+
19
+ export default function Counter() {
20
+ const [count, setCount] = useState(0); // ❌ Will error
21
+ return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
22
+ }
23
+ ```
24
+
25
+ **Correct (with directive):**
26
+
27
+ ```tsx
28
+ // app/counter/client.tsx
29
+
30
+ "use client";
31
+
32
+ import { useState } from "react";
33
+
34
+ export default function Counter() {
35
+ const [count, setCount] = useState(0); // ✅ Works
36
+ return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
37
+ }
38
+ ```
39
+
40
+ ## When to Use "use client"
41
+
42
+ Use the directive when your component:
43
+ - Uses React hooks (useState, useEffect, useRef, etc.)
44
+ - Has event handlers (onClick, onChange, etc.)
45
+ - Uses browser-only APIs (window, document, localStorage)
46
+ - Needs client-side interactivity
47
+
48
+ ## File Naming Convention
49
+
50
+ ```
51
+ app/
52
+ ├── counter/
53
+ │ ├── page.tsx # Server component (no directive needed)
54
+ │ └── client.tsx # Client component ("use client" required)
55
+ ```