@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,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inline validation for <Form validate="inline">.
|
|
3
|
+
*
|
|
4
|
+
* Progressive enhancement over native constraint validation:
|
|
5
|
+
* - Without JS: the browser's own validation bubbles + server-side errors.
|
|
6
|
+
* - With JS: the form becomes `novalidate`, and messages are written into each field's
|
|
7
|
+
* FormError (located via data-sinn-error-id, or the enclosing Fieldset for radios).
|
|
8
|
+
*
|
|
9
|
+
* Timing (avoids "error while still typing"):
|
|
10
|
+
* - A field is checked when you leave it, if you changed it, or after the first submit attempt.
|
|
11
|
+
* - A field that's showing an error is re-checked on every input, so it clears as soon as it's fixed.
|
|
12
|
+
* - Submit checks everything, shows all errors, focuses the first invalid control and fires
|
|
13
|
+
* `sinn-form-invalid`. An invalid submit is stopped in the capture phase, so the page's own
|
|
14
|
+
* `submit` listeners only ever see valid forms. A valid native submit is let through once
|
|
15
|
+
* (double-submit guard); a handler that calls preventDefault (fetch) manages its own state.
|
|
16
|
+
*
|
|
17
|
+
* Messages: the browser's localized `validationMessage`, unless the control carries a
|
|
18
|
+
* custom one in data-msg-<validity-key> (set from the field's `messages` prop).
|
|
19
|
+
*
|
|
20
|
+
* Event delegation on `document`: works for forms added later and with view transitions.
|
|
21
|
+
*/
|
|
22
|
+
import type { SinnFormInvalidDetail } from '../types';
|
|
23
|
+
|
|
24
|
+
type Control = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
|
|
25
|
+
|
|
26
|
+
const SELECTOR = 'form[data-sinn-validate="inline"]';
|
|
27
|
+
const KEYS = [
|
|
28
|
+
'valueMissing',
|
|
29
|
+
'typeMismatch',
|
|
30
|
+
'patternMismatch',
|
|
31
|
+
'tooShort',
|
|
32
|
+
'tooLong',
|
|
33
|
+
'rangeUnderflow',
|
|
34
|
+
'rangeOverflow',
|
|
35
|
+
'stepMismatch',
|
|
36
|
+
'badInput',
|
|
37
|
+
] as const;
|
|
38
|
+
|
|
39
|
+
const dirty = new WeakSet<Control>();
|
|
40
|
+
const submitting = new WeakSet<HTMLFormElement>();
|
|
41
|
+
|
|
42
|
+
function isControl(el: unknown): el is Control {
|
|
43
|
+
return el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function enhancedForm(el: Element | null): HTMLFormElement | null {
|
|
47
|
+
const form = el instanceof HTMLFormElement ? el : (el as Control | null)?.form ?? null;
|
|
48
|
+
return form?.matches(SELECTOR) ? form : null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Radios sharing a name validate (and show errors) as one control. */
|
|
52
|
+
function group(c: Control): Control[] {
|
|
53
|
+
if (c instanceof HTMLInputElement && c.type === 'radio' && c.name && c.form) {
|
|
54
|
+
return [...c.form.querySelectorAll<HTMLInputElement>(`input[type="radio"][name="${CSS.escape(c.name)}"]`)];
|
|
55
|
+
}
|
|
56
|
+
return [c];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function messageFor(c: Control): string {
|
|
60
|
+
for (const key of KEYS) {
|
|
61
|
+
if (c.validity[key]) {
|
|
62
|
+
const custom = c.dataset[`msg${key[0].toUpperCase()}${key.slice(1)}`];
|
|
63
|
+
return custom || c.validationMessage;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return c.validationMessage;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function errorElementFor(c: Control): HTMLElement | null {
|
|
70
|
+
const id = c.dataset.sinnErrorId;
|
|
71
|
+
if (id) return document.getElementById(id);
|
|
72
|
+
return c.closest('fieldset')?.querySelector<HTMLElement>(':scope > [data-sinn-error]') ?? null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toggleToken(el: Element, attr: string, token: string, on: boolean) {
|
|
76
|
+
const tokens = new Set((el.getAttribute(attr) ?? '').split(/\s+/).filter(Boolean));
|
|
77
|
+
if (on) tokens.add(token);
|
|
78
|
+
else tokens.delete(token);
|
|
79
|
+
if (tokens.size) el.setAttribute(attr, [...tokens].join(' '));
|
|
80
|
+
else el.removeAttribute(attr);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function setError(c: Control, message: string) {
|
|
84
|
+
const members = group(c);
|
|
85
|
+
for (const m of members) {
|
|
86
|
+
if (message) m.setAttribute('aria-invalid', 'true');
|
|
87
|
+
else m.removeAttribute('aria-invalid');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const el = errorElementFor(c);
|
|
91
|
+
if (!el) return;
|
|
92
|
+
const text = el.querySelector('[data-sinn-error-text]') ?? el;
|
|
93
|
+
text.textContent = message;
|
|
94
|
+
el.hidden = !message;
|
|
95
|
+
|
|
96
|
+
// Point aria-describedby at the error only while it has content.
|
|
97
|
+
// Group errors are described on the fieldset, field errors on the control.
|
|
98
|
+
if (el.id) {
|
|
99
|
+
const owners = el.parentElement instanceof HTMLFieldSetElement && !c.dataset.sinnErrorId ? [el.parentElement] : members;
|
|
100
|
+
for (const o of owners) toggleToken(o, 'aria-describedby', el.id, Boolean(message));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Returns true when valid. */
|
|
105
|
+
function validate(c: Control): boolean {
|
|
106
|
+
if (!c.willValidate) return true;
|
|
107
|
+
const valid = c.validity.valid;
|
|
108
|
+
setError(c, valid ? '' : messageFor(c));
|
|
109
|
+
return valid;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function controlsOf(form: HTMLFormElement): Control[] {
|
|
113
|
+
const seenRadioNames = new Set<string>();
|
|
114
|
+
const out: Control[] = [];
|
|
115
|
+
for (const el of form.elements) {
|
|
116
|
+
if (!isControl(el) || !el.willValidate) continue;
|
|
117
|
+
if (el instanceof HTMLInputElement && el.type === 'radio') {
|
|
118
|
+
if (seenRadioNames.has(el.name)) continue;
|
|
119
|
+
seenRadioNames.add(el.name);
|
|
120
|
+
}
|
|
121
|
+
out.push(el);
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function enhance(root: ParentNode = document) {
|
|
127
|
+
for (const form of root.querySelectorAll<HTMLFormElement>(SELECTOR)) form.noValidate = true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const FLAG = '__sinnFormValidation';
|
|
131
|
+
const w = window as unknown as Record<string, boolean>;
|
|
132
|
+
|
|
133
|
+
if (!w[FLAG]) {
|
|
134
|
+
w[FLAG] = true;
|
|
135
|
+
enhance();
|
|
136
|
+
document.addEventListener('astro:page-load', () => enhance());
|
|
137
|
+
|
|
138
|
+
document.addEventListener(
|
|
139
|
+
'submit',
|
|
140
|
+
(event) => {
|
|
141
|
+
const form = enhancedForm(event.target as Element);
|
|
142
|
+
if (!form) return;
|
|
143
|
+
|
|
144
|
+
if (submitting.has(form)) {
|
|
145
|
+
event.preventDefault();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
form.dataset.sinnSubmitted = '';
|
|
150
|
+
const invalid = controlsOf(form).filter((c) => !validate(c));
|
|
151
|
+
|
|
152
|
+
if (invalid.length) {
|
|
153
|
+
event.preventDefault();
|
|
154
|
+
// Stop here (capture phase), so the page's own submit handlers only run for valid forms.
|
|
155
|
+
event.stopPropagation();
|
|
156
|
+
invalid[0].focus();
|
|
157
|
+
form.dispatchEvent(
|
|
158
|
+
new CustomEvent<SinnFormInvalidDetail>('sinn-form-invalid', {
|
|
159
|
+
bubbles: true,
|
|
160
|
+
composed: true,
|
|
161
|
+
detail: { controls: invalid },
|
|
162
|
+
}),
|
|
163
|
+
);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
},
|
|
168
|
+
true,
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
// Runs last (bubble phase on window). If no page handler took over with preventDefault
|
|
172
|
+
// (e.g. a fetch-based submit), the browser is navigating: lock the form against double submits.
|
|
173
|
+
window.addEventListener('submit', (event) => {
|
|
174
|
+
const form = enhancedForm(event.target as Element);
|
|
175
|
+
if (!form || event.defaultPrevented) return;
|
|
176
|
+
submitting.add(form);
|
|
177
|
+
form.setAttribute('aria-busy', 'true');
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
document.addEventListener('input', (event) => {
|
|
181
|
+
const c = event.target;
|
|
182
|
+
if (!isControl(c) || !enhancedForm(c)) return;
|
|
183
|
+
dirty.add(c);
|
|
184
|
+
if (c.getAttribute('aria-invalid') === 'true') validate(c);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
document.addEventListener('change', (event) => {
|
|
188
|
+
const c = event.target;
|
|
189
|
+
if (!isControl(c) || !enhancedForm(c)) return;
|
|
190
|
+
dirty.add(c);
|
|
191
|
+
// Checkboxes, radios and selects commit on change, so check right away once touched.
|
|
192
|
+
if (c instanceof HTMLSelectElement || (c instanceof HTMLInputElement && (c.type === 'checkbox' || c.type === 'radio'))) {
|
|
193
|
+
validate(c);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
document.addEventListener('focusout', (event) => {
|
|
198
|
+
const c = event.target;
|
|
199
|
+
if (!isControl(c)) return;
|
|
200
|
+
const form = enhancedForm(c);
|
|
201
|
+
if (!form) return;
|
|
202
|
+
if (dirty.has(c) || 'sinnSubmitted' in form.dataset) validate(c);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Error-summary links: move focus to the field, not just scroll to it.
|
|
206
|
+
document.addEventListener('click', (event) => {
|
|
207
|
+
const link = (event.target as Element | null)?.closest?.('.sinn-form-error a[href^="#"]');
|
|
208
|
+
if (!(link instanceof HTMLAnchorElement)) return;
|
|
209
|
+
const target = document.getElementById(decodeURIComponent(link.hash.slice(1)));
|
|
210
|
+
if (!target) return;
|
|
211
|
+
event.preventDefault();
|
|
212
|
+
const label = target.id ? document.querySelector(`label[for="${CSS.escape(target.id)}"]`) : null;
|
|
213
|
+
(label ?? target).scrollIntoView({ block: 'center' });
|
|
214
|
+
target.focus({ preventScroll: true });
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Back/forward cache restores the page as it was mid-submit: unlock it.
|
|
218
|
+
window.addEventListener('pageshow', (event) => {
|
|
219
|
+
if (!event.persisted) return;
|
|
220
|
+
for (const form of document.querySelectorAll<HTMLFormElement>(SELECTOR)) {
|
|
221
|
+
submitting.delete(form);
|
|
222
|
+
form.removeAttribute('aria-busy');
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fallbacks for Dialog, Drawer and Sheet. In current browsers this does almost nothing:
|
|
3
|
+
* the platform already handles everything.
|
|
4
|
+
*
|
|
5
|
+
* 1. Invoker commands (`<button commandfor="id" command="show-modal">`): native in Chrome 135+,
|
|
6
|
+
* Safari 26+, Firefox 144+. Older browsers get a delegated click handler with the same
|
|
7
|
+
* behavior (show-modal, close with the button's value, request-close).
|
|
8
|
+
* 2. `closedby="any"` (click on the backdrop closes): native in Chromium 134+ and Firefox 141+.
|
|
9
|
+
* Elsewhere a backdrop click is detected (the event target is the <dialog> itself, since
|
|
10
|
+
* the dialog has no padding and its content fills it).
|
|
11
|
+
* 3. `closedby="none"` (alert dialogs: Esc must not dismiss): where unsupported, the
|
|
12
|
+
* `cancel` event is prevented.
|
|
13
|
+
*
|
|
14
|
+
* Event delegation on `document`, so dialogs added later work too.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const FLAG = '__sinnOverlay';
|
|
18
|
+
const w = window as unknown as Record<string, boolean>;
|
|
19
|
+
|
|
20
|
+
const supportsCommands = 'command' in HTMLButtonElement.prototype;
|
|
21
|
+
const supportsClosedBy = 'closedBy' in HTMLDialogElement.prototype;
|
|
22
|
+
|
|
23
|
+
function requestClose(dialog: HTMLDialogElement, value?: string) {
|
|
24
|
+
const d = dialog as HTMLDialogElement & { requestClose?: (v?: string) => void };
|
|
25
|
+
if (typeof d.requestClose === 'function') d.requestClose(value);
|
|
26
|
+
else {
|
|
27
|
+
// requestClose fires `cancel` first, so listeners can veto. Mirror that.
|
|
28
|
+
const ok = dialog.dispatchEvent(new Event('cancel', { cancelable: true }));
|
|
29
|
+
if (ok) dialog.close(value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!w[FLAG]) {
|
|
34
|
+
w[FLAG] = true;
|
|
35
|
+
|
|
36
|
+
if (!supportsCommands) {
|
|
37
|
+
document.addEventListener('click', (event) => {
|
|
38
|
+
const button = (event.target as Element | null)?.closest?.('button[commandfor]');
|
|
39
|
+
if (!(button instanceof HTMLButtonElement) || button.disabled) return;
|
|
40
|
+
const target = document.getElementById(button.getAttribute('commandfor') ?? '');
|
|
41
|
+
if (!(target instanceof HTMLDialogElement)) return;
|
|
42
|
+
const command = button.getAttribute('command');
|
|
43
|
+
if (command === 'show-modal' && !target.open) target.showModal();
|
|
44
|
+
else if (command === 'close' && target.open) target.close(button.value || undefined);
|
|
45
|
+
else if (command === 'request-close' && target.open) requestClose(target, button.value || undefined);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!supportsClosedBy) {
|
|
50
|
+
document.addEventListener('click', (event) => {
|
|
51
|
+
const dialog = event.target;
|
|
52
|
+
if (!(dialog instanceof HTMLDialogElement) || !dialog.open) return;
|
|
53
|
+
if (!dialog.classList.contains('sinn-dialog') || dialog.getAttribute('closedby') !== 'any') return;
|
|
54
|
+
requestClose(dialog);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
document.addEventListener(
|
|
58
|
+
'cancel',
|
|
59
|
+
(event) => {
|
|
60
|
+
const dialog = event.target;
|
|
61
|
+
if (dialog instanceof HTMLDialogElement && dialog.getAttribute('closedby') === 'none') {
|
|
62
|
+
event.preventDefault();
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
true,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Module scope (keeps top-level names private to this file).
|
|
71
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ReducedMotion: plays, pauses and swaps motion content to match the user's preference.
|
|
3
|
+
* - Videos marked data-autoplay start only when motion is allowed (and pause when it isn't),
|
|
4
|
+
* so reduced-motion users never see them begin.
|
|
5
|
+
* - Web Animations and CSS animations inside are paused via getAnimations() and data-paused.
|
|
6
|
+
* - The optional Pause/Play control (WCAG 2.2.2) toggles the same paused state; a user's
|
|
7
|
+
* explicit pause survives preference changes.
|
|
8
|
+
*/
|
|
9
|
+
import { onReducedMotionChange, prefersReducedMotion } from '../lib/motion';
|
|
10
|
+
|
|
11
|
+
const FLAG = '__sinnReducedMotion';
|
|
12
|
+
const w = window as unknown as Record<string, boolean>;
|
|
13
|
+
const userPaused = new WeakSet<HTMLElement>();
|
|
14
|
+
|
|
15
|
+
function setPaused(region: HTMLElement, paused: boolean) {
|
|
16
|
+
region.toggleAttribute('data-paused', paused);
|
|
17
|
+
const content = region.querySelector<HTMLElement>(':scope > .sinn-motion__motion');
|
|
18
|
+
if (!content) return;
|
|
19
|
+
for (const video of content.querySelectorAll<HTMLVideoElement>('video[data-autoplay], video[autoplay]')) {
|
|
20
|
+
if (paused) video.pause();
|
|
21
|
+
else {
|
|
22
|
+
video.muted = true; // autoplay policies require muted
|
|
23
|
+
video.play().catch(() => {});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
for (const animation of content.getAnimations?.({ subtree: true }) ?? []) {
|
|
27
|
+
if (paused) animation.pause();
|
|
28
|
+
else animation.play();
|
|
29
|
+
}
|
|
30
|
+
const toggle = region.querySelector<HTMLButtonElement>(':scope > [data-sinn-motion-toggle]');
|
|
31
|
+
if (toggle) {
|
|
32
|
+
toggle.setAttribute('aria-pressed', String(paused));
|
|
33
|
+
const label = toggle.querySelector('[data-label]');
|
|
34
|
+
if (label) label.textContent = (paused ? toggle.dataset.playLabel : toggle.dataset.pauseLabel) ?? '';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function sync(reduced = prefersReducedMotion()) {
|
|
39
|
+
for (const region of document.querySelectorAll<HTMLElement>('[data-sinn-motion]')) {
|
|
40
|
+
setPaused(region, reduced || userPaused.has(region));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!w[FLAG]) {
|
|
45
|
+
w[FLAG] = true;
|
|
46
|
+
sync();
|
|
47
|
+
onReducedMotionChange(sync);
|
|
48
|
+
document.addEventListener('astro:page-load', () => sync());
|
|
49
|
+
|
|
50
|
+
document.addEventListener('click', (event) => {
|
|
51
|
+
const toggle = (event.target as Element | null)?.closest?.<HTMLButtonElement>('[data-sinn-motion-toggle]');
|
|
52
|
+
const region = toggle?.closest<HTMLElement>('[data-sinn-motion]');
|
|
53
|
+
if (!toggle || !region) return;
|
|
54
|
+
const pause = !region.hasAttribute('data-paused');
|
|
55
|
+
if (pause) userPaused.add(region);
|
|
56
|
+
else userPaused.delete(region);
|
|
57
|
+
setPaused(region, pause);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slider: keeps the rail fill, ticks and readout in step with the native thumbs, announces
|
|
3
|
+
* formatted values (aria-valuetext), stops a range's thumbs crossing, and lets a click on a
|
|
4
|
+
* range's rail move the nearer thumb. Delegated from `document`, so sliders added later
|
|
5
|
+
* (view transitions, client rendering) need no setup.
|
|
6
|
+
*
|
|
7
|
+
* Events: native `input`/`change` fire on each thumb as usual. `sinn-slider-change`
|
|
8
|
+
* (bubbles, composed) fires on the slider when a value is committed, with
|
|
9
|
+
* `{ value: number | [low, high] }`.
|
|
10
|
+
*/
|
|
11
|
+
import type { SinnSliderChangeDetail, ValueFormat } from '../types';
|
|
12
|
+
import { formatValue } from '../utils';
|
|
13
|
+
import { rangeText, snap, toPct, topThumb } from '../lib/slider';
|
|
14
|
+
|
|
15
|
+
const FLAG = '__sinnSlider';
|
|
16
|
+
const w = window as unknown as Record<string, boolean>;
|
|
17
|
+
|
|
18
|
+
const inputsOf = (root: HTMLElement) => [...root.querySelectorAll<HTMLInputElement>('.sinn-slider__input')];
|
|
19
|
+
|
|
20
|
+
function formatter(root: HTMLElement): (v: number) => string {
|
|
21
|
+
let spec: ValueFormat = 'number';
|
|
22
|
+
try {
|
|
23
|
+
if (root.dataset.format) spec = JSON.parse(root.dataset.format) as ValueFormat;
|
|
24
|
+
} catch {
|
|
25
|
+
/* keep the plain number */
|
|
26
|
+
}
|
|
27
|
+
return (v) => formatValue(v, spec, root.dataset.locale || 'en-US');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Enforce order and the minimum gap. The thumb being moved gives way, never the other one. */
|
|
31
|
+
function clamp(root: HTMLElement, moved?: HTMLInputElement) {
|
|
32
|
+
const inputs = inputsOf(root);
|
|
33
|
+
if (inputs.length !== 2) return;
|
|
34
|
+
const [a, b] = inputs;
|
|
35
|
+
const min = Number(a.min);
|
|
36
|
+
const max = Number(a.max);
|
|
37
|
+
const step = Number(a.step) || 1;
|
|
38
|
+
const gap = (Number(root.dataset.minGap) || 0) * step;
|
|
39
|
+
const lo = Number(a.value);
|
|
40
|
+
const hi = Number(b.value);
|
|
41
|
+
if (hi - lo >= gap) return;
|
|
42
|
+
if (moved === b) b.value = String(snap(Math.min(max, lo + gap), min, max, step));
|
|
43
|
+
else a.value = String(snap(Math.max(min, hi - gap), min, max, step));
|
|
44
|
+
// At an end there may be no room: push the other thumb.
|
|
45
|
+
if (Number(b.value) - Number(a.value) < gap) {
|
|
46
|
+
if (moved === b) a.value = String(snap(Number(b.value) - gap, min, max, step));
|
|
47
|
+
else b.value = String(snap(Number(a.value) + gap, min, max, step));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function sync(root: HTMLElement) {
|
|
52
|
+
const inputs = inputsOf(root);
|
|
53
|
+
if (!inputs.length) return;
|
|
54
|
+
const min = Number(inputs[0].min);
|
|
55
|
+
const max = Number(inputs[0].max);
|
|
56
|
+
const values = inputs.map((i) => Number(i.value));
|
|
57
|
+
const pcts = values.map((v) => toPct(v, min, max));
|
|
58
|
+
const fmt = formatter(root);
|
|
59
|
+
const texts = values.map(fmt);
|
|
60
|
+
inputs.forEach((input, i) => input.setAttribute('aria-valuetext', texts[i]));
|
|
61
|
+
|
|
62
|
+
const range = inputs.length === 2;
|
|
63
|
+
const [from, to] = range ? [pcts[0], pcts[1]] : [0, pcts[0]];
|
|
64
|
+
const fill = root.querySelector('.sinn-slider__fill');
|
|
65
|
+
fill?.setAttribute('x', `${from}%`);
|
|
66
|
+
fill?.setAttribute('width', `${Math.max(0, to - from)}%`);
|
|
67
|
+
const lo = range ? values[0] : min;
|
|
68
|
+
const hi = values[values.length - 1];
|
|
69
|
+
for (const tick of root.querySelectorAll<SVGElement>('.sinn-slider__tick')) {
|
|
70
|
+
const v = Number(tick.dataset.value);
|
|
71
|
+
tick.toggleAttribute('data-active', v >= lo && v <= hi);
|
|
72
|
+
}
|
|
73
|
+
const out = root.querySelector('.sinn-slider__output');
|
|
74
|
+
if (out) out.textContent = range ? rangeText(texts[0], texts[1]) : texts[0];
|
|
75
|
+
if (range) root.dataset.top = topThumb(values[0], min, max);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const rootOf = (t: EventTarget | null) =>
|
|
79
|
+
t instanceof HTMLInputElement && t.classList.contains('sinn-slider__input') ? t.closest<HTMLElement>('[data-sinn-slider]') : null;
|
|
80
|
+
|
|
81
|
+
const valueOf = (root: HTMLElement): SinnSliderChangeDetail['value'] => {
|
|
82
|
+
const v = inputsOf(root).map((i) => Number(i.value));
|
|
83
|
+
return v.length === 2 ? [v[0], v[1]] : v[0];
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const syncAll = (scope: ParentNode = document) => {
|
|
87
|
+
for (const root of scope.querySelectorAll<HTMLElement>('[data-sinn-slider]')) sync(root);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
if (!w[FLAG]) {
|
|
91
|
+
w[FLAG] = true;
|
|
92
|
+
|
|
93
|
+
document.addEventListener('input', (e) => {
|
|
94
|
+
const root = rootOf(e.target);
|
|
95
|
+
if (!root) return;
|
|
96
|
+
clamp(root, e.target as HTMLInputElement);
|
|
97
|
+
sync(root);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
document.addEventListener('change', (e) => {
|
|
101
|
+
const root = rootOf(e.target);
|
|
102
|
+
if (!root) return;
|
|
103
|
+
root.dispatchEvent(
|
|
104
|
+
new CustomEvent<SinnSliderChangeDetail>('sinn-slider-change', { bubbles: true, composed: true, detail: { value: valueOf(root) } }),
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// A range's inputs ignore the pointer except on their thumbs; a press on the rail moves the nearer thumb.
|
|
109
|
+
document.addEventListener('pointerdown', (e) => {
|
|
110
|
+
const control = (e.target as Element | null)?.closest?.<HTMLElement>('[data-sinn-slider][data-range] .sinn-slider__control');
|
|
111
|
+
if (!control || (e.target as Element).classList.contains('sinn-slider__input') || e.button !== 0) return;
|
|
112
|
+
const root = control.closest<HTMLElement>('[data-sinn-slider]')!;
|
|
113
|
+
if (root.hasAttribute('data-disabled')) return;
|
|
114
|
+
const rail = root.querySelector('.sinn-slider__rail')!.getBoundingClientRect();
|
|
115
|
+
const rtl = getComputedStyle(root).direction === 'rtl';
|
|
116
|
+
const [a, b] = inputsOf(root);
|
|
117
|
+
const min = Number(a.min);
|
|
118
|
+
const max = Number(a.max);
|
|
119
|
+
const frac = Math.min(1, Math.max(0, (e.clientX - rail.left) / rail.width));
|
|
120
|
+
const v = snap(min + (rtl ? 1 - frac : frac) * (max - min), min, max, Number(a.step) || 1);
|
|
121
|
+
const lo = Number(a.value);
|
|
122
|
+
const hi = Number(b.value);
|
|
123
|
+
// Ties (both thumbs at the same value) go to the side the press is on.
|
|
124
|
+
const target = Math.abs(v - lo) < Math.abs(v - hi) || (lo === hi && v < lo) ? a : b;
|
|
125
|
+
target.value = String(v);
|
|
126
|
+
target.focus({ preventScroll: true });
|
|
127
|
+
target.dispatchEvent(new Event('input', { bubbles: true }));
|
|
128
|
+
target.dispatchEvent(new Event('change', { bubbles: true }));
|
|
129
|
+
e.preventDefault();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Form reset restores the inputs' default values after this event; re-sync on the next frame.
|
|
133
|
+
document.addEventListener('reset', (e) => {
|
|
134
|
+
const form = e.target as HTMLFormElement;
|
|
135
|
+
requestAnimationFrame(() => {
|
|
136
|
+
syncAll(form);
|
|
137
|
+
for (const input of document.querySelectorAll<HTMLInputElement>(`.sinn-slider__input[form="${CSS.escape(form.id)}"]`)) {
|
|
138
|
+
const root = rootOf(input);
|
|
139
|
+
if (root) sync(root);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Browsers restore form values on reload and back/forward: match the drawing to them.
|
|
145
|
+
window.addEventListener('pageshow', () => syncAll());
|
|
146
|
+
document.addEventListener('astro:page-load', () => syncAll());
|
|
147
|
+
}
|
|
148
|
+
syncAll();
|
|
149
|
+
|
|
150
|
+
export {};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sparkline readout for MetricCard: a crosshair snaps to the nearest point under the pointer
|
|
3
|
+
* (readers aim at a period, not a 2px line), the point lifts, and a tooltip shows the value
|
|
4
|
+
* (strong) and its label. Keyboard: focus the sparkline, then ←/→/Home/End; each step is
|
|
5
|
+
* announced politely. Values come from data attributes rendered by the server and are
|
|
6
|
+
* inserted with textContent.
|
|
7
|
+
*/
|
|
8
|
+
const FLAG = '__sinnSparkline';
|
|
9
|
+
const w = window as unknown as Record<string, boolean>;
|
|
10
|
+
|
|
11
|
+
interface Spark {
|
|
12
|
+
el: HTMLElement;
|
|
13
|
+
pts: Array<[number, number]>;
|
|
14
|
+
labels: string[];
|
|
15
|
+
values: string[];
|
|
16
|
+
vw: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const cache = new WeakMap<HTMLElement, Spark>();
|
|
20
|
+
const indexOf = new WeakMap<HTMLElement, number>();
|
|
21
|
+
|
|
22
|
+
function read(el: HTMLElement): Spark {
|
|
23
|
+
let s = cache.get(el);
|
|
24
|
+
if (!s) {
|
|
25
|
+
s = {
|
|
26
|
+
el,
|
|
27
|
+
pts: (el.dataset.points ?? '').split(';').map((p) => p.split(',').map(Number) as [number, number]),
|
|
28
|
+
labels: (el.dataset.labels ?? '').split('|'),
|
|
29
|
+
values: (el.dataset.values ?? '').split('|'),
|
|
30
|
+
vw: Number(el.dataset.vw) || 240,
|
|
31
|
+
};
|
|
32
|
+
cache.set(el, s);
|
|
33
|
+
}
|
|
34
|
+
return s;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function show(s: Spark, i: number, announce = false) {
|
|
38
|
+
const [x, y] = s.pts[i];
|
|
39
|
+
indexOf.set(s.el, i);
|
|
40
|
+
const layer = s.el.querySelector('.sinn-sparkline__hover-layer');
|
|
41
|
+
const line = s.el.querySelector('.sinn-sparkline__crosshair');
|
|
42
|
+
const dot = s.el.querySelector('.sinn-sparkline__hover');
|
|
43
|
+
layer?.setAttribute('visibility', 'visible');
|
|
44
|
+
line?.setAttribute('x1', String(x));
|
|
45
|
+
line?.setAttribute('x2', String(x));
|
|
46
|
+
dot?.setAttribute('cx', String(x));
|
|
47
|
+
dot?.setAttribute('cy', String(y));
|
|
48
|
+
|
|
49
|
+
const tip = s.el.querySelector<HTMLElement>('.sinn-sparkline__tip');
|
|
50
|
+
if (tip) {
|
|
51
|
+
const strong = tip.querySelector('strong');
|
|
52
|
+
const span = tip.querySelector('span');
|
|
53
|
+
if (strong) strong.textContent = s.values[i] ?? '';
|
|
54
|
+
if (span) span.textContent = s.labels[i] ?? '';
|
|
55
|
+
const ratio = x / s.vw;
|
|
56
|
+
// CSSOM writes are allowed under a strict CSP (only style *attributes* in markup are blocked).
|
|
57
|
+
// Physical `left`: SVG coordinates aren't mirrored in right-to-left pages.
|
|
58
|
+
tip.style.left = `${(ratio * 100).toFixed(2)}%`;
|
|
59
|
+
tip.dataset.edge = ratio < 0.15 ? 'start' : ratio > 0.85 ? 'end' : '';
|
|
60
|
+
tip.hidden = false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (announce) {
|
|
64
|
+
const live = s.el.closest('.sinn-metric')?.querySelector('[data-sinn-sparkline-live]');
|
|
65
|
+
if (live) live.textContent = `${s.labels[i] ?? ''}: ${s.values[i] ?? ''}`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function hide(el: HTMLElement) {
|
|
70
|
+
// Forget the position: the next focus starts again at the current (latest) period.
|
|
71
|
+
indexOf.delete(el);
|
|
72
|
+
el.querySelector('.sinn-sparkline__hover-layer')?.setAttribute('visibility', 'hidden');
|
|
73
|
+
const tip = el.querySelector<HTMLElement>('.sinn-sparkline__tip');
|
|
74
|
+
if (tip) tip.hidden = true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function nearest(s: Spark, clientX: number): number {
|
|
78
|
+
const svg = s.el.querySelector('svg');
|
|
79
|
+
if (!svg) return 0;
|
|
80
|
+
const rect = svg.getBoundingClientRect();
|
|
81
|
+
const fx = (clientX - rect.left) / rect.width;
|
|
82
|
+
const x = fx * s.vw;
|
|
83
|
+
let best = 0;
|
|
84
|
+
for (let i = 1; i < s.pts.length; i++) if (Math.abs(s.pts[i][0] - x) < Math.abs(s.pts[best][0] - x)) best = i;
|
|
85
|
+
return best;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!w[FLAG]) {
|
|
89
|
+
w[FLAG] = true;
|
|
90
|
+
const target = (e: Event) => (e.target as Element | null)?.closest?.<HTMLElement>('[data-sinn-sparkline]') ?? null;
|
|
91
|
+
|
|
92
|
+
document.addEventListener('pointermove', (e) => {
|
|
93
|
+
const el = target(e);
|
|
94
|
+
if (!el) return;
|
|
95
|
+
const s = read(el);
|
|
96
|
+
show(s, nearest(s, e.clientX));
|
|
97
|
+
});
|
|
98
|
+
document.addEventListener('pointerout', (e) => {
|
|
99
|
+
const el = target(e);
|
|
100
|
+
if (el && !(e.relatedTarget instanceof Node && el.contains(e.relatedTarget)) && document.activeElement !== el) hide(el);
|
|
101
|
+
});
|
|
102
|
+
document.addEventListener('focusin', (e) => {
|
|
103
|
+
const el = target(e);
|
|
104
|
+
if (el) {
|
|
105
|
+
const s = read(el);
|
|
106
|
+
show(s, indexOf.get(el) ?? s.pts.length - 1);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
document.addEventListener('focusout', (e) => {
|
|
110
|
+
const el = target(e);
|
|
111
|
+
if (el) hide(el);
|
|
112
|
+
});
|
|
113
|
+
document.addEventListener('keydown', (e) => {
|
|
114
|
+
const el = target(e);
|
|
115
|
+
if (!el || document.activeElement !== el) return;
|
|
116
|
+
const s = read(el);
|
|
117
|
+
const cur = indexOf.get(el) ?? s.pts.length - 1;
|
|
118
|
+
const map: Record<string, number> = {
|
|
119
|
+
ArrowLeft: Math.max(0, cur - 1),
|
|
120
|
+
ArrowRight: Math.min(s.pts.length - 1, cur + 1),
|
|
121
|
+
Home: 0,
|
|
122
|
+
End: s.pts.length - 1,
|
|
123
|
+
};
|
|
124
|
+
if (!(e.key in map)) return;
|
|
125
|
+
e.preventDefault();
|
|
126
|
+
show(s, map[e.key], true);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Module scope (keeps top-level names private to this file).
|
|
131
|
+
export {};
|