@silvery/examples 0.4.2

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 (37) hide show
  1. package/bin/cli.ts +286 -0
  2. package/examples/apps/aichat/components.tsx +469 -0
  3. package/examples/apps/aichat/index.tsx +207 -0
  4. package/examples/apps/aichat/script.ts +460 -0
  5. package/examples/apps/aichat/state.ts +326 -0
  6. package/examples/apps/aichat/types.ts +19 -0
  7. package/examples/apps/app-todo.tsx +201 -0
  8. package/examples/apps/async-data.tsx +208 -0
  9. package/examples/apps/cli-wizard.tsx +332 -0
  10. package/examples/apps/clipboard.tsx +183 -0
  11. package/examples/apps/components.tsx +463 -0
  12. package/examples/apps/data-explorer.tsx +490 -0
  13. package/examples/apps/dev-tools.tsx +379 -0
  14. package/examples/apps/explorer.tsx +731 -0
  15. package/examples/apps/gallery.tsx +653 -0
  16. package/examples/apps/inline-bench.tsx +136 -0
  17. package/examples/apps/kanban.tsx +267 -0
  18. package/examples/apps/layout-ref.tsx +185 -0
  19. package/examples/apps/outline.tsx +171 -0
  20. package/examples/apps/panes/index.tsx +205 -0
  21. package/examples/apps/paste-demo.tsx +198 -0
  22. package/examples/apps/scroll.tsx +77 -0
  23. package/examples/apps/search-filter.tsx +240 -0
  24. package/examples/apps/task-list.tsx +271 -0
  25. package/examples/apps/terminal.tsx +800 -0
  26. package/examples/apps/textarea.tsx +103 -0
  27. package/examples/apps/theme.tsx +515 -0
  28. package/examples/apps/transform.tsx +242 -0
  29. package/examples/apps/virtual-10k.tsx +405 -0
  30. package/examples/components/counter.tsx +45 -0
  31. package/examples/components/hello.tsx +34 -0
  32. package/examples/components/progress-bar.tsx +48 -0
  33. package/examples/components/select-list.tsx +50 -0
  34. package/examples/components/spinner.tsx +40 -0
  35. package/examples/components/text-input.tsx +57 -0
  36. package/examples/components/virtual-list.tsx +52 -0
  37. package/package.json +27 -0
@@ -0,0 +1,208 @@
1
+ /**
2
+ * Async Data Example
3
+ *
4
+ * Demonstrates React Suspense for async data loading:
5
+ * - Suspense boundaries with fallback UI
6
+ * - Multiple independent suspending components
7
+ * - Error handling with ErrorBoundary
8
+ */
9
+
10
+ import React, { Suspense, useState, use } from "react"
11
+ import {
12
+ render,
13
+ Box,
14
+ Text,
15
+ H1,
16
+ Kbd,
17
+ Muted,
18
+ useInput,
19
+ useApp,
20
+ createTerm,
21
+ ErrorBoundary,
22
+ type Key,
23
+ } from "../../src/index.js"
24
+ import { ExampleBanner, type ExampleMeta } from "../_banner.js"
25
+
26
+ export const meta: ExampleMeta = {
27
+ name: "Async Data",
28
+ description: "React Suspense with independent data sources and error boundaries",
29
+ features: ["React Suspense", "use() hook", "ErrorBoundary"],
30
+ }
31
+
32
+ // ============================================================================
33
+ // Data Fetching (simulated)
34
+ // ============================================================================
35
+
36
+ // Cache for promises (React's use() requires stable promise references)
37
+ const cache = new Map<string, Promise<unknown>>()
38
+
39
+ function fetchData<T>(key: string, ms: number, data: T): Promise<T> {
40
+ if (!cache.has(key)) {
41
+ cache.set(key, new Promise<T>((resolve) => setTimeout(() => resolve(data), ms)))
42
+ }
43
+ return cache.get(key) as Promise<T>
44
+ }
45
+
46
+ function clearCache() {
47
+ cache.clear()
48
+ }
49
+
50
+ // ============================================================================
51
+ // Async Components
52
+ // ============================================================================
53
+
54
+ interface UserData {
55
+ name: string
56
+ email: string
57
+ role: string
58
+ }
59
+
60
+ function UserProfile() {
61
+ const user = use(
62
+ fetchData<UserData>("user", 1500, {
63
+ name: "Alice Chen",
64
+ email: "alice@example.com",
65
+ role: "Developer",
66
+ }),
67
+ )
68
+
69
+ return (
70
+ <Box flexDirection="column" borderStyle="round" borderColor="$success" padding={1}>
71
+ <H1 color="$success">User Profile</H1>
72
+ <Text>Name: {user.name}</Text>
73
+ <Text>Email: {user.email}</Text>
74
+ <Text>Role: {user.role}</Text>
75
+ </Box>
76
+ )
77
+ }
78
+
79
+ interface StatsData {
80
+ projects: number
81
+ commits: number
82
+ reviews: number
83
+ }
84
+
85
+ function Statistics() {
86
+ const stats = use(
87
+ fetchData<StatsData>("stats", 2500, {
88
+ projects: 12,
89
+ commits: 847,
90
+ reviews: 156,
91
+ }),
92
+ )
93
+
94
+ return (
95
+ <Box flexDirection="column" borderStyle="round" borderColor="$primary" padding={1}>
96
+ <H1>Statistics</H1>
97
+ <Text>Projects: {stats.projects}</Text>
98
+ <Text>Commits: {stats.commits}</Text>
99
+ <Text>Reviews: {stats.reviews}</Text>
100
+ </Box>
101
+ )
102
+ }
103
+
104
+ interface Activity {
105
+ id: number
106
+ action: string
107
+ time: string
108
+ }
109
+
110
+ function RecentActivity() {
111
+ const activities = use(
112
+ fetchData<Activity[]>("activity", 3500, [
113
+ { id: 1, action: "Merged PR #423", time: "2h ago" },
114
+ { id: 2, action: "Reviewed PR #421", time: "4h ago" },
115
+ { id: 3, action: "Created issue #89", time: "1d ago" },
116
+ ]),
117
+ )
118
+
119
+ return (
120
+ <Box flexDirection="column" borderStyle="round" borderColor="$warning" padding={1}>
121
+ <H1 color="$warning">Recent Activity</H1>
122
+ {activities.map((a) => (
123
+ <Text key={a.id}>
124
+ <Text dim>{a.time}</Text> {a.action}
125
+ </Text>
126
+ ))}
127
+ </Box>
128
+ )
129
+ }
130
+
131
+ // Loading fallbacks
132
+ function LoadingBox({ label, color }: { label: string; color: string }) {
133
+ return (
134
+ <Box borderStyle="round" borderColor="$border" padding={1}>
135
+ <Text color="$muted">Loading {label}...</Text>
136
+ </Box>
137
+ )
138
+ }
139
+
140
+ // ============================================================================
141
+ // Main App
142
+ // ============================================================================
143
+
144
+ export function AsyncDataApp() {
145
+ const { exit } = useApp()
146
+ const [refreshKey, setRefreshKey] = useState(0)
147
+
148
+ useInput((input: string, key: Key) => {
149
+ if (input === "q" || key.escape) {
150
+ exit()
151
+ return
152
+ }
153
+ if (input === "r") {
154
+ // Refresh: clear cache and force re-render
155
+ clearCache()
156
+ setRefreshKey((k) => k + 1)
157
+ }
158
+ })
159
+
160
+ return (
161
+ <Box flexDirection="column" padding={1} key={refreshKey}>
162
+ <Box flexGrow={1} flexDirection="row" gap={1}>
163
+ {/* Each Suspense boundary loads independently */}
164
+ <ErrorBoundary fallback={<Text color="$error">User error</Text>}>
165
+ <Suspense fallback={<LoadingBox label="user" color="green" />}>
166
+ <UserProfile />
167
+ </Suspense>
168
+ </ErrorBoundary>
169
+
170
+ <ErrorBoundary fallback={<Text color="$error">Stats error</Text>}>
171
+ <Suspense fallback={<LoadingBox label="stats" color="blue" />}>
172
+ <Statistics />
173
+ </Suspense>
174
+ </ErrorBoundary>
175
+
176
+ <ErrorBoundary fallback={<Text color="$error">Activity error</Text>}>
177
+ <Suspense fallback={<LoadingBox label="activity" color="yellow" />}>
178
+ <RecentActivity />
179
+ </Suspense>
180
+ </ErrorBoundary>
181
+ </Box>
182
+
183
+ <Muted>
184
+ {" "}
185
+ <Kbd>r</Kbd> refresh <Kbd>Esc/q</Kbd> quit
186
+ </Muted>
187
+ </Box>
188
+ )
189
+ }
190
+
191
+ // ============================================================================
192
+ // Main
193
+ // ============================================================================
194
+
195
+ async function main() {
196
+ using term = createTerm()
197
+ const { waitUntilExit } = await render(
198
+ <ExampleBanner meta={meta} controls="r refresh Esc/q quit">
199
+ <AsyncDataApp />
200
+ </ExampleBanner>,
201
+ term,
202
+ )
203
+ await waitUntilExit()
204
+ }
205
+
206
+ if (import.meta.main) {
207
+ main().catch(console.error)
208
+ }
@@ -0,0 +1,332 @@
1
+ /**
2
+ * CLI Wizard Example
3
+ *
4
+ * A multi-step project scaffolding wizard demonstrating:
5
+ * - SelectList for option selection with keyboard navigation
6
+ * - TextInput for free-form text entry
7
+ * - ProgressBar for visual progress feedback
8
+ * - Step-by-step flow with state transitions
9
+ *
10
+ * Usage: bun run examples/apps/cli-wizard.tsx
11
+ *
12
+ * Controls:
13
+ * j/k or Up/Down - Navigate options (step 1)
14
+ * Enter - Confirm selection / submit input
15
+ * Type - Enter project name (step 2)
16
+ * q or Esc - Quit (when not typing)
17
+ */
18
+
19
+ import React, { useState, useCallback, useEffect } from "react"
20
+ import {
21
+ render,
22
+ Box,
23
+ Text,
24
+ SelectList,
25
+ TextInput,
26
+ ProgressBar,
27
+ Spinner,
28
+ useInput,
29
+ useApp,
30
+ createTerm,
31
+ H1,
32
+ Muted,
33
+ Lead,
34
+ Kbd,
35
+ Code,
36
+ type Key,
37
+ type SelectOption,
38
+ } from "../../src/index.js"
39
+ import { ExampleBanner, type ExampleMeta } from "../_banner.js"
40
+
41
+ export const meta: ExampleMeta = {
42
+ name: "CLI Wizard",
43
+ description: "Multi-step project scaffolding wizard with selection, input, and progress",
44
+ demo: true,
45
+ features: ["SelectList", "TextInput", "ProgressBar", "Spinner", "useInput()"],
46
+ }
47
+
48
+ // ============================================================================
49
+ // Types
50
+ // ============================================================================
51
+
52
+ type WizardStep = "framework" | "name" | "installing" | "done"
53
+
54
+ interface WizardState {
55
+ step: WizardStep
56
+ framework: string | null
57
+ projectName: string
58
+ progress: number
59
+ }
60
+
61
+ // ============================================================================
62
+ // Data
63
+ // ============================================================================
64
+
65
+ const FRAMEWORKS: SelectOption[] = [
66
+ { label: "React — A JavaScript library for building user interfaces", value: "react" },
67
+ { label: "Vue — The progressive JavaScript framework", value: "vue" },
68
+ { label: "Svelte — Cybernetically enhanced web apps", value: "svelte" },
69
+ { label: "Solid — Simple and performant reactivity", value: "solid" },
70
+ {
71
+ label: "Angular — Platform for building web applications",
72
+ value: "angular",
73
+ disabled: true,
74
+ },
75
+ ]
76
+
77
+ const INSTALL_STEPS = [
78
+ "Resolving dependencies...",
79
+ "Downloading packages...",
80
+ "Linking dependencies...",
81
+ "Building project...",
82
+ "Generating types...",
83
+ "Setting up config...",
84
+ "Done!",
85
+ ]
86
+
87
+ // ============================================================================
88
+ // Components
89
+ // ============================================================================
90
+
91
+ /** Step indicator showing current position in the wizard */
92
+ function StepIndicator({ current, total }: { current: number; total: number }) {
93
+ return (
94
+ <Box paddingX={1} marginBottom={1}>
95
+ {Array.from({ length: total }, (_, i) => {
96
+ const isDone = i < current
97
+ const isCurrent = i === current
98
+ const dot = isDone ? "\u25cf" : isCurrent ? "\u25cb" : "\u25cb"
99
+ const color = isDone ? "$success" : isCurrent ? "$primary" : "$muted"
100
+ return (
101
+ <Text key={i} color={color} bold={isCurrent}>
102
+ {dot}
103
+ {i < total - 1 ? " \u2500 " : ""}
104
+ </Text>
105
+ )
106
+ })}
107
+ </Box>
108
+ )
109
+ }
110
+
111
+ /** Step 1: Framework selection */
112
+ function FrameworkStep({ onSelect }: { onSelect: (option: SelectOption) => void }) {
113
+ return (
114
+ <Box flexDirection="column" paddingX={1}>
115
+ <Box marginBottom={1}>
116
+ <H1>? Select a framework:</H1>
117
+ </Box>
118
+ <SelectList items={FRAMEWORKS} onSelect={onSelect} />
119
+ <Box marginTop={1}>
120
+ <Lead>(Angular is coming soon)</Lead>
121
+ </Box>
122
+ </Box>
123
+ )
124
+ }
125
+
126
+ /** Step 2: Project name input */
127
+ function NameStep({
128
+ value,
129
+ onChange,
130
+ onSubmit,
131
+ framework,
132
+ }: {
133
+ value: string
134
+ onChange: (v: string) => void
135
+ onSubmit: (v: string) => void
136
+ framework: string
137
+ }) {
138
+ return (
139
+ <Box flexDirection="column" paddingX={1}>
140
+ <Box marginBottom={1}>
141
+ <H1>? Project name</H1>
142
+ <Muted> ({framework})</Muted>
143
+ <H1>:</H1>
144
+ </Box>
145
+ <Box>
146
+ <Text color="$muted">{"\u276f "}</Text>
147
+ <TextInput value={value} onChange={onChange} onSubmit={onSubmit} prompt="" />
148
+ </Box>
149
+ <Box marginTop={1}>
150
+ <Muted>Press Enter to confirm</Muted>
151
+ </Box>
152
+ </Box>
153
+ )
154
+ }
155
+
156
+ /** Step 3: Installation progress */
157
+ function InstallStep({ progress, stepIndex }: { progress: number; stepIndex: number }) {
158
+ const currentStep = INSTALL_STEPS[Math.min(stepIndex, INSTALL_STEPS.length - 1)]!
159
+
160
+ return (
161
+ <Box flexDirection="column" paddingX={1}>
162
+ <Box marginBottom={1}>
163
+ <Spinner type="dots" />
164
+ <Text bold color="$warning">
165
+ {" "}
166
+ Installing dependencies...
167
+ </Text>
168
+ </Box>
169
+
170
+ <Box marginBottom={1}>
171
+ <ProgressBar value={progress} color="$primary" label="" />
172
+ </Box>
173
+
174
+ <Muted>{currentStep}</Muted>
175
+ </Box>
176
+ )
177
+ }
178
+
179
+ /** Step 4: Completion summary */
180
+ function DoneStep({ framework, projectName }: { framework: string; projectName: string }) {
181
+ return (
182
+ <Box flexDirection="column" paddingX={1}>
183
+ <Box marginBottom={1}>
184
+ <H1 color="$success">{"\u2714"} Project created successfully!</H1>
185
+ </Box>
186
+
187
+ <Box flexDirection="column" borderStyle="round" borderColor="$success" paddingX={2} paddingY={1}>
188
+ <Box>
189
+ <Muted>Framework: </Muted>
190
+ <Text bold>{framework}</Text>
191
+ </Box>
192
+ <Box>
193
+ <Muted>Project: </Muted>
194
+ <Text bold>{projectName}</Text>
195
+ </Box>
196
+ <Box>
197
+ <Muted>Location: </Muted>
198
+ <Text bold>./{projectName}/</Text>
199
+ </Box>
200
+ </Box>
201
+
202
+ <Box flexDirection="column" marginTop={1}>
203
+ <Muted>Get started:</Muted>
204
+ <Code>
205
+ {" "}cd {projectName}
206
+ </Code>
207
+ <Code>{" "}bun install</Code>
208
+ <Code>{" "}bun dev</Code>
209
+ </Box>
210
+
211
+ <Box marginTop={1}>
212
+ <Muted>
213
+ Press <Kbd>q</Kbd> or <Kbd>Esc</Kbd> to exit
214
+ </Muted>
215
+ </Box>
216
+ </Box>
217
+ )
218
+ }
219
+
220
+ // ============================================================================
221
+ // Main App
222
+ // ============================================================================
223
+
224
+ export function CliWizard() {
225
+ const { exit } = useApp()
226
+ const [state, setState] = useState<WizardState>({
227
+ step: "framework",
228
+ framework: null,
229
+ projectName: "",
230
+ progress: 0,
231
+ })
232
+
233
+ // Handle framework selection
234
+ const handleFrameworkSelect = useCallback((option: SelectOption) => {
235
+ setState((prev) => ({
236
+ ...prev,
237
+ step: "name",
238
+ framework: option.value,
239
+ projectName: `my-${option.value}-app`,
240
+ }))
241
+ }, [])
242
+
243
+ // Handle project name change
244
+ const handleNameChange = useCallback((value: string) => {
245
+ setState((prev) => ({ ...prev, projectName: value }))
246
+ }, [])
247
+
248
+ // Handle project name submission
249
+ const handleNameSubmit = useCallback((value: string) => {
250
+ if (value.trim()) {
251
+ setState((prev) => ({ ...prev, step: "installing", progress: 0 }))
252
+ }
253
+ }, [])
254
+
255
+ // Simulate installation progress
256
+ useEffect(() => {
257
+ if (state.step !== "installing") return
258
+
259
+ const timer = setInterval(() => {
260
+ setState((prev) => {
261
+ const next = prev.progress + 0.08 + Math.random() * 0.04
262
+ if (next >= 1) {
263
+ clearInterval(timer)
264
+ return { ...prev, step: "done", progress: 1 }
265
+ }
266
+ return { ...prev, progress: next }
267
+ })
268
+ }, 200)
269
+
270
+ return () => clearInterval(timer)
271
+ }, [state.step])
272
+
273
+ // Global quit handler (only when not in text input step)
274
+ useInput((input: string, key: Key) => {
275
+ if (state.step === "name") return // TextInput handles its own input
276
+ if (input === "q" || key.escape) {
277
+ exit()
278
+ }
279
+ })
280
+
281
+ // Map progress to step index for display
282
+ const installStepIndex = Math.floor(state.progress * (INSTALL_STEPS.length - 1))
283
+
284
+ const stepNumber = state.step === "framework" ? 0 : state.step === "name" ? 1 : state.step === "installing" ? 2 : 3
285
+
286
+ return (
287
+ <Box flexDirection="column" flexGrow={1}>
288
+ <Box borderStyle="single" borderColor="$primary" paddingX={2} marginBottom={1}>
289
+ <H1>create-app</H1>
290
+ <Muted> v1.0.0</Muted>
291
+ </Box>
292
+
293
+ <StepIndicator current={stepNumber} total={4} />
294
+
295
+ {state.step === "framework" && <FrameworkStep onSelect={handleFrameworkSelect} />}
296
+
297
+ {state.step === "name" && state.framework && (
298
+ <NameStep
299
+ value={state.projectName}
300
+ onChange={handleNameChange}
301
+ onSubmit={handleNameSubmit}
302
+ framework={state.framework}
303
+ />
304
+ )}
305
+
306
+ {state.step === "installing" && <InstallStep progress={state.progress} stepIndex={installStepIndex} />}
307
+
308
+ {state.step === "done" && state.framework && (
309
+ <DoneStep framework={state.framework} projectName={state.projectName} />
310
+ )}
311
+ </Box>
312
+ )
313
+ }
314
+
315
+ // ============================================================================
316
+ // Main
317
+ // ============================================================================
318
+
319
+ async function main() {
320
+ using term = createTerm()
321
+ const { waitUntilExit } = await render(
322
+ <ExampleBanner meta={meta} controls="j/k navigate Enter select q/Esc quit">
323
+ <CliWizard />
324
+ </ExampleBanner>,
325
+ term,
326
+ )
327
+ await waitUntilExit()
328
+ }
329
+
330
+ if (import.meta.main) {
331
+ main().catch(console.error)
332
+ }