@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,114 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* @internal Shared markup for Dialog, Drawer and Sheet: a native modal <dialog> with a
|
|
4
|
+
* header (title, description, close button), scrollable body, and optional footer.
|
|
5
|
+
*
|
|
6
|
+
* Opening: any button with `commandfor={id} command="show-modal"`. Closing: the built-in
|
|
7
|
+
* close button, Esc, `command="close"`, `<form method="dialog">`, or a backdrop click when
|
|
8
|
+
* `dismissible="any"`. Focus moves into the dialog on open and back to the trigger on close
|
|
9
|
+
* (native). The page behind is inert.
|
|
10
|
+
*/
|
|
11
|
+
import type { ModalPanelProps } from '../types';
|
|
12
|
+
import { uid } from '../utils';
|
|
13
|
+
import OverlayScript from './OverlayScript.astro';
|
|
14
|
+
import '../styles/utilities.css';
|
|
15
|
+
import '../styles/typography.css';
|
|
16
|
+
import '../styles/button.css';
|
|
17
|
+
import '../styles/overlay.css';
|
|
18
|
+
|
|
19
|
+
type Props = ModalPanelProps & {
|
|
20
|
+
placement?: 'center' | 'start' | 'end' | 'top' | 'bottom';
|
|
21
|
+
handle?: boolean;
|
|
22
|
+
contained?: boolean;
|
|
23
|
+
/** Class name added for the specific component (sinn-drawer, sinn-sheet). */
|
|
24
|
+
kindClass?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const {
|
|
28
|
+
title,
|
|
29
|
+
hideTitle = false,
|
|
30
|
+
titleLevel = 2,
|
|
31
|
+
description,
|
|
32
|
+
size = 'm',
|
|
33
|
+
role = 'dialog',
|
|
34
|
+
dismissible = role === 'alertdialog' ? 'none' : 'any',
|
|
35
|
+
closeButton = role !== 'alertdialog',
|
|
36
|
+
closeLabel = 'Close',
|
|
37
|
+
footerJustify = 'end',
|
|
38
|
+
placement = 'center',
|
|
39
|
+
handle = false,
|
|
40
|
+
contained = false,
|
|
41
|
+
kindClass,
|
|
42
|
+
id = uid('sinn-dialog'),
|
|
43
|
+
class: className,
|
|
44
|
+
...rest
|
|
45
|
+
} = Astro.props;
|
|
46
|
+
|
|
47
|
+
// Newer platform attributes go through spreads so type-checking doesn't depend on
|
|
48
|
+
// Astro's DOM typings knowing them yet.
|
|
49
|
+
const dialogAttrs = { closedby: dismissible };
|
|
50
|
+
const closeAttrs = { commandfor: id, command: 'close' };
|
|
51
|
+
const titleId = `${id}-title`;
|
|
52
|
+
const descId = description ? `${id}-desc` : undefined;
|
|
53
|
+
const T = `h${titleLevel}` as 'h2';
|
|
54
|
+
const hasFooter = Astro.slots.has('footer');
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
<dialog
|
|
58
|
+
id={id}
|
|
59
|
+
class:list={['sinn-dialog', kindClass, className]}
|
|
60
|
+
role={role === 'alertdialog' ? 'alertdialog' : undefined}
|
|
61
|
+
aria-labelledby={titleId}
|
|
62
|
+
aria-describedby={descId}
|
|
63
|
+
{...dialogAttrs}
|
|
64
|
+
data-size={size === 'm' ? undefined : size}
|
|
65
|
+
data-placement={placement === 'center' ? undefined : placement}
|
|
66
|
+
data-contained={contained ? '' : undefined}
|
|
67
|
+
{...rest}
|
|
68
|
+
>
|
|
69
|
+
{handle && <div class="sinn-dialog__handle" aria-hidden="true" />}
|
|
70
|
+
<header class="sinn-dialog__header">
|
|
71
|
+
<div class="sinn-dialog__heading">
|
|
72
|
+
<T id={titleId} class:list={['sinn-dialog__title', { 'sinn-sr-only': hideTitle }]}>{title}</T>
|
|
73
|
+
{
|
|
74
|
+
description && (
|
|
75
|
+
<p id={descId} class="sinn-type sinn-text sinn-dialog__description" data-size="s" data-tone="muted">
|
|
76
|
+
{description}
|
|
77
|
+
</p>
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
<slot name="header" />
|
|
81
|
+
</div>
|
|
82
|
+
{
|
|
83
|
+
closeButton && (
|
|
84
|
+
<button
|
|
85
|
+
type="button"
|
|
86
|
+
class="sinn-button sinn-dialog__close"
|
|
87
|
+
data-variant="ghost"
|
|
88
|
+
data-tone="neutral"
|
|
89
|
+
data-size="s"
|
|
90
|
+
data-icon-only
|
|
91
|
+
{...closeAttrs}
|
|
92
|
+
>
|
|
93
|
+
<span class="sinn-button__content">
|
|
94
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true">
|
|
95
|
+
<path d="M6 6l12 12M18 6 6 18" />
|
|
96
|
+
</svg>
|
|
97
|
+
<span class="sinn-sr-only">{closeLabel}</span>
|
|
98
|
+
</span>
|
|
99
|
+
</button>
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
</header>
|
|
103
|
+
<div class="sinn-dialog__body">
|
|
104
|
+
<slot />
|
|
105
|
+
</div>
|
|
106
|
+
{
|
|
107
|
+
hasFooter && (
|
|
108
|
+
<footer class="sinn-dialog__footer" data-justify={footerJustify === 'end' ? undefined : footerJustify}>
|
|
109
|
+
<slot name="footer" />
|
|
110
|
+
</footer>
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
</dialog>
|
|
114
|
+
<OverlayScript />
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deprecated prop names and values.
|
|
3
|
+
*
|
|
4
|
+
* When the vocabulary changes, the old name keeps working for at least one minor release: it is
|
|
5
|
+
* mapped to the new one here and a warning is logged once per message in development (never in
|
|
6
|
+
* production builds). The rendered HTML is identical whichever name is used.
|
|
7
|
+
*
|
|
8
|
+
* The current vocabulary and every rename are listed under "Prop vocabulary" in the primitives
|
|
9
|
+
* README.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const seen = new Set<string>();
|
|
13
|
+
|
|
14
|
+
/** Log a development-only warning, once per component and message. */
|
|
15
|
+
export function warnOnce(component: string, message: string): void {
|
|
16
|
+
const key = `${component}\u0000${message}`;
|
|
17
|
+
if (seen.has(key)) return;
|
|
18
|
+
seen.add(key);
|
|
19
|
+
if (import.meta.env?.DEV) console.warn(`[${component}] ${message}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Map a deprecated value to its replacement. Values not in `renames` pass through unchanged.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const v = renameValue('Tabs', 'variant', variant, { pills: 'pill' });
|
|
27
|
+
*/
|
|
28
|
+
export function renameValue<T>(component: string, prop: string, value: T, renames: Readonly<Record<string, string>>): T {
|
|
29
|
+
if (typeof value === 'string' && Object.prototype.hasOwnProperty.call(renames, value)) {
|
|
30
|
+
const next = renames[value];
|
|
31
|
+
warnOnce(component, `${prop}="${value}" is deprecated; use ${prop}="${next}".`);
|
|
32
|
+
return next as T;
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Read a renamed prop: the new name wins; the old name is used (with a warning) when only it is set.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const columns = renameProp('Grid', 'columns', props.columns, 'cols', props.cols);
|
|
42
|
+
*/
|
|
43
|
+
export function renameProp<T>(component: string, name: string, value: T | undefined, oldName: string, oldValue: T | undefined): T | undefined {
|
|
44
|
+
if (oldValue === undefined) return value;
|
|
45
|
+
warnOnce(component, `\`${oldName}\` is deprecated; use \`${name}\`.`);
|
|
46
|
+
return value ?? oldValue;
|
|
47
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <sinn-focus-trap> — keeps keyboard focus inside a region while active.
|
|
3
|
+
*
|
|
4
|
+
* Prefer the platform: a modal <dialog> (Dialog/Drawer/Sheet) and popovers already manage
|
|
5
|
+
* focus. Use this for custom widgets that aren't dialogs: an inline editor, a full-screen
|
|
6
|
+
* search overlay built from other parts, a custom menu, a kiosk step.
|
|
7
|
+
*
|
|
8
|
+
* Attributes
|
|
9
|
+
* - active: trap on/off (reflects; also .activate() / .deactivate()).
|
|
10
|
+
* - mode="tab" (default): Tab and Shift+Tab wrap around inside; focus that escapes (mouse,
|
|
11
|
+
* screen-reader virtual cursor) is pulled back in. The rest of the page stays readable.
|
|
12
|
+
* mode="inert": everything outside becomes `inert` (unreachable by keyboard, pointer and
|
|
13
|
+
* assistive tech), like a modal. Restored exactly on deactivate.
|
|
14
|
+
* - initial-focus="<selector>": what to focus on activate (default: first tabbable, else the trap).
|
|
15
|
+
* - return-focus="false": don't return focus to where it was on deactivate.
|
|
16
|
+
* - escape-deactivates: Esc deactivates (fires a cancelable sinn-focus-trap-escape first).
|
|
17
|
+
*
|
|
18
|
+
* Nested traps stack: only the most recently activated one acts; deactivating it hands
|
|
19
|
+
* control back to the previous one.
|
|
20
|
+
*
|
|
21
|
+
* Events: sinn-focus-trap-activate, sinn-focus-trap-deactivate, sinn-focus-trap-escape.
|
|
22
|
+
*/
|
|
23
|
+
import { deepActiveElement, tabbables } from './focus';
|
|
24
|
+
|
|
25
|
+
export interface SinnFocusTrapElement extends HTMLElement {
|
|
26
|
+
readonly active: boolean;
|
|
27
|
+
activate(): void;
|
|
28
|
+
deactivate(): void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
declare global {
|
|
32
|
+
interface HTMLElementTagNameMap {
|
|
33
|
+
'sinn-focus-trap': SinnFocusTrapElement;
|
|
34
|
+
}
|
|
35
|
+
interface HTMLElementEventMap {
|
|
36
|
+
'sinn-focus-trap-activate': CustomEvent<void>;
|
|
37
|
+
'sinn-focus-trap-deactivate': CustomEvent<void>;
|
|
38
|
+
'sinn-focus-trap-escape': CustomEvent<void>;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Defines the element. Call in the browser only (the Astro component does this for you). */
|
|
43
|
+
export function defineFocusTrap(tag = 'sinn-focus-trap'): void {
|
|
44
|
+
if (typeof window === 'undefined' || customElements.get(tag)) return;
|
|
45
|
+
|
|
46
|
+
const stack: SinnFocusTrap[] = [];
|
|
47
|
+
|
|
48
|
+
class SinnFocusTrap extends HTMLElement implements SinnFocusTrapElement {
|
|
49
|
+
static observedAttributes = ['active'];
|
|
50
|
+
|
|
51
|
+
#returnTo: HTMLElement | null = null;
|
|
52
|
+
#lastInside: HTMLElement | null = null;
|
|
53
|
+
#inerted: HTMLElement[] = [];
|
|
54
|
+
#syncing = false;
|
|
55
|
+
|
|
56
|
+
get active(): boolean {
|
|
57
|
+
return stack.includes(this);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get #mode(): 'tab' | 'inert' {
|
|
61
|
+
return this.getAttribute('mode') === 'inert' ? 'inert' : 'tab';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#isTop(): boolean {
|
|
65
|
+
return stack[stack.length - 1] === this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
connectedCallback() {
|
|
69
|
+
if (this.hasAttribute('active')) queueMicrotask(() => this.activate());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
disconnectedCallback() {
|
|
73
|
+
if (this.active) this.#teardown(false);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
attributeChangedCallback(_name: string, oldValue: string | null, value: string | null) {
|
|
77
|
+
if (this.#syncing || !this.isConnected || (oldValue === null) === (value === null)) return;
|
|
78
|
+
if (value === null) this.deactivate();
|
|
79
|
+
else this.activate();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
activate() {
|
|
83
|
+
if (this.active) return;
|
|
84
|
+
const current = deepActiveElement();
|
|
85
|
+
this.#returnTo = current instanceof HTMLElement && current !== document.body ? current : null;
|
|
86
|
+
stack.push(this);
|
|
87
|
+
this.#reflect(true);
|
|
88
|
+
if (this.#mode === 'inert') this.#inertOutside();
|
|
89
|
+
document.addEventListener('keydown', this.#onKeydown, true);
|
|
90
|
+
document.addEventListener('focusin', this.#onFocusIn, true);
|
|
91
|
+
this.#focusInitial();
|
|
92
|
+
this.dispatchEvent(new CustomEvent('sinn-focus-trap-activate', { bubbles: true, composed: true }));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
deactivate() {
|
|
96
|
+
if (!this.active) return;
|
|
97
|
+
this.#teardown(this.getAttribute('return-focus') !== 'false');
|
|
98
|
+
this.dispatchEvent(new CustomEvent('sinn-focus-trap-deactivate', { bubbles: true, composed: true }));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#teardown(returnFocus: boolean) {
|
|
102
|
+
stack.splice(stack.indexOf(this), 1);
|
|
103
|
+
document.removeEventListener('keydown', this.#onKeydown, true);
|
|
104
|
+
document.removeEventListener('focusin', this.#onFocusIn, true);
|
|
105
|
+
for (const el of this.#inerted) el.inert = false;
|
|
106
|
+
this.#inerted = [];
|
|
107
|
+
this.#reflect(false);
|
|
108
|
+
if (returnFocus && this.#returnTo?.isConnected) this.#returnTo.focus({ preventScroll: true });
|
|
109
|
+
this.#returnTo = null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#reflect(on: boolean) {
|
|
113
|
+
this.#syncing = true;
|
|
114
|
+
this.toggleAttribute('active', on);
|
|
115
|
+
this.#syncing = false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Make every sibling along the path from <body> to this element inert. */
|
|
119
|
+
#inertOutside() {
|
|
120
|
+
let node: HTMLElement | null = this;
|
|
121
|
+
while (node && node !== document.body) {
|
|
122
|
+
const parent: HTMLElement | null = node.parentElement;
|
|
123
|
+
if (!parent) break;
|
|
124
|
+
for (const sibling of parent.children) {
|
|
125
|
+
if (sibling === node || !(sibling instanceof HTMLElement)) continue;
|
|
126
|
+
if (sibling.inert || ['SCRIPT', 'STYLE', 'TEMPLATE', 'LINK'].includes(sibling.tagName)) continue;
|
|
127
|
+
// Leave open modal dialogs/popovers in the top layer alone.
|
|
128
|
+
if (sibling.matches(':modal, :popover-open')) continue;
|
|
129
|
+
sibling.inert = true;
|
|
130
|
+
this.#inerted.push(sibling);
|
|
131
|
+
}
|
|
132
|
+
node = parent;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
#focusInitial() {
|
|
137
|
+
const selector = this.getAttribute('initial-focus');
|
|
138
|
+
const target = (selector && this.querySelector<HTMLElement>(selector)) || tabbables(this)[0];
|
|
139
|
+
if (target) {
|
|
140
|
+
target.focus();
|
|
141
|
+
this.#lastInside = target;
|
|
142
|
+
} else {
|
|
143
|
+
if (!this.hasAttribute('tabindex')) this.tabIndex = -1;
|
|
144
|
+
this.focus();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#onKeydown = (event: KeyboardEvent) => {
|
|
149
|
+
if (!this.#isTop()) return;
|
|
150
|
+
|
|
151
|
+
if (event.key === 'Escape' && this.hasAttribute('escape-deactivates')) {
|
|
152
|
+
const proceed = this.dispatchEvent(
|
|
153
|
+
new CustomEvent('sinn-focus-trap-escape', { bubbles: true, composed: true, cancelable: true }),
|
|
154
|
+
);
|
|
155
|
+
if (proceed) {
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
this.deactivate();
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (event.key !== 'Tab' || event.altKey || event.ctrlKey || event.metaKey) return;
|
|
163
|
+
const items = tabbables(this);
|
|
164
|
+
const active = deepActiveElement();
|
|
165
|
+
if (items.length === 0) {
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
this.focus();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const first = items[0];
|
|
171
|
+
const last = items[items.length - 1];
|
|
172
|
+
const inside = active instanceof Node && this.contains(active);
|
|
173
|
+
if (event.shiftKey && (active === first || !inside || active === this)) {
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
last.focus();
|
|
176
|
+
} else if (!event.shiftKey && (active === last || !inside)) {
|
|
177
|
+
event.preventDefault();
|
|
178
|
+
first.focus();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
#onFocusIn = (event: FocusEvent) => {
|
|
183
|
+
if (!this.#isTop()) return;
|
|
184
|
+
const target = event.target as Node | null;
|
|
185
|
+
if (target && this.contains(target)) {
|
|
186
|
+
if (target instanceof HTMLElement) this.#lastInside = target;
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
// Focus escaped (pointer, screen-reader cursor, script): bring it back.
|
|
190
|
+
const back = (this.#lastInside?.isConnected && this.contains(this.#lastInside) && this.#lastInside) || tabbables(this)[0];
|
|
191
|
+
if (back) back.focus({ preventScroll: true });
|
|
192
|
+
else this.focus({ preventScroll: true });
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
customElements.define(tag, SinnFocusTrap);
|
|
197
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Focus utilities shared by FocusTrap and client islands. DOM-only, no top-level DOM access
|
|
3
|
+
* (safe to import during SSR).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const CANDIDATES = [
|
|
7
|
+
'a[href]',
|
|
8
|
+
'area[href]',
|
|
9
|
+
'button:not([disabled])',
|
|
10
|
+
'input:not([disabled]):not([type="hidden"])',
|
|
11
|
+
'select:not([disabled])',
|
|
12
|
+
'textarea:not([disabled])',
|
|
13
|
+
'summary',
|
|
14
|
+
'iframe',
|
|
15
|
+
'audio[controls]',
|
|
16
|
+
'video[controls]',
|
|
17
|
+
'[contenteditable]:not([contenteditable="false"])',
|
|
18
|
+
'[tabindex]',
|
|
19
|
+
].join(',');
|
|
20
|
+
|
|
21
|
+
function isVisible(el: HTMLElement): boolean {
|
|
22
|
+
const check = (el as HTMLElement & { checkVisibility?: (o?: object) => boolean }).checkVisibility;
|
|
23
|
+
if (typeof check === 'function') return check.call(el, { visibilityProperty: true });
|
|
24
|
+
return Boolean(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Elements reachable with Tab inside `root`, in tab order: positive tabindex first (ascending),
|
|
29
|
+
* then document order. Skips disabled, hidden, inert and tabindex="-1" elements, closed
|
|
30
|
+
* <details> content, all but the first <summary>, and all but one radio per group
|
|
31
|
+
* (the checked one, else the first), matching browser behavior.
|
|
32
|
+
*/
|
|
33
|
+
export function tabbables(root: ParentNode): HTMLElement[] {
|
|
34
|
+
const all = [...root.querySelectorAll<HTMLElement>(CANDIDATES)].filter((el) => {
|
|
35
|
+
if (el.tabIndex < 0) return false;
|
|
36
|
+
if (el.closest('[inert]')) return false;
|
|
37
|
+
if (el.closest('fieldset[disabled]') && !el.closest('legend')) return false;
|
|
38
|
+
if (!isVisible(el)) return false;
|
|
39
|
+
if (el.tagName === 'SUMMARY') {
|
|
40
|
+
const details = el.parentElement;
|
|
41
|
+
if (!(details instanceof HTMLDetailsElement) || details.querySelector(':scope > summary') !== el) return false;
|
|
42
|
+
}
|
|
43
|
+
if (el instanceof HTMLInputElement && el.type === 'radio' && el.name) {
|
|
44
|
+
const scope = el.form ?? el.ownerDocument;
|
|
45
|
+
const group = [...scope.querySelectorAll<HTMLInputElement>(`input[type="radio"][name="${CSS.escape(el.name)}"]`)];
|
|
46
|
+
const checked = group.find((r) => r.checked);
|
|
47
|
+
return checked ? el === checked : el === group[0];
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
});
|
|
51
|
+
const positive = all.filter((el) => el.tabIndex > 0).sort((a, b) => a.tabIndex - b.tabIndex);
|
|
52
|
+
return [...positive, ...all.filter((el) => el.tabIndex === 0)];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The deepest active element, following open shadow roots. */
|
|
56
|
+
export function deepActiveElement(doc: Document = document): Element | null {
|
|
57
|
+
let el = doc.activeElement;
|
|
58
|
+
while (el?.shadowRoot?.activeElement) el = el.shadowRoot.activeElement;
|
|
59
|
+
return el;
|
|
60
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure planning for responsive images: which widths to generate, the `sizes` hint, and the
|
|
3
|
+
* width/height attributes that reserve space (CLS ≈ 0). No Astro imports, so it's unit-testable;
|
|
4
|
+
* `image.ts` feeds the plan to Astro's getImage().
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type Aspect = 'auto' | '1/1' | '4/3' | '3/2' | '16/9' | '21/9' | '3/4' | '2/3' | '4/5';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* - `constrained`: shown at up to `width` CSS px, shrinking with its container (the default).
|
|
11
|
+
* - `full`: spans the viewport (heroes, banners).
|
|
12
|
+
* - `fixed`: always exactly `width` px (avatars, logos); 1x and 2x densities.
|
|
13
|
+
*/
|
|
14
|
+
export type ImageLayout = 'constrained' | 'full' | 'fixed';
|
|
15
|
+
|
|
16
|
+
/** Common device/layout widths; candidates for srcset. */
|
|
17
|
+
export const BREAKPOINTS = [320, 480, 640, 768, 960, 1280, 1600, 1920, 2560] as const;
|
|
18
|
+
|
|
19
|
+
export interface ImagePlanInput {
|
|
20
|
+
/** Intrinsic size of the source file. */
|
|
21
|
+
sourceWidth: number;
|
|
22
|
+
sourceHeight: number;
|
|
23
|
+
/** Largest CSS width it will be displayed at. Default: the source width (capped at 1280 for constrained). */
|
|
24
|
+
width?: number;
|
|
25
|
+
aspect?: Aspect;
|
|
26
|
+
layout?: ImageLayout;
|
|
27
|
+
/** Explicit sizes attribute; overrides the computed one. */
|
|
28
|
+
sizes?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Lazy images get `sizes="auto, …"`: browsers that support it use the image's real laid-out
|
|
31
|
+
* width (a 286px grid cell gets a ~300w file, not the 800w maximum); others fall back to the hint.
|
|
32
|
+
*/
|
|
33
|
+
lazy?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ImagePlan {
|
|
37
|
+
/** Width for the `src` fallback. */
|
|
38
|
+
width: number;
|
|
39
|
+
/** width/height attributes (aspect applied), so the browser reserves the box before load. */
|
|
40
|
+
attrWidth: number;
|
|
41
|
+
attrHeight: number;
|
|
42
|
+
/** Widths to generate for a `w` srcset (constrained/full). */
|
|
43
|
+
widths?: number[];
|
|
44
|
+
/** Densities for an `x` srcset (fixed). */
|
|
45
|
+
densities?: number[];
|
|
46
|
+
sizes?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function ratioOf(aspect: Aspect | undefined, fallback: number): number {
|
|
50
|
+
if (!aspect || aspect === 'auto') return fallback;
|
|
51
|
+
const [a, b] = aspect.split('/').map(Number);
|
|
52
|
+
return a > 0 && b > 0 ? a / b : fallback;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function planImage(input: ImagePlanInput): ImagePlan {
|
|
56
|
+
const { sourceWidth, sourceHeight, aspect, layout = 'constrained' } = input;
|
|
57
|
+
if (!(sourceWidth > 0) || !(sourceHeight > 0)) throw new Error('planImage: source dimensions must be positive');
|
|
58
|
+
|
|
59
|
+
const defaultWidth = layout === 'constrained' ? Math.min(sourceWidth, 1280) : sourceWidth;
|
|
60
|
+
// Never upscale.
|
|
61
|
+
const width = Math.min(Math.round(input.width ?? defaultWidth), sourceWidth);
|
|
62
|
+
const ratio = ratioOf(aspect, sourceWidth / sourceHeight);
|
|
63
|
+
const attrWidth = width;
|
|
64
|
+
const attrHeight = Math.round(width / ratio);
|
|
65
|
+
|
|
66
|
+
if (layout === 'fixed') {
|
|
67
|
+
// 2x only if the source can actually provide it.
|
|
68
|
+
const densities = sourceWidth >= width * 2 ? [1, 2] : [1];
|
|
69
|
+
return { width, attrWidth, attrHeight, densities, sizes: input.sizes };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const cap = layout === 'full' ? sourceWidth : Math.min(sourceWidth, width * 2);
|
|
73
|
+
const set = new Set<number>(BREAKPOINTS.filter((bp) => bp < cap));
|
|
74
|
+
set.add(Math.min(width, sourceWidth));
|
|
75
|
+
set.add(cap);
|
|
76
|
+
const widths = [...set].filter((w) => w > 0).sort((a, b) => a - b);
|
|
77
|
+
|
|
78
|
+
const hint = layout === 'full' ? '100vw' : `(min-width: ${width}px) ${width}px, 100vw`;
|
|
79
|
+
const sizes = input.sizes ?? (input.lazy ? `auto, ${hint}` : hint);
|
|
80
|
+
|
|
81
|
+
return { width, attrWidth, attrHeight, widths, sizes };
|
|
82
|
+
}
|