@stacksjs/components 0.2.990 → 0.2.997

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 (95) hide show
  1. package/dist/ApiOperation-369y1zgt.stx +114 -0
  2. package/dist/ApiReference-ww45jj32.stx +92 -0
  3. package/dist/ApiSchema-dsx4tsv4.stx +47 -0
  4. package/dist/AreaChart-8fy34adp.stx +77 -0
  5. package/dist/BarChart-4xdrjhhn.stx +77 -0
  6. package/dist/{Combobox-9n04ec5w.stx → Combobox-fgbpxbty.stx} +174 -5
  7. package/dist/ComboboxOptions-sfkc98a9.stx +36 -0
  8. package/dist/{CommandPalette-cj6pe5ph.stx → CommandPalette-sf03s7b2.stx} +3 -1
  9. package/dist/LineChart-ctebkkm8.stx +77 -0
  10. package/dist/{List-5ey6k813.stx → List-mjkged1z.stx} +1 -1
  11. package/dist/{Notification-b3cz24f0.stx → Notification-1pvt347q.stx} +16 -4
  12. package/dist/{Popover-jjt7p7sn.stx → Popover-gntg33xd.stx} +1 -1
  13. package/dist/PricingPlan-h7jt2696.stx +67 -0
  14. package/dist/PricingTable-v3vdn6av.stx +149 -0
  15. package/dist/SecretField-q2xgth94.stx +219 -0
  16. package/dist/{Select-36s22z6d.stx → Select-yhmtvqcx.stx} +7 -4
  17. package/dist/Sparkline-gp859n4h.stx +29 -0
  18. package/dist/StatCard-gff0yx6b.stx +93 -0
  19. package/dist/StatGroup-e0d5dmt7.stx +17 -0
  20. package/dist/components/CodeBlock.d.ts +4 -1
  21. package/dist/components/CodeGroup.d.ts +34 -0
  22. package/dist/components/code-group.d.ts +19 -0
  23. package/dist/components/index.d.ts +2 -0
  24. package/dist/index.d.ts +13 -0
  25. package/dist/index.js +25 -22
  26. package/dist/ui/api-reference/ApiOperation.d.ts +17 -0
  27. package/dist/ui/api-reference/ApiReference.d.ts +28 -0
  28. package/dist/ui/api-reference/ApiSchema.d.ts +17 -0
  29. package/dist/ui/api-reference/index.d.ts +30 -0
  30. package/dist/ui/api-reference/openapi.d.ts +137 -0
  31. package/dist/ui/chart/AreaChart.d.ts +33 -0
  32. package/dist/ui/chart/BarChart.d.ts +33 -0
  33. package/dist/ui/chart/LineChart.d.ts +33 -0
  34. package/dist/ui/chart/Sparkline.d.ts +19 -0
  35. package/dist/ui/chart/core.d.ts +111 -0
  36. package/dist/ui/chart/index.d.ts +51 -0
  37. package/dist/ui/chart/interaction.d.ts +11 -0
  38. package/dist/ui/combobox/Combobox.d.ts +10 -1
  39. package/dist/ui/combobox/ComboboxOptions.d.ts +6 -0
  40. package/dist/ui/combobox/index.d.ts +7 -0
  41. package/dist/ui/command-palette/CommandPalette.d.ts +2 -0
  42. package/dist/ui/input/SecretField.d.ts +56 -0
  43. package/dist/ui/input/index.d.ts +21 -0
  44. package/dist/ui/notification/Notification.d.ts +2 -0
  45. package/dist/ui/pricing/PricingPlan.d.ts +21 -0
  46. package/dist/ui/pricing/PricingTable.d.ts +50 -0
  47. package/dist/ui/pricing/index.d.ts +38 -0
  48. package/dist/ui/pricing/pricing.d.ts +64 -0
  49. package/dist/ui/select/Select.d.ts +2 -0
  50. package/dist/ui/stat/StatCard.d.ts +42 -0
  51. package/dist/ui/stat/StatGroup.d.ts +12 -0
  52. package/dist/ui/stat/format.d.ts +37 -0
  53. package/dist/ui/stat/index.d.ts +23 -0
  54. package/package.json +3 -3
  55. package/src/components/CodeBlock.stx +4 -1
  56. package/src/components/CodeGroup.stx +217 -0
  57. package/src/components/code-group.ts +57 -0
  58. package/src/components/index.ts +2 -0
  59. package/src/index.ts +18 -0
  60. package/src/ui/api-reference/ApiOperation.stx +114 -0
  61. package/src/ui/api-reference/ApiReference.stx +92 -0
  62. package/src/ui/api-reference/ApiSchema.stx +47 -0
  63. package/src/ui/api-reference/README.md +44 -0
  64. package/src/ui/api-reference/index.ts +35 -0
  65. package/src/ui/api-reference/openapi.ts +825 -0
  66. package/src/ui/chart/AreaChart.stx +77 -0
  67. package/src/ui/chart/BarChart.stx +77 -0
  68. package/src/ui/chart/LineChart.stx +77 -0
  69. package/src/ui/chart/README.md +97 -0
  70. package/src/ui/chart/Sparkline.stx +29 -0
  71. package/src/ui/chart/core.ts +730 -0
  72. package/src/ui/chart/index.ts +71 -0
  73. package/src/ui/chart/interaction.ts +175 -0
  74. package/src/ui/combobox/Combobox.stx +174 -5
  75. package/src/ui/combobox/ComboboxOptions.stx +20 -0
  76. package/src/ui/combobox/README.md +41 -0
  77. package/src/ui/combobox/index.ts +13 -0
  78. package/src/ui/command-palette/CommandPalette.stx +3 -1
  79. package/src/ui/input/SecretField.stx +219 -0
  80. package/src/ui/input/index.ts +32 -0
  81. package/src/ui/list/List.stx +1 -1
  82. package/src/ui/notification/Notification.stx +16 -4
  83. package/src/ui/popover/Popover.stx +1 -1
  84. package/src/ui/pricing/PricingPlan.stx +67 -0
  85. package/src/ui/pricing/PricingTable.stx +149 -0
  86. package/src/ui/pricing/README.md +74 -0
  87. package/src/ui/pricing/index.ts +49 -0
  88. package/src/ui/pricing/pricing.ts +177 -0
  89. package/src/ui/select/Select.stx +7 -4
  90. package/src/ui/stat/README.md +60 -0
  91. package/src/ui/stat/StatCard.stx +93 -0
  92. package/src/ui/stat/StatGroup.stx +17 -0
  93. package/src/ui/stat/format.ts +147 -0
  94. package/src/ui/stat/index.ts +38 -0
  95. package/dist/ComboboxOptions-zhr9b6rx.stx +0 -16
@@ -0,0 +1,114 @@
1
+ <script server cache>
2
+ /*
3
+ * <ApiOperation> (stacksjs/stx#2324): one operation from buildApiReference,
4
+ * as a section with its own anchor: method and path, summary, description,
5
+ * deprecation, the auth it needs, parameters, the request body, each response
6
+ * with its schema, and request examples in curl, JavaScript and Python as a
7
+ * CodeGroup, so the reader's language follows them across the page.
8
+ */
9
+ export const op = $props.operation && typeof $props.operation === 'object' ? $props.operation : null
10
+ export const syncKey = $props.syncKey || 'code-language'
11
+ export const className = $props.className || ''
12
+ const methodTone = { GET: 'bg-info-soft text-info-soft-ink', POST: 'bg-success-soft text-success-soft-ink', PUT: 'bg-warning-soft text-warning-soft-ink', PATCH: 'bg-warning-soft text-warning-soft-ink', DELETE: 'bg-danger-soft text-danger-soft-ink' }
13
+ export const methodClasses = `inline-flex items-center rounded px-2 py-0.5 font-mono text-xs font-semibold ${op ? (methodTone[op.method] || 'bg-surface-sunken text-fg') : ''}`
14
+ export const groups = op ? ['path', 'query', 'header', 'cookie'].map(place => ({ place, title: `${place.charAt(0).toUpperCase()}${place.slice(1)} parameters`, items: op.parameters.filter(p => p.in === place) })).filter(g => g.items.length) : []
15
+ export const statusTone = op ? op.responses.map(r => r.status.startsWith('2') ? 'text-success' : r.status.startsWith('4') || r.status.startsWith('5') ? 'text-danger' : 'text-fg-soft') : []
16
+ </script>
17
+
18
+ @if(op)
19
+ <section id="{{ op.anchor }}" class="scroll-mt-20 border-t border-line py-10 {{ className }}" data-stx-api-operation="{{ op.id }}" aria-labelledby="{{ op.anchor }}-title">
20
+ <h3 id="{{ op.anchor }}-title" class="flex flex-wrap items-center gap-2 text-xl font-semibold text-fg">
21
+ <a href="#{{ op.anchor }}" class="hover:underline">{{ op.summary || op.method + ' ' + op.path }}</a>
22
+ @if(op.webhook)<span class="rounded bg-info-soft px-2 py-0.5 text-xs font-medium text-info-soft-ink">Webhook</span>@endif
23
+ @if(op.deprecated)<span class="rounded bg-warning-soft px-2 py-0.5 text-xs font-medium text-warning-soft-ink">Deprecated</span>@endif
24
+ </h3>
25
+ <p class="mt-2 flex items-center gap-2"><span class="{{ methodClasses }}">{{ op.method }}</span><code class="font-mono text-sm text-fg">{{ op.path }}</code></p>
26
+ @if(op.description)<p class="mt-3 text-sm text-fg-muted">{{ op.description }}</p>@endif
27
+
28
+ <p class="mt-3 text-sm text-fg-muted" data-stx-api-security>
29
+ @if(op.webhook)
30
+ Sent by the API to the URL you subscribe; answer with one of the responses below.
31
+ @elseif(op.security.length)
32
+ Requires
33
+ @foreach(op.security as i => alternative)@if(i > 0) or @endif@foreach(alternative as j => requirement)@if(j > 0) and @endif<a href="#{{ requirement.anchor }}" class="font-medium text-link hover:underline">{{ requirement.name }}</a>@if(requirement.scopes.length) ({{ requirement.scopes.join(', ') }})@endif@endforeach@endforeach
34
+ @else
35
+ No authentication.
36
+ @endif
37
+ </p>
38
+
39
+ @foreach(groups as group)
40
+ <h4 class="mt-6 text-sm font-semibold text-fg">{{ group.title }}</h4>
41
+ <table class="mt-2 w-full border-collapse text-left text-sm">
42
+ <caption class="sr-only">{{ group.title }}</caption>
43
+ <thead>
44
+ <tr class="border-b border-line text-xs uppercase tracking-wide text-fg-soft"><th scope="col" class="py-2 pr-4 font-medium">Name</th><th scope="col" class="py-2 pr-4 font-medium">Type</th><th scope="col" class="py-2 font-medium">Description</th></tr>
45
+ </thead>
46
+ <tbody>
47
+ @foreach(group.items as param)
48
+ <tr class="border-b border-line align-top">
49
+ <th scope="row" class="py-2 pr-4 font-mono text-[13px] font-normal text-fg">{{ param.name }}@if(param.required)<span class="ml-1 text-danger" aria-hidden="true">*</span><span class="sr-only"> (required)</span>@endif</th>
50
+ <td class="py-2 pr-4 font-mono text-[13px] text-fg-muted">{{ param.type }}</td>
51
+ <td class="py-2 text-fg-muted">{{ param.description }}@if(param.deprecated) <span class="text-warning">Deprecated.</span>@endif</td>
52
+ </tr>
53
+ @endforeach
54
+ </tbody>
55
+ </table>
56
+ @endforeach
57
+
58
+ @if(op.body)
59
+ <h4 class="mt-6 text-sm font-semibold text-fg">Request body <span class="font-mono text-xs font-normal text-fg-soft">{{ op.body.mediaType }}</span>@if(op.body.ref) <a href="#{{ op.body.ref }}" class="font-mono text-xs font-normal text-link hover:underline">{{ op.body.schema }}</a>@endif@if(op.body.required)<span class="ml-1 text-xs font-normal text-danger">required</span>@endif</h4>
60
+ @if(op.body.description)<p class="mt-1 text-sm text-fg-muted">{{ op.body.description }}</p>@endif
61
+ <ApiSchema :rows="op.body.rows" caption="Request body fields" />
62
+ @if(op.body.example)
63
+ <details class="mt-3" data-stx-api-example="request">
64
+ <summary class="cursor-pointer text-sm text-fg-muted">Example body</summary>
65
+ <div class="mt-2"><CodeBlock language="json" :code="op.body.example" /></div>
66
+ </details>
67
+ @endif
68
+ @endif
69
+
70
+ <h4 class="mt-6 text-sm font-semibold text-fg">Responses</h4>
71
+ <dl class="mt-2 space-y-4">
72
+ @foreach(op.responses as i => response)
73
+ <div data-stx-api-response="{{ response.status }}">
74
+ <dt class="flex items-center gap-2 text-sm"><span class="font-mono font-semibold {{ statusTone[i] }}">{{ response.status }}</span><span class="text-fg-muted">{{ response.description }}</span>@if(response.ref)<a href="#{{ response.ref }}" class="font-mono text-xs text-link hover:underline">{{ response.schema }}</a>@endif@if(response.mediaType)<span class="font-mono text-xs text-fg-soft">{{ response.mediaType }}</span>@endif</dt>
75
+ @if(response.headers.length)
76
+ <dd class="mt-2">
77
+ <table class="w-full border-collapse text-left text-sm">
78
+ <caption class="sr-only">{{ response.status }} response headers</caption>
79
+ <thead><tr class="border-b border-line text-xs uppercase tracking-wide text-fg-soft"><th scope="col" class="py-2 pr-4 font-medium">Header</th><th scope="col" class="py-2 pr-4 font-medium">Type</th><th scope="col" class="py-2 font-medium">Description</th></tr></thead>
80
+ <tbody>
81
+ @foreach(response.headers as header)
82
+ <tr class="border-b border-line align-top"><th scope="row" class="py-2 pr-4 font-mono text-[13px] font-normal text-fg">{{ header.name }}</th><td class="py-2 pr-4 font-mono text-[13px] text-fg-muted">{{ header.type }}</td><td class="py-2 text-fg-muted">{{ header.description }}</td></tr>
83
+ @endforeach
84
+ </tbody>
85
+ </table>
86
+ </dd>
87
+ @endif
88
+ @if(response.rows.length)
89
+ <dd><ApiSchema :rows="response.rows" :caption="response.status + ' response fields'" /></dd>
90
+ @endif
91
+ @if(response.example && response.example !== 'null')
92
+ <dd>
93
+ <details class="mt-2" data-stx-api-example="{{ response.status }}">
94
+ <summary class="cursor-pointer text-sm text-fg-muted">Example response</summary>
95
+ <div class="mt-2"><CodeBlock language="json" :code="response.example" /></div>
96
+ </details>
97
+ </dd>
98
+ @endif
99
+ </div>
100
+ @endforeach
101
+ </dl>
102
+
103
+ @if(op.examples)
104
+ <h4 class="mt-6 text-sm font-semibold text-fg">Example request</h4>
105
+ <div class="mt-2">
106
+ <CodeGroup :syncKey="syncKey" label="Example request">
107
+ <CodeBlock language="bash" label="curl" :code="op.examples.curl" />
108
+ <CodeBlock language="javascript" label="JavaScript" :code="op.examples.javascript" />
109
+ <CodeBlock language="python" label="Python" :code="op.examples.python" />
110
+ </CodeGroup>
111
+ </div>
112
+ @endif
113
+ </section>
114
+ @endif
@@ -0,0 +1,92 @@
1
+ <script server>
2
+ import { buildApiReference, loadOpenApi } from './openapi'
3
+
4
+ /*
5
+ * <ApiReference> (stacksjs/stx#2324): a whole API reference from an OpenAPI
6
+ * 3.0 or 3.1 document, read at build time.
7
+ *
8
+ * <ApiReference spec="./openapi.json" />
9
+ *
10
+ * `spec` is a path (JSON or YAML), a URL, or the parsed document. Everything
11
+ * is rendered on the server: a table of contents, the authentication
12
+ * schemes, each tag's operations (<ApiOperation>) and every named schema
13
+ * (<ApiSchema>), each with a stable anchor. No script is needed to read it;
14
+ * the examples' language tabs are a CodeGroup.
15
+ *
16
+ * A document that does not validate throws an OpenApiError naming the
17
+ * offending path. It is a declared build failure (`stxBuildFailure`), so a
18
+ * static build or `build-views` stops with that message instead of writing
19
+ * the page without its reference; in development it is logged and the
20
+ * component renders a notice in its place.
21
+ */
22
+ export const reference = buildApiReference(await loadOpenApi($props.spec), { baseUrl: $props.baseUrl || '' })
23
+ export const syncKey = $props.syncKey || 'code-language'
24
+ export const showToc = $bool($props.toc, true)
25
+ export const className = $props.className || ''
26
+ /* The search index entries, for a docs site's search to read off the page (escaped as an attribute). */
27
+ export const searchJson = JSON.stringify(reference.search)
28
+ </script>
29
+
30
+ @if(reference)
31
+ <div class="lg:grid lg:grid-cols-[16rem_minmax(0,1fr)] lg:gap-10 {{ className }}" data-stx-api-reference data-search-index="{{ searchJson }}">
32
+ @if(showToc)
33
+ <nav class="mb-8 text-sm lg:sticky lg:top-6 lg:mb-0 lg:max-h-[calc(100vh-3rem)] lg:self-start lg:overflow-y-auto" aria-label="API reference">
34
+ <ul class="space-y-4" role="list">
35
+ @foreach(reference.toc as section)
36
+ <li>
37
+ <a href="#{{ section.anchor }}" class="font-semibold text-fg hover:underline">{{ section.text }}</a>
38
+ <ul class="mt-2 space-y-1 border-l border-line pl-3" role="list">
39
+ @foreach(section.children as item)
40
+ <li><a href="#{{ item.anchor }}" class="flex items-baseline gap-2 text-fg-muted hover:text-fg">@if(item.method)<span class="w-12 shrink-0 font-mono text-[10px] font-semibold text-fg-soft">{{ item.method }}</span>@endif<span>{{ item.text }}</span></a></li>
41
+ @endforeach
42
+ </ul>
43
+ </li>
44
+ @endforeach
45
+ </ul>
46
+ </nav>
47
+ @endif
48
+
49
+ <div class="min-w-0">
50
+ <header>
51
+ <h1 class="text-3xl font-semibold tracking-tight text-fg">{{ reference.title }}@if(reference.version) <span class="align-middle text-sm font-normal text-fg-soft">v{{ reference.version }}</span>@endif</h1>
52
+ @if(reference.description)<p class="mt-2 text-fg-muted">{{ reference.description }}</p>@endif
53
+ <p class="mt-2 text-sm text-fg-soft">Base URL <code class="font-mono text-fg">{{ reference.baseUrl }}</code></p>
54
+ </header>
55
+
56
+ @if(reference.securitySchemes.length)
57
+ <section id="authentication" class="scroll-mt-20 mt-10" aria-labelledby="authentication-title">
58
+ <h2 id="authentication-title" class="text-2xl font-semibold text-fg"><a href="#authentication" class="hover:underline">Authentication</a></h2>
59
+ <dl class="mt-4 space-y-3">
60
+ @foreach(reference.securitySchemes as scheme)
61
+ <div id="{{ scheme.anchor }}" class="scroll-mt-20" data-stx-api-security-scheme="{{ scheme.name }}">
62
+ <dt class="font-mono text-sm font-semibold text-fg">{{ scheme.name }}</dt>
63
+ <dd class="text-sm text-fg-muted">{{ scheme.summary }}@if(scheme.description). {{ scheme.description }}@endif</dd>
64
+ </div>
65
+ @endforeach
66
+ </dl>
67
+ </section>
68
+ @endif
69
+
70
+ @foreach(reference.tags as tag)
71
+ <section id="{{ tag.anchor }}" class="scroll-mt-20 mt-12" aria-labelledby="{{ tag.anchor }}-title" data-stx-api-tag="{{ tag.name }}">
72
+ <h2 id="{{ tag.anchor }}-title" class="text-2xl font-semibold text-fg"><a href="#{{ tag.anchor }}" class="hover:underline">{{ tag.name }}</a></h2>
73
+ @if(tag.description)<p class="mt-2 text-fg-muted">{{ tag.description }}</p>@endif
74
+ @foreach(tag.operations as operation)
75
+ <ApiOperation :operation="operation" :syncKey="syncKey" />
76
+ @endforeach
77
+ </section>
78
+ @endforeach
79
+
80
+ @if(reference.schemas.length)
81
+ <section id="schemas" class="scroll-mt-20 mt-12 space-y-10" aria-labelledby="schemas-title">
82
+ <h2 id="schemas-title" class="text-2xl font-semibold text-fg"><a href="#schemas" class="hover:underline">Schemas</a></h2>
83
+ @foreach(reference.schemas as schema)
84
+ <ApiSchema :schema="schema" />
85
+ @endforeach
86
+ </section>
87
+ @endif
88
+ </div>
89
+ </div>
90
+ @else
91
+ <div role="alert" class="rounded-panel border border-danger-line bg-danger-soft p-4 text-sm text-danger-soft-ink {{ className }}" data-stx-api-reference-error>The API reference could not be rendered: its OpenAPI document is invalid. The build log names the offending path.</div>
92
+ @endif
@@ -0,0 +1,47 @@
1
+ <script server cache>
2
+ /*
3
+ * <ApiSchema> (stacksjs/stx#2324): a schema's fields as a table. Given a
4
+ * named `schema` from buildApiReference it is a section with its own anchor
5
+ * (`schema-<name>`); given only `rows` it is the table, as an operation's
6
+ * body and responses use it. A field whose type is a named schema links to
7
+ * that schema's section rather than expanding it again.
8
+ */
9
+ const named = $props.schema && typeof $props.schema === 'object' ? $props.schema : null
10
+ export const schema = named
11
+ export const rows = named ? named.rows : (Array.isArray($props.rows) ? $props.rows : [])
12
+ export const caption = $props.caption || (named ? `${named.name} fields` : 'Fields')
13
+ export const className = $props.className || ''
14
+ export const indent = rows.map(row => `padding-left: ${row.depth * 1.25}rem`)
15
+ </script>
16
+
17
+ @if(schema)
18
+ <section id="{{ schema.anchor }}" class="scroll-mt-20 {{ className }}" data-stx-api-schema="{{ schema.name }}" aria-labelledby="{{ schema.anchor }}-title">
19
+ <h3 id="{{ schema.anchor }}-title" class="text-lg font-semibold text-fg"><a href="#{{ schema.anchor }}" class="hover:underline">{{ schema.name }}</a> <span class="ml-1 font-mono text-sm font-normal text-fg-soft">{{ schema.type }}</span></h3>
20
+ @if(schema.description)<p class="mt-1 text-sm text-fg-muted">{{ schema.description }}</p>@endif
21
+ @endif
22
+ @if(rows.length)
23
+ <div class="mt-3 overflow-x-auto">
24
+ <table class="w-full border-collapse text-left text-sm">
25
+ <caption class="sr-only">{{ caption }}</caption>
26
+ <thead>
27
+ <tr class="border-b border-line text-xs uppercase tracking-wide text-fg-soft">
28
+ <th scope="col" class="py-2 pr-4 font-medium">Field</th>
29
+ <th scope="col" class="py-2 pr-4 font-medium">Type</th>
30
+ <th scope="col" class="py-2 font-medium">Description</th>
31
+ </tr>
32
+ </thead>
33
+ <tbody>
34
+ @foreach(rows as i => row)
35
+ <tr class="border-b border-line align-top">
36
+ <th scope="row" class="py-2 pr-4 font-mono text-[13px] font-normal text-fg" style="{{ indent[i] }}">{{ row.name }}@if(row.required)<span class="ml-1 text-danger" aria-hidden="true">*</span><span class="sr-only"> (required)</span>@endif</th>
37
+ <td class="py-2 pr-4 font-mono text-[13px] text-fg-muted">@if(row.ref)<a href="#{{ row.ref }}" class="text-link hover:underline">{{ row.type }}</a>@else{{ row.type }}@endif</td>
38
+ <td class="py-2 text-fg-muted">{{ row.description }}@foreach(row.notes as note)<span class="ml-1 inline-block rounded bg-surface-sunken px-1.5 py-0.5 font-mono text-xs text-fg-soft">{{ note }}</span>@endforeach</td>
39
+ </tr>
40
+ @endforeach
41
+ </tbody>
42
+ </table>
43
+ </div>
44
+ @endif
45
+ @if(schema)
46
+ </section>
47
+ @endif
@@ -0,0 +1,77 @@
1
+ <script server>
2
+ import { prepareChart } from './core'
3
+
4
+ /*
5
+ * <AreaChart> (stacksjs/stx#2318). The SVG is drawn here, on the server, so
6
+ * the chart shows before any script runs; the client script only lays it out
7
+ * again at its real width and adds the crosshair, tooltip and keyboard.
8
+ *
9
+ * The data and the accessors stay on the server (`serverOnlyProps`): the
10
+ * client gets the prepared model, every label already formatted, so a
11
+ * `formatY` function works and the records are not shipped twice.
12
+ */
13
+ export const serverOnlyProps = ['data', 'x', 'y', 'series', 'formatX', 'formatY', 'colors', 'yDomain']
14
+ const chart = prepareChart('area', $props)
15
+ export const label = $props.label || 'Chart'
16
+ export const loading = $bool($props.loading)
17
+ export const emptyText = $props.emptyText || 'No data to show yet'
18
+ export const empty = chart.empty
19
+ export const svg = chart.svg
20
+ export const modelJson = chart.modelJson
21
+ export const summary = chart.summary
22
+ export const tableHead = chart.tableHead
23
+ export const tableRows = chart.tableRows
24
+ export const legend = chart.legend
25
+ export const height = chart.model.height
26
+ export const chartId = $props.id || `chart-${$uid}`
27
+ export const summaryId = `${chartId}-summary`
28
+ export const className = $props.className || ''
29
+ export const figureClasses = `relative ${className}`.trim()
30
+ export const surfaceClasses = 'relative rounded-control outline-none focus-visible:ring-2 focus-visible:ring-accent'
31
+ export const tooltipClasses = 'pointer-events-none absolute top-2 z-10 min-w-32 rounded-panel bg-inverse px-3 py-2 text-xs text-inverse-ink shadow-lg'
32
+ </script>
33
+
34
+ <script client>
35
+ import { mountChart } from './interaction'
36
+
37
+ const emit = defineEmits(['hover'])
38
+ const chartId = {{ chartId }}
39
+ const model = {!! modelJson !!}
40
+ let dispose = null
41
+
42
+ onMount(() => {
43
+ const root = document.getElementById(chartId)
44
+ if (root) dispose = mountChart(root, model, { onHover: detail => emit('hover', detail) })
45
+ })
46
+
47
+ onDestroy(() => { if (dispose) dispose() })
48
+ </script>
49
+
50
+ <figure id="{{ chartId }}" class="{{ figureClasses }}" data-stx-chart="area" @if(!empty && !loading)tabindex="0" role="group" aria-label="{{ label }}" aria-describedby="{{ summaryId }}"@endif>
51
+ @if(loading)
52
+ <div aria-busy="true"><span class="sr-only">Loading {{ label }}</span><Skeleton variant="rect" :height="height" width="100%" /></div>
53
+ @elseif(empty)
54
+ <EmptyState variant="bare" :title="emptyText" />
55
+ @else
56
+ @if(legend.length)
57
+ <ul class="mb-2 flex flex-wrap gap-x-4 gap-y-1 text-xs text-fg-soft" aria-hidden="true" data-stx-chart-legend>
58
+ @foreach(legend as item)
59
+ <li class="inline-flex items-center gap-1.5"><span class="inline-block h-2 w-2 rounded-full" style="background: {{ item.color }}"></span>{{ item.label }}</li>
60
+ @endforeach
61
+ </ul>
62
+ @endif
63
+ <div class="{{ surfaceClasses }}" data-stx-chart-surface>{!! svg !!}</div>
64
+ <div class="{{ tooltipClasses }}" data-stx-chart-tooltip hidden aria-hidden="true"></div>
65
+ <p id="{{ summaryId }}" class="sr-only">{{ summary }}. Use the arrow keys to step through the points.</p>
66
+ <table class="sr-only" data-stx-chart-table>
67
+ <caption>{{ label }}</caption>
68
+ <thead><tr>@foreach(tableHead as heading)<th scope="col">{{ heading }}</th>@endforeach</tr></thead>
69
+ <tbody>
70
+ @foreach(tableRows as row)
71
+ <tr>@foreach(row as cell)<td>{{ cell }}</td>@endforeach</tr>
72
+ @endforeach
73
+ </tbody>
74
+ </table>
75
+ <span class="sr-only" role="status" aria-live="polite" data-stx-chart-live></span>
76
+ @endif
77
+ </figure>
@@ -0,0 +1,77 @@
1
+ <script server>
2
+ import { prepareChart } from './core'
3
+
4
+ /*
5
+ * <BarChart> (stacksjs/stx#2318). The SVG is drawn here, on the server, so
6
+ * the chart shows before any script runs; the client script only lays it out
7
+ * again at its real width and adds the crosshair, tooltip and keyboard.
8
+ *
9
+ * The data and the accessors stay on the server (`serverOnlyProps`): the
10
+ * client gets the prepared model, every label already formatted, so a
11
+ * `formatY` function works and the records are not shipped twice.
12
+ */
13
+ export const serverOnlyProps = ['data', 'x', 'y', 'series', 'formatX', 'formatY', 'colors', 'yDomain']
14
+ const chart = prepareChart('bar', $props)
15
+ export const label = $props.label || 'Chart'
16
+ export const loading = $bool($props.loading)
17
+ export const emptyText = $props.emptyText || 'No data to show yet'
18
+ export const empty = chart.empty
19
+ export const svg = chart.svg
20
+ export const modelJson = chart.modelJson
21
+ export const summary = chart.summary
22
+ export const tableHead = chart.tableHead
23
+ export const tableRows = chart.tableRows
24
+ export const legend = chart.legend
25
+ export const height = chart.model.height
26
+ export const chartId = $props.id || `chart-${$uid}`
27
+ export const summaryId = `${chartId}-summary`
28
+ export const className = $props.className || ''
29
+ export const figureClasses = `relative ${className}`.trim()
30
+ export const surfaceClasses = 'relative rounded-control outline-none focus-visible:ring-2 focus-visible:ring-accent'
31
+ export const tooltipClasses = 'pointer-events-none absolute top-2 z-10 min-w-32 rounded-panel bg-inverse px-3 py-2 text-xs text-inverse-ink shadow-lg'
32
+ </script>
33
+
34
+ <script client>
35
+ import { mountChart } from './interaction'
36
+
37
+ const emit = defineEmits(['hover'])
38
+ const chartId = {{ chartId }}
39
+ const model = {!! modelJson !!}
40
+ let dispose = null
41
+
42
+ onMount(() => {
43
+ const root = document.getElementById(chartId)
44
+ if (root) dispose = mountChart(root, model, { onHover: detail => emit('hover', detail) })
45
+ })
46
+
47
+ onDestroy(() => { if (dispose) dispose() })
48
+ </script>
49
+
50
+ <figure id="{{ chartId }}" class="{{ figureClasses }}" data-stx-chart="bar" @if(!empty && !loading)tabindex="0" role="group" aria-label="{{ label }}" aria-describedby="{{ summaryId }}"@endif>
51
+ @if(loading)
52
+ <div aria-busy="true"><span class="sr-only">Loading {{ label }}</span><Skeleton variant="rect" :height="height" width="100%" /></div>
53
+ @elseif(empty)
54
+ <EmptyState variant="bare" :title="emptyText" />
55
+ @else
56
+ @if(legend.length)
57
+ <ul class="mb-2 flex flex-wrap gap-x-4 gap-y-1 text-xs text-fg-soft" aria-hidden="true" data-stx-chart-legend>
58
+ @foreach(legend as item)
59
+ <li class="inline-flex items-center gap-1.5"><span class="inline-block h-2 w-2 rounded-full" style="background: {{ item.color }}"></span>{{ item.label }}</li>
60
+ @endforeach
61
+ </ul>
62
+ @endif
63
+ <div class="{{ surfaceClasses }}" data-stx-chart-surface>{!! svg !!}</div>
64
+ <div class="{{ tooltipClasses }}" data-stx-chart-tooltip hidden aria-hidden="true"></div>
65
+ <p id="{{ summaryId }}" class="sr-only">{{ summary }}. Use the arrow keys to step through the points.</p>
66
+ <table class="sr-only" data-stx-chart-table>
67
+ <caption>{{ label }}</caption>
68
+ <thead><tr>@foreach(tableHead as heading)<th scope="col">{{ heading }}</th>@endforeach</tr></thead>
69
+ <tbody>
70
+ @foreach(tableRows as row)
71
+ <tr>@foreach(row as cell)<td>{{ cell }}</td>@endforeach</tr>
72
+ @endforeach
73
+ </tbody>
74
+ </table>
75
+ <span class="sr-only" role="status" aria-live="polite" data-stx-chart-live></span>
76
+ @endif
77
+ </figure>
@@ -3,6 +3,15 @@ export const value = $props.value
3
3
  export const valueJson = JSON.stringify(value === undefined ? null : value)
4
4
  export const className = $props.className || ''
5
5
  export const as = $props.as || 'div'
6
+ /*
7
+ * Remote suggestions (stacksjs/stx#2319). `source` is a client function,
8
+ * `(query, { signal }) => Promise<Option[]>`, so it can only arrive through a
9
+ * bound attribute (`:source="search"`) and the client reads it live. These are
10
+ * the knobs around it.
11
+ */
12
+ export const debounce = $num($props.debounce, 150)
13
+ export const minLength = $num($props.minLength, 1)
14
+ export const limit = $num($props.limit, 0)
6
15
 
7
16
  const baseClasses = 'relative'
8
17
  export const comboboxClasses = `${baseClasses} ${className}`.trim()
@@ -25,6 +34,26 @@ const comboboxInstanceId = {{ comboboxInstanceId }}
25
34
  * (stacksjs/stx#1981).
26
35
  */
27
36
  const initialValue = {!! valueJson !!}
37
+ const liveSource = useReactiveProp('source', null, { parse: v => v })
38
+ const isRemote = () => typeof liveSource() === 'function'
39
+ /*
40
+ * The WHEN of remote suggestions (debounce, abort, out-of-order answers) is
41
+ * useSuggestions; this file draws what it answers into the options panel.
42
+ */
43
+ const remote = useSuggestions({
44
+ source: (q, ctx) => {
45
+ const source = liveSource()
46
+ return typeof source === 'function' ? source(q, ctx) : []
47
+ },
48
+ debounceMs: {{ debounce }},
49
+ minLength: {{ minLength }},
50
+ limit: {{ limit }} > 0 ? {{ limit }} : undefined,
51
+ onSearch: q => emit('search', q),
52
+ onResults: (q, options) => emit('results', q, options),
53
+ onError: (error, q) => emit('error', error, q),
54
+ })
55
+ /* The value each rendered remote option selects, by element. */
56
+ const remoteValues = new WeakMap()
28
57
 
29
58
  const isOpen = state(false)
30
59
  const selected = state(initialValue)
@@ -58,10 +87,13 @@ function syncOptionStates() {
58
87
  if (!rootEl) return
59
88
  const options = rootEl.querySelectorAll('[data-stx-combobox-option]')
60
89
  for (const opt of options) {
61
- const raw = opt.dataset.optionValue
62
90
  let v
63
- try { v = JSON.parse(raw ?? 'null') }
64
- catch { v = raw }
91
+ if (remoteValues.has(opt)) v = remoteValues.get(opt)
92
+ else {
93
+ const raw = opt.dataset.optionValue
94
+ try { v = JSON.parse(raw ?? 'null') }
95
+ catch { v = raw }
96
+ }
65
97
  const sel = isSelectedValue(v)
66
98
  opt.setAttribute('aria-selected', sel ? 'true' : 'false')
67
99
  opt.setAttribute('data-selected', sel ? 'true' : 'false')
@@ -198,8 +230,126 @@ function onQuery(event) {
198
230
  syncActiveOption()
199
231
  if (!isOpen()) open()
200
232
  emit('query', event.detail)
233
+ if (isRemote()) remote.search(event.detail)
234
+ }
235
+
236
+ /*
237
+ * Remote options are plain strings or `{ value, label, description, disabled }`.
238
+ * An object without `value` selects itself, so a geocoder result keeps its
239
+ * coordinates through the change event.
240
+ */
241
+ function optionLabel(option) {
242
+ if (option == null) return ''
243
+ if (typeof option !== 'object') return String(option)
244
+ if (option.label != null) return String(option.label)
245
+ if (option.name != null) return String(option.name)
246
+ return option.value != null ? String(option.value) : ''
247
+ }
248
+ function optionValue(option) {
249
+ if (option != null && typeof option === 'object' && 'value' in option) return option.value
250
+ return option
201
251
  }
202
252
 
253
+ /*
254
+ * Draw the remote results into the options panel, after any static options,
255
+ * with text nodes only: a suggestion is data and is never parsed as markup.
256
+ */
257
+ function renderRemote() {
258
+ if (!rootEl) return
259
+ const list = rootEl.querySelector('[data-stx-combobox-options]')
260
+ if (!list) return
261
+ for (const old of list.querySelectorAll('[data-stx-combobox-remote]')) old.remove()
262
+ const results = remote.results()
263
+ const tag = list.tagName === 'UL' || list.tagName === 'OL' ? 'li' : 'div'
264
+ const anchor = list.querySelector('[data-stx-combobox-status]')
265
+ const doc = rootEl.ownerDocument
266
+ for (const option of results) {
267
+ const el = doc.createElement(tag)
268
+ el.className = 'relative cursor-pointer select-none py-2 pl-10 pr-4 text-fg hover:bg-accent-soft'
269
+ el.setAttribute('data-stx-combobox-option', '')
270
+ el.setAttribute('data-stx-combobox-remote', '')
271
+ el.setAttribute('role', 'option')
272
+ el.setAttribute('aria-selected', 'false')
273
+ const disabled = !!(option && typeof option === 'object' && option.disabled)
274
+ if (disabled) {
275
+ el.setAttribute('aria-disabled', 'true')
276
+ el.className += ' opacity-50 cursor-not-allowed'
277
+ }
278
+ const check = doc.createElement('span')
279
+ check.className = 'absolute inset-y-0 left-0 flex items-center pl-3 text-accent'
280
+ check.setAttribute('data-stx-combobox-check', '')
281
+ check.setAttribute('aria-hidden', 'true')
282
+ const svg = doc.createElementNS('http://www.w3.org/2000/svg', 'svg')
283
+ svg.setAttribute('class', 'h-5 w-5')
284
+ svg.setAttribute('viewBox', '0 0 20 20')
285
+ svg.setAttribute('fill', 'currentColor')
286
+ const path = doc.createElementNS('http://www.w3.org/2000/svg', 'path')
287
+ path.setAttribute('fill-rule', 'evenodd')
288
+ path.setAttribute('clip-rule', 'evenodd')
289
+ path.setAttribute('d', 'M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z')
290
+ svg.appendChild(path)
291
+ check.appendChild(svg)
292
+ el.appendChild(check)
293
+ const label = doc.createElement('span')
294
+ label.className = 'block truncate'
295
+ label.textContent = optionLabel(option)
296
+ el.appendChild(label)
297
+ if (option && typeof option === 'object' && option.description != null) {
298
+ const description = doc.createElement('span')
299
+ description.className = 'block truncate text-xs text-fg-soft'
300
+ description.textContent = String(option.description)
301
+ el.appendChild(description)
302
+ }
303
+ const value = optionValue(option)
304
+ remoteValues.set(el, value)
305
+ el.addEventListener('click', (event) => {
306
+ if (disabled) return
307
+ const input = rootEl && rootEl.querySelector('[data-stx-combobox-input]')
308
+ if (input) input.value = optionLabel(option)
309
+ event.currentTarget.dispatchEvent(new CustomEvent('stx:combobox-select', { bubbles: true, detail: value }))
310
+ })
311
+ list.insertBefore(el, anchor)
312
+ }
313
+ activeIndex.set(-1)
314
+ syncOptionStates()
315
+ syncActiveOption()
316
+ }
317
+
318
+ /*
319
+ * Which of the panel's states is showing: `idle` (no query yet, panel hidden),
320
+ * `loading`, `results`, `empty` or `error`. The slots in <ComboboxOptions>
321
+ * are shown by CSS on this attribute; the live region says the same thing
322
+ * once, politely, since the status rows are hidden from the accessibility
323
+ * tree to keep the listbox to its options.
324
+ */
325
+ const asyncState = derived(() => {
326
+ if (!isRemote()) return ''
327
+ if (remote.loading()) return remote.results().length ? 'results' : 'loading'
328
+ if (remote.error()) return 'error'
329
+ if (remote.empty()) return 'empty'
330
+ return remote.results().length ? 'results' : 'idle'
331
+ })
332
+ const announcement = derived(() => {
333
+ if (!isRemote() || remote.loading()) return ''
334
+ if (remote.error()) return 'Suggestions could not be loaded.'
335
+ if (remote.empty()) return 'No results.'
336
+ const count = remote.results().length
337
+ if (!count) return ''
338
+ return count === 1 ? '1 result available.' : count + ' results available.'
339
+ })
340
+
341
+ // A subscription, not an effect: only the results decide when to redraw, and
342
+ // an effect would also track the active index the redraw reads and resets.
343
+ // Managed in onMount/onDestroy so `:if` hide/show cycles re-subscribe cleanly.
344
+ let stopRemoteRender = null
345
+
346
+ effect(() => {
347
+ const busy = isRemote() && remote.loading()
348
+ if (!rootEl) return
349
+ const list = rootEl.querySelector('[data-stx-combobox-options]')
350
+ if (list) list.setAttribute('aria-busy', busy ? 'true' : 'false')
351
+ })
352
+
203
353
  /*
204
354
  * Escape dismisses it, from anywhere rather than only from inside the panel.
205
355
  *
@@ -232,6 +382,9 @@ onMount(() => {
232
382
  }
233
383
  document.addEventListener('click', outsideHandler, true)
234
384
  document.addEventListener('keydown', onComboboxKey)
385
+ stopRemoteRender?.()
386
+ stopRemoteRender = remote.results.subscribe(() => renderRemote())
387
+ renderRemote()
235
388
  })
236
389
 
237
390
  /*
@@ -248,6 +401,9 @@ effect(() => {
248
401
  })
249
402
 
250
403
  onDestroy(() => {
404
+ remote.cancel()
405
+ stopRemoteRender?.()
406
+ stopRemoteRender = null
251
407
  window.stx.overlays.close(comboboxInstanceId)
252
408
  if (outsideHandler) document.removeEventListener('click', outsideHandler, true)
253
409
  document.removeEventListener('keydown', onComboboxKey)
@@ -255,7 +411,7 @@ onDestroy(() => {
255
411
 
256
412
  const dataState = derived(() => isOpen() ? 'open' : 'closed')
257
413
 
258
- defineExpose({ isOpen, selected, query, activeIndex, toggle, open, close, selectValue })
414
+ defineExpose({ isOpen, selected, query, activeIndex, toggle, open, close, selectValue, results: remote.results, loading: remote.loading, error: remote.error })
259
415
  </script>
260
416
 
261
417
  <component
@@ -264,15 +420,28 @@ defineExpose({ isOpen, selected, query, activeIndex, toggle, open, close, select
264
420
  data-stx-combobox
265
421
  data-combobox-id="{{ comboboxInstanceId }}"
266
422
  :data-state="dataState()"
423
+ :data-async-state="asyncState()"
267
424
  @stx:combobox-toggle="onToggle($event)"
268
425
  @stx:combobox-select="onSelect($event)"
269
426
  @stx:combobox-query="onQuery($event)"
270
427
  >
271
428
  <slot />
429
+ <span class="sr-only" role="status" aria-live="polite" aria-atomic="true" data-stx-combobox-live x-text="announcement()"></span>
272
430
  </component>
273
431
 
274
432
  <style>
275
- [data-stx-combobox][data-state="closed"] [data-stx-combobox-options] {
433
+ [data-stx-combobox][data-state="closed"] [data-stx-combobox-options],
434
+ [data-stx-combobox][data-async-state="idle"] [data-stx-combobox-options]:not(:has([data-stx-combobox-option]:not([data-stx-combobox-remote]))) {
276
435
  display: none;
277
436
  }
437
+
438
+ /* Remote-suggestion status rows: one shows at a time, per data-async-state. */
439
+ [data-stx-combobox-status] {
440
+ display: none;
441
+ }
442
+ [data-stx-combobox][data-async-state="loading"] [data-stx-combobox-status="loading"],
443
+ [data-stx-combobox][data-async-state="empty"] [data-stx-combobox-status="empty"],
444
+ [data-stx-combobox][data-async-state="error"] [data-stx-combobox-status="error"] {
445
+ display: block;
446
+ }
278
447
  </style>