@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,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 '../internal/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 {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side sorting for <Table sortable="client">.
|
|
3
|
+
* Header buttons reorder <tbody> rows by each cell's data-value (raw value rendered by the
|
|
4
|
+
* server), numerically for numeric/date columns and naturally for text. Empty values stay
|
|
5
|
+
* last in both directions; ties keep their previous order (stable). Updates aria-sort and
|
|
6
|
+
* announces the new order in a polite live region.
|
|
7
|
+
*/
|
|
8
|
+
const FLAG = '__sinnTableSort';
|
|
9
|
+
const w = window as unknown as Record<string, boolean>;
|
|
10
|
+
|
|
11
|
+
function cellValue(row: HTMLTableRowElement, index: number): string {
|
|
12
|
+
const cell = row.cells[index];
|
|
13
|
+
return cell?.dataset.value ?? cell?.textContent?.trim() ?? '';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!w[FLAG]) {
|
|
17
|
+
w[FLAG] = true;
|
|
18
|
+
const collator = new Intl.Collator(document.documentElement.lang || undefined, { numeric: true, sensitivity: 'base' });
|
|
19
|
+
|
|
20
|
+
document.addEventListener('click', (event) => {
|
|
21
|
+
const button = (event.target as Element | null)?.closest?.('button.sinn-table__sort');
|
|
22
|
+
if (!button) return;
|
|
23
|
+
const th = button.closest('th');
|
|
24
|
+
const table = th?.closest('table');
|
|
25
|
+
const tbody = table?.tBodies[0];
|
|
26
|
+
if (!th || !table || !tbody) return;
|
|
27
|
+
|
|
28
|
+
const index = (th as HTMLTableCellElement).cellIndex;
|
|
29
|
+
const next = th.getAttribute('aria-sort') === 'ascending' ? 'descending' : 'ascending';
|
|
30
|
+
const numeric = th.dataset.sortType === 'number';
|
|
31
|
+
const dir = next === 'ascending' ? 1 : -1;
|
|
32
|
+
|
|
33
|
+
const rows = [...tbody.rows].map((row, i) => ({ row, i, v: cellValue(row, index) }));
|
|
34
|
+
rows.sort((a, b) => {
|
|
35
|
+
const ea = a.v === '';
|
|
36
|
+
const eb = b.v === '';
|
|
37
|
+
if (ea !== eb) return ea ? 1 : -1;
|
|
38
|
+
if (ea && eb) return a.i - b.i;
|
|
39
|
+
let cmp: number;
|
|
40
|
+
if (numeric) {
|
|
41
|
+
const na = Number(a.v);
|
|
42
|
+
const nb = Number(b.v);
|
|
43
|
+
cmp = !Number.isNaN(na) && !Number.isNaN(nb) ? na - nb : Date.parse(a.v) - Date.parse(b.v);
|
|
44
|
+
} else {
|
|
45
|
+
cmp = collator.compare(a.v, b.v);
|
|
46
|
+
}
|
|
47
|
+
return cmp * dir || a.i - b.i;
|
|
48
|
+
});
|
|
49
|
+
tbody.append(...rows.map((r) => r.row));
|
|
50
|
+
|
|
51
|
+
for (const header of th.parentElement?.children ?? []) {
|
|
52
|
+
if (header !== th && header.hasAttribute('aria-sort')) header.removeAttribute('aria-sort');
|
|
53
|
+
}
|
|
54
|
+
th.setAttribute('aria-sort', next);
|
|
55
|
+
|
|
56
|
+
const status = table.closest('.sinn-table')?.querySelector<HTMLElement>('[data-sinn-table-status]');
|
|
57
|
+
if (status) {
|
|
58
|
+
const label = button.textContent?.trim() ?? '';
|
|
59
|
+
const direction = status.dataset[next] ?? next;
|
|
60
|
+
status.textContent = (status.dataset.template ?? 'Sorted by {column}, {direction}')
|
|
61
|
+
.replace('{column}', () => label)
|
|
62
|
+
.replace('{direction}', () => direction);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Module scope (keeps top-level names private to this file).
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tabs: upgrades the server-rendered links + panels to ARIA tabs (WAI-ARIA APG pattern).
|
|
3
|
+
* Roles are added here, not in the HTML, so without this script the markup tells the truth
|
|
4
|
+
* (links to sections). Selection: aria-selected + data-selected on the tab, data-selected on the
|
|
5
|
+
* panel (CSS hides the rest; not `hidden`, so print CSS can still show every panel).
|
|
6
|
+
*
|
|
7
|
+
* Keys: ←/→ (↑/↓ too when vertical; reversed in RTL), Home/End. Automatic activation selects
|
|
8
|
+
* on focus; manual moves focus and selects on Enter/Space. Disabled tabs are skipped.
|
|
9
|
+
* Event: `sinn-tab-change` (bubbles, composed) with `{ id, index, previousId }`.
|
|
10
|
+
*/
|
|
11
|
+
import type { SinnTabChangeDetail } from '../types';
|
|
12
|
+
|
|
13
|
+
const FLAG = '__sinnTabs';
|
|
14
|
+
const w = window as unknown as Record<string, boolean>;
|
|
15
|
+
|
|
16
|
+
const parts = (root: HTMLElement) => ({
|
|
17
|
+
tabs: [...root.querySelectorAll<HTMLAnchorElement>(':scope > .sinn-tabs__list > .sinn-tabs__tab')],
|
|
18
|
+
panels: [...root.querySelectorAll<HTMLElement>(':scope > .sinn-tabs__panel')],
|
|
19
|
+
});
|
|
20
|
+
const disabled = (t: Element) => t.getAttribute('aria-disabled') === 'true';
|
|
21
|
+
|
|
22
|
+
function select(root: HTMLElement, index: number, opts: { focus?: boolean; emit?: boolean } = {}) {
|
|
23
|
+
const { tabs, panels } = parts(root);
|
|
24
|
+
const tab = tabs[index];
|
|
25
|
+
if (!tab || disabled(tab)) return;
|
|
26
|
+
const prev = tabs.findIndex((t) => t.getAttribute('aria-selected') === 'true');
|
|
27
|
+
tabs.forEach((t, k) => {
|
|
28
|
+
const on = k === index;
|
|
29
|
+
t.setAttribute('aria-selected', String(on));
|
|
30
|
+
t.toggleAttribute('data-selected', on);
|
|
31
|
+
t.tabIndex = on ? 0 : -1;
|
|
32
|
+
panels[k]?.toggleAttribute('data-selected', on);
|
|
33
|
+
});
|
|
34
|
+
if (opts.focus) {
|
|
35
|
+
tab.focus();
|
|
36
|
+
// Bring a tab scrolled out of an overflowing list into view, without moving the page.
|
|
37
|
+
tab.scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
|
38
|
+
}
|
|
39
|
+
if (root.hasAttribute('data-sync-hash') && opts.emit && panels[index]) {
|
|
40
|
+
history.replaceState(history.state, '', `#${panels[index].id}`);
|
|
41
|
+
}
|
|
42
|
+
if (opts.emit && prev !== index) {
|
|
43
|
+
root.dispatchEvent(
|
|
44
|
+
new CustomEvent<SinnTabChangeDetail>('sinn-tab-change', {
|
|
45
|
+
bubbles: true,
|
|
46
|
+
composed: true,
|
|
47
|
+
detail: { id: panels[index]?.id ?? '', index, previousId: prev >= 0 ? (panels[prev]?.id ?? null) : null },
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The tab whose panel holds the URL target (the panel itself or something inside it). */
|
|
54
|
+
function hashIndex(root: HTMLElement): number {
|
|
55
|
+
const id = decodeURIComponent(location.hash.slice(1));
|
|
56
|
+
if (!id) return -1;
|
|
57
|
+
const el = document.getElementById(id);
|
|
58
|
+
const panel = el?.closest('.sinn-tabs__panel');
|
|
59
|
+
return panel && panel.parentElement === root ? parts(root).panels.indexOf(panel as HTMLElement) : -1;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function enhance(root: HTMLElement) {
|
|
63
|
+
const { tabs, panels } = parts(root);
|
|
64
|
+
const list = root.querySelector<HTMLElement>(':scope > .sinn-tabs__list');
|
|
65
|
+
if (!list || !tabs.length) return;
|
|
66
|
+
list.setAttribute('role', 'tablist');
|
|
67
|
+
if (root.dataset.label) list.setAttribute('aria-label', root.dataset.label);
|
|
68
|
+
if (root.dataset.orientation === 'vertical') list.setAttribute('aria-orientation', 'vertical');
|
|
69
|
+
tabs.forEach((t, i) => {
|
|
70
|
+
t.setAttribute('role', 'tab');
|
|
71
|
+
if (panels[i]) t.setAttribute('aria-controls', panels[i].id);
|
|
72
|
+
});
|
|
73
|
+
panels.forEach((p, i) => {
|
|
74
|
+
p.setAttribute('role', 'tabpanel');
|
|
75
|
+
if (tabs[i]) p.setAttribute('aria-labelledby', tabs[i].id);
|
|
76
|
+
p.tabIndex = 0;
|
|
77
|
+
});
|
|
78
|
+
const fromHash = hashIndex(root);
|
|
79
|
+
const initial = fromHash >= 0 && !disabled(tabs[fromHash]) ? fromHash : Math.max(0, tabs.findIndex((t) => t.hasAttribute('data-selected')));
|
|
80
|
+
select(root, initial);
|
|
81
|
+
root.dataset.enhanced = '';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const init = () => {
|
|
85
|
+
for (const root of document.querySelectorAll<HTMLElement>('[data-sinn-tabs]:not([data-enhanced])')) enhance(root);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const tabFrom = (t: EventTarget | null) => {
|
|
89
|
+
const tab = (t as Element | null)?.closest?.<HTMLAnchorElement>('.sinn-tabs__tab');
|
|
90
|
+
const root = tab?.parentElement?.parentElement as HTMLElement | undefined;
|
|
91
|
+
return tab && root?.hasAttribute('data-enhanced') ? { tab, root } : null;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
if (!w[FLAG]) {
|
|
95
|
+
w[FLAG] = true;
|
|
96
|
+
|
|
97
|
+
document.addEventListener('click', (e) => {
|
|
98
|
+
const hit = tabFrom(e.target);
|
|
99
|
+
if (!hit) return;
|
|
100
|
+
// Let modified clicks (new tab/window) keep their link behavior.
|
|
101
|
+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return;
|
|
102
|
+
e.preventDefault();
|
|
103
|
+
const { tabs } = parts(hit.root);
|
|
104
|
+
select(hit.root, tabs.indexOf(hit.tab), { focus: true, emit: true });
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
document.addEventListener('keydown', (e) => {
|
|
108
|
+
const hit = tabFrom(e.target);
|
|
109
|
+
if (!hit || e.altKey || e.ctrlKey || e.metaKey) return;
|
|
110
|
+
const { root, tab } = hit;
|
|
111
|
+
const { tabs } = parts(root);
|
|
112
|
+
const i = tabs.indexOf(tab);
|
|
113
|
+
const manual = root.dataset.activation === 'manual';
|
|
114
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
115
|
+
e.preventDefault();
|
|
116
|
+
select(root, i, { focus: true, emit: true });
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const vertical = root.dataset.orientation === 'vertical';
|
|
120
|
+
const rtl = getComputedStyle(root).direction === 'rtl';
|
|
121
|
+
const fwd = [rtl ? 'ArrowLeft' : 'ArrowRight', ...(vertical ? ['ArrowDown'] : [])];
|
|
122
|
+
const back = [rtl ? 'ArrowRight' : 'ArrowLeft', ...(vertical ? ['ArrowUp'] : [])];
|
|
123
|
+
const enabled = tabs.map((t, k) => (disabled(t) ? -1 : k)).filter((k) => k >= 0);
|
|
124
|
+
if (!enabled.length) return;
|
|
125
|
+
const pos = enabled.indexOf(i);
|
|
126
|
+
let next: number | null = null;
|
|
127
|
+
if (fwd.includes(e.key)) next = enabled[(pos + 1) % enabled.length];
|
|
128
|
+
else if (back.includes(e.key)) next = enabled[(pos - 1 + enabled.length) % enabled.length];
|
|
129
|
+
else if (e.key === 'Home') next = enabled[0];
|
|
130
|
+
else if (e.key === 'End') next = enabled[enabled.length - 1];
|
|
131
|
+
if (next === null) return;
|
|
132
|
+
e.preventDefault();
|
|
133
|
+
if (manual) {
|
|
134
|
+
tabs[next].focus();
|
|
135
|
+
tabs[next].scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
|
136
|
+
} else select(root, next, { focus: true, emit: true });
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Links elsewhere on the page to #panel (or to something inside a panel) switch tabs.
|
|
140
|
+
window.addEventListener('hashchange', () => {
|
|
141
|
+
for (const root of document.querySelectorAll<HTMLElement>('[data-sinn-tabs][data-enhanced]')) {
|
|
142
|
+
const i = hashIndex(root);
|
|
143
|
+
if (i >= 0) select(root, i, { emit: true });
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
document.addEventListener('astro:page-load', init);
|
|
148
|
+
}
|
|
149
|
+
init();
|
|
150
|
+
|
|
151
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tooltip: Esc dismisses the visible tooltip without moving focus or the pointer
|
|
3
|
+
* (WCAG 1.4.13 "dismissible"). It shows again after the pointer leaves or focus moves on.
|
|
4
|
+
*/
|
|
5
|
+
const FLAG = '__sinnTooltip';
|
|
6
|
+
const w = window as unknown as Record<string, boolean>;
|
|
7
|
+
|
|
8
|
+
if (!w[FLAG]) {
|
|
9
|
+
w[FLAG] = true;
|
|
10
|
+
|
|
11
|
+
document.addEventListener('keydown', (event) => {
|
|
12
|
+
if (event.key !== 'Escape') return;
|
|
13
|
+
for (const tip of document.querySelectorAll<HTMLElement>('.sinn-tooltip')) {
|
|
14
|
+
if (tip.matches(':hover') || tip.matches(':focus-within')) tip.dataset.dismissed = '';
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const reset = (event: Event) => {
|
|
19
|
+
const tip = (event.target as Element | null)?.closest?.<HTMLElement>('.sinn-tooltip[data-dismissed]');
|
|
20
|
+
const next = (event as FocusEvent | PointerEvent).relatedTarget as Node | null;
|
|
21
|
+
if (tip && !(next && tip.contains(next))) delete tip.dataset.dismissed;
|
|
22
|
+
};
|
|
23
|
+
document.addEventListener('pointerout', reset);
|
|
24
|
+
document.addEventListener('focusout', reset);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Module scope (keeps top-level names private to this file).
|
|
28
|
+
export {};
|