@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,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridges planImage() to Astro's image service. Imports `astro:assets`, so it runs only
|
|
3
|
+
* inside an Astro build (it's used by Image, Picture and Thumbnail at render time).
|
|
4
|
+
*/
|
|
5
|
+
import { getImage } from 'astro:assets';
|
|
6
|
+
import type { ImageMetadata } from 'astro';
|
|
7
|
+
import { planImage, type Aspect, type ImageLayout } from './image-plan';
|
|
8
|
+
|
|
9
|
+
export type ImageSource = ImageMetadata | string;
|
|
10
|
+
export type ImageFormat = 'avif' | 'webp' | 'jpeg' | 'jpg' | 'png' | 'svg';
|
|
11
|
+
export type ImageQuality = 'low' | 'mid' | 'high' | 'max' | number;
|
|
12
|
+
|
|
13
|
+
export interface ImageSetOptions {
|
|
14
|
+
src: ImageSource;
|
|
15
|
+
/** Required for remote (string) images unless `inferSize`. */
|
|
16
|
+
width?: number;
|
|
17
|
+
height?: number;
|
|
18
|
+
aspect?: Aspect;
|
|
19
|
+
layout?: ImageLayout;
|
|
20
|
+
sizes?: string;
|
|
21
|
+
format?: ImageFormat;
|
|
22
|
+
quality?: ImageQuality;
|
|
23
|
+
/** Remote images: fetch the file to read its size (slower builds). */
|
|
24
|
+
inferSize?: boolean;
|
|
25
|
+
/** Focus point when cropping to `aspect`. */
|
|
26
|
+
position?: 'center' | 'top' | 'bottom' | 'start' | 'end';
|
|
27
|
+
/**
|
|
28
|
+
* Largest CSS width it's shown at, when `width` describes the file (remote images).
|
|
29
|
+
* For local images `width` already means display width.
|
|
30
|
+
*/
|
|
31
|
+
displayWidth?: number;
|
|
32
|
+
/** Rendered with loading="lazy" (enables sizes="auto"). */
|
|
33
|
+
lazy?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Art-direction source for Picture. */
|
|
37
|
+
export interface PictureSource {
|
|
38
|
+
/** Media query, e.g. '(max-width: 40rem)'. */
|
|
39
|
+
media: string;
|
|
40
|
+
src: ImageSource;
|
|
41
|
+
width?: number;
|
|
42
|
+
height?: number;
|
|
43
|
+
displayWidth?: number;
|
|
44
|
+
aspect?: Aspect;
|
|
45
|
+
sizes?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ImageSet {
|
|
49
|
+
src: string;
|
|
50
|
+
srcset?: string;
|
|
51
|
+
sizes?: string;
|
|
52
|
+
width: number;
|
|
53
|
+
height: number;
|
|
54
|
+
type?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const MIME: Record<string, string> = {
|
|
58
|
+
avif: 'image/avif',
|
|
59
|
+
webp: 'image/webp',
|
|
60
|
+
jpeg: 'image/jpeg',
|
|
61
|
+
jpg: 'image/jpeg',
|
|
62
|
+
png: 'image/png',
|
|
63
|
+
svg: 'image/svg+xml',
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
async function sourceSize(o: ImageSetOptions): Promise<{ w: number; h: number }> {
|
|
67
|
+
if (typeof o.src !== 'string') return { w: o.src.width, h: o.src.height };
|
|
68
|
+
if (o.width && o.height) return { w: o.width, h: o.height };
|
|
69
|
+
if (o.inferSize) {
|
|
70
|
+
const probe = await getImage({ src: o.src, inferSize: true });
|
|
71
|
+
const w = Number(probe.attributes.width ?? probe.options.width);
|
|
72
|
+
const h = Number(probe.attributes.height ?? probe.options.height);
|
|
73
|
+
if (w && h) return { w, h };
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`Image "${o.src}": remote images need width and height (or inferSize).`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function buildImageSet(o: ImageSetOptions): Promise<ImageSet> {
|
|
79
|
+
const { w: sourceWidth, h: sourceHeight } = await sourceSize(o);
|
|
80
|
+
const remote = typeof o.src === 'string';
|
|
81
|
+
// For local files, `width` is the display width; for remote ones it's also the intrinsic width.
|
|
82
|
+
const plan = planImage({
|
|
83
|
+
sourceWidth,
|
|
84
|
+
sourceHeight,
|
|
85
|
+
width: o.displayWidth ?? (remote ? undefined : o.width),
|
|
86
|
+
aspect: o.aspect,
|
|
87
|
+
layout: o.layout,
|
|
88
|
+
sizes: o.sizes,
|
|
89
|
+
lazy: o.lazy,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// With an aspect, crop the files themselves (fit: cover) so every srcset candidate has the
|
|
93
|
+
// reserved ratio: the box never changes once the image loads, even when art direction
|
|
94
|
+
// swaps sources. Without one, keep the source's proportions.
|
|
95
|
+
const crop = Boolean(o.aspect && o.aspect !== 'auto');
|
|
96
|
+
const POSITION = { center: 'center', top: 'top', bottom: 'bottom', start: 'left', end: 'right' } as const;
|
|
97
|
+
const result = await getImage({
|
|
98
|
+
src: o.src,
|
|
99
|
+
width: plan.width,
|
|
100
|
+
...(crop
|
|
101
|
+
? { height: plan.attrHeight, fit: 'cover', position: POSITION[o.position ?? 'center'] }
|
|
102
|
+
: remote
|
|
103
|
+
? { height: Math.round((plan.width * sourceHeight) / sourceWidth) }
|
|
104
|
+
: {}),
|
|
105
|
+
...(o.format ? { format: o.format } : {}),
|
|
106
|
+
...(o.quality !== undefined ? { quality: o.quality } : {}),
|
|
107
|
+
...(plan.widths ? { widths: plan.widths } : {}),
|
|
108
|
+
...(plan.densities && plan.densities.length > 1 ? { densities: plan.densities } : {}),
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
src: result.src,
|
|
113
|
+
srcset: result.srcSet?.attribute || undefined,
|
|
114
|
+
sizes: plan.densities ? undefined : plan.sizes,
|
|
115
|
+
width: plan.attrWidth,
|
|
116
|
+
height: plan.attrHeight,
|
|
117
|
+
type: o.format ? MIME[o.format] : undefined,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Default fallback format for <Picture>: keep PNG (transparency), otherwise JPEG. */
|
|
122
|
+
export function fallbackFormatFor(src: ImageSource): ImageFormat {
|
|
123
|
+
if (typeof src !== 'string') {
|
|
124
|
+
if (src.format === 'svg') return 'svg';
|
|
125
|
+
if (src.format === 'png' || src.format === 'gif') return 'png';
|
|
126
|
+
return 'jpg';
|
|
127
|
+
}
|
|
128
|
+
return /\.png($|\?)/i.test(src) ? 'png' : 'jpg';
|
|
129
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Motion preference helpers for scripts and client islands (scroll effects, canvas,
|
|
3
|
+
* carousels, Lottie…). SSR-safe: no DOM access at import time.
|
|
4
|
+
*
|
|
5
|
+
* The effective preference is the OS setting (prefers-reduced-motion), overridden by
|
|
6
|
+
* <html data-motion="reduce|full"> when a site offers its own switch.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type MotionPreference = 'reduce' | 'full' | 'system';
|
|
10
|
+
|
|
11
|
+
const QUERY = '(prefers-reduced-motion: reduce)';
|
|
12
|
+
const COOKIE = 'sinn-motion';
|
|
13
|
+
|
|
14
|
+
/** True when motion should be reduced right now. Always false on the server. */
|
|
15
|
+
export function prefersReducedMotion(): boolean {
|
|
16
|
+
if (typeof window === 'undefined') return false;
|
|
17
|
+
const override = document.documentElement.dataset.motion;
|
|
18
|
+
if (override === 'reduce') return true;
|
|
19
|
+
if (override === 'full') return false;
|
|
20
|
+
return window.matchMedia(QUERY).matches;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Calls `callback(reduced)` whenever the effective preference changes (OS setting or the
|
|
25
|
+
* html data-motion override). Returns an unsubscribe function.
|
|
26
|
+
*/
|
|
27
|
+
export function onReducedMotionChange(callback: (reduced: boolean) => void): () => void {
|
|
28
|
+
if (typeof window === 'undefined') return () => {};
|
|
29
|
+
let last = prefersReducedMotion();
|
|
30
|
+
const check = () => {
|
|
31
|
+
const now = prefersReducedMotion();
|
|
32
|
+
if (now !== last) {
|
|
33
|
+
last = now;
|
|
34
|
+
callback(now);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const media = window.matchMedia(QUERY);
|
|
38
|
+
media.addEventListener('change', check);
|
|
39
|
+
const observer = new MutationObserver(check);
|
|
40
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['data-motion'] });
|
|
41
|
+
return () => {
|
|
42
|
+
media.removeEventListener('change', check);
|
|
43
|
+
observer.disconnect();
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Site-level switch. Sets <html data-motion> and remembers the choice in a cookie, so a
|
|
49
|
+
* server-rendered page can print the attribute on the next request (no flash).
|
|
50
|
+
* 'system' clears the override and follows the OS again.
|
|
51
|
+
*/
|
|
52
|
+
export function setMotionPreference(preference: MotionPreference, options: { persist?: boolean } = {}): void {
|
|
53
|
+
if (typeof window === 'undefined') return;
|
|
54
|
+
const root = document.documentElement;
|
|
55
|
+
if (preference === 'system') delete root.dataset.motion;
|
|
56
|
+
else root.dataset.motion = preference;
|
|
57
|
+
if (options.persist !== false) {
|
|
58
|
+
const value = preference === 'system' ? '' : preference;
|
|
59
|
+
const maxAge = preference === 'system' ? 0 : 60 * 60 * 24 * 365;
|
|
60
|
+
document.cookie = `${COOKIE}=${value}; Path=/; Max-Age=${maxAge}; SameSite=Lax`;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Reads the saved preference from a Cookie header (for SSR): `<html data-motion={motionFromCookie(...)}>`. */
|
|
65
|
+
export function motionFromCookie(cookieHeader: string | null | undefined): 'reduce' | 'full' | undefined {
|
|
66
|
+
const match = /(?:^|;\s*)sinn-motion=(reduce|full)/.exec(cookieHeader ?? '');
|
|
67
|
+
return match ? (match[1] as 'reduce' | 'full') : undefined;
|
|
68
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slider model: snaps and clamps values, works out track positions and formatted text.
|
|
3
|
+
* Pure (no DOM), shared by Slider.astro and the tests. The client script only re-runs the
|
|
4
|
+
* small parts it needs (snap, pct, text) as the thumbs move.
|
|
5
|
+
*/
|
|
6
|
+
import type { ValueFormat } from '../types';
|
|
7
|
+
import { formatValue } from '../utils';
|
|
8
|
+
|
|
9
|
+
export interface SliderMarkInput {
|
|
10
|
+
value: number;
|
|
11
|
+
/** Text under the track. Omit for a tick with no label. */
|
|
12
|
+
label?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SliderInput {
|
|
16
|
+
min?: number;
|
|
17
|
+
max?: number;
|
|
18
|
+
step?: number;
|
|
19
|
+
/** One number for a single thumb, two for a range. */
|
|
20
|
+
value?: number | readonly [number, number];
|
|
21
|
+
/** Range only: the smallest allowed distance between the thumbs, in steps. */
|
|
22
|
+
minGap?: number;
|
|
23
|
+
format?: ValueFormat;
|
|
24
|
+
locale?: string;
|
|
25
|
+
/**
|
|
26
|
+
* `true`: label the two ends. An array: ticks at those values (with optional labels).
|
|
27
|
+
* Values outside min–max are dropped.
|
|
28
|
+
*/
|
|
29
|
+
marks?: boolean | ReadonlyArray<number | SliderMarkInput>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SliderMark {
|
|
33
|
+
value: number;
|
|
34
|
+
/** Position along the track, 0–100. */
|
|
35
|
+
pct: number;
|
|
36
|
+
/** Whole-number position for the data-pct label rule. */
|
|
37
|
+
pctRound: number;
|
|
38
|
+
label?: string;
|
|
39
|
+
/** Inside the selected part of the track (drawn in the fill color). */
|
|
40
|
+
active: boolean;
|
|
41
|
+
/** Labels at the ends hug the edge instead of centering past it. */
|
|
42
|
+
edge?: 'start' | 'end';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface SliderModel {
|
|
46
|
+
range: boolean;
|
|
47
|
+
min: number;
|
|
48
|
+
max: number;
|
|
49
|
+
step: number;
|
|
50
|
+
/** Snapped, clamped, ordered. One value, or [low, high]. */
|
|
51
|
+
values: number[];
|
|
52
|
+
/** Thumb positions, 0–100. */
|
|
53
|
+
pcts: number[];
|
|
54
|
+
/** Formatted values, for aria-valuetext and the readout. */
|
|
55
|
+
texts: string[];
|
|
56
|
+
/** The visible readout: "$20" or "$20 – $80". */
|
|
57
|
+
output: string;
|
|
58
|
+
/** Selected part of the track as [from, to] in percent. A single slider fills from 0. */
|
|
59
|
+
fill: [number, number];
|
|
60
|
+
marks: SliderMark[];
|
|
61
|
+
/** Range only: which thumb is stacked on top (so a thumb parked at an end can still move). */
|
|
62
|
+
top: 'lo' | 'hi';
|
|
63
|
+
minGap: number;
|
|
64
|
+
warnings: string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Decimal places in a step (0.25 → 2), so snapping doesn't produce 0.30000000000000004. */
|
|
68
|
+
export function stepDecimals(step: number): number {
|
|
69
|
+
const s = String(step);
|
|
70
|
+
if (s.includes('e-')) return Number(s.split('e-')[1]);
|
|
71
|
+
return s.includes('.') ? s.split('.')[1].length : 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function snap(value: number, min: number, max: number, step: number): number {
|
|
75
|
+
if (!Number.isFinite(value)) return min;
|
|
76
|
+
const d = stepDecimals(step);
|
|
77
|
+
const n = Math.round((Math.min(max, Math.max(min, value)) - min) / step);
|
|
78
|
+
// The highest reachable value may sit below max when (max - min) isn't a multiple of step.
|
|
79
|
+
const last = Math.floor((max - min) / step + 1e-9);
|
|
80
|
+
return Number((min + Math.min(n, last) * step).toFixed(d));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function toPct(value: number, min: number, max: number): number {
|
|
84
|
+
return max === min ? 0 : Math.round(((value - min) / (max - min)) * 1e6) / 1e4;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The thumb stacked on top: low when it's in the upper half (else it could get stuck at max). */
|
|
88
|
+
export function topThumb(lo: number, min: number, max: number): 'lo' | 'hi' {
|
|
89
|
+
return lo > (min + max) / 2 ? 'lo' : 'hi';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Readout for a range. The en dash is spaced so "−$20 – $80" stays readable. */
|
|
93
|
+
export function rangeText(a: string, b: string): string {
|
|
94
|
+
return `${a} – ${b}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function sliderModel(input: SliderInput): SliderModel {
|
|
98
|
+
const warnings: string[] = [];
|
|
99
|
+
let min = Number(input.min ?? 0);
|
|
100
|
+
let max = Number(input.max ?? 100);
|
|
101
|
+
let step = Number(input.step ?? 1);
|
|
102
|
+
if (!(max > min)) {
|
|
103
|
+
warnings.push(`Slider max (${max}) must be greater than min (${min}).`);
|
|
104
|
+
max = min + 1;
|
|
105
|
+
}
|
|
106
|
+
if (!(step > 0)) {
|
|
107
|
+
warnings.push(`Slider step must be positive; using 1.`);
|
|
108
|
+
step = 1;
|
|
109
|
+
}
|
|
110
|
+
const range = Array.isArray(input.value);
|
|
111
|
+
const minGap = Math.max(0, Math.floor(input.minGap ?? 0));
|
|
112
|
+
const gap = minGap * step;
|
|
113
|
+
if (range && gap > max - min) warnings.push(`minGap (${minGap} steps) is wider than the whole range.`);
|
|
114
|
+
|
|
115
|
+
let values: number[];
|
|
116
|
+
if (range) {
|
|
117
|
+
const [a, b] = input.value as readonly [number, number];
|
|
118
|
+
let lo = snap(Math.min(a, b), min, max, step);
|
|
119
|
+
let hi = snap(Math.max(a, b), min, max, step);
|
|
120
|
+
if (hi - lo < gap) {
|
|
121
|
+
hi = snap(Math.min(max, lo + gap), min, max, step);
|
|
122
|
+
lo = snap(Math.max(min, hi - gap), min, max, step);
|
|
123
|
+
}
|
|
124
|
+
values = [lo, hi];
|
|
125
|
+
} else {
|
|
126
|
+
const v = input.value === undefined ? min + (max - min) / 2 : (input.value as number);
|
|
127
|
+
values = [snap(v, min, max, step)];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const pcts = values.map((v) => toPct(v, min, max));
|
|
131
|
+
const fmt = (v: number) => formatValue(v, input.format ?? 'number', input.locale ?? 'en-US');
|
|
132
|
+
const texts = values.map(fmt);
|
|
133
|
+
const fill: [number, number] = range ? [pcts[0], pcts[1]] : [0, pcts[0]];
|
|
134
|
+
|
|
135
|
+
const raw: SliderMarkInput[] =
|
|
136
|
+
input.marks === true
|
|
137
|
+
? [{ value: min, label: fmt(min) }, { value: max, label: fmt(max) }]
|
|
138
|
+
: Array.isArray(input.marks)
|
|
139
|
+
? input.marks.map((m) => (typeof m === 'number' ? { value: m, label: fmt(m) } : m))
|
|
140
|
+
: [];
|
|
141
|
+
const marks: SliderMark[] = raw
|
|
142
|
+
.filter((m) => Number.isFinite(m.value) && m.value >= min && m.value <= max)
|
|
143
|
+
.map((m) => {
|
|
144
|
+
const pct = toPct(m.value, min, max);
|
|
145
|
+
return {
|
|
146
|
+
value: m.value,
|
|
147
|
+
pct,
|
|
148
|
+
pctRound: Math.round(pct),
|
|
149
|
+
label: m.label,
|
|
150
|
+
active: m.value >= (range ? values[0] : min) && m.value <= values[values.length - 1],
|
|
151
|
+
edge: pct <= 2 ? 'start' : pct >= 98 ? 'end' : undefined,
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
range,
|
|
157
|
+
min,
|
|
158
|
+
max,
|
|
159
|
+
step,
|
|
160
|
+
values,
|
|
161
|
+
pcts,
|
|
162
|
+
texts,
|
|
163
|
+
output: range ? rangeText(texts[0], texts[1]) : texts[0],
|
|
164
|
+
fill,
|
|
165
|
+
marks,
|
|
166
|
+
top: range ? topThumb(values[0], min, max) : 'hi',
|
|
167
|
+
minGap,
|
|
168
|
+
warnings,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accordion enhancements. The sections are native <details> and work without this.
|
|
3
|
+
* - "Show all sections / Hide all sections" button (made visible here, so it never shows dead).
|
|
4
|
+
* - ↑/↓ move between section headers, Home/End jump to the first/last (APG, optional keys).
|
|
5
|
+
* - A link to an id inside a closed section opens it (browsers differ on doing this themselves).
|
|
6
|
+
* - Printing opens every section, then restores what was open.
|
|
7
|
+
* Delegated from `document`; per-instance setup is only revealing the button.
|
|
8
|
+
*/
|
|
9
|
+
const FLAG = '__sinnAccordion';
|
|
10
|
+
const w = window as unknown as Record<string, boolean>;
|
|
11
|
+
|
|
12
|
+
const itemsOf = (acc: Element) => [...acc.querySelectorAll<HTMLDetailsElement>(':scope > details.sinn-accordion__item')];
|
|
13
|
+
|
|
14
|
+
function updateButton(acc: Element) {
|
|
15
|
+
const btn = acc.querySelector<HTMLButtonElement>(':scope > .sinn-accordion__toolbar [data-sinn-accordion-all]');
|
|
16
|
+
if (!btn) return;
|
|
17
|
+
const allOpen = itemsOf(acc).every((d) => d.open);
|
|
18
|
+
btn.textContent = allOpen ? btn.dataset.hide || 'Hide all sections' : btn.dataset.show || 'Show all sections';
|
|
19
|
+
btn.setAttribute('aria-expanded', String(allOpen));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function init() {
|
|
23
|
+
for (const acc of document.querySelectorAll<HTMLElement>('[data-sinn-accordion]:not([data-ready])')) {
|
|
24
|
+
updateButton(acc);
|
|
25
|
+
acc.dataset.ready = ''; // reveals the button (its space was already reserved)
|
|
26
|
+
}
|
|
27
|
+
openTarget();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Open every closed <details> around the URL's target, then bring it into view. */
|
|
31
|
+
function openTarget() {
|
|
32
|
+
const id = decodeURIComponent(location.hash.slice(1));
|
|
33
|
+
if (!id) return;
|
|
34
|
+
const target = document.getElementById(id);
|
|
35
|
+
if (!target) return;
|
|
36
|
+
let opened = false;
|
|
37
|
+
for (let el: Element | null = target; el; el = el.parentElement) {
|
|
38
|
+
if (el instanceof HTMLDetailsElement && !el.open && (el === target || !el.querySelector(':scope > summary')?.contains(target))) {
|
|
39
|
+
// Open without the height animation: mid-animation the content is 0px tall and can't be scrolled to.
|
|
40
|
+
el.dataset.instant = '';
|
|
41
|
+
el.open = true;
|
|
42
|
+
opened = true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!opened) return;
|
|
46
|
+
// Instant, like the browser's own fragment scroll. Before `load` the browser may still reset
|
|
47
|
+
// the position (its own fragment scroll failed on the closed section), so wait for it.
|
|
48
|
+
const go = () =>
|
|
49
|
+
requestAnimationFrame(() => {
|
|
50
|
+
target.scrollIntoView({ block: 'start', behavior: 'instant' });
|
|
51
|
+
for (const d of document.querySelectorAll<HTMLElement>('details[data-instant]')) delete d.dataset.instant;
|
|
52
|
+
});
|
|
53
|
+
if (document.readyState === 'complete') go();
|
|
54
|
+
else window.addEventListener('load', go, { once: true });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!w[FLAG]) {
|
|
58
|
+
w[FLAG] = true;
|
|
59
|
+
|
|
60
|
+
document.addEventListener('click', (e) => {
|
|
61
|
+
const btn = (e.target as Element | null)?.closest?.<HTMLButtonElement>('[data-sinn-accordion-all]');
|
|
62
|
+
const acc = btn?.closest('[data-sinn-accordion]');
|
|
63
|
+
if (!btn || !acc) return;
|
|
64
|
+
const open = !itemsOf(acc).every((d) => d.open);
|
|
65
|
+
for (const d of itemsOf(acc)) d.open = open;
|
|
66
|
+
updateButton(acc);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// `toggle` doesn't bubble; listen in the capture phase.
|
|
70
|
+
document.addEventListener(
|
|
71
|
+
'toggle',
|
|
72
|
+
(e) => {
|
|
73
|
+
const acc = (e.target as Element).closest?.('[data-sinn-accordion]');
|
|
74
|
+
if (acc && (e.target as Element).classList.contains('sinn-accordion__item')) updateButton(acc);
|
|
75
|
+
},
|
|
76
|
+
true,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
document.addEventListener('keydown', (e) => {
|
|
80
|
+
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
|
|
81
|
+
const summary = (e.target as Element | null)?.closest?.('summary');
|
|
82
|
+
const item = summary?.parentElement;
|
|
83
|
+
if (!summary || !item?.classList.contains('sinn-accordion__item') || summary !== e.target) return;
|
|
84
|
+
const acc = item.parentElement!;
|
|
85
|
+
const heads = itemsOf(acc).map((d) => d.querySelector<HTMLElement>(':scope > summary')!);
|
|
86
|
+
const i = heads.indexOf(summary as HTMLElement);
|
|
87
|
+
const next =
|
|
88
|
+
e.key === 'ArrowDown' ? heads[(i + 1) % heads.length]
|
|
89
|
+
: e.key === 'ArrowUp' ? heads[(i - 1 + heads.length) % heads.length]
|
|
90
|
+
: e.key === 'Home' ? heads[0]
|
|
91
|
+
: e.key === 'End' ? heads[heads.length - 1]
|
|
92
|
+
: null;
|
|
93
|
+
if (!next) return;
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
next.focus();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
window.addEventListener('hashchange', openTarget);
|
|
99
|
+
|
|
100
|
+
let closedForPrint: HTMLDetailsElement[] = [];
|
|
101
|
+
window.addEventListener('beforeprint', () => {
|
|
102
|
+
closedForPrint = [...document.querySelectorAll<HTMLDetailsElement>('[data-sinn-accordion] > details:not([open])')];
|
|
103
|
+
// Single-mode sections share a name, which would let only one stay open: lift it while printing.
|
|
104
|
+
for (const d of closedForPrint) {
|
|
105
|
+
if (d.name) d.dataset.name = d.name;
|
|
106
|
+
d.removeAttribute('name');
|
|
107
|
+
}
|
|
108
|
+
for (const d of closedForPrint) d.open = true;
|
|
109
|
+
});
|
|
110
|
+
window.addEventListener('afterprint', () => {
|
|
111
|
+
for (const d of closedForPrint) {
|
|
112
|
+
d.open = false;
|
|
113
|
+
if (d.dataset.name) d.name = d.dataset.name;
|
|
114
|
+
delete d.dataset.name;
|
|
115
|
+
}
|
|
116
|
+
closedForPrint = [];
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
document.addEventListener('astro:page-load', init);
|
|
120
|
+
}
|
|
121
|
+
init();
|
|
122
|
+
|
|
123
|
+
export {};
|