-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.canvas.tsx
More file actions
679 lines (650 loc) · 20.4 KB
/
Copy pathdemo.canvas.tsx
File metadata and controls
679 lines (650 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/**
* Showcase canvas — authored against `cursor/canvas` ONLY.
*
* This is the single source of truth: the web app imports it directly and the
* same file is copied into the IDE-managed canvases folder, so Cursor and
* GitHub Pages render identical source. It doubles as living documentation —
* a gallery of every shim module, leading with the visuals and ending with a
* short "how it works". In the IDE `cursor/canvas` is the real SDK; in the web
* build it is aliased to a Mantine-backed shim.
*/
import {
BarChart,
Button,
Callout,
Card,
CardBody,
CardHeader,
Checkbox,
Code,
CollapsibleSection,
DiffStats,
DiffView,
Divider,
Grid,
H1,
H2,
H3,
IconButton,
LineChart,
Link,
PieChart,
Pill,
Row,
Select,
Spacer,
Stack,
Stat,
Swatch,
Table,
Text,
TextArea,
TextInput,
Toggle,
TodoList,
TodoListCard,
UsageBar,
colorPalette,
computeDAGLayout,
mergeStyle,
useCanvasAction,
useCanvasState,
useHostTheme,
type ChartSeries,
type Color,
type DiffLineData,
type TodoItem,
} from 'cursor/canvas';
/* --------------------------------------------------------------- demo data */
const LATENCY: ChartSeries[] = [
{ name: 'p50', data: [120, 110, 130, 105, 98, 112], tone: 'info' },
{ name: 'p95', data: [320, 410, 380, 350, 300, 290], tone: 'warning' },
];
const REQUESTS: ChartSeries[] = [
{ name: 'IDE', data: [40, 52, 48, 61, 55, 67], tone: 'info' },
{ name: 'CLI', data: [12, 18, 15, 20, 22, 19], tone: 'success' },
{ name: 'Cloud', data: [8, 9, 14, 11, 16, 21], tone: 'neutral' },
];
const CATEGORIES = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const OUTCOMES = [
{ label: 'Passing', value: 68, tone: 'success' as const },
{ label: 'Flaky', value: 14, tone: 'warning' as const },
{ label: 'Failing', value: 9, tone: 'danger' as const },
];
const CATEGORY_LEGEND: { color: Color; label: string }[] = [
{ color: 'purple', label: 'tools' },
{ color: 'green', label: 'rules' },
{ color: 'blue', label: 'conversation' },
{ color: 'orange', label: 'skills' },
];
const TODOS: TodoItem[] = [
{ id: '1', content: 'Scaffold Vite + shim alias', status: 'completed' },
{
id: '2',
content: 'Implement the full cursor/canvas surface',
status: 'completed',
},
{
id: '3',
content: 'Render the showcase in the IDE and on Pages',
status: 'in_progress',
},
{ id: '4', content: 'Pixel-perfect theme parity', status: 'pending' },
{
id: '5',
content: 'Vendor official .d.ts as the type source',
status: 'cancelled',
},
];
const DIFF_LINES: DiffLineData[] = [
{
type: 'unchanged',
content: "import { H1 } from 'cursor/canvas';",
lineNumber: 1,
},
{ type: 'unchanged', content: '', lineNumber: 2 },
{
type: 'removed',
content: 'export default function Demo() {',
lineNumber: 3,
},
{
type: 'added',
content: 'export default function Showcase() {',
lineNumber: 3,
},
{ type: 'unchanged', content: ' return <H1>Hello</H1>;', lineNumber: 4 },
{ type: 'unchanged', content: '}', lineNumber: 5 },
];
const STEPS: { n: string; title: string; desc: string }[] = [
{
n: '1',
title: 'Author',
desc: 'Write a .canvas.tsx that imports only cursor/canvas.',
},
{
n: '2',
title: 'Alias',
desc: 'Vite + tsconfig redirect cursor/canvas at build time.',
},
{
n: '3',
title: 'Shim',
desc: 'Mantine implements the same API, props, and tones.',
},
{
n: '4',
title: 'Deploy',
desc: 'Static Vite build published to GitHub Pages.',
},
];
const DAG = computeDAGLayout({
nodes: [
{ id: 'Plan' },
{ id: 'Search' },
{ id: 'Edit' },
{ id: 'Test' },
{ id: 'Review' },
],
edges: [
{ from: 'Plan', to: 'Search' },
{ from: 'Search', to: 'Edit' },
{ from: 'Edit', to: 'Test' },
{ from: 'Test', to: 'Review' },
{ from: 'Review', to: 'Edit' },
],
direction: 'horizontal',
nodeWidth: 96,
nodeHeight: 34,
rankGap: 40,
nodeGap: 18,
});
/* -------------------------------------------------------------- subsections */
function Section({
title,
children,
}: {
title: string;
children: JSX.Element;
}) {
return (
<Stack gap={10}>
<H2>{title}</H2>
{children}
</Stack>
);
}
function DagDiagram() {
const theme = useHostTheme();
const labelOf = (id: string) => id;
return (
<div style={{ overflowX: 'auto' }}>
<svg
width={DAG.width}
height={DAG.height}
role="img"
aria-label="Agent loop graph"
>
<defs>
<marker
id="arrow"
viewBox="0 0 8 8"
refX={7}
refY={4}
markerWidth={6}
markerHeight={6}
orient="auto-start-reverse"
>
<path d="M0 0L8 4L0 8z" fill={theme.text.tertiary} />
</marker>
</defs>
{DAG.edges.map((edge) =>
edge.isBackEdge ? (
<path
key={`${edge.from}-${edge.to}`}
d={`M ${edge.sourceX} ${edge.sourceY} C ${edge.sourceX} ${edge.sourceY - 42}, ${edge.targetX} ${edge.targetY - 42}, ${edge.targetX} ${edge.targetY}`}
fill="none"
stroke={theme.accent.primary}
strokeWidth={1.5}
strokeDasharray="4 3"
markerEnd="url(#arrow)"
/>
) : (
<line
key={`${edge.from}-${edge.to}`}
x1={edge.sourceX}
y1={edge.sourceY}
x2={edge.targetX}
y2={edge.targetY}
stroke={theme.text.tertiary}
strokeWidth={1.5}
markerEnd="url(#arrow)"
/>
),
)}
{DAG.nodes.map((node) => (
<g key={node.id}>
<rect
x={node.x}
y={node.y}
width={96}
height={34}
rx={6}
fill={theme.fill.tertiary}
stroke={theme.stroke.primary}
/>
<text
x={node.x + 48}
y={node.y + 17}
textAnchor="middle"
dominantBaseline="central"
fontSize={12}
fill={theme.text.primary}
>
{labelOf(node.id)}
</text>
</g>
))}
</svg>
</div>
);
}
/* --------------------------------------------------------------------- main */
export default function DemoCanvas() {
const dispatch = useCanvasAction();
const [env, setEnv] = useCanvasState('show.env', 'production');
const [filter, setFilter] = useCanvasState('show.filter', '');
const [note, setNote] = useCanvasState('show.note', '');
const [verbose, setVerbose] = useCanvasState('show.verbose', false);
const [live, setLive] = useCanvasState('show.live', true);
const [tab, setTab] = useCanvasState('show.tab', 'charts');
const [selectedTodo, setSelectedTodo] = useCanvasState('show.todo', '3');
const theme = useHostTheme();
const dimmed = new Set(
TODOS.filter((t) => t.status === 'completed').map((t) => t.id),
);
return (
<Stack gap={20}>
{/* Header --------------------------------------------------------- */}
<Row gap={12}>
<H1>Cursor Canvas on the Web</H1>
<Spacer />
<Pill tone="info" active={live}>
{live ? 'live' : 'paused'}
</Pill>
<IconButton
title="Ask about this canvas in chat"
onClick={() =>
dispatch({
type: 'newComposerChat',
userPrompt: 'Explain this canvas',
})
}
>
?
</IconButton>
</Row>
<Text tone="secondary">
A real Cursor canvas, rendered on the web. It imports only{' '}
<Code>cursor/canvas</Code>; a build-time alias swaps that module for a
Mantine-backed shim, so one file renders in the IDE and on GitHub Pages.
Everything below is one component gallery exercising each shim module.
</Text>
{/* How it works (stepped explanation, not a callout) -------------- */}
<Section title="How it works">
<Grid columns={4} gap={12}>
{STEPS.map((step) => (
<Card key={step.n}>
<CardBody>
<Stack gap={6}>
<Pill tone="info">{step.n}</Pill>
<Text weight="semibold">{step.title}</Text>
<Text size="small" tone="secondary">
{step.desc}
</Text>
</Stack>
</CardBody>
</Card>
))}
</Grid>
</Section>
{/* Stats ---------------------------------------------------------- */}
<Grid columns={4} gap={12}>
<Card>
<CardBody>
<Stat value="35" label="Components" tone="info" />
</CardBody>
</Card>
<Card>
<CardBody>
<Stat value="3" label="Hooks" tone="success" />
</CardBody>
</Card>
<Card>
<CardBody>
<Stat value="1" label="Source of truth" />
</CardBody>
</Card>
<Card>
<CardBody>
<Stat value="0" label="Canvas edits to deploy" tone="success" />
</CardBody>
</Card>
</Grid>
{/* Charts, tables & context --------------------------------------- */}
<Section title="Charts, tables & context">
<Stack gap={12}>
<Row gap={8} wrap>
{(['charts', 'table', 'context'] as const).map((key) => (
<Pill key={key} active={tab === key} onClick={() => setTab(key)}>
{key}
</Pill>
))}
</Row>
{tab === 'charts' && (
<Grid columns={2} gap={16}>
<Card>
<CardHeader>Latency (ms)</CardHeader>
<CardBody>
<LineChart
categories={CATEGORIES}
series={LATENCY}
height={200}
valueSuffix="ms"
/>
</CardBody>
</Card>
<Card>
<CardHeader>Requests by source</CardHeader>
<CardBody>
<BarChart
categories={CATEGORIES}
series={REQUESTS}
height={200}
stacked
/>
</CardBody>
</Card>
<Card>
<CardHeader>Test outcomes</CardHeader>
<CardBody>
<Row justify="center">
<PieChart data={OUTCOMES} donut size={180} />
</Row>
</CardBody>
</Card>
<Card>
<CardHeader>Callout tones</CardHeader>
<CardBody>
<Stack gap={8}>
<Callout tone="success" title="Deployed">
Build published to Pages.
</Callout>
<Callout tone="warning">
Cooldown gate active for fresh packages.
</Callout>
<Callout tone="danger" title="Failing">
1 service degraded.
</Callout>
</Stack>
</CardBody>
</Card>
</Grid>
)}
{tab === 'table' && (
<Table
headers={['Service', 'Status', 'RPS']}
columnAlign={['left', 'left', 'right']}
rowTone={['success', undefined, 'danger']}
striped
rows={[
['api-gateway', <Pill tone="success">healthy</Pill>, '3.2k'],
['workers', <Pill tone="warning">hot</Pill>, '8.1k'],
['billing', <Pill tone="deleted">down</Pill>, '0'],
]}
/>
)}
{tab === 'context' && (
<Card>
<CardHeader>Context budget</CardHeader>
<CardBody>
<Stack gap={10}>
<UsageBar
total={120000}
topLeftLabel="64% full"
topRightLabel="76.8K / 120K tokens"
segments={[
{ id: 'tools', value: 24000, color: 'purple' },
{ id: 'rules', value: 12000, color: 'green' },
{ id: 'conversation', value: 28000, color: 'blue' },
{ id: 'skills', value: 12800, color: 'orange' },
]}
/>
<Row gap={14} wrap>
{CATEGORY_LEGEND.map((c) => (
<Row key={c.label} gap={6}>
<Swatch color={c.color} />
<Text size="small" tone="secondary">
{c.label}
</Text>
</Row>
))}
</Row>
</Stack>
</CardBody>
</Card>
)}
</Stack>
</Section>
{/* Graph ---------------------------------------------------------- */}
<Section title="Graph layout (computeDAGLayout)">
<Card>
<CardBody>
<Stack gap={8}>
<Text size="small" tone="secondary">
Pure layout math; rendering is the canvas's job. The dashed
accent edge is a detected back-edge (cycle).
</Text>
<DagDiagram />
</Stack>
</CardBody>
</Card>
</Section>
{/* Diff ----------------------------------------------------------- */}
<Section title="Diff">
<Card collapsible defaultOpen>
<CardHeader trailing={<DiffStats additions={1} deletions={1} />}>
demo.canvas.tsx
</CardHeader>
<CardBody style={{ padding: 0 }}>
<DiffView path="demo.canvas.tsx" lines={DIFF_LINES} />
</CardBody>
</Card>
</Section>
{/* Disclosure & tasks -------------------------------------------- */}
<Section title="Disclosure & tasks">
<Grid columns={2} gap={16}>
<Stack gap={8}>
<CollapsibleSection
title="Tools"
count={2}
leading={<Swatch color="purple" />}
trailing={
<Text size="small" tone="tertiary">
12.3k
</Text>
}
defaultOpen
>
<CollapsibleSection title="Grep">
<Text size="small" tone="secondary">
Nested borderless rows read as a tree.
</Text>
</CollapsibleSection>
<CollapsibleSection title="Edit">
<Text size="small" tone="secondary">
Each row toggles independently.
</Text>
</CollapsibleSection>
</CollapsibleSection>
<TodoListCard todos={TODOS} dimmedTodoIds={dimmed} defaultExpanded />
</Stack>
<Card>
<CardHeader>TodoList (selectable)</CardHeader>
<CardBody>
<TodoList
todos={TODOS}
onTodoClick={(t) => setSelectedTodo(t.id)}
/>
<Text size="small" tone="tertiary" style={{ marginTop: 8 }}>
Selected: {selectedTodo}
</Text>
</CardBody>
</Card>
</Grid>
</Section>
{/* Typography ----------------------------------------------------- */}
<Section title="Typography & text">
<Card>
<CardBody>
<Stack gap={6}>
<H3>Heading level 3</H3>
<Text>Primary body text in the default tone and weight.</Text>
<Text tone="secondary">Secondary — supporting copy.</Text>
<Text tone="tertiary" size="small">
Tertiary, small — captions and hints.
</Text>
<Text weight="semibold">Semibold emphasis.</Text>
<Text italic tone="secondary">
Italic remark.
</Text>
<Row gap={6} wrap>
<Pill tone="success">success</Pill>
<Pill tone="warning">warning</Pill>
<Pill tone="info" leadingContent={<Swatch color="blue" />}>
with swatch
</Pill>
<Pill tone="neutral" keyboardHint="⇧Tab">
hint
</Pill>
</Row>
</Stack>
</CardBody>
</Card>
</Section>
{/* Forms ---------------------------------------------------------- */}
<Section title="Forms (state persists via useCanvasState)">
<Card>
<CardBody>
<Stack gap={12}>
<Grid columns={2} gap={16}>
<Stack gap={6}>
<Text size="small" tone="secondary" weight="medium">
Environment
</Text>
<Select
value={env}
onChange={setEnv}
options={[
{ value: 'production', label: 'Production' },
{ value: 'staging', label: 'Staging' },
{ value: 'dev', label: 'Development' },
]}
/>
</Stack>
<Stack gap={6}>
<Text size="small" tone="secondary" weight="medium">
Filter
</Text>
<TextInput
value={filter}
onChange={setFilter}
placeholder="service name…"
/>
</Stack>
</Grid>
<Stack gap={6}>
<Text size="small" tone="secondary" weight="medium">
Release note
</Text>
<TextArea
value={note}
onChange={setNote}
placeholder="What changed…"
rows={2}
/>
</Stack>
<Row gap={16} wrap>
<Checkbox
label="Verbose logging"
checked={verbose}
onChange={setVerbose}
/>
<Row gap={8}>
<Text size="small" tone="secondary">
Live updates
</Text>
<Toggle checked={live} onChange={setLive} />
</Row>
<Spacer />
<Button variant="ghost">Reset</Button>
<Button variant="secondary">Preview</Button>
<Button
variant="primary"
onClick={() =>
dispatch({ type: 'openAgent', agentId: 'demo' })
}
>
Deploy
</Button>
</Row>
</Stack>
</CardBody>
</Card>
</Section>
{/* Theme tokens --------------------------------------------------- */}
<Section title="Theme tokens (useHostTheme)">
<Row gap={10} wrap>
{(['primary', 'secondary', 'tertiary', 'quaternary'] as const).map(
(key) => (
<div
key={key}
style={mergeStyle(
{
display: 'flex',
alignItems: 'center',
gap: 8,
padding: '6px 10px',
borderRadius: 6,
border: `1px solid ${theme.stroke.secondary}`,
},
{ background: theme.fill.quaternary },
)}
>
<span
style={{
width: 14,
height: 14,
borderRadius: 4,
background: theme.text[key],
}}
/>
<Text size="small" tone="secondary">
text.{key}
</Text>
</div>
),
)}
</Row>
</Section>
<Divider />
<Text size="small" tone="tertiary">
Imports only <Code>cursor/canvas</Code> · colors from{' '}
<Code>useHostTheme()</Code> · category hues from <Code>colorPalette</Code>{' '}
({Object.keys(colorPalette).length} hues) ·{' '}
<Link href="https://pages.github.com/">deployed to GitHub Pages</Link> ·
one file renders in both the IDE and the browser.
</Text>
</Stack>
);
}