@sinnstack/ui 0.0.0-stage → 0.2.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.
- package/CHANGELOG.md +15 -0
- package/LICENSE +373 -0
- package/README.md +186 -2
- package/docs/blocks.md +364 -0
- package/docs/data.md +128 -0
- package/docs/motion.md +166 -0
- package/docs/primitives.md +510 -0
- package/docs/seo.md +187 -0
- package/docs/tokens.md +122 -0
- package/package.json +141 -3
- package/scripts/check-contrast.mjs +180 -0
- package/src/blocks/BlockRenderer.astro +54 -0
- package/src/blocks/CTABanner.astro +121 -0
- package/src/blocks/CTAPair.astro +110 -0
- package/src/blocks/CardGrid.astro +167 -0
- package/src/blocks/ConsentEmbed.astro +125 -0
- package/src/blocks/ConsentManager.astro +147 -0
- package/src/blocks/ConsentScript.astro +47 -0
- package/src/blocks/ContactCard.astro +164 -0
- package/src/blocks/ContactForm.astro +224 -0
- package/src/blocks/ErrorPage.astro +242 -0
- package/src/blocks/FAQ.astro +135 -0
- package/src/blocks/FeatureGrid.astro +148 -0
- package/src/blocks/Hero.astro +171 -0
- package/src/blocks/HeroCarousel.astro +220 -0
- package/src/blocks/HighlightCards.astro +107 -0
- package/src/blocks/LocationHours.astro +311 -0
- package/src/blocks/LogoCloud.astro +135 -0
- package/src/blocks/MaintenancePage.astro +168 -0
- package/src/blocks/MediaText.astro +177 -0
- package/src/blocks/PageHeader.astro +126 -0
- package/src/blocks/ProseSection.astro +98 -0
- package/src/blocks/Reviews.astro +194 -0
- package/src/blocks/RichText.astro +57 -0
- package/src/blocks/StatsBand.astro +117 -0
- package/src/blocks/Steps.astro +141 -0
- package/src/blocks/Testimonials.astro +144 -0
- package/src/blocks/Topbar.astro +158 -0
- package/src/blocks/cms/card-grid.ts +80 -0
- package/src/blocks/cms/consent.ts +45 -0
- package/src/blocks/cms/contact-card.ts +88 -0
- package/src/blocks/cms/contact-form.ts +55 -0
- package/src/blocks/cms/cta-banner.ts +48 -0
- package/src/blocks/cms/cta-pair.ts +63 -0
- package/src/blocks/cms/data-base.ts +7 -0
- package/src/blocks/cms/data.ts +257 -0
- package/src/blocks/cms/faq.ts +70 -0
- package/src/blocks/cms/feature-grid.ts +80 -0
- package/src/blocks/cms/fixtures.ts +599 -0
- package/src/blocks/cms/hero-carousel.ts +80 -0
- package/src/blocks/cms/hero.ts +77 -0
- package/src/blocks/cms/highlight-cards.ts +76 -0
- package/src/blocks/cms/images.ts +38 -0
- package/src/blocks/cms/index.ts +87 -0
- package/src/blocks/cms/location-hours.ts +149 -0
- package/src/blocks/cms/logo-cloud.ts +62 -0
- package/src/blocks/cms/media-text.ts +44 -0
- package/src/blocks/cms/page-header.ts +49 -0
- package/src/blocks/cms/pages.ts +81 -0
- package/src/blocks/cms/prose-section.ts +45 -0
- package/src/blocks/cms/reviews.ts +101 -0
- package/src/blocks/cms/shared.ts +115 -0
- package/src/blocks/cms/stats-band.ts +61 -0
- package/src/blocks/cms/steps.ts +67 -0
- package/src/blocks/cms/testimonials.ts +80 -0
- package/src/blocks/cms/topbar.ts +64 -0
- package/src/blocks/cms/types.ts +63 -0
- package/src/blocks/consent.ts +14 -0
- package/src/blocks/icons.ts +47 -0
- package/src/blocks/index.ts +42 -0
- package/src/blocks/internal/Actions.astro +29 -0
- package/src/blocks/internal/BlockHeader.astro +45 -0
- package/src/blocks/internal/BlockShell.astro +58 -0
- package/src/blocks/internal/CarouselScript.astro +7 -0
- package/src/blocks/internal/ConsentEmbedScript.astro +7 -0
- package/src/blocks/internal/ConsentManagerScript.astro +7 -0
- package/src/blocks/internal/HoursScript.astro +7 -0
- package/src/blocks/internal/Placeholder.astro +13 -0
- package/src/blocks/internal/RichTextNode.astro +133 -0
- package/src/blocks/internal/StandaloneDocument.astro +53 -0
- package/src/blocks/internal/tina.ts +19 -0
- package/src/blocks/internal/util.ts +23 -0
- package/src/blocks/lib/consent.ts +96 -0
- package/src/blocks/lib/contact.ts +86 -0
- package/src/blocks/lib/error-pages.ts +119 -0
- package/src/blocks/lib/faq.ts +37 -0
- package/src/blocks/lib/hours.ts +201 -0
- package/src/blocks/lib/maintenance.ts +129 -0
- package/src/blocks/lib/reviews.ts +22 -0
- package/src/blocks/lib/rich-text.ts +143 -0
- package/src/blocks/lib/slug.ts +12 -0
- package/src/blocks/registry.ts +79 -0
- package/src/blocks/scripts/carousel.ts +218 -0
- package/src/blocks/scripts/consent-embed.ts +105 -0
- package/src/blocks/scripts/consent-manager.ts +112 -0
- package/src/blocks/scripts/consent-store.ts +86 -0
- package/src/blocks/scripts/error-page.ts +71 -0
- package/src/blocks/scripts/hours.ts +106 -0
- package/src/blocks/scripts/maintenance.ts +109 -0
- package/src/blocks/styles/blocks.css +1320 -0
- package/src/blocks/styles/consent.css +102 -0
- package/src/blocks/styles/faq.css +28 -0
- package/src/blocks/styles/location.css +92 -0
- package/src/blocks/styles/prose-section.css +13 -0
- package/src/blocks/styles/quotes.css +123 -0
- package/src/blocks/styles/rich-text.css +14 -0
- package/src/blocks/types.ts +283 -0
- package/src/data/ChartAxis.astro +19 -0
- package/src/data/ChartContainer.astro +151 -0
- package/src/data/ChartLegend.astro +25 -0
- package/src/data/ChartSeries.astro +14 -0
- package/src/data/ChartTable.astro +17 -0
- package/src/data/ChartTooltip.astro +20 -0
- package/src/data/Sparkline.astro +55 -0
- package/src/data/cms.ts +190 -0
- package/src/data/index.ts +10 -0
- package/src/data/lib/chart.ts +450 -0
- package/src/data/lib/render.ts +219 -0
- package/src/data/lib/scale.ts +107 -0
- package/src/data/lib/sparkline.ts +117 -0
- package/src/data/scripts/chart.ts +324 -0
- package/src/data/styles/chart.css +392 -0
- package/src/motion/Marquee.astro +90 -0
- package/src/motion/MotionProvider.astro +50 -0
- package/src/motion/PageTransitions.astro +61 -0
- package/src/motion/Parallax.astro +44 -0
- package/src/motion/ParallaxLayer.astro +31 -0
- package/src/motion/RevealOnScroll.astro +59 -0
- package/src/motion/SceneRegistry.astro +38 -0
- package/src/motion/ScrollScene.astro +81 -0
- package/src/motion/index.ts +12 -0
- package/src/motion/integration.ts +48 -0
- package/src/motion/runtime/page-transitions.ts +68 -0
- package/src/motion/runtime/runtime.ts +180 -0
- package/src/motion/runtime/scenes.ts +248 -0
- package/src/motion/runtime/types.ts +103 -0
- package/src/motion/styles/motion.css +332 -0
- package/src/motion/transitions.ts +74 -0
- package/src/motion/virtual.d.ts +4 -0
- package/src/primitives/Accordion.astro +129 -0
- package/src/primitives/Breadcrumbs.astro +50 -0
- package/src/primitives/BulletList.astro +69 -0
- package/src/primitives/Button.astro +73 -0
- package/src/primitives/ButtonGroup.astro +54 -0
- package/src/primitives/Callout.astro +137 -0
- package/src/primitives/Caption.astro +35 -0
- package/src/primitives/Card.astro +72 -0
- package/src/primitives/CardContent.astro +24 -0
- package/src/primitives/CardFooter.astro +36 -0
- package/src/primitives/CardHeader.astro +86 -0
- package/src/primitives/Checkbox.astro +38 -0
- package/src/primitives/ChoiceGroup.astro +23 -0
- package/src/primitives/Cluster.astro +49 -0
- package/src/primitives/Container.astro +44 -0
- package/src/primitives/DescriptionList.astro +83 -0
- package/src/primitives/Dialog.astro +38 -0
- package/src/primitives/Disclosure.astro +67 -0
- package/src/primitives/Divider.astro +93 -0
- package/src/primitives/Drawer.astro +33 -0
- package/src/primitives/Fieldset.astro +80 -0
- package/src/primitives/Flex.astro +64 -0
- package/src/primitives/FocusRing.astro +63 -0
- package/src/primitives/FocusTrap.astro +64 -0
- package/src/primitives/Footer.astro +155 -0
- package/src/primitives/Form.astro +68 -0
- package/src/primitives/FormDescription.astro +26 -0
- package/src/primitives/FormError.astro +106 -0
- package/src/primitives/FormField.astro +72 -0
- package/src/primitives/Grid.astro +64 -0
- package/src/primitives/Heading.astro +45 -0
- package/src/primitives/IconButton.astro +48 -0
- package/src/primitives/Image.astro +125 -0
- package/src/primitives/Input.astro +84 -0
- package/src/primitives/Label.astro +50 -0
- package/src/primitives/Link.astro +81 -0
- package/src/primitives/LinkButton.astro +66 -0
- package/src/primitives/List.astro +39 -0
- package/src/primitives/ListItem.astro +178 -0
- package/src/primitives/MediaObject.astro +66 -0
- package/src/primitives/MetricCard.astro +276 -0
- package/src/primitives/NavLink.astro +81 -0
- package/src/primitives/NavMenu.astro +109 -0
- package/src/primitives/Navbar.astro +197 -0
- package/src/primitives/Panel.astro +153 -0
- package/src/primitives/Picture.astro +167 -0
- package/src/primitives/Popover.astro +95 -0
- package/src/primitives/Prose.astro +45 -0
- package/src/primitives/Radio.astro +21 -0
- package/src/primitives/Rating.astro +135 -0
- package/src/primitives/ReducedMotion.astro +90 -0
- package/src/primitives/SegmentedControl.astro +132 -0
- package/src/primitives/Select.astro +107 -0
- package/src/primitives/Sheet.astro +45 -0
- package/src/primitives/SkipLink.astro +31 -0
- package/src/primitives/Slider.astro +186 -0
- package/src/primitives/Spacer.astro +35 -0
- package/src/primitives/Stack.astro +51 -0
- package/src/primitives/Surface.astro +70 -0
- package/src/primitives/Switch.astro +22 -0
- package/src/primitives/Table.astro +382 -0
- package/src/primitives/Tabs.astro +126 -0
- package/src/primitives/Text.astro +56 -0
- package/src/primitives/Textarea.astro +73 -0
- package/src/primitives/Thumbnail.astro +154 -0
- package/src/primitives/Toggle.astro +99 -0
- package/src/primitives/Tooltip.astro +50 -0
- package/src/primitives/VisuallyHidden.astro +29 -0
- package/src/primitives/index.ts +103 -0
- package/src/primitives/internal/AccordionScript.astro +7 -0
- package/src/primitives/internal/ChoiceField.astro +89 -0
- package/src/primitives/internal/FocusTrapScript.astro +8 -0
- package/src/primitives/internal/FormValidationScript.astro +10 -0
- package/src/primitives/internal/IndeterminateScript.astro +13 -0
- package/src/primitives/internal/ModalPanel.astro +114 -0
- package/src/primitives/internal/OverlayScript.astro +7 -0
- package/src/primitives/internal/ReducedMotionScript.astro +7 -0
- package/src/primitives/internal/SliderScript.astro +7 -0
- package/src/primitives/internal/SparklineScript.astro +7 -0
- package/src/primitives/internal/TableSortScript.astro +7 -0
- package/src/primitives/internal/TabsScript.astro +7 -0
- package/src/primitives/internal/TooltipScript.astro +7 -0
- package/src/primitives/lib/deprecate.ts +47 -0
- package/src/primitives/lib/focus-trap.ts +197 -0
- package/src/primitives/lib/focus.ts +60 -0
- package/src/primitives/lib/image-plan.ts +82 -0
- package/src/primitives/lib/image.ts +129 -0
- package/src/primitives/lib/motion.ts +68 -0
- package/src/primitives/lib/slider.ts +170 -0
- package/src/primitives/scripts/accordion.ts +123 -0
- package/src/primitives/scripts/form-validation.ts +225 -0
- package/src/primitives/scripts/overlay.ts +71 -0
- package/src/primitives/scripts/reduced-motion.ts +59 -0
- package/src/primitives/scripts/slider.ts +150 -0
- package/src/primitives/scripts/sparkline.ts +131 -0
- package/src/primitives/scripts/table-sort.ts +68 -0
- package/src/primitives/scripts/tabs.ts +151 -0
- package/src/primitives/scripts/tooltip.ts +28 -0
- package/src/primitives/styles/a11y.css +104 -0
- package/src/primitives/styles/accordion.css +53 -0
- package/src/primitives/styles/breadcrumbs.css +59 -0
- package/src/primitives/styles/bullet-list.css +89 -0
- package/src/primitives/styles/button.css +237 -0
- package/src/primitives/styles/choice.css +164 -0
- package/src/primitives/styles/cluster.css +24 -0
- package/src/primitives/styles/container.css +19 -0
- package/src/primitives/styles/data.css +849 -0
- package/src/primitives/styles/disclosure.css +140 -0
- package/src/primitives/styles/divider.css +50 -0
- package/src/primitives/styles/flex.css +42 -0
- package/src/primitives/styles/form.css +241 -0
- package/src/primitives/styles/grid.css +36 -0
- package/src/primitives/styles/media.css +247 -0
- package/src/primitives/styles/nav.css +792 -0
- package/src/primitives/styles/overlay.css +362 -0
- package/src/primitives/styles/prose.css +146 -0
- package/src/primitives/styles/scale.css +175 -0
- package/src/primitives/styles/segmented.css +165 -0
- package/src/primitives/styles/slider.css +170 -0
- package/src/primitives/styles/spacer.css +17 -0
- package/src/primitives/styles/stack.css +25 -0
- package/src/primitives/styles/surface.css +403 -0
- package/src/primitives/styles/tabs.css +223 -0
- package/src/primitives/styles/typography.css +144 -0
- package/src/primitives/styles/utilities.css +24 -0
- package/src/primitives/types.ts +385 -0
- package/src/primitives/utils.ts +254 -0
- package/src/seo/JSONLD.astro +35 -0
- package/src/seo/SEOHead.astro +47 -0
- package/src/seo/cms.ts +80 -0
- package/src/seo/index.ts +10 -0
- package/src/seo/integration.ts +231 -0
- package/src/seo/lib/jsonld.ts +334 -0
- package/src/seo/lib/meta.ts +205 -0
- package/src/seo/lib/redirects.ts +283 -0
- package/src/seo/lib/robots.ts +33 -0
- package/src/seo/lib/rss.ts +98 -0
- package/src/seo/lib/sitemap.ts +105 -0
- package/src/seo/lib/url.ts +68 -0
- package/src/tokens/base.css +71 -0
- package/src/tokens/tokens.css +210 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartContainer — the frame every chart sits in: title, description, legend, the plot, the data
|
|
4
|
+
* table (the chart's accessible twin) and the source. Responsive with container queries.
|
|
5
|
+
*
|
|
6
|
+
* <ChartContainer chart={chart} title="Site visits per month" description="All locations, 2026"
|
|
7
|
+
* source={{ label: 'Analytics export', href: '/data' }}>
|
|
8
|
+
* <ChartLegend slot="legend" chart={chart} />
|
|
9
|
+
* <ChartAxis chart={chart} axis="y" /> <ChartAxis chart={chart} axis="x" />
|
|
10
|
+
* <ChartSeries chart={chart} /> <ChartTooltip chart={chart} />
|
|
11
|
+
* </ChartContainer>
|
|
12
|
+
*
|
|
13
|
+
* - With `chart`, it adds a sentence summary for screen readers and a "Show data table" table
|
|
14
|
+
* (with CSV download) under the plot. Every value is reachable without the tooltip.
|
|
15
|
+
* - `height` is the plot height; the x-axis band is added on top of it (no nested scrolling).
|
|
16
|
+
* - States: `loading` (skeleton), `refetching` (keeps the previous render, dimmed, no layout
|
|
17
|
+
* jump), `empty` and `error` (same footprint as the plot; slots `empty` / `error` for actions).
|
|
18
|
+
* - `surface`: the background the chart sits on (`raised` inside cards), so the 2px gaps and dot
|
|
19
|
+
* rings match it.
|
|
20
|
+
* - Filters belong in one row above the charts they scope, never inside a chart.
|
|
21
|
+
*/
|
|
22
|
+
import Heading from '../primitives/Heading.astro';
|
|
23
|
+
import type { HeadingLevel } from '../primitives/types';
|
|
24
|
+
import type { ChartModel } from './lib/chart';
|
|
25
|
+
import { renderTable } from './lib/render';
|
|
26
|
+
import './styles/chart.css';
|
|
27
|
+
import { renameProp } from '../primitives/lib/deprecate';
|
|
28
|
+
|
|
29
|
+
type Props = {
|
|
30
|
+
chart?: ChartModel;
|
|
31
|
+
title: string;
|
|
32
|
+
headingLevel?: HeadingLevel;
|
|
33
|
+
description?: string;
|
|
34
|
+
source?: string | { label: string; href?: string };
|
|
35
|
+
height?: 's' | 'm' | 'l' | 'xl';
|
|
36
|
+
/** Card background: `default`, `raised` or `subtle` (Surface's words). */
|
|
37
|
+
background?: 'default' | 'raised' | 'subtle';
|
|
38
|
+
/** @deprecated Use `background`. */
|
|
39
|
+
surface?: 'default' | 'raised' | 'subtle';
|
|
40
|
+
state?: 'ready' | 'loading' | 'refetching' | 'empty' | 'error';
|
|
41
|
+
emptyMessage?: string;
|
|
42
|
+
errorMessage?: string;
|
|
43
|
+
/** Show the data table under the chart: closed (default), open, or not at all. */
|
|
44
|
+
table?: boolean | 'open';
|
|
45
|
+
tableLabel?: string;
|
|
46
|
+
/** Offer a CSV download in the table view. */
|
|
47
|
+
download?: boolean;
|
|
48
|
+
id?: string;
|
|
49
|
+
class?: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const {
|
|
53
|
+
chart,
|
|
54
|
+
title,
|
|
55
|
+
headingLevel = 3,
|
|
56
|
+
description,
|
|
57
|
+
source,
|
|
58
|
+
height = 'm',
|
|
59
|
+
background: backgroundProp,
|
|
60
|
+
surface,
|
|
61
|
+
state = 'ready',
|
|
62
|
+
emptyMessage = 'No data for this period.',
|
|
63
|
+
errorMessage = 'This chart couldn’t load.',
|
|
64
|
+
table = true,
|
|
65
|
+
tableLabel = 'Show data table',
|
|
66
|
+
download = true,
|
|
67
|
+
id,
|
|
68
|
+
class: className,
|
|
69
|
+
} = Astro.props;
|
|
70
|
+
// `data-surface` is the blocks' strong/tinted section attribute; the chart card says data-background.
|
|
71
|
+
const background = renameProp('ChartContainer', 'background', backgroundProp, 'surface', surface) ?? 'default';
|
|
72
|
+
|
|
73
|
+
const base = id ?? chart?.id ?? `sinn-chart-${Math.random().toString(36).slice(2, 8)}`;
|
|
74
|
+
const titleId = `${base}-title`;
|
|
75
|
+
const descId = description ? `${base}-desc` : undefined;
|
|
76
|
+
const summaryId = `${base}-summary`;
|
|
77
|
+
const ready = state === 'ready' || state === 'refetching';
|
|
78
|
+
const src = typeof source === 'string' ? { label: source } : source;
|
|
79
|
+
const fileName = title.replace(/[^\w\- ]+/g, '').trim().replace(/\s+/g, '-').toLowerCase() || 'chart-data';
|
|
80
|
+
const hasToolbar = Astro.slots.has('legend') || Astro.slots.has('toolbar');
|
|
81
|
+
if (chart?.warnings.length && import.meta.env?.DEV) console.warn(`[Chart] "${title}"\n - ${chart.warnings.join('\n - ')}`);
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
<figure
|
|
85
|
+
class:list={['sinn-chart', className]}
|
|
86
|
+
id={base}
|
|
87
|
+
data-height={height === 'm' ? undefined : height}
|
|
88
|
+
data-background={background === 'default' ? undefined : background}
|
|
89
|
+
data-state={state === 'ready' ? undefined : state}
|
|
90
|
+
data-type={chart?.type}
|
|
91
|
+
data-y-ch={chart?.yLabelCh}
|
|
92
|
+
data-bar-pct={chart?.type === 'bar' ? chart.barPct : undefined}
|
|
93
|
+
aria-labelledby={titleId}
|
|
94
|
+
aria-describedby={descId}
|
|
95
|
+
>
|
|
96
|
+
<figcaption class="sinn-chart__header">
|
|
97
|
+
<Heading level={headingLevel} size="m" weight="semibold" id={titleId} class="sinn-chart__title">{title}</Heading>
|
|
98
|
+
{description && <p class="sinn-chart__description" id={descId}>{description}</p>}
|
|
99
|
+
</figcaption>
|
|
100
|
+
{
|
|
101
|
+
hasToolbar && ready && (
|
|
102
|
+
<div class="sinn-chart__toolbar">
|
|
103
|
+
<slot name="legend" />
|
|
104
|
+
<slot name="toolbar" />
|
|
105
|
+
</div>
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
<div class="sinn-chart__body" aria-busy={state === 'loading' || state === 'refetching' ? 'true' : undefined}>
|
|
109
|
+
{
|
|
110
|
+
state === 'loading' ? (
|
|
111
|
+
<div class="sinn-chart__skeleton" role="status">
|
|
112
|
+
<span class="sinn-sr-only">Loading chart…</span>
|
|
113
|
+
</div>
|
|
114
|
+
) : state === 'empty' ? (
|
|
115
|
+
<div class="sinn-chart__state" data-kind="empty" role="status">
|
|
116
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" aria-hidden="true"><path d="M3.5 20.5h17M6.5 16v-3M11 16v-6M15.5 16v-4M20 16V8" /></svg>
|
|
117
|
+
<p>{emptyMessage}</p>
|
|
118
|
+
<slot name="empty" />
|
|
119
|
+
</div>
|
|
120
|
+
) : state === 'error' ? (
|
|
121
|
+
<div class="sinn-chart__state" data-kind="error" role="alert">
|
|
122
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" aria-hidden="true"><circle cx="12" cy="12" r="9" /><path d="M12 7.5v5.5" /><circle cx="12" cy="16.5" r="0.6" fill="currentColor" /></svg>
|
|
123
|
+
<p>{errorMessage}</p>
|
|
124
|
+
<slot name="error" />
|
|
125
|
+
</div>
|
|
126
|
+
) : (
|
|
127
|
+
<>
|
|
128
|
+
{chart && <p class="sinn-sr-only" id={summaryId}>{chart.summary}</p>}
|
|
129
|
+
<div class="sinn-chart__plot">
|
|
130
|
+
<slot />
|
|
131
|
+
</div>
|
|
132
|
+
</>
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
</div>
|
|
136
|
+
{
|
|
137
|
+
table && chart && ready && (
|
|
138
|
+
<details class="sinn-chart__data" open={table === 'open'}>
|
|
139
|
+
<summary>{tableLabel}</summary>
|
|
140
|
+
<Fragment set:html={renderTable(chart, { caption: title, hideCaption: true, download: download ? fileName : false })} />
|
|
141
|
+
</details>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
{
|
|
145
|
+
src && (
|
|
146
|
+
<p class="sinn-chart__source">
|
|
147
|
+
Source: {src.href ? <a href={src.href}>{src.label}</a> : src.label}
|
|
148
|
+
</p>
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
</figure>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartLegend — names each series next to a key that mirrors its mark (a line for line charts, a
|
|
4
|
+
* rounded square for bars and areas). Shown for 2+ series; one series needs none (the title names
|
|
5
|
+
* it) unless `force`.
|
|
6
|
+
* - `toggle` (default): each entry is a button that shows/hides its series; Alt/Option-click shows
|
|
7
|
+
* only that one; "Show all" appears when something is hidden. Colors never change, the y scale
|
|
8
|
+
* stays put, and at least one series stays visible.
|
|
9
|
+
* Put it in ChartContainer's `legend` slot.
|
|
10
|
+
*/
|
|
11
|
+
import type { ChartModel } from './lib/chart';
|
|
12
|
+
import { renderLegend } from './lib/render';
|
|
13
|
+
|
|
14
|
+
type Props = { chart: ChartModel; toggle?: boolean; force?: boolean; label?: string };
|
|
15
|
+
const { chart, toggle = true, force, label } = Astro.props;
|
|
16
|
+
const html = renderLegend(chart, { toggle, force, label });
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
<Fragment set:html={html} />
|
|
20
|
+
{/* Top-level, not inside a condition: a <script> nested in an expression may be emitted inline
|
|
21
|
+
instead of bundled, which a strict CSP blocks. The module is shared with
|
|
22
|
+
ChartTooltip, loaded once, and does nothing when no toggles are on the page. */}
|
|
23
|
+
<script>
|
|
24
|
+
import './scripts/chart';
|
|
25
|
+
</script>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartSeries — the marks: 2px lines (end dot with a surface ring), 10% area washes, or bars
|
|
4
|
+
* (≤ 24px, 4px rounded data end, square baseline, 2px surface gaps when grouped or stacked).
|
|
5
|
+
* Colors come from the validated series slots and follow the series, never its rank.
|
|
6
|
+
*/
|
|
7
|
+
import type { ChartModel } from './lib/chart';
|
|
8
|
+
import { renderSeries } from './lib/render';
|
|
9
|
+
|
|
10
|
+
type Props = { chart: ChartModel; endDots?: boolean };
|
|
11
|
+
const { chart, endDots } = Astro.props;
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
<Fragment set:html={renderSeries(chart, { endDots })} />
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartTable — the chart's data as a real table: row headers for x, one column per series,
|
|
4
|
+
* values formatted like the axis and tooltip (tabular figures, right-aligned), scrollable and
|
|
5
|
+
* keyboard-focusable when wide. Optional CSV download of the raw values.
|
|
6
|
+
* ChartContainer renders it automatically (in "Show data table"); use it directly to place the
|
|
7
|
+
* table elsewhere or show it open.
|
|
8
|
+
*/
|
|
9
|
+
import type { ChartModel } from './lib/chart';
|
|
10
|
+
import { renderTable } from './lib/render';
|
|
11
|
+
import './styles/chart.css';
|
|
12
|
+
|
|
13
|
+
type Props = { chart: ChartModel; caption?: string; hideCaption?: boolean; download?: string | false };
|
|
14
|
+
const { chart, caption, hideCaption, download = false } = Astro.props;
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
<Fragment set:html={renderTable(chart, { caption, hideCaption, download })} />
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartTooltip — exact values under the pointer or keyboard focus.
|
|
4
|
+
* - Lines/areas: a crosshair snaps to the nearest x; the tooltip lists every series there.
|
|
5
|
+
* - Bars: the band lifts; the bar under the pointer leads the list (stacked: plus the total).
|
|
6
|
+
* - Keyboard: Tab to the plot, ←/→ (Home/End, PageUp/PageDown), Esc; each step is announced.
|
|
7
|
+
* - Values lead (strong), names follow; series keyed with a short line, not a box.
|
|
8
|
+
* The tooltip enhances, never gates: every value is also in the table view.
|
|
9
|
+
*/
|
|
10
|
+
import type { ChartModel } from './lib/chart';
|
|
11
|
+
import { renderTooltip } from './lib/render';
|
|
12
|
+
|
|
13
|
+
type Props = { chart: ChartModel; label?: string };
|
|
14
|
+
const { chart, label } = Astro.props;
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
<Fragment set:html={renderTooltip(chart, { label })} />
|
|
18
|
+
<script>
|
|
19
|
+
import './scripts/chart';
|
|
20
|
+
</script>
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Sparkline — a compact, axis-free trend inline with text or beside a metric.
|
|
4
|
+
*
|
|
5
|
+
* Visits are up this year <Sparkline values={monthly} labels={months} label="Visits" format="integer" />.
|
|
6
|
+
*
|
|
7
|
+
* - The line is the de-emphasis gray; the current (or max/min) point is the accent dot.
|
|
8
|
+
* - `size`: `inline` (sits in a line of text, 1em tall), `s`, `m`, or `fill` (full width).
|
|
9
|
+
* - `type`: line, area or bar. `reference` draws a target/average as a dashed hairline.
|
|
10
|
+
* - Read as one sentence by screen readers ("Visits: 1,240 in Jan to 1,390 in Dec (up 12%)…").
|
|
11
|
+
* - `interactive`: hover/focus readout of each value (←/→ on keyboard).
|
|
12
|
+
*/
|
|
13
|
+
import type { ValueFormat } from '../primitives/types';
|
|
14
|
+
import { defineSparkline } from './lib/sparkline';
|
|
15
|
+
import { renderSparkline } from './lib/render';
|
|
16
|
+
import './styles/chart.css';
|
|
17
|
+
|
|
18
|
+
type Props = {
|
|
19
|
+
values: Array<number | null | undefined>;
|
|
20
|
+
labels?: string[];
|
|
21
|
+
label?: string;
|
|
22
|
+
format?: ValueFormat;
|
|
23
|
+
type?: 'line' | 'area' | 'bar';
|
|
24
|
+
highlight?: 'last' | 'max' | 'min' | 'none';
|
|
25
|
+
reference?: number;
|
|
26
|
+
referenceLabel?: string;
|
|
27
|
+
zero?: boolean;
|
|
28
|
+
size?: 'inline' | 's' | 'm' | 'fill';
|
|
29
|
+
interactive?: boolean;
|
|
30
|
+
/** Series color slot for the line (default: the de-emphasis gray). */
|
|
31
|
+
slot?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 'muted';
|
|
32
|
+
locale?: string;
|
|
33
|
+
class?: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const { size = 'inline', interactive = false, slot, class: className, ...input } = Astro.props;
|
|
37
|
+
const model = defineSparkline(input);
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
<span
|
|
41
|
+
class:list={['sinn-spark', className]}
|
|
42
|
+
data-size={size === 'inline' ? undefined : size}
|
|
43
|
+
data-direction={model.direction}
|
|
44
|
+
role="img"
|
|
45
|
+
aria-label={model.summary}
|
|
46
|
+
tabindex={interactive ? 0 : undefined}
|
|
47
|
+
data-sinn-spark={interactive ? '' : undefined}
|
|
48
|
+
set:html={renderSparkline(model, { interactive, slot })}
|
|
49
|
+
/>{interactive && <span class="sinn-sr-only" aria-live="polite" data-sinn-chart-live />}
|
|
50
|
+
{/* Top-level, not inside `{interactive && …}`: a <script> nested in an expression may be emitted
|
|
51
|
+
inline instead of bundled, which a strict CSP blocks. Deduped with the chart script; it does
|
|
52
|
+
nothing for sparklines without data-sinn-spark. */}
|
|
53
|
+
<script>
|
|
54
|
+
import './scripts/chart';
|
|
55
|
+
</script>
|
package/src/data/cms.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tina fields for editor-managed charts: a `chart` object for small, hand-entered data (a page's
|
|
3
|
+
* "by the numbers" section, a report summary) and a `sparkline` object. Structural (no tinacms
|
|
4
|
+
* dependency), same shape as Tina's field definitions. Charts fed from an API or a database
|
|
5
|
+
* should call defineChart() in code instead.
|
|
6
|
+
*
|
|
7
|
+
* fields: [ ...blockFields, chartField ]
|
|
8
|
+
* const { chart, frame, errors } = chartFromCms(block.chart);
|
|
9
|
+
* <ChartContainer chart={chart} {...frame}> … </ChartContainer>
|
|
10
|
+
*
|
|
11
|
+
* Editors type one value per line, lined up with the labels; an empty line is a missing value.
|
|
12
|
+
* "1,240", "$9,000" and "91%" are all understood.
|
|
13
|
+
*/
|
|
14
|
+
import type { ValueFormat } from '../primitives/types';
|
|
15
|
+
import { defineChart, type ChartInput, type ChartModel } from './lib/chart';
|
|
16
|
+
import type { SparklineInput } from './lib/sparkline';
|
|
17
|
+
|
|
18
|
+
type Field = Record<string, unknown>;
|
|
19
|
+
|
|
20
|
+
const formatOptions = [
|
|
21
|
+
{ value: 'integer', label: 'Whole numbers (1,240)' },
|
|
22
|
+
{ value: 'number', label: 'Numbers with decimals (4.5)' },
|
|
23
|
+
{ value: 'percent', label: 'Percent (91%)' },
|
|
24
|
+
{ value: 'currency', label: 'Dollars ($9,000)' },
|
|
25
|
+
{ value: 'currency-compact', label: 'Dollars, short axis ($200K)' },
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
export const chartField: Field = {
|
|
29
|
+
type: 'object',
|
|
30
|
+
name: 'chart',
|
|
31
|
+
label: 'Chart',
|
|
32
|
+
fields: [
|
|
33
|
+
{ type: 'string', name: 'title', label: 'Title', required: true, description: 'Say what the chart shows, e.g. "Site visits per month".' },
|
|
34
|
+
{ type: 'string', name: 'description', label: 'Description', description: 'One line of context: period, units, what to notice.' },
|
|
35
|
+
{
|
|
36
|
+
type: 'string', name: 'type', label: 'Chart type', required: true,
|
|
37
|
+
options: [
|
|
38
|
+
{ value: 'line', label: 'Line (change over time)' },
|
|
39
|
+
{ value: 'area', label: 'Area (stacked totals over time)' },
|
|
40
|
+
{ value: 'bar', label: 'Bars (compare categories)' },
|
|
41
|
+
],
|
|
42
|
+
},
|
|
43
|
+
{ type: 'boolean', name: 'stacked', label: 'Stack the series', description: 'Only when the parts add up to a meaningful total.' },
|
|
44
|
+
{ type: 'string', name: 'format', label: 'Number format', options: formatOptions },
|
|
45
|
+
{ type: 'string', name: 'labels', label: 'Labels (one per line)', required: true, ui: { component: 'textarea' }, description: 'Dates as 2026-01-01 or names like Q1, Fort Worth.' },
|
|
46
|
+
{
|
|
47
|
+
type: 'object', name: 'series', label: 'Series', list: true,
|
|
48
|
+
description: 'Up to 8; 4 or fewer reads best.',
|
|
49
|
+
ui: { itemProps: (item: { label?: string }) => ({ label: item?.label || 'Series' }) },
|
|
50
|
+
fields: [
|
|
51
|
+
{ type: 'string', name: 'label', label: 'Name', required: true },
|
|
52
|
+
{ type: 'string', name: 'values', label: 'Values (one per line)', required: true, ui: { component: 'textarea' }, description: 'Same order as the labels. Leave a line empty for a missing value.' },
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
{ type: 'string', name: 'yTitle', label: 'Value axis title', description: 'Units, if the title doesn’t make them clear (e.g. "Visits").' },
|
|
56
|
+
{ type: 'string', name: 'sourceLabel', label: 'Source' },
|
|
57
|
+
{ type: 'string', name: 'sourceHref', label: 'Source link' },
|
|
58
|
+
{ type: 'string', name: 'height', label: 'Height', options: [{ value: 's', label: 'Short' }, { value: 'm', label: 'Medium' }, { value: 'l', label: 'Tall' }] },
|
|
59
|
+
{ type: 'boolean', name: 'tableOpen', label: 'Show the data table open' },
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const sparklineField: Field = {
|
|
64
|
+
type: 'object',
|
|
65
|
+
name: 'sparkline',
|
|
66
|
+
label: 'Sparkline',
|
|
67
|
+
fields: [
|
|
68
|
+
{ type: 'string', name: 'label', label: 'What it measures', required: true, description: 'Read aloud to screen reader users, e.g. "Visits".' },
|
|
69
|
+
{ type: 'string', name: 'values', label: 'Values (one per line)', required: true, ui: { component: 'textarea' } },
|
|
70
|
+
{ type: 'string', name: 'labels', label: 'Labels (one per line, optional)', ui: { component: 'textarea' } },
|
|
71
|
+
{ type: 'string', name: 'format', label: 'Number format', options: formatOptions.slice(0, 4) },
|
|
72
|
+
{ type: 'string', name: 'type', label: 'Style', options: ['line', 'area', 'bar'] },
|
|
73
|
+
{ type: 'number', name: 'reference', label: 'Target line (optional)' },
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export interface CmsChart {
|
|
78
|
+
title?: string | null;
|
|
79
|
+
description?: string | null;
|
|
80
|
+
type?: 'line' | 'area' | 'bar' | null;
|
|
81
|
+
stacked?: boolean | null;
|
|
82
|
+
format?: string | null;
|
|
83
|
+
labels?: string | null;
|
|
84
|
+
series?: Array<{ label?: string | null; values?: string | null } | null> | null;
|
|
85
|
+
yTitle?: string | null;
|
|
86
|
+
sourceLabel?: string | null;
|
|
87
|
+
sourceHref?: string | null;
|
|
88
|
+
height?: 's' | 'm' | 'l' | null;
|
|
89
|
+
tableOpen?: boolean | null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface CmsSparkline {
|
|
93
|
+
label?: string | null;
|
|
94
|
+
values?: string | null;
|
|
95
|
+
labels?: string | null;
|
|
96
|
+
format?: string | null;
|
|
97
|
+
type?: 'line' | 'area' | 'bar' | null;
|
|
98
|
+
reference?: number | null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const lines = (s?: string | null) => (s ?? '').replace(/\r\n?/g, '\n').replace(/\n+$/, '').split('\n');
|
|
102
|
+
|
|
103
|
+
function toFormat(f?: string | null): ValueFormat {
|
|
104
|
+
if (f === 'currency-compact') return { kind: 'currency', compact: 'auto' };
|
|
105
|
+
if (f === 'integer' || f === 'number' || f === 'percent' || f === 'currency') return f;
|
|
106
|
+
return 'number';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** "1,240" → 1240, "$9,000" → 9000, "91%" → 0.91, "" → null; anything else is an error. */
|
|
110
|
+
export function parseCmsNumber(raw: string): number | null | undefined {
|
|
111
|
+
const t = raw.trim().replace(/[\s,$€£]/g, '').replace(/[−–]/g, '-');
|
|
112
|
+
if (!t) return null;
|
|
113
|
+
const pct = t.endsWith('%');
|
|
114
|
+
const n = Number(pct ? t.slice(0, -1) : t);
|
|
115
|
+
if (!Number.isFinite(n)) return undefined;
|
|
116
|
+
return pct ? n / 100 : n;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function parseValues(raw: string | null | undefined, format: ValueFormat, name: string, errors: string[]): Array<number | null> {
|
|
120
|
+
const isPercent = (typeof format === 'string' ? format : format.kind) === 'percent';
|
|
121
|
+
return lines(raw).map((line, i) => {
|
|
122
|
+
const v = parseCmsNumber(line);
|
|
123
|
+
if (v === undefined) {
|
|
124
|
+
errors.push(`${name}, line ${i + 1}: "${line.trim()}" isn't a number.`);
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
// In a percent chart "91" means 91%, as editors type it.
|
|
128
|
+
return v !== null && isPercent && !line.includes('%') && Math.abs(v) > 1 ? v / 100 : v;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const slug = (s: string) => s.toLowerCase().normalize('NFKD').replace(/[^\w]+/g, '-').replace(/^-|-$/g, '') || 'chart';
|
|
133
|
+
|
|
134
|
+
/** CMS chart → a chart model and ChartContainer props. Problems come back as plain sentences. */
|
|
135
|
+
export function chartFromCms(data: CmsChart | null | undefined): {
|
|
136
|
+
chart: ChartModel;
|
|
137
|
+
frame: { title: string; description?: string; source?: { label: string; href?: string }; height?: 's' | 'm' | 'l'; table: true | 'open' };
|
|
138
|
+
errors: string[];
|
|
139
|
+
} {
|
|
140
|
+
const errors: string[] = [];
|
|
141
|
+
const title = data?.title?.trim() || 'Chart';
|
|
142
|
+
const format = toFormat(data?.format);
|
|
143
|
+
const x = lines(data?.labels).map((l) => l.trim());
|
|
144
|
+
const series = (data?.series ?? []).filter(Boolean).map((s, i) => {
|
|
145
|
+
const label = s!.label?.trim() || `Series ${i + 1}`;
|
|
146
|
+
const values = parseValues(s!.values, format, label, errors);
|
|
147
|
+
if (values.length !== x.length) errors.push(`${label} has ${values.length} values for ${x.length} labels.`);
|
|
148
|
+
return { label, values: x.map((_, k) => values[k] ?? null) };
|
|
149
|
+
});
|
|
150
|
+
if (!series.length) errors.push('Add at least one series.');
|
|
151
|
+
const input: ChartInput = {
|
|
152
|
+
id: slug(title),
|
|
153
|
+
type: data?.type ?? 'line',
|
|
154
|
+
stacked: Boolean(data?.stacked),
|
|
155
|
+
x,
|
|
156
|
+
yFormat: format,
|
|
157
|
+
yTitle: data?.yTitle?.trim() || undefined,
|
|
158
|
+
// "Site visits" → "site visits" for the summary sentence; leave acronyms ("SEO visits") alone.
|
|
159
|
+
subject: /^[A-Z][a-z]/.test(title) ? title.charAt(0).toLowerCase() + title.slice(1) : title,
|
|
160
|
+
series: series.length ? series : [{ label: title, values: x.map(() => null) }],
|
|
161
|
+
};
|
|
162
|
+
const chart = defineChart(input);
|
|
163
|
+
return {
|
|
164
|
+
chart,
|
|
165
|
+
frame: {
|
|
166
|
+
title,
|
|
167
|
+
description: data?.description?.trim() || undefined,
|
|
168
|
+
source: data?.sourceLabel?.trim() ? { label: data.sourceLabel.trim(), href: data.sourceHref?.trim() || undefined } : undefined,
|
|
169
|
+
height: data?.height ?? undefined,
|
|
170
|
+
table: data?.tableOpen ? 'open' : true,
|
|
171
|
+
},
|
|
172
|
+
errors: [...errors, ...chart.warnings],
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** CMS sparkline → Sparkline props. */
|
|
177
|
+
export function sparklineFromCms(data: CmsSparkline | null | undefined): SparklineInput & { errors: string[] } {
|
|
178
|
+
const errors: string[] = [];
|
|
179
|
+
const format = toFormat(data?.format);
|
|
180
|
+
const labels = data?.labels ? lines(data.labels).map((l) => l.trim()) : undefined;
|
|
181
|
+
return {
|
|
182
|
+
label: data?.label?.trim() || 'Trend',
|
|
183
|
+
values: parseValues(data?.values, format, data?.label || 'Sparkline', errors),
|
|
184
|
+
labels,
|
|
185
|
+
format,
|
|
186
|
+
type: data?.type ?? 'line',
|
|
187
|
+
reference: data?.reference ?? undefined,
|
|
188
|
+
errors,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// @sinnstack/ui/data — charts and sparklines.
|
|
2
|
+
export { default as ChartContainer } from './ChartContainer.astro';
|
|
3
|
+
export { default as ChartAxis } from './ChartAxis.astro';
|
|
4
|
+
export { default as ChartSeries } from './ChartSeries.astro';
|
|
5
|
+
export { default as ChartLegend } from './ChartLegend.astro';
|
|
6
|
+
export { default as ChartTooltip } from './ChartTooltip.astro';
|
|
7
|
+
export { default as ChartTable } from './ChartTable.astro';
|
|
8
|
+
export { default as Sparkline } from './Sparkline.astro';
|
|
9
|
+
export { defineChart, type ChartInput, type ChartModel, type SeriesInput, type SeriesSlot } from './lib/chart';
|
|
10
|
+
export { defineSparkline, type SparklineInput, type SparklineModel } from './lib/sparkline';
|