@sinnstack/ui 0.0.0-stage → 0.3.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 (306) hide show
  1. package/CHANGELOG.md +89 -0
  2. package/LICENSE +373 -0
  3. package/README.md +188 -2
  4. package/docs/blocks.md +373 -0
  5. package/docs/data.md +128 -0
  6. package/docs/finishes.md +165 -0
  7. package/docs/motion.md +166 -0
  8. package/docs/primitives.md +510 -0
  9. package/docs/seo.md +191 -0
  10. package/docs/tokens.md +122 -0
  11. package/package.json +132 -3
  12. package/scripts/check-contrast.mjs +180 -0
  13. package/src/blocks/BlockRenderer.astro +60 -0
  14. package/src/blocks/CTABanner.astro +121 -0
  15. package/src/blocks/CTAPair.astro +110 -0
  16. package/src/blocks/CardGrid.astro +167 -0
  17. package/src/blocks/ConsentEmbed.astro +125 -0
  18. package/src/blocks/ConsentManager.astro +147 -0
  19. package/src/blocks/ConsentScript.astro +47 -0
  20. package/src/blocks/ContactCard.astro +164 -0
  21. package/src/blocks/ContactForm.astro +224 -0
  22. package/src/blocks/ErrorPage.astro +242 -0
  23. package/src/blocks/FAQ.astro +135 -0
  24. package/src/blocks/FeatureGrid.astro +148 -0
  25. package/src/blocks/Hero.astro +171 -0
  26. package/src/blocks/HeroCarousel.astro +220 -0
  27. package/src/blocks/HighlightCards.astro +107 -0
  28. package/src/blocks/LocationHours.astro +311 -0
  29. package/src/blocks/LogoCloud.astro +135 -0
  30. package/src/blocks/MaintenancePage.astro +173 -0
  31. package/src/blocks/MediaText.astro +177 -0
  32. package/src/blocks/PageHeader.astro +126 -0
  33. package/src/blocks/ProseSection.astro +98 -0
  34. package/src/blocks/Reviews.astro +194 -0
  35. package/src/blocks/RichText.astro +58 -0
  36. package/src/blocks/StatsBand.astro +117 -0
  37. package/src/blocks/Steps.astro +141 -0
  38. package/src/blocks/Testimonials.astro +144 -0
  39. package/src/blocks/Topbar.astro +158 -0
  40. package/src/blocks/cms/card-grid.ts +48 -0
  41. package/src/blocks/cms/consent.ts +16 -0
  42. package/src/blocks/cms/contact-card.ts +46 -0
  43. package/src/blocks/cms/contact-form.ts +37 -0
  44. package/src/blocks/cms/cta-banner.ts +27 -0
  45. package/src/blocks/cms/cta-pair.ts +30 -0
  46. package/src/blocks/cms/data-base.ts +5 -0
  47. package/src/blocks/cms/data.ts +257 -0
  48. package/src/blocks/cms/faq.ts +38 -0
  49. package/src/blocks/cms/feature-grid.ts +47 -0
  50. package/src/blocks/cms/fixtures.ts +591 -0
  51. package/src/blocks/cms/hero-carousel.ts +36 -0
  52. package/src/blocks/cms/hero.ts +33 -0
  53. package/src/blocks/cms/highlight-cards.ts +41 -0
  54. package/src/blocks/cms/images.ts +37 -0
  55. package/src/blocks/cms/index.ts +59 -0
  56. package/src/blocks/cms/location-hours.ts +67 -0
  57. package/src/blocks/cms/logo-cloud.ts +36 -0
  58. package/src/blocks/cms/media-text.ts +26 -0
  59. package/src/blocks/cms/page-header.ts +27 -0
  60. package/src/blocks/cms/pages.ts +35 -0
  61. package/src/blocks/cms/prose-section.ts +25 -0
  62. package/src/blocks/cms/reviews.ts +50 -0
  63. package/src/blocks/cms/schema-dsl.ts +134 -0
  64. package/src/blocks/cms/schema.ts +794 -0
  65. package/src/blocks/cms/shared.ts +63 -0
  66. package/src/blocks/cms/stats-band.ts +35 -0
  67. package/src/blocks/cms/steps.ts +40 -0
  68. package/src/blocks/cms/testimonials.ts +46 -0
  69. package/src/blocks/cms/topbar.ts +27 -0
  70. package/src/blocks/cms/types.ts +27 -0
  71. package/src/blocks/consent.ts +14 -0
  72. package/src/blocks/icons.ts +47 -0
  73. package/src/blocks/index.ts +42 -0
  74. package/src/blocks/internal/Actions.astro +35 -0
  75. package/src/blocks/internal/BlockHeader.astro +45 -0
  76. package/src/blocks/internal/BlockShell.astro +58 -0
  77. package/src/blocks/internal/CarouselScript.astro +7 -0
  78. package/src/blocks/internal/ConsentEmbedScript.astro +7 -0
  79. package/src/blocks/internal/ConsentManagerScript.astro +7 -0
  80. package/src/blocks/internal/HoursScript.astro +7 -0
  81. package/src/blocks/internal/Placeholder.astro +13 -0
  82. package/src/blocks/internal/RichTextNode.astro +133 -0
  83. package/src/blocks/internal/StandaloneDocument.astro +53 -0
  84. package/src/blocks/internal/consent.ts +96 -0
  85. package/src/blocks/internal/fields.ts +18 -0
  86. package/src/blocks/internal/registry-contract.ts +67 -0
  87. package/src/blocks/internal/reviews.ts +22 -0
  88. package/src/blocks/internal/slug.ts +12 -0
  89. package/src/blocks/internal/util.ts +23 -0
  90. package/src/blocks/lib/contact.ts +92 -0
  91. package/src/blocks/lib/error-pages.ts +119 -0
  92. package/src/blocks/lib/faq.ts +37 -0
  93. package/src/blocks/lib/hours.ts +201 -0
  94. package/src/blocks/lib/maintenance.ts +129 -0
  95. package/src/blocks/lib/markdown.ts +350 -0
  96. package/src/blocks/lib/rich-text.ts +132 -0
  97. package/src/blocks/registry.ts +79 -0
  98. package/src/blocks/scripts/carousel.ts +218 -0
  99. package/src/blocks/scripts/consent-embed.ts +105 -0
  100. package/src/blocks/scripts/consent-manager.ts +112 -0
  101. package/src/blocks/scripts/consent-store.ts +86 -0
  102. package/src/blocks/scripts/error-page.ts +71 -0
  103. package/src/blocks/scripts/hours.ts +107 -0
  104. package/src/blocks/scripts/maintenance.ts +109 -0
  105. package/src/blocks/styles/blocks.css +1320 -0
  106. package/src/blocks/styles/consent.css +102 -0
  107. package/src/blocks/styles/faq.css +28 -0
  108. package/src/blocks/styles/location.css +92 -0
  109. package/src/blocks/styles/prose-section.css +13 -0
  110. package/src/blocks/styles/quotes.css +123 -0
  111. package/src/blocks/styles/rich-text.css +14 -0
  112. package/src/blocks/types.ts +293 -0
  113. package/src/data/ChartAxis.astro +19 -0
  114. package/src/data/ChartContainer.astro +152 -0
  115. package/src/data/ChartLegend.astro +25 -0
  116. package/src/data/ChartSeries.astro +14 -0
  117. package/src/data/ChartTable.astro +17 -0
  118. package/src/data/ChartTooltip.astro +20 -0
  119. package/src/data/Sparkline.astro +55 -0
  120. package/src/data/cms.ts +130 -0
  121. package/src/data/index.ts +10 -0
  122. package/src/data/internal/scale.ts +107 -0
  123. package/src/data/lib/chart.ts +459 -0
  124. package/src/data/lib/render.ts +219 -0
  125. package/src/data/lib/sparkline.ts +117 -0
  126. package/src/data/schema.ts +65 -0
  127. package/src/data/scripts/chart.ts +324 -0
  128. package/src/data/styles/chart.css +392 -0
  129. package/src/finishes/aurora.css +75 -0
  130. package/src/finishes/autumn.css +87 -0
  131. package/src/finishes/blueprint.css +69 -0
  132. package/src/finishes/brutalist.css +55 -0
  133. package/src/finishes/clay.css +48 -0
  134. package/src/finishes/day-cycle.css +46 -0
  135. package/src/finishes/fog.css +71 -0
  136. package/src/finishes/glass.css +149 -0
  137. package/src/finishes/holographic.css +91 -0
  138. package/src/finishes/night.css +77 -0
  139. package/src/finishes/paper.css +58 -0
  140. package/src/finishes/polished-glass.css +159 -0
  141. package/src/finishes/rain.css +84 -0
  142. package/src/finishes/scripts/day-cycle.ts +30 -0
  143. package/src/finishes/scripts/holographic.ts +37 -0
  144. package/src/finishes/scripts/spotlight.ts +30 -0
  145. package/src/finishes/spotlight.css +41 -0
  146. package/src/finishes/spring.css +80 -0
  147. package/src/finishes/terminal.css +49 -0
  148. package/src/motion/Marquee.astro +90 -0
  149. package/src/motion/MotionProvider.astro +50 -0
  150. package/src/motion/PageTransitions.astro +61 -0
  151. package/src/motion/Parallax.astro +44 -0
  152. package/src/motion/ParallaxLayer.astro +31 -0
  153. package/src/motion/RevealOnScroll.astro +59 -0
  154. package/src/motion/SceneRegistry.astro +38 -0
  155. package/src/motion/ScrollScene.astro +81 -0
  156. package/src/motion/index.ts +12 -0
  157. package/src/motion/integration.ts +48 -0
  158. package/src/motion/runtime/page-transitions.ts +68 -0
  159. package/src/motion/runtime/runtime.ts +180 -0
  160. package/src/motion/runtime/scenes.ts +248 -0
  161. package/src/motion/runtime/types.ts +103 -0
  162. package/src/motion/styles/motion.css +332 -0
  163. package/src/motion/transitions.ts +74 -0
  164. package/src/motion/virtual.d.ts +4 -0
  165. package/src/primitives/Accordion.astro +129 -0
  166. package/src/primitives/Breadcrumbs.astro +50 -0
  167. package/src/primitives/BulletList.astro +69 -0
  168. package/src/primitives/Button.astro +73 -0
  169. package/src/primitives/ButtonGroup.astro +54 -0
  170. package/src/primitives/Callout.astro +137 -0
  171. package/src/primitives/Caption.astro +35 -0
  172. package/src/primitives/Card.astro +72 -0
  173. package/src/primitives/CardContent.astro +24 -0
  174. package/src/primitives/CardFooter.astro +36 -0
  175. package/src/primitives/CardHeader.astro +86 -0
  176. package/src/primitives/Checkbox.astro +38 -0
  177. package/src/primitives/ChoiceGroup.astro +23 -0
  178. package/src/primitives/Cluster.astro +49 -0
  179. package/src/primitives/Container.astro +44 -0
  180. package/src/primitives/DescriptionList.astro +83 -0
  181. package/src/primitives/Dialog.astro +38 -0
  182. package/src/primitives/Disclosure.astro +67 -0
  183. package/src/primitives/Divider.astro +93 -0
  184. package/src/primitives/Drawer.astro +33 -0
  185. package/src/primitives/Fieldset.astro +80 -0
  186. package/src/primitives/Flex.astro +64 -0
  187. package/src/primitives/FocusRing.astro +63 -0
  188. package/src/primitives/FocusTrap.astro +64 -0
  189. package/src/primitives/Footer.astro +155 -0
  190. package/src/primitives/Form.astro +68 -0
  191. package/src/primitives/FormDescription.astro +26 -0
  192. package/src/primitives/FormError.astro +106 -0
  193. package/src/primitives/FormField.astro +72 -0
  194. package/src/primitives/Grid.astro +64 -0
  195. package/src/primitives/Heading.astro +45 -0
  196. package/src/primitives/IconButton.astro +48 -0
  197. package/src/primitives/Image.astro +125 -0
  198. package/src/primitives/Input.astro +86 -0
  199. package/src/primitives/Label.astro +50 -0
  200. package/src/primitives/Link.astro +81 -0
  201. package/src/primitives/LinkButton.astro +66 -0
  202. package/src/primitives/List.astro +39 -0
  203. package/src/primitives/ListItem.astro +178 -0
  204. package/src/primitives/MediaObject.astro +66 -0
  205. package/src/primitives/MetricCard.astro +278 -0
  206. package/src/primitives/NavLink.astro +81 -0
  207. package/src/primitives/NavMenu.astro +109 -0
  208. package/src/primitives/Navbar.astro +197 -0
  209. package/src/primitives/Panel.astro +153 -0
  210. package/src/primitives/Picture.astro +167 -0
  211. package/src/primitives/Popover.astro +95 -0
  212. package/src/primitives/Prose.astro +45 -0
  213. package/src/primitives/Radio.astro +21 -0
  214. package/src/primitives/Rating.astro +135 -0
  215. package/src/primitives/ReducedMotion.astro +90 -0
  216. package/src/primitives/SegmentedControl.astro +132 -0
  217. package/src/primitives/Select.astro +109 -0
  218. package/src/primitives/Sheet.astro +45 -0
  219. package/src/primitives/SkipLink.astro +31 -0
  220. package/src/primitives/Slider.astro +186 -0
  221. package/src/primitives/Spacer.astro +35 -0
  222. package/src/primitives/Stack.astro +51 -0
  223. package/src/primitives/Surface.astro +70 -0
  224. package/src/primitives/Switch.astro +22 -0
  225. package/src/primitives/Table.astro +382 -0
  226. package/src/primitives/Tabs.astro +126 -0
  227. package/src/primitives/Text.astro +56 -0
  228. package/src/primitives/Textarea.astro +75 -0
  229. package/src/primitives/Thumbnail.astro +154 -0
  230. package/src/primitives/Toggle.astro +99 -0
  231. package/src/primitives/Tooltip.astro +50 -0
  232. package/src/primitives/VisuallyHidden.astro +29 -0
  233. package/src/primitives/index.ts +103 -0
  234. package/src/primitives/internal/AccordionScript.astro +7 -0
  235. package/src/primitives/internal/ChoiceField.astro +91 -0
  236. package/src/primitives/internal/FocusTrapScript.astro +8 -0
  237. package/src/primitives/internal/FormValidationScript.astro +10 -0
  238. package/src/primitives/internal/IndeterminateScript.astro +13 -0
  239. package/src/primitives/internal/ModalPanel.astro +114 -0
  240. package/src/primitives/internal/OverlayScript.astro +7 -0
  241. package/src/primitives/internal/ReducedMotionScript.astro +7 -0
  242. package/src/primitives/internal/SliderScript.astro +7 -0
  243. package/src/primitives/internal/SparklineScript.astro +7 -0
  244. package/src/primitives/internal/TableSortScript.astro +7 -0
  245. package/src/primitives/internal/TabsScript.astro +7 -0
  246. package/src/primitives/internal/TooltipScript.astro +7 -0
  247. package/src/primitives/internal/image-plan.ts +82 -0
  248. package/src/primitives/internal/image.ts +129 -0
  249. package/src/primitives/internal/slider.ts +170 -0
  250. package/src/primitives/lib/deprecate.ts +47 -0
  251. package/src/primitives/lib/focus-trap.ts +197 -0
  252. package/src/primitives/lib/focus.ts +60 -0
  253. package/src/primitives/lib/motion.ts +68 -0
  254. package/src/primitives/scripts/accordion.ts +123 -0
  255. package/src/primitives/scripts/form-validation.ts +225 -0
  256. package/src/primitives/scripts/overlay.ts +71 -0
  257. package/src/primitives/scripts/reduced-motion.ts +59 -0
  258. package/src/primitives/scripts/slider.ts +150 -0
  259. package/src/primitives/scripts/sparkline.ts +131 -0
  260. package/src/primitives/scripts/table-sort.ts +68 -0
  261. package/src/primitives/scripts/tabs.ts +151 -0
  262. package/src/primitives/scripts/tooltip.ts +28 -0
  263. package/src/primitives/styles/a11y.css +104 -0
  264. package/src/primitives/styles/accordion.css +53 -0
  265. package/src/primitives/styles/breadcrumbs.css +59 -0
  266. package/src/primitives/styles/bullet-list.css +89 -0
  267. package/src/primitives/styles/button.css +237 -0
  268. package/src/primitives/styles/choice.css +164 -0
  269. package/src/primitives/styles/cluster.css +24 -0
  270. package/src/primitives/styles/container.css +19 -0
  271. package/src/primitives/styles/data.css +849 -0
  272. package/src/primitives/styles/disclosure.css +140 -0
  273. package/src/primitives/styles/divider.css +50 -0
  274. package/src/primitives/styles/flex.css +42 -0
  275. package/src/primitives/styles/form.css +241 -0
  276. package/src/primitives/styles/grid.css +36 -0
  277. package/src/primitives/styles/media.css +247 -0
  278. package/src/primitives/styles/nav.css +792 -0
  279. package/src/primitives/styles/overlay.css +362 -0
  280. package/src/primitives/styles/prose.css +146 -0
  281. package/src/primitives/styles/scale.css +175 -0
  282. package/src/primitives/styles/segmented.css +165 -0
  283. package/src/primitives/styles/slider.css +170 -0
  284. package/src/primitives/styles/spacer.css +17 -0
  285. package/src/primitives/styles/stack.css +25 -0
  286. package/src/primitives/styles/surface.css +403 -0
  287. package/src/primitives/styles/tabs.css +223 -0
  288. package/src/primitives/styles/typography.css +144 -0
  289. package/src/primitives/styles/utilities.css +24 -0
  290. package/src/primitives/types.ts +385 -0
  291. package/src/primitives/utils.ts +254 -0
  292. package/src/seo/JSONLD.astro +35 -0
  293. package/src/seo/SEOHead.astro +47 -0
  294. package/src/seo/cms.ts +59 -0
  295. package/src/seo/index.ts +10 -0
  296. package/src/seo/integration.ts +268 -0
  297. package/src/seo/internal/url.ts +68 -0
  298. package/src/seo/lib/jsonld.ts +342 -0
  299. package/src/seo/lib/meta.ts +205 -0
  300. package/src/seo/lib/redirects.ts +293 -0
  301. package/src/seo/lib/robots.ts +33 -0
  302. package/src/seo/lib/rss.ts +98 -0
  303. package/src/seo/lib/sitemap.ts +105 -0
  304. package/src/seo/schema.ts +43 -0
  305. package/src/tokens/base.css +71 -0
  306. package/src/tokens/tokens.css +210 -0
@@ -0,0 +1,219 @@
1
+ /**
2
+ * HTML for every chart piece, as strings. The .astro components render these with set:html, so
3
+ * the markup tested in the browser is exactly the markup that ships. All text is escaped.
4
+ *
5
+ * Responsive without JavaScript and CSP-safe: positions are SVG percentage attributes (allowed
6
+ * under a strict CSP, unlike style attributes); lines live in a nested 0–1000 viewBox with
7
+ * non-scaling strokes; text stays at its real size. Bar thickness, grouping and label density
8
+ * come from generated CSS keyed by data attributes.
9
+ */
10
+ import type { ChartModel, HitModel } from './chart';
11
+ import type { SparklineModel } from './sparkline';
12
+
13
+ export const esc = (v: unknown) =>
14
+ String(v ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c]!);
15
+ const pct = (v: number) => `${Math.round(v * 100) / 100}%`;
16
+ const attrs = (a: Record<string, string | number | boolean | undefined>) =>
17
+ Object.entries(a)
18
+ .filter(([, v]) => v !== undefined && v !== false)
19
+ .map(([k, v]) => (v === true ? ` ${k}` : ` ${k}="${esc(v)}"`))
20
+ .join('');
21
+
22
+ // ---------------- Axes ----------------
23
+
24
+ export interface AxisOptions {
25
+ /** Draw gridlines across the plot (y: horizontal lines at each tick; x: vertical lines). */
26
+ grid?: boolean;
27
+ /** Axis title (defaults to the chart's xTitle / yTitle). */
28
+ title?: string;
29
+ }
30
+
31
+ /** Y axis: tick labels in their own column, optional title above it, and horizontal gridlines. */
32
+ export function renderYAxis(m: ChartModel, o: AxisOptions = {}): string {
33
+ const grid = o.grid ?? true;
34
+ const title = o.title ?? m.yTitle;
35
+ const labels = m.yTicks.map((t) => `<text x="100%" dx="-8" y="${pct(t.pct)}" dy="0.32em">${esc(t.label)}</text>`).join('');
36
+ const lines = grid ? m.yTicks.map((t) => `<line x1="0" x2="100%" y1="${pct(t.pct)}" y2="${pct(t.pct)}"></line>`).join('') : '';
37
+ return (
38
+ (title ? `<p class="sinn-chart__axis-title" data-axis="y" aria-hidden="true">${esc(title)}</p>` : '') +
39
+ `<div class="sinn-chart__y-axis" aria-hidden="true"><svg class="sinn-chart__ticks" width="100%" height="100%">${labels}</svg></div>` +
40
+ (grid ? `<svg class="sinn-chart__layer sinn-chart__grid" aria-hidden="true" width="100%" height="100%">${lines}</svg>` : '')
41
+ );
42
+ }
43
+
44
+ /** X axis: the baseline, tick labels thinned by container width, optional vertical gridlines. */
45
+ export function renderXAxis(m: ChartModel, o: AxisOptions = {}): string {
46
+ const title = o.title ?? m.xTitle;
47
+ const labels = m.xTicks
48
+ .map((t) => `<text${attrs({ x: pct(t.pct), y: '1.1em', 'text-anchor': t.anchor, 'data-tier': t.tier })}>${esc(t.label)}</text>`)
49
+ .join('');
50
+ const vlines = o.grid ? m.xTicks.map((t) => `<line${attrs({ x1: pct(t.pct), x2: pct(t.pct), y1: '0', y2: '100%', 'data-tier': t.tier })}></line>`).join('') : '';
51
+ const steps = attrs({ 'data-step-xs': m.xSteps.xs, 'data-step-sm': m.xSteps.sm, 'data-step-md': m.xSteps.md, 'data-step-lg': m.xSteps.lg });
52
+ const base = `<line class="sinn-chart__baseline" x1="0" x2="100%" y1="${pct(m.baselinePct)}" y2="${pct(m.baselinePct)}"></line>`;
53
+ return (
54
+ `<svg class="sinn-chart__layer sinn-chart__grid" data-axis="x" aria-hidden="true" width="100%" height="100%"${steps}>${vlines}${base}</svg>` +
55
+ `<div class="sinn-chart__x-axis" aria-hidden="true"${steps}><svg class="sinn-chart__ticks" width="100%" height="100%">${labels}</svg></div>` +
56
+ (title ? `<p class="sinn-chart__axis-title" data-axis="x" aria-hidden="true">${esc(title)}</p>` : '')
57
+ );
58
+ }
59
+
60
+ // ---------------- Marks ----------------
61
+
62
+ export function renderSeries(m: ChartModel, o: { endDots?: boolean } = {}): string {
63
+ const endDots = o.endDots ?? m.type !== 'bar';
64
+ let out = '';
65
+ if (m.type === 'bar') {
66
+ for (const s of m.series) {
67
+ for (const b of s.bars) {
68
+ const h = b.bottomPct - b.topPct;
69
+ const a = { 'data-series': s.id, 'data-slot': s.muted ? 'muted' : s.slot, 'data-index': b.index, 'data-offset': b.offset };
70
+ // One square rect; CSS clips a 4px round onto the data end only (clip-path inset … round), so
71
+ // the baseline end stays square with no second shape and no seam. Without clip-path support
72
+ // the bar is simply square.
73
+ out += `<rect class="sinn-chart__bar"${attrs({ ...a, 'data-round': b.rounded === 'none' ? undefined : b.rounded, x: pct(b.xPct), y: pct(b.topPct), height: pct(h), width: 24 })}></rect>`;
74
+ }
75
+ }
76
+ if (m.stacked) {
77
+ // 2px surface gaps at every inner segment edge (the gap, not a stroke, separates them).
78
+ const byIndex = new Map<number, Array<{ topPct: number; bottomPct: number; xPct: number }>>();
79
+ for (const s of m.series) for (const b of s.bars) byIndex.set(b.index, [...(byIndex.get(b.index) ?? []), b]);
80
+ for (const [index, segs] of byIndex) {
81
+ const outerTop = Math.min(...segs.map((b) => b.topPct));
82
+ const outerBottom = Math.max(...segs.map((b) => b.bottomPct));
83
+ const edges = new Set<number>();
84
+ for (const b of segs) for (const e of [b.topPct, b.bottomPct]) if (Math.abs(e - m.baselinePct) > 0.01 && Math.abs(e - outerTop) > 0.01 && Math.abs(e - outerBottom) > 0.01) edges.add(Math.round(e * 100) / 100);
85
+ for (const e of edges) out += `<rect class="sinn-chart__gap"${attrs({ 'data-index': index, 'data-offset': '0', x: pct(segs[0].xPct), y: pct(e), height: 2, width: 24 })}></rect>`;
86
+ }
87
+ }
88
+ } else {
89
+ const areas = m.type === 'area' ? m.series.map((s) => `<path class="sinn-chart__area"${attrs({ 'data-series': s.id, 'data-slot': s.muted ? 'muted' : s.slot, d: s.areaPath })}></path>`).join('') : '';
90
+ const lines = m.series.map((s) => `<path class="sinn-chart__line"${attrs({ 'data-series': s.id, 'data-slot': s.muted ? 'muted' : s.slot, d: s.linePath, 'vector-effect': 'non-scaling-stroke' })}></path>`).join('');
91
+ out += `<svg class="sinn-chart__paths" viewBox="0 0 1000 1000" preserveAspectRatio="none" width="100%" height="100%" overflow="visible">${areas}${lines}</svg>`;
92
+ // Isolated points (a value between two gaps) would be invisible as a line: draw a dot.
93
+ for (const s of m.series)
94
+ s.points.forEach((p, i) => {
95
+ if (p && !s.points[i - 1] && !s.points[i + 1] && !(endDots && s.last === p))
96
+ out += `<circle class="sinn-chart__dot"${attrs({ 'data-series': s.id, 'data-slot': s.muted ? 'muted' : s.slot, cx: pct(p.xPct), cy: pct(p.yPct), r: 4 })}></circle>`;
97
+ });
98
+ if (endDots)
99
+ for (const s of m.series)
100
+ if (s.last)
101
+ out += `<circle class="sinn-chart__dot sinn-chart__end"${attrs({ 'data-series': s.id, 'data-slot': s.muted ? 'muted' : s.slot, cx: pct(s.last.xPct), cy: pct(s.last.yPct), r: 4 })}></circle>`;
102
+ }
103
+ return `<svg class="sinn-chart__layer sinn-chart__marks" aria-hidden="true" width="100%" height="100%" overflow="visible"${attrs({ 'data-type': m.type, 'data-stacked': m.stacked || undefined })}>${out}</svg>`;
104
+ }
105
+
106
+ // ---------------- Tooltip / interaction layer ----------------
107
+
108
+ export interface TooltipOptions {
109
+ /** Accessible name of the interactive layer. */
110
+ label?: string;
111
+ }
112
+
113
+ /** Compact hit data for the script: [from, to, x, label, rows[[series, display, yPct]], total]. */
114
+ function hitData(h: HitModel) {
115
+ return [h.fromPct, h.toPct, h.xPct, h.label, h.rows.map((r) => [r.series, r.display, r.yPct ?? null]), h.total ?? null];
116
+ }
117
+
118
+ export function renderTooltip(m: ChartModel, o: TooltipOptions = {}): string {
119
+ const names = Object.fromEntries(m.series.map((s) => [s.id, [s.label, s.muted ? 'muted' : s.slot]]));
120
+ const data = JSON.stringify({ type: m.type, stacked: m.stacked, series: names, hits: m.hits.map(hitData) });
121
+ const dots = m.type === 'bar' ? '' : m.series.map((s) => `<circle class="sinn-chart__hover-dot"${attrs({ 'data-series': s.id, 'data-slot': s.muted ? 'muted' : s.slot, cx: '0', cy: '0', r: 4 })}></circle>`).join('');
122
+ const label = o.label ?? 'Chart values. Use the left and right arrow keys to move between points.';
123
+ return (
124
+ `<div class="sinn-chart__layer sinn-chart__interact" tabindex="0" role="group" aria-roledescription="interactive chart"${attrs({ 'aria-label': label, 'data-sinn-chart-interact': '', 'data-chart': data })}>` +
125
+ `<svg class="sinn-chart__hover" aria-hidden="true" width="100%" height="100%" overflow="visible" visibility="hidden">` +
126
+ (m.type === 'bar' ? `<rect class="sinn-chart__band" x="0" y="0" width="0" height="100%"></rect>` : `<line class="sinn-chart__crosshair" x1="0" x2="0" y1="0" y2="100%"></line>`) +
127
+ dots +
128
+ `</svg>` +
129
+ `<div class="sinn-chart-tooltip" aria-hidden="true" hidden><p class="sinn-chart-tooltip__title"></p><ul class="sinn-chart-tooltip__rows" role="list"></ul><p class="sinn-chart-tooltip__total" hidden></p></div>` +
130
+ `<p class="sinn-sr-only" aria-live="polite" data-sinn-chart-live></p>` +
131
+ `</div>`
132
+ );
133
+ }
134
+
135
+ // ---------------- Legend ----------------
136
+
137
+ export interface LegendOptions {
138
+ /** Buttons that show/hide each series (needs the chart script). Default true. */
139
+ toggle?: boolean;
140
+ /** Render even for a single series (by default one series needs no legend: the title names it). */
141
+ force?: boolean;
142
+ /** Group label for assistive tech. */
143
+ label?: string;
144
+ }
145
+
146
+ export function renderLegend(m: ChartModel, o: LegendOptions = {}): string {
147
+ if (m.series.length < 2 && !o.force) return '';
148
+ const toggle = o.toggle ?? true;
149
+ const key = m.type === 'line' ? 'line' : 'rect';
150
+ const items = m.series
151
+ .map((s) => {
152
+ const swatch = `<span class="sinn-chart-legend__key"${attrs({ 'data-key': key, 'data-slot': s.muted ? 'muted' : s.slot, 'aria-hidden': 'true' })}></span>`;
153
+ const body = `${swatch}<span class="sinn-chart-legend__label">${esc(s.label)}</span>`;
154
+ return toggle
155
+ ? `<li><button type="button" class="sinn-chart-legend__item"${attrs({ 'aria-pressed': 'true', 'data-sinn-legend-toggle': s.id })}>${body}</button></li>`
156
+ : `<li class="sinn-chart-legend__item">${body}</li>`;
157
+ })
158
+ .join('');
159
+ const label = o.label ?? (toggle ? 'Series. Press a series to show or hide it.' : 'Series');
160
+ return (
161
+ `<div class="sinn-chart-legend"${attrs({ role: 'group', 'aria-label': label, 'data-toggle': toggle || undefined })}>` +
162
+ `<ul class="sinn-chart-legend__list" role="list">${items}</ul>` +
163
+ (toggle ? `<button type="button" class="sinn-chart-legend__reset" data-sinn-legend-reset hidden>Show all</button><p class="sinn-sr-only" aria-live="polite" data-sinn-legend-live></p>` : '') +
164
+ `</div>`
165
+ );
166
+ }
167
+
168
+ // ---------------- Table ----------------
169
+
170
+ export interface TableOptions {
171
+ caption?: string;
172
+ /** Offer a CSV download of the raw values (file name without extension). */
173
+ download?: string | false;
174
+ /** Hide the caption visually (the chart title already shows). */
175
+ hideCaption?: boolean;
176
+ }
177
+
178
+ export function renderTable(m: ChartModel, o: TableOptions = {}): string {
179
+ const head = m.table.headers.map((h, i) => `<th scope="col"${i ? ' data-numeric' : ''}>${esc(h)}</th>`).join('');
180
+ const body = m.table.rows.map((r) => `<tr>${r.map((c, i) => (i ? `<td data-numeric>${esc(c)}</td>` : `<th scope="row">${esc(c)}</th>`)).join('')}</tr>`).join('');
181
+ const caption = o.caption ? `<caption${o.hideCaption ? ' class="sinn-sr-only"' : ''}>${esc(o.caption)}</caption>` : '';
182
+ const dl = o.download
183
+ ? `<a class="sinn-chart-table__download" download="${esc(o.download)}.csv" href="data:text/csv;charset=utf-8,${encodeURIComponent(`${m.table.csv}`)}">Download CSV</a>`
184
+ : '';
185
+ return `<div class="sinn-chart-table"><div class="sinn-chart-table__scroll" tabindex="0" role="region"${attrs({ 'aria-label': o.caption ? `${o.caption} (table)` : 'Chart data (table)' })}><table>${caption}<thead><tr>${head}</tr></thead><tbody>${body}</tbody></table></div>${dl}</div>`;
186
+ }
187
+
188
+ // ---------------- Sparkline ----------------
189
+
190
+ export interface SparklineRenderOptions {
191
+ interactive?: boolean;
192
+ /** Slot color for the line (default: the de-emphasis gray, with the accent highlight dot). */
193
+ slot?: number | 'muted';
194
+ }
195
+
196
+ export function renderSparkline(s: SparklineModel, o: SparklineRenderOptions = {}): string {
197
+ const slot = o.slot ?? 'muted';
198
+ let marks = '';
199
+ if (s.referencePct !== undefined) marks += `<line class="sinn-spark__ref" x1="0" x2="100%" y1="${pct(s.referencePct)}" y2="${pct(s.referencePct)}"></line>`;
200
+ if (s.type === 'bar') {
201
+ for (const b of s.bars) marks += `<rect class="sinn-spark__bar"${attrs({ x: pct(b.xPct - b.widthPct / 2), width: pct(b.widthPct), y: pct(b.topPct), height: pct(b.heightPct), 'data-highlight': b.highlight || undefined, rx: 1 })}></rect>`;
202
+ } else {
203
+ marks +=
204
+ `<svg class="sinn-spark__paths" viewBox="0 0 1000 1000" preserveAspectRatio="none" width="100%" height="100%" overflow="visible">` +
205
+ (s.type === 'area' ? `<path class="sinn-spark__area" d="${s.areaPath}"></path>` : '') +
206
+ `<path class="sinn-spark__line" vector-effect="non-scaling-stroke" d="${s.linePath}"></path></svg>`;
207
+ if (s.highlight) marks += `<circle class="sinn-spark__dot" cx="${pct(s.highlight.xPct)}" cy="${pct(s.highlight.yPct)}" r="3.5"></circle>`;
208
+ }
209
+ const interactive = o.interactive && s.points.some(Boolean);
210
+ const data = interactive ? JSON.stringify(s.points.map((p) => (p ? [p.xPct, p.yPct, p.display, p.label] : null))) : undefined;
211
+ const hover = interactive
212
+ ? `<g class="sinn-spark__hover" visibility="hidden"><line class="sinn-spark__crosshair" x1="0" x2="0" y1="0" y2="100%"></line><circle class="sinn-spark__hover-dot" cx="0" cy="0" r="3.5"></circle></g>`
213
+ : '';
214
+ return (
215
+ `<svg class="sinn-spark__svg"${attrs({ width: '100%', height: '100%', overflow: 'visible', 'aria-hidden': 'true', 'data-slot': slot })}>${marks}${hover}</svg>` +
216
+ (interactive ? `<span class="sinn-chart-tooltip sinn-spark__tip" aria-hidden="true" hidden><strong></strong> <span></span></span>` : '') +
217
+ (data ? `<span hidden data-sinn-spark-data="${esc(data)}"></span>` : '')
218
+ );
219
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * defineSparkline — a compact, axis-free trend: positions as percentages (responsive without JS),
3
+ * a de-emphasized line with the highlighted point in the accent, and a sentence for screen readers.
4
+ */
5
+ import { formatValue } from '../../primitives/utils';
6
+ import type { ValueFormat } from '../../primitives/types';
7
+
8
+ export interface SparklineInput {
9
+ values: Array<number | null | undefined>;
10
+ /** Label per value ("Jan", "Week 3"), used in the tooltip and summary. */
11
+ labels?: string[];
12
+ /** What it measures ("Visits"), for the summary. */
13
+ label?: string;
14
+ format?: ValueFormat;
15
+ type?: 'line' | 'area' | 'bar';
16
+ /** Which point gets the accent dot. */
17
+ highlight?: 'last' | 'max' | 'min' | 'none';
18
+ /** A target or average drawn as a hairline. */
19
+ reference?: number;
20
+ referenceLabel?: string;
21
+ /** Include zero in the range (always for bars). */
22
+ zero?: boolean;
23
+ locale?: string;
24
+ }
25
+
26
+ export interface SparklineModel {
27
+ linePath: string;
28
+ areaPath: string;
29
+ points: Array<{ xPct: number; yPct: number; display: string; label: string } | null>;
30
+ bars: Array<{ xPct: number; widthPct: number; topPct: number; heightPct: number; highlight: boolean; negative: boolean }>;
31
+ highlight?: { xPct: number; yPct: number; index: number };
32
+ referencePct?: number;
33
+ baselinePct: number;
34
+ direction: 'up' | 'down' | 'flat';
35
+ summary: string;
36
+ type: 'line' | 'area' | 'bar';
37
+ }
38
+
39
+ const r2 = (v: number) => Math.round(v * 100) / 100;
40
+
41
+ export function defineSparkline(input: SparklineInput): SparklineModel {
42
+ const locale = input.locale ?? 'en-US';
43
+ const type = input.type ?? 'line';
44
+ const fmt = (v: number | null | undefined) => formatValue(v, input.format ?? 'number', locale);
45
+ const values = input.values.map((v) => (typeof v === 'number' && Number.isFinite(v) ? v : null));
46
+ const n = values.length;
47
+ const nums = values.filter((v): v is number => v !== null);
48
+ let lo = nums.length ? Math.min(...nums) : 0;
49
+ let hi = nums.length ? Math.max(...nums) : 1;
50
+ if (input.reference !== undefined) (lo = Math.min(lo, input.reference)), (hi = Math.max(hi, input.reference));
51
+ if (type === 'bar' || input.zero) (lo = Math.min(lo, 0)), (hi = Math.max(hi, 0));
52
+ if (lo === hi) (lo -= 1), (hi += 1);
53
+ // A little headroom so the line and dots never touch the edges.
54
+ const pad = type === 'bar' ? 0 : (hi - lo) * 0.08;
55
+ const yMin = lo - (type === 'bar' && lo === 0 ? 0 : pad);
56
+ const yMax = hi + pad;
57
+ const Y = (v: number) => r2((1 - (v - yMin) / (yMax - yMin)) * 100);
58
+ const X = (i: number) => (type === 'bar' ? r2(((i + 0.5) / n) * 100) : n === 1 ? 50 : r2((i / (n - 1)) * 100));
59
+ const labelAt = (i: number) => input.labels?.[i] ?? `${i + 1} of ${n}`;
60
+
61
+ const points = values.map((v, i) => (v === null ? null : { xPct: X(i), yPct: Y(v), display: fmt(v), label: labelAt(i) }));
62
+ let linePath = '';
63
+ let areaPath = '';
64
+ // Areas fill down to zero when zero is in range, otherwise to the bottom edge.
65
+ const base = yMin <= 0 && yMax >= 0 ? Y(0) : 100;
66
+ let run: Array<{ xPct: number; yPct: number }> = [];
67
+ const flush = () => {
68
+ if (!run.length) return;
69
+ linePath += run.map((p, j) => `${j ? 'L' : 'M'}${r2(p.xPct * 10)} ${r2(p.yPct * 10)}`).join('');
70
+ areaPath += `M${r2(run[0].xPct * 10)} ${r2(base * 10)}${run.map((p) => `L${r2(p.xPct * 10)} ${r2(p.yPct * 10)}`).join('')}L${r2(run[run.length - 1].xPct * 10)} ${r2(base * 10)}Z`;
71
+ run = [];
72
+ };
73
+ points.forEach((p) => (p ? run.push(p) : flush()));
74
+ flush();
75
+
76
+ const idx = values.map((v, i) => [v, i] as const).filter(([v]) => v !== null) as Array<readonly [number, number]>;
77
+ const pick = input.highlight ?? 'last';
78
+ const hiIdx = !idx.length || pick === 'none' ? -1 : pick === 'max' ? idx.reduce((a, b) => (b[0] > a[0] ? b : a))[1] : pick === 'min' ? idx.reduce((a, b) => (b[0] < a[0] ? b : a))[1] : idx[idx.length - 1][1];
79
+
80
+ const zeroPct = Y(0);
81
+ const bw = r2((100 / Math.max(1, n)) * 0.62);
82
+ const bars =
83
+ type === 'bar'
84
+ ? values.flatMap((v, i) =>
85
+ v === null ? [] : [{ xPct: X(i), widthPct: bw, topPct: Math.min(Y(v), zeroPct), heightPct: Math.max(r2(Math.abs(zeroPct - Y(v))), 0.5), highlight: i === hiIdx, negative: v < 0 }],
86
+ )
87
+ : [];
88
+
89
+ const first = idx[0];
90
+ const last = idx[idx.length - 1];
91
+ let direction: SparklineModel['direction'] = 'flat';
92
+ let summary = `${input.label ?? 'Trend'}: no data.`;
93
+ if (first && last) {
94
+ const change = first[0] === 0 ? undefined : (last[0] - first[0]) / Math.abs(first[0]);
95
+ direction = Math.abs(last[0] - first[0]) < 1e-9 || (change !== undefined && Math.abs(change) < 0.005) ? 'flat' : last[0] > first[0] ? 'up' : 'down';
96
+ const max = idx.reduce((a, b) => (b[0] > a[0] ? b : a));
97
+ const min = idx.reduce((a, b) => (b[0] < a[0] ? b : a));
98
+ const pct = change !== undefined ? ` (${direction === 'flat' ? 'no change' : `${direction} ${formatValue(Math.abs(change), { kind: 'percent', digits: 0 }, locale)}`})` : '';
99
+ summary =
100
+ `${input.label ?? 'Trend'}: ${fmt(first[0])} ${input.labels ? `in ${labelAt(first[1])} ` : ''}to ${fmt(last[0])}${input.labels ? ` in ${labelAt(last[1])}` : ''}${pct}. ` +
101
+ `High ${fmt(max[0])}${input.labels ? ` (${labelAt(max[1])})` : ''}, low ${fmt(min[0])}${input.labels ? ` (${labelAt(min[1])})` : ''}.` +
102
+ (input.reference !== undefined ? ` ${input.referenceLabel ?? 'Target'}: ${fmt(input.reference)}.` : '');
103
+ }
104
+
105
+ return {
106
+ type,
107
+ linePath,
108
+ areaPath,
109
+ points,
110
+ bars,
111
+ highlight: hiIdx >= 0 && points[hiIdx] ? { xPct: points[hiIdx]!.xPct, yPct: points[hiIdx]!.yPct, index: hiIdx } : undefined,
112
+ referencePct: input.reference !== undefined ? Y(input.reference) : undefined,
113
+ baselinePct: zeroPct,
114
+ direction,
115
+ summary,
116
+ };
117
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Ready-made SinnStackCMS fields for editor-managed charts, checked against the stored shapes in
3
+ * `./cms`:
4
+ *
5
+ * import { chartSchema, sparklineSchema } from '@sinnstack/ui/data/schema';
6
+ * fields: { chart: chartSchema } → chartFromCms(entry.chart)
7
+ */
8
+ import { objectSchema, type ObjectField, type SelectOption } from '../blocks/cms/schema-dsl';
9
+ import type { CmsChart, CmsSparkline } from './cms';
10
+
11
+ const format = {
12
+ type: 'select' as const,
13
+ label: 'Number format',
14
+ options: [
15
+ { value: 'number', label: 'Number (1,240.5)' },
16
+ { value: 'integer', label: 'Whole number (1,240)' },
17
+ { value: 'percent', label: 'Percent (91%)' },
18
+ { value: 'currency', label: 'Money ($9,000)' },
19
+ { value: 'currency-compact', label: 'Money, short ($9K)' },
20
+ ] as SelectOption[],
21
+ };
22
+ const chartType = (label: string) => ({
23
+ type: 'select' as const,
24
+ label,
25
+ options: [{ value: 'line', label: 'Line' }, { value: 'area', label: 'Area' }, { value: 'bar', label: 'Bar' }] as Array<{ value: 'line' | 'area' | 'bar'; label: string }>,
26
+ });
27
+ const oneValuePerLine = 'One value per line, lined up with the labels. Leave a line empty for a missing value.';
28
+
29
+ /** A `chart` object for chartFromCms. */
30
+ export const chartSchema: ObjectField = objectSchema<CmsChart>({
31
+ label: 'Chart',
32
+ fields: {
33
+ title: { type: 'text', label: 'Title', required: true },
34
+ description: { type: 'textarea', label: 'Description', rows: 2, help: 'What the chart shows, in a sentence. Read out by screen readers.' },
35
+ type: chartType('Chart type'),
36
+ stacked: { type: 'boolean', label: 'Stack the series' },
37
+ format,
38
+ labels: { type: 'textarea', label: 'Labels', rows: 6, help: 'One per line, e.g. months.' },
39
+ series: {
40
+ type: 'list',
41
+ label: 'Series',
42
+ itemLabel: 'label',
43
+ min: 1,
44
+ of: { type: 'object', fields: { label: { type: 'text', label: 'Name', required: true }, values: { type: 'textarea', label: 'Values', rows: 6, help: oneValuePerLine } } },
45
+ },
46
+ yTitle: { type: 'text', label: 'Axis title' },
47
+ sourceLabel: { type: 'text', label: 'Source' },
48
+ sourceHref: { type: 'link', label: 'Source link' },
49
+ height: { type: 'select', label: 'Height', options: [{ value: 's', label: 'Small' }, { value: 'm', label: 'Medium' }, { value: 'l', label: 'Large' }] },
50
+ tableOpen: { type: 'boolean', label: 'Show the data table open' },
51
+ },
52
+ });
53
+
54
+ /** A `sparkline` object for sparklineFromCms. */
55
+ export const sparklineSchema: ObjectField = objectSchema<CmsSparkline>({
56
+ label: 'Sparkline',
57
+ fields: {
58
+ label: { type: 'text', label: 'Label', required: true },
59
+ values: { type: 'textarea', label: 'Values', rows: 6, help: oneValuePerLine },
60
+ labels: { type: 'textarea', label: 'Labels', rows: 6, help: 'One per line.' },
61
+ format,
62
+ type: chartType('Type'),
63
+ reference: { type: 'number', label: 'Reference line', help: 'Draws a line at this value, e.g. a target.' },
64
+ },
65
+ });