@sinnstack/ui 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +89 -0
- package/LICENSE +373 -0
- package/README.md +188 -2
- package/docs/blocks.md +373 -0
- package/docs/data.md +128 -0
- package/docs/finishes.md +165 -0
- package/docs/motion.md +166 -0
- package/docs/primitives.md +510 -0
- package/docs/seo.md +191 -0
- package/docs/tokens.md +122 -0
- package/package.json +132 -3
- package/scripts/check-contrast.mjs +180 -0
- package/src/blocks/BlockRenderer.astro +60 -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 +173 -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 +58 -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 +48 -0
- package/src/blocks/cms/consent.ts +16 -0
- package/src/blocks/cms/contact-card.ts +46 -0
- package/src/blocks/cms/contact-form.ts +37 -0
- package/src/blocks/cms/cta-banner.ts +27 -0
- package/src/blocks/cms/cta-pair.ts +30 -0
- package/src/blocks/cms/data-base.ts +5 -0
- package/src/blocks/cms/data.ts +257 -0
- package/src/blocks/cms/faq.ts +38 -0
- package/src/blocks/cms/feature-grid.ts +47 -0
- package/src/blocks/cms/fixtures.ts +591 -0
- package/src/blocks/cms/hero-carousel.ts +36 -0
- package/src/blocks/cms/hero.ts +33 -0
- package/src/blocks/cms/highlight-cards.ts +41 -0
- package/src/blocks/cms/images.ts +37 -0
- package/src/blocks/cms/index.ts +59 -0
- package/src/blocks/cms/location-hours.ts +67 -0
- package/src/blocks/cms/logo-cloud.ts +36 -0
- package/src/blocks/cms/media-text.ts +26 -0
- package/src/blocks/cms/page-header.ts +27 -0
- package/src/blocks/cms/pages.ts +35 -0
- package/src/blocks/cms/prose-section.ts +25 -0
- package/src/blocks/cms/reviews.ts +50 -0
- package/src/blocks/cms/schema-dsl.ts +134 -0
- package/src/blocks/cms/schema.ts +794 -0
- package/src/blocks/cms/shared.ts +63 -0
- package/src/blocks/cms/stats-band.ts +35 -0
- package/src/blocks/cms/steps.ts +40 -0
- package/src/blocks/cms/testimonials.ts +46 -0
- package/src/blocks/cms/topbar.ts +27 -0
- package/src/blocks/cms/types.ts +27 -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 +35 -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/consent.ts +96 -0
- package/src/blocks/internal/fields.ts +18 -0
- package/src/blocks/internal/registry-contract.ts +67 -0
- package/src/blocks/internal/reviews.ts +22 -0
- package/src/blocks/internal/slug.ts +12 -0
- package/src/blocks/internal/util.ts +23 -0
- package/src/blocks/lib/contact.ts +92 -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/markdown.ts +350 -0
- package/src/blocks/lib/rich-text.ts +132 -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 +107 -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 +293 -0
- package/src/data/ChartAxis.astro +19 -0
- package/src/data/ChartContainer.astro +152 -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 +130 -0
- package/src/data/index.ts +10 -0
- package/src/data/internal/scale.ts +107 -0
- package/src/data/lib/chart.ts +459 -0
- package/src/data/lib/render.ts +219 -0
- package/src/data/lib/sparkline.ts +117 -0
- package/src/data/schema.ts +65 -0
- package/src/data/scripts/chart.ts +324 -0
- package/src/data/styles/chart.css +392 -0
- package/src/finishes/aurora.css +75 -0
- package/src/finishes/autumn.css +87 -0
- package/src/finishes/blueprint.css +69 -0
- package/src/finishes/brutalist.css +55 -0
- package/src/finishes/clay.css +48 -0
- package/src/finishes/day-cycle.css +46 -0
- package/src/finishes/fog.css +71 -0
- package/src/finishes/glass.css +149 -0
- package/src/finishes/holographic.css +91 -0
- package/src/finishes/night.css +77 -0
- package/src/finishes/paper.css +58 -0
- package/src/finishes/polished-glass.css +159 -0
- package/src/finishes/rain.css +84 -0
- package/src/finishes/scripts/day-cycle.ts +30 -0
- package/src/finishes/scripts/holographic.ts +37 -0
- package/src/finishes/scripts/spotlight.ts +30 -0
- package/src/finishes/spotlight.css +41 -0
- package/src/finishes/spring.css +80 -0
- package/src/finishes/terminal.css +49 -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 +86 -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 +278 -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 +109 -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 +75 -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 +91 -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/internal/image-plan.ts +82 -0
- package/src/primitives/internal/image.ts +129 -0
- package/src/primitives/internal/slider.ts +170 -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/motion.ts +68 -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 +59 -0
- package/src/seo/index.ts +10 -0
- package/src/seo/integration.ts +268 -0
- package/src/seo/internal/url.ts +68 -0
- package/src/seo/lib/jsonld.ts +342 -0
- package/src/seo/lib/meta.ts +205 -0
- package/src/seo/lib/redirects.ts +293 -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/schema.ts +43 -0
- package/src/tokens/base.css +71 -0
- package/src/tokens/tokens.css +210 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HeroCarousel enhancement. The server renders a scroll-snap strip that works on its own; this
|
|
3
|
+
* adds controls, keeps them in sync with swipes/scrolls, makes inactive slides inert and runs the
|
|
4
|
+
* optional autoplay (WAI-ARIA APG carousel pattern).
|
|
5
|
+
*
|
|
6
|
+
* Moving between slides scrolls the strip (`scrollTo`), never the page. Reduced motion: instant
|
|
7
|
+
* jumps and no autoplay. Event: `sinn-slide-change` (bubbles, composed) { index, previousIndex }.
|
|
8
|
+
* SSR-safe: nothing runs at import time outside the browser.
|
|
9
|
+
*/
|
|
10
|
+
import { prefersReducedMotion, onReducedMotionChange } from '../../primitives/lib/motion';
|
|
11
|
+
|
|
12
|
+
export interface SinnSlideChangeDetail {
|
|
13
|
+
index: number;
|
|
14
|
+
previousIndex: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const FLAG = '__sinnCarousel';
|
|
18
|
+
const w = (typeof window !== 'undefined' ? window : {}) as unknown as Record<string, boolean>;
|
|
19
|
+
|
|
20
|
+
class Carousel {
|
|
21
|
+
root: HTMLElement;
|
|
22
|
+
track: HTMLElement;
|
|
23
|
+
slides: HTMLElement[];
|
|
24
|
+
dots: HTMLButtonElement[];
|
|
25
|
+
rotate: HTMLButtonElement | null;
|
|
26
|
+
index = 0;
|
|
27
|
+
delay: number;
|
|
28
|
+
timer: number | undefined;
|
|
29
|
+
/** The user pressed pause (or reduced motion is on): never resume on its own. */
|
|
30
|
+
stopped: boolean;
|
|
31
|
+
hovering = false;
|
|
32
|
+
focused = false;
|
|
33
|
+
visible = true;
|
|
34
|
+
settleTimer: number | undefined;
|
|
35
|
+
cleanup: Array<() => void> = [];
|
|
36
|
+
|
|
37
|
+
constructor(root: HTMLElement) {
|
|
38
|
+
this.root = root;
|
|
39
|
+
this.track = root.querySelector<HTMLElement>(':scope > .sinn-carousel__track')!;
|
|
40
|
+
this.slides = [...this.track.querySelectorAll<HTMLElement>(':scope > .sinn-carousel__slide')];
|
|
41
|
+
const controls = root.querySelector<HTMLElement>(':scope > [data-carousel-controls]');
|
|
42
|
+
this.dots = [...(controls?.querySelectorAll<HTMLButtonElement>('[data-carousel-dot]') ?? [])];
|
|
43
|
+
this.rotate = controls?.querySelector<HTMLButtonElement>('[data-carousel-rotate]') ?? null;
|
|
44
|
+
this.delay = Math.max(5, Number(root.dataset.autoplay) || 0) * 1000;
|
|
45
|
+
this.stopped = !root.dataset.autoplay || prefersReducedMotion();
|
|
46
|
+
|
|
47
|
+
controls?.removeAttribute('hidden');
|
|
48
|
+
controls?.querySelector('[data-carousel-prev]')?.addEventListener('click', () => this.go(this.index - 1, true));
|
|
49
|
+
controls?.querySelector('[data-carousel-next]')?.addEventListener('click', () => this.go(this.index + 1, true));
|
|
50
|
+
this.dots.forEach((dot, i) => dot.addEventListener('click', () => this.go(i, true)));
|
|
51
|
+
this.rotate?.addEventListener('click', () => (this.stopped ? this.play() : this.pause(true)));
|
|
52
|
+
|
|
53
|
+
// Arrow keys on the focused strip move one slide (instead of scrolling by a few pixels).
|
|
54
|
+
this.track.addEventListener('keydown', (e) => {
|
|
55
|
+
if (e.target !== this.track || e.altKey || e.ctrlKey || e.metaKey) return;
|
|
56
|
+
const rtl = this.rtl();
|
|
57
|
+
const next = rtl ? 'ArrowLeft' : 'ArrowRight';
|
|
58
|
+
const prev = rtl ? 'ArrowRight' : 'ArrowLeft';
|
|
59
|
+
let to: number | null = null;
|
|
60
|
+
if (e.key === next) to = this.index + 1;
|
|
61
|
+
else if (e.key === prev) to = this.index - 1;
|
|
62
|
+
else if (e.key === 'Home') to = 0;
|
|
63
|
+
else if (e.key === 'End') to = this.slides.length - 1;
|
|
64
|
+
if (to === null) return;
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
this.go(to, true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Swipes and native scrolling: sync the state once the strip settles.
|
|
70
|
+
const onScroll = () => {
|
|
71
|
+
window.clearTimeout(this.settleTimer);
|
|
72
|
+
this.settleTimer = window.setTimeout(() => this.syncFromScroll(), 120);
|
|
73
|
+
};
|
|
74
|
+
this.track.addEventListener('scroll', onScroll, { passive: true });
|
|
75
|
+
this.track.addEventListener('scrollend', () => this.syncFromScroll());
|
|
76
|
+
|
|
77
|
+
root.addEventListener('pointerenter', () => { this.hovering = true; this.schedule(); });
|
|
78
|
+
root.addEventListener('pointerleave', () => { this.hovering = false; this.schedule(); });
|
|
79
|
+
root.addEventListener('focusin', () => { this.focused = true; this.schedule(); });
|
|
80
|
+
root.addEventListener('focusout', (e) => {
|
|
81
|
+
if (!root.contains(e.relatedTarget as Node | null)) { this.focused = false; this.schedule(); }
|
|
82
|
+
});
|
|
83
|
+
const onVisibility = () => this.schedule();
|
|
84
|
+
document.addEventListener('visibilitychange', onVisibility);
|
|
85
|
+
this.cleanup.push(() => document.removeEventListener('visibilitychange', onVisibility));
|
|
86
|
+
if ('IntersectionObserver' in window) {
|
|
87
|
+
const io = new IntersectionObserver(([entry]) => { this.visible = Boolean(entry?.isIntersecting); this.schedule(); }, { threshold: 0.35 });
|
|
88
|
+
io.observe(root);
|
|
89
|
+
this.cleanup.push(() => io.disconnect());
|
|
90
|
+
}
|
|
91
|
+
this.cleanup.push(onReducedMotionChange((reduced) => { if (reduced) this.pause(false); }));
|
|
92
|
+
|
|
93
|
+
// Keep the current slide aligned when the strip changes width (rotation, resize).
|
|
94
|
+
if ('ResizeObserver' in window) {
|
|
95
|
+
const ro = new ResizeObserver(() => this.scrollToIndex(this.index, false));
|
|
96
|
+
ro.observe(this.track);
|
|
97
|
+
this.cleanup.push(() => ro.disconnect());
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
this.index = this.indexFromScroll();
|
|
101
|
+
this.render(false);
|
|
102
|
+
this.updateRotate();
|
|
103
|
+
this.root.dataset.enhanced = '';
|
|
104
|
+
this.schedule();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
rtl() {
|
|
108
|
+
return getComputedStyle(this.track).direction === 'rtl';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
indexFromScroll(): number {
|
|
112
|
+
const width = this.track.clientWidth || 1;
|
|
113
|
+
return Math.min(this.slides.length - 1, Math.max(0, Math.round(Math.abs(this.track.scrollLeft) / width)));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
scrollToIndex(i: number, smooth: boolean) {
|
|
117
|
+
const left = (this.rtl() ? -1 : 1) * i * this.track.clientWidth;
|
|
118
|
+
this.track.scrollTo({ left, behavior: smooth && !prefersReducedMotion() ? 'smooth' : 'auto' });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `user`: a person moved the carousel (announce politely, stop autoplay for good). */
|
|
122
|
+
go(i: number, user: boolean) {
|
|
123
|
+
const n = this.slides.length;
|
|
124
|
+
const to = ((i % n) + n) % n;
|
|
125
|
+
if (user) this.pause(true);
|
|
126
|
+
this.scrollToIndex(to, true);
|
|
127
|
+
this.setIndex(to);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
syncFromScroll() {
|
|
131
|
+
const i = this.indexFromScroll();
|
|
132
|
+
if (i !== this.index) this.setIndex(i);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
setIndex(i: number) {
|
|
136
|
+
if (i === this.index) return this.render(false);
|
|
137
|
+
const previousIndex = this.index;
|
|
138
|
+
this.index = i;
|
|
139
|
+
this.render(true);
|
|
140
|
+
this.root.dispatchEvent(
|
|
141
|
+
new CustomEvent<SinnSlideChangeDetail>('sinn-slide-change', { bubbles: true, composed: true, detail: { index: i, previousIndex } }),
|
|
142
|
+
);
|
|
143
|
+
this.schedule();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
render(_announce: boolean) {
|
|
147
|
+
this.slides.forEach((slide, k) => {
|
|
148
|
+
const on = k === this.index;
|
|
149
|
+
slide.toggleAttribute('data-current', on);
|
|
150
|
+
slide.inert = !on;
|
|
151
|
+
});
|
|
152
|
+
this.dots.forEach((dot, k) => (k === this.index ? dot.setAttribute('aria-current', 'true') : dot.removeAttribute('aria-current')));
|
|
153
|
+
// Silent while rotating on its own; polite when a person is driving it.
|
|
154
|
+
this.track.setAttribute('aria-live', this.running() ? 'off' : 'polite');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
running() {
|
|
158
|
+
return !this.stopped;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
play() {
|
|
162
|
+
this.stopped = false;
|
|
163
|
+
this.updateRotate();
|
|
164
|
+
this.render(false);
|
|
165
|
+
this.schedule();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
pause(byUser: boolean) {
|
|
169
|
+
if (byUser || !this.stopped) this.stopped = true;
|
|
170
|
+
this.updateRotate();
|
|
171
|
+
this.render(false);
|
|
172
|
+
this.schedule();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
updateRotate() {
|
|
176
|
+
if (!this.rotate) return;
|
|
177
|
+
const playing = !this.stopped;
|
|
178
|
+
this.rotate.dataset.state = playing ? 'playing' : 'paused';
|
|
179
|
+
const text = this.rotate.querySelector('[data-carousel-rotate-text]');
|
|
180
|
+
if (text) text.textContent = (playing ? this.rotate.dataset.labelPause : this.rotate.dataset.labelPlay) ?? '';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** (Re)start or clear the autoplay timer for the current conditions. */
|
|
184
|
+
schedule() {
|
|
185
|
+
window.clearTimeout(this.timer);
|
|
186
|
+
this.timer = undefined;
|
|
187
|
+
if (this.stopped || this.hovering || this.focused || !this.visible || document.hidden || this.slides.length < 2) return;
|
|
188
|
+
this.timer = window.setTimeout(() => this.go(this.index + 1, false), this.delay);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const instances = new Set<Carousel>();
|
|
193
|
+
|
|
194
|
+
const init = () => {
|
|
195
|
+
for (const root of document.querySelectorAll<HTMLElement>('[data-sinn-carousel]:not([data-enhanced])')) {
|
|
196
|
+
if (!root.querySelector(':scope > .sinn-carousel__track')) continue;
|
|
197
|
+
instances.add(new Carousel(root));
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/** Client-side navigation (Astro view transitions): drop timers and observers of the old page. */
|
|
202
|
+
const teardown = () => {
|
|
203
|
+
for (const c of instances) {
|
|
204
|
+
window.clearTimeout(c.timer);
|
|
205
|
+
window.clearTimeout(c.settleTimer);
|
|
206
|
+
c.cleanup.forEach((fn) => fn());
|
|
207
|
+
}
|
|
208
|
+
instances.clear();
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
if (typeof window !== 'undefined' && !w[FLAG]) {
|
|
212
|
+
w[FLAG] = true;
|
|
213
|
+
document.addEventListener('astro:page-load', init);
|
|
214
|
+
document.addEventListener('astro:before-swap', teardown);
|
|
215
|
+
}
|
|
216
|
+
if (typeof document !== 'undefined') init();
|
|
217
|
+
|
|
218
|
+
export {};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ConsentEmbed and ConsentScript behavior.
|
|
3
|
+
*
|
|
4
|
+
* Embeds: the iframe is kept in an inert `<template>` (the browser never requests it) until its
|
|
5
|
+
* category is allowed, or the visitor loads that one embed ("Load map"). "Always allow" saves
|
|
6
|
+
* consent for the category, which loads every embed of it on the page. When the category is
|
|
7
|
+
* revoked, loaded embeds go back to their notice.
|
|
8
|
+
*
|
|
9
|
+
* Scripts: `<template data-sinn-consent-script>` holds a URL; once its category is allowed a
|
|
10
|
+
* real `<script src>` is added to <head> (your CSP's script-src must list its origin).
|
|
11
|
+
*/
|
|
12
|
+
import { hasConsent, onConsentChange, setConsent } from './consent-store';
|
|
13
|
+
|
|
14
|
+
const FLAG = '__sinnConsentEmbed';
|
|
15
|
+
const w = (typeof window !== 'undefined' ? window : {}) as unknown as Record<string, boolean>;
|
|
16
|
+
const notices = new WeakMap<HTMLElement, HTMLElement>();
|
|
17
|
+
|
|
18
|
+
function load(embed: HTMLElement, focus = false) {
|
|
19
|
+
if (embed.dataset.state === 'loaded') return;
|
|
20
|
+
const tpl = embed.querySelector<HTMLTemplateElement>('template[data-embed-template]');
|
|
21
|
+
const frame = embed.querySelector<HTMLElement>('[data-embed-frame]');
|
|
22
|
+
const notice = embed.querySelector<HTMLElement>('[data-embed-notice]');
|
|
23
|
+
if (!tpl || !frame) return;
|
|
24
|
+
const node = tpl.content.firstElementChild?.cloneNode(true) as HTMLElement | undefined;
|
|
25
|
+
if (!node) return;
|
|
26
|
+
if (notice) {
|
|
27
|
+
notices.set(embed, notice);
|
|
28
|
+
notice.remove();
|
|
29
|
+
}
|
|
30
|
+
frame.append(node);
|
|
31
|
+
embed.dataset.state = 'loaded';
|
|
32
|
+
if (focus) node.focus();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function unload(embed: HTMLElement) {
|
|
36
|
+
if (embed.dataset.state !== 'loaded' || embed.dataset.session === '') return;
|
|
37
|
+
const frame = embed.querySelector<HTMLElement>('[data-embed-frame]');
|
|
38
|
+
const notice = notices.get(embed);
|
|
39
|
+
frame?.querySelector('iframe')?.remove();
|
|
40
|
+
if (frame && notice) frame.append(notice);
|
|
41
|
+
embed.dataset.state = 'blocked';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function runScript(tpl: HTMLTemplateElement) {
|
|
45
|
+
if (tpl.hasAttribute('data-loaded')) return;
|
|
46
|
+
const src = tpl.dataset.src;
|
|
47
|
+
if (!src) return;
|
|
48
|
+
const s = document.createElement('script');
|
|
49
|
+
s.src = src;
|
|
50
|
+
if (tpl.dataset.module !== undefined) s.type = 'module';
|
|
51
|
+
if (tpl.dataset.async !== undefined) s.async = true;
|
|
52
|
+
if (tpl.dataset.defer !== undefined) s.defer = true;
|
|
53
|
+
if (tpl.dataset.id) s.id = tpl.dataset.id;
|
|
54
|
+
if (tpl.dataset.crossorigin) s.crossOrigin = tpl.dataset.crossorigin;
|
|
55
|
+
if (tpl.dataset.integrity) s.integrity = tpl.dataset.integrity;
|
|
56
|
+
if (tpl.dataset.referrerpolicy) s.referrerPolicy = tpl.dataset.referrerpolicy;
|
|
57
|
+
tpl.setAttribute('data-loaded', '');
|
|
58
|
+
document.head.append(s);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function apply() {
|
|
62
|
+
for (const embed of document.querySelectorAll<HTMLElement>('[data-sinn-embed]')) {
|
|
63
|
+
if (hasConsent(embed.dataset.category ?? 'media')) load(embed);
|
|
64
|
+
else unload(embed);
|
|
65
|
+
}
|
|
66
|
+
for (const tpl of document.querySelectorAll<HTMLTemplateElement>('template[data-sinn-consent-script]')) {
|
|
67
|
+
if (hasConsent(tpl.dataset.category ?? 'analytics')) runScript(tpl);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function init() {
|
|
72
|
+
for (const embed of document.querySelectorAll<HTMLElement>('[data-sinn-embed]:not([data-enhanced])')) {
|
|
73
|
+
embed.dataset.enhanced = '';
|
|
74
|
+
embed.querySelector<HTMLElement>('[data-embed-actions]')?.removeAttribute('hidden');
|
|
75
|
+
embed.addEventListener('click', (e) => {
|
|
76
|
+
const btn = (e.target as Element).closest<HTMLElement>('[data-embed-load], [data-embed-always]');
|
|
77
|
+
if (!btn || !embed.contains(btn)) return;
|
|
78
|
+
if (btn.hasAttribute('data-embed-always')) {
|
|
79
|
+
embed.dataset.focusNext = '';
|
|
80
|
+
setConsent({ [embed.dataset.category ?? 'media']: true });
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
// This embed only, this page view only: nothing is stored.
|
|
84
|
+
embed.dataset.session = '';
|
|
85
|
+
load(embed, true);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
apply();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (typeof window !== 'undefined' && !w[FLAG]) {
|
|
92
|
+
w[FLAG] = true;
|
|
93
|
+
onConsentChange(() => {
|
|
94
|
+
apply();
|
|
95
|
+
const pending = document.querySelector<HTMLElement>('[data-sinn-embed][data-focus-next]');
|
|
96
|
+
if (pending) {
|
|
97
|
+
delete pending.dataset.focusNext;
|
|
98
|
+
pending.querySelector<HTMLElement>('iframe')?.focus();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
document.addEventListener('astro:page-load', init);
|
|
102
|
+
}
|
|
103
|
+
if (typeof window !== 'undefined') init();
|
|
104
|
+
|
|
105
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ConsentManager behavior: shows the banner until the visitor decides, saves Accept all /
|
|
3
|
+
* Reject all / the dialog's choices, keeps the dialog's switches in sync, and opens the dialog
|
|
4
|
+
* from any `[data-consent-open]` element (a "Cookie settings" link in the footer).
|
|
5
|
+
*
|
|
6
|
+
* Revoking a category that already ran scripts (analytics, marketing) reloads the page, because
|
|
7
|
+
* a script that has run can't be unloaded; embeds are unloaded in place.
|
|
8
|
+
*/
|
|
9
|
+
import { allChoices, revoked } from '../internal/consent';
|
|
10
|
+
import { getConsentRecord, onConsentChange, openConsentSettings, setConsent } from './consent-store';
|
|
11
|
+
|
|
12
|
+
const FLAG = '__sinnConsentManager';
|
|
13
|
+
const w = (typeof window !== 'undefined' ? window : {}) as unknown as Record<string, boolean>;
|
|
14
|
+
|
|
15
|
+
function categoriesOf(root: HTMLElement): string[] {
|
|
16
|
+
return (root.dataset.categories ?? '').split(' ').filter(Boolean);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function syncSwitches(dialog: HTMLDialogElement) {
|
|
20
|
+
const rec = getConsentRecord();
|
|
21
|
+
for (const input of dialog.querySelectorAll<HTMLInputElement>('input[data-consent-category]')) {
|
|
22
|
+
const cat = input.dataset.consentCategory!;
|
|
23
|
+
input.checked = cat === 'necessary' ? true : rec?.c[cat] === true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** After a choice in the banner, focus goes to the page's main content, not to <body>. */
|
|
28
|
+
function focusAfterBanner(banner: HTMLElement) {
|
|
29
|
+
if (!banner.contains(document.activeElement)) return;
|
|
30
|
+
const target = document.querySelector<HTMLElement>('[data-skip-target], main, #main');
|
|
31
|
+
if (!target) return;
|
|
32
|
+
if (!target.hasAttribute('tabindex')) target.setAttribute('tabindex', '-1');
|
|
33
|
+
target.focus({ preventScroll: true });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function decide(root: HTMLElement, choices: Record<string, boolean>) {
|
|
37
|
+
const before = getConsentRecord();
|
|
38
|
+
setConsent(choices);
|
|
39
|
+
const banner = root.querySelector<HTMLElement>('[data-consent-banner]');
|
|
40
|
+
if (banner && !banner.hidden) {
|
|
41
|
+
focusAfterBanner(banner);
|
|
42
|
+
banner.hidden = true;
|
|
43
|
+
}
|
|
44
|
+
const dialog = root.querySelector<HTMLDialogElement>('dialog[data-sinn-consent-dialog]');
|
|
45
|
+
if (dialog?.open) dialog.close();
|
|
46
|
+
// Scripts that already ran for a revoked category can only be stopped by a reload.
|
|
47
|
+
const gone = revoked(before, getConsentRecord());
|
|
48
|
+
if (gone.some((c) => document.querySelector(`[data-sinn-consent-script][data-category="${c}"][data-loaded]`))) window.location.reload();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function init() {
|
|
52
|
+
const root = document.querySelector<HTMLElement>('[data-sinn-consent]:not([data-enhanced])');
|
|
53
|
+
if (!root) return;
|
|
54
|
+
root.dataset.enhanced = '';
|
|
55
|
+
const categories = categoriesOf(root);
|
|
56
|
+
const banner = root.querySelector<HTMLElement>('[data-consent-banner]');
|
|
57
|
+
const dialog = root.querySelector<HTMLDialogElement>('dialog[data-sinn-consent-dialog]');
|
|
58
|
+
|
|
59
|
+
if (banner) banner.hidden = Boolean(getConsentRecord());
|
|
60
|
+
|
|
61
|
+
root.addEventListener('click', (e) => {
|
|
62
|
+
const t = (e.target as Element).closest<HTMLElement>('[data-consent-accept], [data-consent-reject]');
|
|
63
|
+
if (!t || !root.contains(t)) return;
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
decide(root, allChoices(categories, t.hasAttribute('data-consent-accept')));
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (dialog) {
|
|
69
|
+
syncSwitches(dialog);
|
|
70
|
+
dialog.addEventListener('sinn-consent-sync', () => syncSwitches(dialog));
|
|
71
|
+
// Opened by its invoker button (commandfor) or the fallback script: show the saved state.
|
|
72
|
+
dialog.addEventListener('beforetoggle', (e) => {
|
|
73
|
+
if ((e as ToggleEvent).newState === 'open') syncSwitches(dialog);
|
|
74
|
+
});
|
|
75
|
+
const form = dialog.querySelector<HTMLFormElement>('form[data-consent-form]');
|
|
76
|
+
form?.addEventListener('submit', (e) => {
|
|
77
|
+
e.preventDefault();
|
|
78
|
+
const choices: Record<string, boolean> = {};
|
|
79
|
+
for (const input of form.querySelectorAll<HTMLInputElement>('input[data-consent-category]')) {
|
|
80
|
+
if (input.dataset.consentCategory !== 'necessary') choices[input.dataset.consentCategory!] = input.checked;
|
|
81
|
+
}
|
|
82
|
+
decide(root, choices);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (typeof window !== 'undefined' && !w[FLAG]) {
|
|
88
|
+
w[FLAG] = true;
|
|
89
|
+
// "Cookie settings" links anywhere on the page.
|
|
90
|
+
document.addEventListener('click', (e) => {
|
|
91
|
+
const opener = (e.target as Element | null)?.closest?.('[data-consent-open]');
|
|
92
|
+
if (!opener) return;
|
|
93
|
+
if (openConsentSettings()) e.preventDefault();
|
|
94
|
+
});
|
|
95
|
+
// Another tab changed the choice: follow it here (banner and switches).
|
|
96
|
+
window.addEventListener('storage', (e) => {
|
|
97
|
+
if (e.key !== 'sinn-consent') return;
|
|
98
|
+
const root = document.querySelector<HTMLElement>('[data-sinn-consent]');
|
|
99
|
+
const banner = root?.querySelector<HTMLElement>('[data-consent-banner]');
|
|
100
|
+
if (banner) banner.hidden = Boolean(getConsentRecord());
|
|
101
|
+
const rec = getConsentRecord();
|
|
102
|
+
document.dispatchEvent(new CustomEvent('sinn-consent-change', { detail: { choices: { ...(rec?.c ?? {}) }, decided: Boolean(rec) } }));
|
|
103
|
+
});
|
|
104
|
+
onConsentChange(() => {
|
|
105
|
+
const dialog = document.querySelector<HTMLDialogElement>('dialog[data-sinn-consent-dialog]');
|
|
106
|
+
if (dialog) syncSwitches(dialog);
|
|
107
|
+
});
|
|
108
|
+
document.addEventListener('astro:page-load', init);
|
|
109
|
+
}
|
|
110
|
+
if (typeof window !== 'undefined') init();
|
|
111
|
+
|
|
112
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser side of consent: one store per page, shared by ConsentManager, ConsentEmbed,
|
|
3
|
+
* ConsentScript and your own code (`@sinnstack/ui/blocks/consent`).
|
|
4
|
+
*
|
|
5
|
+
* - Reads the policy version and maximum age from the page's ConsentManager
|
|
6
|
+
* (`data-version`, `data-max-age`); without one, any stored decision counts.
|
|
7
|
+
* - localStorage when it works; otherwise the choice lasts until the page is left.
|
|
8
|
+
* - Every change dispatches `sinn-consent-change` on `document`
|
|
9
|
+
* (detail: `{ choices, decided, previous }`).
|
|
10
|
+
* SSR-safe: nothing touches the DOM at import time outside the browser.
|
|
11
|
+
*/
|
|
12
|
+
import { allows, CONSENT_EVENT, CONSENT_KEY, parseConsent, serializeConsent, type ConsentChoices, type ConsentRecord } from '../internal/consent';
|
|
13
|
+
|
|
14
|
+
export interface ConsentChangeDetail {
|
|
15
|
+
choices: ConsentChoices;
|
|
16
|
+
/** The visitor has made a choice (not just loaded the page). */
|
|
17
|
+
decided: boolean;
|
|
18
|
+
previous?: ConsentChoices;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let memory: string | null = null;
|
|
22
|
+
|
|
23
|
+
function config(): { version?: string; maxAgeDays?: number } {
|
|
24
|
+
const el = typeof document !== 'undefined' ? document.querySelector<HTMLElement>('[data-sinn-consent]') : null;
|
|
25
|
+
if (!el) return {};
|
|
26
|
+
const maxAge = Number(el.dataset.maxAge);
|
|
27
|
+
return { version: el.dataset.version || '1', maxAgeDays: Number.isFinite(maxAge) && maxAge > 0 ? maxAge : 365 };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function readRaw(): string | null {
|
|
31
|
+
try {
|
|
32
|
+
return window.localStorage.getItem(CONSENT_KEY) ?? memory;
|
|
33
|
+
} catch {
|
|
34
|
+
return memory;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function writeRaw(value: string): void {
|
|
39
|
+
memory = value;
|
|
40
|
+
try {
|
|
41
|
+
window.localStorage.setItem(CONSENT_KEY, value);
|
|
42
|
+
} catch {
|
|
43
|
+
/* storage blocked: keep the in-memory copy */
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The current decision, or undefined when the visitor hasn't decided (or it expired). */
|
|
48
|
+
export function getConsentRecord(): ConsentRecord | undefined {
|
|
49
|
+
if (typeof window === 'undefined') return undefined;
|
|
50
|
+
return parseConsent(readRaw(), config());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Whether a category is allowed now. `necessary` is always true. */
|
|
54
|
+
export function hasConsent(category: string): boolean {
|
|
55
|
+
return allows(getConsentRecord(), category);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Save the visitor's choices (merged over the current ones) and notify listeners. */
|
|
59
|
+
export function setConsent(choices: ConsentChoices): void {
|
|
60
|
+
if (typeof window === 'undefined') return;
|
|
61
|
+
const before = getConsentRecord();
|
|
62
|
+
const merged = { ...(before?.c ?? {}), ...choices };
|
|
63
|
+
writeRaw(serializeConsent(merged, config().version ?? before?.v ?? '1'));
|
|
64
|
+
const after = getConsentRecord();
|
|
65
|
+
document.dispatchEvent(
|
|
66
|
+
new CustomEvent<ConsentChangeDetail>(CONSENT_EVENT, { detail: { choices: { ...(after?.c ?? merged) }, decided: true, previous: before?.c }, bubbles: true, composed: true }),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Run `fn` whenever choices change. Returns an unsubscribe function. */
|
|
71
|
+
export function onConsentChange(fn: (detail: ConsentChangeDetail) => void): () => void {
|
|
72
|
+
if (typeof document === 'undefined') return () => {};
|
|
73
|
+
const handler = (e: Event) => fn((e as CustomEvent<ConsentChangeDetail>).detail);
|
|
74
|
+
document.addEventListener(CONSENT_EVENT, handler);
|
|
75
|
+
return () => document.removeEventListener(CONSENT_EVENT, handler);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Open the privacy-choices dialog (ConsentManager), e.g. from a "Cookie settings" link. */
|
|
79
|
+
export function openConsentSettings(): boolean {
|
|
80
|
+
if (typeof document === 'undefined') return false;
|
|
81
|
+
const dialog = document.querySelector<HTMLDialogElement>('dialog[data-sinn-consent-dialog]');
|
|
82
|
+
if (!dialog) return false;
|
|
83
|
+
dialog.dispatchEvent(new CustomEvent('sinn-consent-sync'));
|
|
84
|
+
if (!dialog.open) dialog.showModal();
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ErrorPage enhancements (the page is complete without them):
|
|
3
|
+
* - shows the address that failed (as text, never HTML),
|
|
4
|
+
* - "Did you mean …?" from the site's page list (data-pages JSON),
|
|
5
|
+
* - prefills site search with words from the broken address,
|
|
6
|
+
* - reveals "Go back" only when the visitor came from this site,
|
|
7
|
+
* - dispatches `sinn:error-page` { status, path, referrer } on document for analytics.
|
|
8
|
+
*/
|
|
9
|
+
import { searchTermsFromPath, suggestPage, type SuggestCandidate } from '../lib/error-pages';
|
|
10
|
+
|
|
11
|
+
function enhance(root: HTMLElement): void {
|
|
12
|
+
if (root.dataset.enhanced) return;
|
|
13
|
+
root.dataset.enhanced = '';
|
|
14
|
+
const status = Number(root.dataset.sinnErrorPage);
|
|
15
|
+
const path = location.pathname + location.search;
|
|
16
|
+
|
|
17
|
+
const pathOut = root.querySelector<HTMLElement>('[data-sinn-error-path]');
|
|
18
|
+
if (pathOut && !pathOut.textContent?.trim()) {
|
|
19
|
+
let shown = location.pathname;
|
|
20
|
+
try {
|
|
21
|
+
shown = decodeURI(shown);
|
|
22
|
+
} catch {
|
|
23
|
+
/* keep encoded */
|
|
24
|
+
}
|
|
25
|
+
pathOut.textContent = shown;
|
|
26
|
+
pathOut.closest<HTMLElement>('[hidden]')?.removeAttribute('hidden');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const suggest = root.querySelector<HTMLElement>('[data-sinn-error-suggest]');
|
|
30
|
+
if (suggest) {
|
|
31
|
+
let pages: SuggestCandidate[] = [];
|
|
32
|
+
try {
|
|
33
|
+
pages = JSON.parse(suggest.dataset.pages ?? '[]');
|
|
34
|
+
} catch {
|
|
35
|
+
/* ignore bad data */
|
|
36
|
+
}
|
|
37
|
+
const hit = suggestPage(location.pathname, pages);
|
|
38
|
+
const link = suggest.querySelector('a');
|
|
39
|
+
if (hit && link) {
|
|
40
|
+
link.href = hit.href;
|
|
41
|
+
link.textContent = hit.label;
|
|
42
|
+
suggest.hidden = false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const search = root.querySelector<HTMLInputElement>('input[data-sinn-error-search]');
|
|
47
|
+
if (search && !search.value) search.value = searchTermsFromPath(location.pathname);
|
|
48
|
+
|
|
49
|
+
const back = root.querySelector<HTMLButtonElement>('[data-sinn-error-back]');
|
|
50
|
+
if (back) {
|
|
51
|
+
let sameSite = false;
|
|
52
|
+
try {
|
|
53
|
+
sameSite = Boolean(document.referrer) && new URL(document.referrer).origin === location.origin && document.referrer !== location.href;
|
|
54
|
+
} catch {
|
|
55
|
+
/* no referrer */
|
|
56
|
+
}
|
|
57
|
+
if (sameSite && history.length > 1) {
|
|
58
|
+
back.hidden = false;
|
|
59
|
+
back.addEventListener('click', () => history.back());
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
document.dispatchEvent(new CustomEvent('sinn:error-page', { detail: { status, path, referrer: document.referrer || null } }));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const run = () => document.querySelectorAll<HTMLElement>('[data-sinn-error-page]').forEach(enhance);
|
|
67
|
+
run();
|
|
68
|
+
// Client-side navigation (Astro ClientRouter) swaps the page without reloading.
|
|
69
|
+
document.addEventListener('astro:page-load', run);
|
|
70
|
+
|
|
71
|
+
export {};
|