@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.
Files changed (306) hide show
  1. package/CHANGELOG.md +89 -0
  2. package/LICENSE +373 -0
  3. package/README.md +188 -2
  4. package/docs/blocks.md +373 -0
  5. package/docs/data.md +128 -0
  6. package/docs/finishes.md +165 -0
  7. package/docs/motion.md +166 -0
  8. package/docs/primitives.md +510 -0
  9. package/docs/seo.md +191 -0
  10. package/docs/tokens.md +122 -0
  11. package/package.json +132 -3
  12. package/scripts/check-contrast.mjs +180 -0
  13. package/src/blocks/BlockRenderer.astro +60 -0
  14. package/src/blocks/CTABanner.astro +121 -0
  15. package/src/blocks/CTAPair.astro +110 -0
  16. package/src/blocks/CardGrid.astro +167 -0
  17. package/src/blocks/ConsentEmbed.astro +125 -0
  18. package/src/blocks/ConsentManager.astro +147 -0
  19. package/src/blocks/ConsentScript.astro +47 -0
  20. package/src/blocks/ContactCard.astro +164 -0
  21. package/src/blocks/ContactForm.astro +224 -0
  22. package/src/blocks/ErrorPage.astro +242 -0
  23. package/src/blocks/FAQ.astro +135 -0
  24. package/src/blocks/FeatureGrid.astro +148 -0
  25. package/src/blocks/Hero.astro +171 -0
  26. package/src/blocks/HeroCarousel.astro +220 -0
  27. package/src/blocks/HighlightCards.astro +107 -0
  28. package/src/blocks/LocationHours.astro +311 -0
  29. package/src/blocks/LogoCloud.astro +135 -0
  30. package/src/blocks/MaintenancePage.astro +173 -0
  31. package/src/blocks/MediaText.astro +177 -0
  32. package/src/blocks/PageHeader.astro +126 -0
  33. package/src/blocks/ProseSection.astro +98 -0
  34. package/src/blocks/Reviews.astro +194 -0
  35. package/src/blocks/RichText.astro +58 -0
  36. package/src/blocks/StatsBand.astro +117 -0
  37. package/src/blocks/Steps.astro +141 -0
  38. package/src/blocks/Testimonials.astro +144 -0
  39. package/src/blocks/Topbar.astro +158 -0
  40. package/src/blocks/cms/card-grid.ts +48 -0
  41. package/src/blocks/cms/consent.ts +16 -0
  42. package/src/blocks/cms/contact-card.ts +46 -0
  43. package/src/blocks/cms/contact-form.ts +37 -0
  44. package/src/blocks/cms/cta-banner.ts +27 -0
  45. package/src/blocks/cms/cta-pair.ts +30 -0
  46. package/src/blocks/cms/data-base.ts +5 -0
  47. package/src/blocks/cms/data.ts +257 -0
  48. package/src/blocks/cms/faq.ts +38 -0
  49. package/src/blocks/cms/feature-grid.ts +47 -0
  50. package/src/blocks/cms/fixtures.ts +591 -0
  51. package/src/blocks/cms/hero-carousel.ts +36 -0
  52. package/src/blocks/cms/hero.ts +33 -0
  53. package/src/blocks/cms/highlight-cards.ts +41 -0
  54. package/src/blocks/cms/images.ts +37 -0
  55. package/src/blocks/cms/index.ts +59 -0
  56. package/src/blocks/cms/location-hours.ts +67 -0
  57. package/src/blocks/cms/logo-cloud.ts +36 -0
  58. package/src/blocks/cms/media-text.ts +26 -0
  59. package/src/blocks/cms/page-header.ts +27 -0
  60. package/src/blocks/cms/pages.ts +35 -0
  61. package/src/blocks/cms/prose-section.ts +25 -0
  62. package/src/blocks/cms/reviews.ts +50 -0
  63. package/src/blocks/cms/schema-dsl.ts +134 -0
  64. package/src/blocks/cms/schema.ts +794 -0
  65. package/src/blocks/cms/shared.ts +63 -0
  66. package/src/blocks/cms/stats-band.ts +35 -0
  67. package/src/blocks/cms/steps.ts +40 -0
  68. package/src/blocks/cms/testimonials.ts +46 -0
  69. package/src/blocks/cms/topbar.ts +27 -0
  70. package/src/blocks/cms/types.ts +27 -0
  71. package/src/blocks/consent.ts +14 -0
  72. package/src/blocks/icons.ts +47 -0
  73. package/src/blocks/index.ts +42 -0
  74. package/src/blocks/internal/Actions.astro +35 -0
  75. package/src/blocks/internal/BlockHeader.astro +45 -0
  76. package/src/blocks/internal/BlockShell.astro +58 -0
  77. package/src/blocks/internal/CarouselScript.astro +7 -0
  78. package/src/blocks/internal/ConsentEmbedScript.astro +7 -0
  79. package/src/blocks/internal/ConsentManagerScript.astro +7 -0
  80. package/src/blocks/internal/HoursScript.astro +7 -0
  81. package/src/blocks/internal/Placeholder.astro +13 -0
  82. package/src/blocks/internal/RichTextNode.astro +133 -0
  83. package/src/blocks/internal/StandaloneDocument.astro +53 -0
  84. package/src/blocks/internal/consent.ts +96 -0
  85. package/src/blocks/internal/fields.ts +18 -0
  86. package/src/blocks/internal/registry-contract.ts +67 -0
  87. package/src/blocks/internal/reviews.ts +22 -0
  88. package/src/blocks/internal/slug.ts +12 -0
  89. package/src/blocks/internal/util.ts +23 -0
  90. package/src/blocks/lib/contact.ts +92 -0
  91. package/src/blocks/lib/error-pages.ts +119 -0
  92. package/src/blocks/lib/faq.ts +37 -0
  93. package/src/blocks/lib/hours.ts +201 -0
  94. package/src/blocks/lib/maintenance.ts +129 -0
  95. package/src/blocks/lib/markdown.ts +350 -0
  96. package/src/blocks/lib/rich-text.ts +132 -0
  97. package/src/blocks/registry.ts +79 -0
  98. package/src/blocks/scripts/carousel.ts +218 -0
  99. package/src/blocks/scripts/consent-embed.ts +105 -0
  100. package/src/blocks/scripts/consent-manager.ts +112 -0
  101. package/src/blocks/scripts/consent-store.ts +86 -0
  102. package/src/blocks/scripts/error-page.ts +71 -0
  103. package/src/blocks/scripts/hours.ts +107 -0
  104. package/src/blocks/scripts/maintenance.ts +109 -0
  105. package/src/blocks/styles/blocks.css +1320 -0
  106. package/src/blocks/styles/consent.css +102 -0
  107. package/src/blocks/styles/faq.css +28 -0
  108. package/src/blocks/styles/location.css +92 -0
  109. package/src/blocks/styles/prose-section.css +13 -0
  110. package/src/blocks/styles/quotes.css +123 -0
  111. package/src/blocks/styles/rich-text.css +14 -0
  112. package/src/blocks/types.ts +293 -0
  113. package/src/data/ChartAxis.astro +19 -0
  114. package/src/data/ChartContainer.astro +152 -0
  115. package/src/data/ChartLegend.astro +25 -0
  116. package/src/data/ChartSeries.astro +14 -0
  117. package/src/data/ChartTable.astro +17 -0
  118. package/src/data/ChartTooltip.astro +20 -0
  119. package/src/data/Sparkline.astro +55 -0
  120. package/src/data/cms.ts +130 -0
  121. package/src/data/index.ts +10 -0
  122. package/src/data/internal/scale.ts +107 -0
  123. package/src/data/lib/chart.ts +459 -0
  124. package/src/data/lib/render.ts +219 -0
  125. package/src/data/lib/sparkline.ts +117 -0
  126. package/src/data/schema.ts +65 -0
  127. package/src/data/scripts/chart.ts +324 -0
  128. package/src/data/styles/chart.css +392 -0
  129. package/src/finishes/aurora.css +75 -0
  130. package/src/finishes/autumn.css +87 -0
  131. package/src/finishes/blueprint.css +69 -0
  132. package/src/finishes/brutalist.css +55 -0
  133. package/src/finishes/clay.css +48 -0
  134. package/src/finishes/day-cycle.css +46 -0
  135. package/src/finishes/fog.css +71 -0
  136. package/src/finishes/glass.css +149 -0
  137. package/src/finishes/holographic.css +91 -0
  138. package/src/finishes/night.css +77 -0
  139. package/src/finishes/paper.css +58 -0
  140. package/src/finishes/polished-glass.css +159 -0
  141. package/src/finishes/rain.css +84 -0
  142. package/src/finishes/scripts/day-cycle.ts +30 -0
  143. package/src/finishes/scripts/holographic.ts +37 -0
  144. package/src/finishes/scripts/spotlight.ts +30 -0
  145. package/src/finishes/spotlight.css +41 -0
  146. package/src/finishes/spring.css +80 -0
  147. package/src/finishes/terminal.css +49 -0
  148. package/src/motion/Marquee.astro +90 -0
  149. package/src/motion/MotionProvider.astro +50 -0
  150. package/src/motion/PageTransitions.astro +61 -0
  151. package/src/motion/Parallax.astro +44 -0
  152. package/src/motion/ParallaxLayer.astro +31 -0
  153. package/src/motion/RevealOnScroll.astro +59 -0
  154. package/src/motion/SceneRegistry.astro +38 -0
  155. package/src/motion/ScrollScene.astro +81 -0
  156. package/src/motion/index.ts +12 -0
  157. package/src/motion/integration.ts +48 -0
  158. package/src/motion/runtime/page-transitions.ts +68 -0
  159. package/src/motion/runtime/runtime.ts +180 -0
  160. package/src/motion/runtime/scenes.ts +248 -0
  161. package/src/motion/runtime/types.ts +103 -0
  162. package/src/motion/styles/motion.css +332 -0
  163. package/src/motion/transitions.ts +74 -0
  164. package/src/motion/virtual.d.ts +4 -0
  165. package/src/primitives/Accordion.astro +129 -0
  166. package/src/primitives/Breadcrumbs.astro +50 -0
  167. package/src/primitives/BulletList.astro +69 -0
  168. package/src/primitives/Button.astro +73 -0
  169. package/src/primitives/ButtonGroup.astro +54 -0
  170. package/src/primitives/Callout.astro +137 -0
  171. package/src/primitives/Caption.astro +35 -0
  172. package/src/primitives/Card.astro +72 -0
  173. package/src/primitives/CardContent.astro +24 -0
  174. package/src/primitives/CardFooter.astro +36 -0
  175. package/src/primitives/CardHeader.astro +86 -0
  176. package/src/primitives/Checkbox.astro +38 -0
  177. package/src/primitives/ChoiceGroup.astro +23 -0
  178. package/src/primitives/Cluster.astro +49 -0
  179. package/src/primitives/Container.astro +44 -0
  180. package/src/primitives/DescriptionList.astro +83 -0
  181. package/src/primitives/Dialog.astro +38 -0
  182. package/src/primitives/Disclosure.astro +67 -0
  183. package/src/primitives/Divider.astro +93 -0
  184. package/src/primitives/Drawer.astro +33 -0
  185. package/src/primitives/Fieldset.astro +80 -0
  186. package/src/primitives/Flex.astro +64 -0
  187. package/src/primitives/FocusRing.astro +63 -0
  188. package/src/primitives/FocusTrap.astro +64 -0
  189. package/src/primitives/Footer.astro +155 -0
  190. package/src/primitives/Form.astro +68 -0
  191. package/src/primitives/FormDescription.astro +26 -0
  192. package/src/primitives/FormError.astro +106 -0
  193. package/src/primitives/FormField.astro +72 -0
  194. package/src/primitives/Grid.astro +64 -0
  195. package/src/primitives/Heading.astro +45 -0
  196. package/src/primitives/IconButton.astro +48 -0
  197. package/src/primitives/Image.astro +125 -0
  198. package/src/primitives/Input.astro +86 -0
  199. package/src/primitives/Label.astro +50 -0
  200. package/src/primitives/Link.astro +81 -0
  201. package/src/primitives/LinkButton.astro +66 -0
  202. package/src/primitives/List.astro +39 -0
  203. package/src/primitives/ListItem.astro +178 -0
  204. package/src/primitives/MediaObject.astro +66 -0
  205. package/src/primitives/MetricCard.astro +278 -0
  206. package/src/primitives/NavLink.astro +81 -0
  207. package/src/primitives/NavMenu.astro +109 -0
  208. package/src/primitives/Navbar.astro +197 -0
  209. package/src/primitives/Panel.astro +153 -0
  210. package/src/primitives/Picture.astro +167 -0
  211. package/src/primitives/Popover.astro +95 -0
  212. package/src/primitives/Prose.astro +45 -0
  213. package/src/primitives/Radio.astro +21 -0
  214. package/src/primitives/Rating.astro +135 -0
  215. package/src/primitives/ReducedMotion.astro +90 -0
  216. package/src/primitives/SegmentedControl.astro +132 -0
  217. package/src/primitives/Select.astro +109 -0
  218. package/src/primitives/Sheet.astro +45 -0
  219. package/src/primitives/SkipLink.astro +31 -0
  220. package/src/primitives/Slider.astro +186 -0
  221. package/src/primitives/Spacer.astro +35 -0
  222. package/src/primitives/Stack.astro +51 -0
  223. package/src/primitives/Surface.astro +70 -0
  224. package/src/primitives/Switch.astro +22 -0
  225. package/src/primitives/Table.astro +382 -0
  226. package/src/primitives/Tabs.astro +126 -0
  227. package/src/primitives/Text.astro +56 -0
  228. package/src/primitives/Textarea.astro +75 -0
  229. package/src/primitives/Thumbnail.astro +154 -0
  230. package/src/primitives/Toggle.astro +99 -0
  231. package/src/primitives/Tooltip.astro +50 -0
  232. package/src/primitives/VisuallyHidden.astro +29 -0
  233. package/src/primitives/index.ts +103 -0
  234. package/src/primitives/internal/AccordionScript.astro +7 -0
  235. package/src/primitives/internal/ChoiceField.astro +91 -0
  236. package/src/primitives/internal/FocusTrapScript.astro +8 -0
  237. package/src/primitives/internal/FormValidationScript.astro +10 -0
  238. package/src/primitives/internal/IndeterminateScript.astro +13 -0
  239. package/src/primitives/internal/ModalPanel.astro +114 -0
  240. package/src/primitives/internal/OverlayScript.astro +7 -0
  241. package/src/primitives/internal/ReducedMotionScript.astro +7 -0
  242. package/src/primitives/internal/SliderScript.astro +7 -0
  243. package/src/primitives/internal/SparklineScript.astro +7 -0
  244. package/src/primitives/internal/TableSortScript.astro +7 -0
  245. package/src/primitives/internal/TabsScript.astro +7 -0
  246. package/src/primitives/internal/TooltipScript.astro +7 -0
  247. package/src/primitives/internal/image-plan.ts +82 -0
  248. package/src/primitives/internal/image.ts +129 -0
  249. package/src/primitives/internal/slider.ts +170 -0
  250. package/src/primitives/lib/deprecate.ts +47 -0
  251. package/src/primitives/lib/focus-trap.ts +197 -0
  252. package/src/primitives/lib/focus.ts +60 -0
  253. package/src/primitives/lib/motion.ts +68 -0
  254. package/src/primitives/scripts/accordion.ts +123 -0
  255. package/src/primitives/scripts/form-validation.ts +225 -0
  256. package/src/primitives/scripts/overlay.ts +71 -0
  257. package/src/primitives/scripts/reduced-motion.ts +59 -0
  258. package/src/primitives/scripts/slider.ts +150 -0
  259. package/src/primitives/scripts/sparkline.ts +131 -0
  260. package/src/primitives/scripts/table-sort.ts +68 -0
  261. package/src/primitives/scripts/tabs.ts +151 -0
  262. package/src/primitives/scripts/tooltip.ts +28 -0
  263. package/src/primitives/styles/a11y.css +104 -0
  264. package/src/primitives/styles/accordion.css +53 -0
  265. package/src/primitives/styles/breadcrumbs.css +59 -0
  266. package/src/primitives/styles/bullet-list.css +89 -0
  267. package/src/primitives/styles/button.css +237 -0
  268. package/src/primitives/styles/choice.css +164 -0
  269. package/src/primitives/styles/cluster.css +24 -0
  270. package/src/primitives/styles/container.css +19 -0
  271. package/src/primitives/styles/data.css +849 -0
  272. package/src/primitives/styles/disclosure.css +140 -0
  273. package/src/primitives/styles/divider.css +50 -0
  274. package/src/primitives/styles/flex.css +42 -0
  275. package/src/primitives/styles/form.css +241 -0
  276. package/src/primitives/styles/grid.css +36 -0
  277. package/src/primitives/styles/media.css +247 -0
  278. package/src/primitives/styles/nav.css +792 -0
  279. package/src/primitives/styles/overlay.css +362 -0
  280. package/src/primitives/styles/prose.css +146 -0
  281. package/src/primitives/styles/scale.css +175 -0
  282. package/src/primitives/styles/segmented.css +165 -0
  283. package/src/primitives/styles/slider.css +170 -0
  284. package/src/primitives/styles/spacer.css +17 -0
  285. package/src/primitives/styles/stack.css +25 -0
  286. package/src/primitives/styles/surface.css +403 -0
  287. package/src/primitives/styles/tabs.css +223 -0
  288. package/src/primitives/styles/typography.css +144 -0
  289. package/src/primitives/styles/utilities.css +24 -0
  290. package/src/primitives/types.ts +385 -0
  291. package/src/primitives/utils.ts +254 -0
  292. package/src/seo/JSONLD.astro +35 -0
  293. package/src/seo/SEOHead.astro +47 -0
  294. package/src/seo/cms.ts +59 -0
  295. package/src/seo/index.ts +10 -0
  296. package/src/seo/integration.ts +268 -0
  297. package/src/seo/internal/url.ts +68 -0
  298. package/src/seo/lib/jsonld.ts +342 -0
  299. package/src/seo/lib/meta.ts +205 -0
  300. package/src/seo/lib/redirects.ts +293 -0
  301. package/src/seo/lib/robots.ts +33 -0
  302. package/src/seo/lib/rss.ts +98 -0
  303. package/src/seo/lib/sitemap.ts +105 -0
  304. package/src/seo/schema.ts +43 -0
  305. package/src/tokens/base.css +71 -0
  306. package/src/tokens/tokens.css +210 -0
@@ -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,68 @@
1
+ /**
2
+ * Motion preference helpers for scripts and client islands (scroll effects, canvas,
3
+ * carousels, Lottie…). SSR-safe: no DOM access at import time.
4
+ *
5
+ * The effective preference is the OS setting (prefers-reduced-motion), overridden by
6
+ * <html data-motion="reduce|full"> when a site offers its own switch.
7
+ */
8
+
9
+ export type MotionPreference = 'reduce' | 'full' | 'system';
10
+
11
+ const QUERY = '(prefers-reduced-motion: reduce)';
12
+ const COOKIE = 'sinn-motion';
13
+
14
+ /** True when motion should be reduced right now. Always false on the server. */
15
+ export function prefersReducedMotion(): boolean {
16
+ if (typeof window === 'undefined') return false;
17
+ const override = document.documentElement.dataset.motion;
18
+ if (override === 'reduce') return true;
19
+ if (override === 'full') return false;
20
+ return window.matchMedia(QUERY).matches;
21
+ }
22
+
23
+ /**
24
+ * Calls `callback(reduced)` whenever the effective preference changes (OS setting or the
25
+ * html data-motion override). Returns an unsubscribe function.
26
+ */
27
+ export function onReducedMotionChange(callback: (reduced: boolean) => void): () => void {
28
+ if (typeof window === 'undefined') return () => {};
29
+ let last = prefersReducedMotion();
30
+ const check = () => {
31
+ const now = prefersReducedMotion();
32
+ if (now !== last) {
33
+ last = now;
34
+ callback(now);
35
+ }
36
+ };
37
+ const media = window.matchMedia(QUERY);
38
+ media.addEventListener('change', check);
39
+ const observer = new MutationObserver(check);
40
+ observer.observe(document.documentElement, { attributes: true, attributeFilter: ['data-motion'] });
41
+ return () => {
42
+ media.removeEventListener('change', check);
43
+ observer.disconnect();
44
+ };
45
+ }
46
+
47
+ /**
48
+ * Site-level switch. Sets <html data-motion> and remembers the choice in a cookie, so a
49
+ * server-rendered page can print the attribute on the next request (no flash).
50
+ * 'system' clears the override and follows the OS again.
51
+ */
52
+ export function setMotionPreference(preference: MotionPreference, options: { persist?: boolean } = {}): void {
53
+ if (typeof window === 'undefined') return;
54
+ const root = document.documentElement;
55
+ if (preference === 'system') delete root.dataset.motion;
56
+ else root.dataset.motion = preference;
57
+ if (options.persist !== false) {
58
+ const value = preference === 'system' ? '' : preference;
59
+ const maxAge = preference === 'system' ? 0 : 60 * 60 * 24 * 365;
60
+ document.cookie = `${COOKIE}=${value}; Path=/; Max-Age=${maxAge}; SameSite=Lax`;
61
+ }
62
+ }
63
+
64
+ /** Reads the saved preference from a Cookie header (for SSR): `<html data-motion={motionFromCookie(...)}>`. */
65
+ export function motionFromCookie(cookieHeader: string | null | undefined): 'reduce' | 'full' | undefined {
66
+ const match = /(?:^|;\s*)sinn-motion=(reduce|full)/.exec(cookieHeader ?? '');
67
+ return match ? (match[1] as 'reduce' | 'full') : undefined;
68
+ }
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Accordion enhancements. The sections are native <details> and work without this.
3
+ * - "Show all sections / Hide all sections" button (made visible here, so it never shows dead).
4
+ * - ↑/↓ move between section headers, Home/End jump to the first/last (APG, optional keys).
5
+ * - A link to an id inside a closed section opens it (browsers differ on doing this themselves).
6
+ * - Printing opens every section, then restores what was open.
7
+ * Delegated from `document`; per-instance setup is only revealing the button.
8
+ */
9
+ const FLAG = '__sinnAccordion';
10
+ const w = window as unknown as Record<string, boolean>;
11
+
12
+ const itemsOf = (acc: Element) => [...acc.querySelectorAll<HTMLDetailsElement>(':scope > details.sinn-accordion__item')];
13
+
14
+ function updateButton(acc: Element) {
15
+ const btn = acc.querySelector<HTMLButtonElement>(':scope > .sinn-accordion__toolbar [data-sinn-accordion-all]');
16
+ if (!btn) return;
17
+ const allOpen = itemsOf(acc).every((d) => d.open);
18
+ btn.textContent = allOpen ? btn.dataset.hide || 'Hide all sections' : btn.dataset.show || 'Show all sections';
19
+ btn.setAttribute('aria-expanded', String(allOpen));
20
+ }
21
+
22
+ function init() {
23
+ for (const acc of document.querySelectorAll<HTMLElement>('[data-sinn-accordion]:not([data-ready])')) {
24
+ updateButton(acc);
25
+ acc.dataset.ready = ''; // reveals the button (its space was already reserved)
26
+ }
27
+ openTarget();
28
+ }
29
+
30
+ /** Open every closed <details> around the URL's target, then bring it into view. */
31
+ function openTarget() {
32
+ const id = decodeURIComponent(location.hash.slice(1));
33
+ if (!id) return;
34
+ const target = document.getElementById(id);
35
+ if (!target) return;
36
+ let opened = false;
37
+ for (let el: Element | null = target; el; el = el.parentElement) {
38
+ if (el instanceof HTMLDetailsElement && !el.open && (el === target || !el.querySelector(':scope > summary')?.contains(target))) {
39
+ // Open without the height animation: mid-animation the content is 0px tall and can't be scrolled to.
40
+ el.dataset.instant = '';
41
+ el.open = true;
42
+ opened = true;
43
+ }
44
+ }
45
+ if (!opened) return;
46
+ // Instant, like the browser's own fragment scroll. Before `load` the browser may still reset
47
+ // the position (its own fragment scroll failed on the closed section), so wait for it.
48
+ const go = () =>
49
+ requestAnimationFrame(() => {
50
+ target.scrollIntoView({ block: 'start', behavior: 'instant' });
51
+ for (const d of document.querySelectorAll<HTMLElement>('details[data-instant]')) delete d.dataset.instant;
52
+ });
53
+ if (document.readyState === 'complete') go();
54
+ else window.addEventListener('load', go, { once: true });
55
+ }
56
+
57
+ if (!w[FLAG]) {
58
+ w[FLAG] = true;
59
+
60
+ document.addEventListener('click', (e) => {
61
+ const btn = (e.target as Element | null)?.closest?.<HTMLButtonElement>('[data-sinn-accordion-all]');
62
+ const acc = btn?.closest('[data-sinn-accordion]');
63
+ if (!btn || !acc) return;
64
+ const open = !itemsOf(acc).every((d) => d.open);
65
+ for (const d of itemsOf(acc)) d.open = open;
66
+ updateButton(acc);
67
+ });
68
+
69
+ // `toggle` doesn't bubble; listen in the capture phase.
70
+ document.addEventListener(
71
+ 'toggle',
72
+ (e) => {
73
+ const acc = (e.target as Element).closest?.('[data-sinn-accordion]');
74
+ if (acc && (e.target as Element).classList.contains('sinn-accordion__item')) updateButton(acc);
75
+ },
76
+ true,
77
+ );
78
+
79
+ document.addEventListener('keydown', (e) => {
80
+ if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return;
81
+ const summary = (e.target as Element | null)?.closest?.('summary');
82
+ const item = summary?.parentElement;
83
+ if (!summary || !item?.classList.contains('sinn-accordion__item') || summary !== e.target) return;
84
+ const acc = item.parentElement!;
85
+ const heads = itemsOf(acc).map((d) => d.querySelector<HTMLElement>(':scope > summary')!);
86
+ const i = heads.indexOf(summary as HTMLElement);
87
+ const next =
88
+ e.key === 'ArrowDown' ? heads[(i + 1) % heads.length]
89
+ : e.key === 'ArrowUp' ? heads[(i - 1 + heads.length) % heads.length]
90
+ : e.key === 'Home' ? heads[0]
91
+ : e.key === 'End' ? heads[heads.length - 1]
92
+ : null;
93
+ if (!next) return;
94
+ e.preventDefault();
95
+ next.focus();
96
+ });
97
+
98
+ window.addEventListener('hashchange', openTarget);
99
+
100
+ let closedForPrint: HTMLDetailsElement[] = [];
101
+ window.addEventListener('beforeprint', () => {
102
+ closedForPrint = [...document.querySelectorAll<HTMLDetailsElement>('[data-sinn-accordion] > details:not([open])')];
103
+ // Single-mode sections share a name, which would let only one stay open: lift it while printing.
104
+ for (const d of closedForPrint) {
105
+ if (d.name) d.dataset.name = d.name;
106
+ d.removeAttribute('name');
107
+ }
108
+ for (const d of closedForPrint) d.open = true;
109
+ });
110
+ window.addEventListener('afterprint', () => {
111
+ for (const d of closedForPrint) {
112
+ d.open = false;
113
+ if (d.dataset.name) d.name = d.dataset.name;
114
+ delete d.dataset.name;
115
+ }
116
+ closedForPrint = [];
117
+ });
118
+
119
+ document.addEventListener('astro:page-load', init);
120
+ }
121
+ init();
122
+
123
+ export {};
@@ -0,0 +1,225 @@
1
+ /**
2
+ * Inline validation for <Form validate="inline">.
3
+ *
4
+ * Progressive enhancement over native constraint validation:
5
+ * - Without JS: the browser's own validation bubbles + server-side errors.
6
+ * - With JS: the form becomes `novalidate`, and messages are written into each field's
7
+ * FormError (located via data-sinn-error-id, or the enclosing Fieldset for radios).
8
+ *
9
+ * Timing (avoids "error while still typing"):
10
+ * - A field is checked when you leave it, if you changed it, or after the first submit attempt.
11
+ * - A field that's showing an error is re-checked on every input, so it clears as soon as it's fixed.
12
+ * - Submit checks everything, shows all errors, focuses the first invalid control and fires
13
+ * `sinn-form-invalid`. An invalid submit is stopped in the capture phase, so the page's own
14
+ * `submit` listeners only ever see valid forms. A valid native submit is let through once
15
+ * (double-submit guard); a handler that calls preventDefault (fetch) manages its own state.
16
+ *
17
+ * Messages: the browser's localized `validationMessage`, unless the control carries a
18
+ * custom one in data-msg-<validity-key> (set from the field's `messages` prop).
19
+ *
20
+ * Event delegation on `document`: works for forms added later and with view transitions.
21
+ */
22
+ import type { SinnFormInvalidDetail } from '../types';
23
+
24
+ type Control = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
25
+
26
+ const SELECTOR = 'form[data-sinn-validate="inline"]';
27
+ const KEYS = [
28
+ 'valueMissing',
29
+ 'typeMismatch',
30
+ 'patternMismatch',
31
+ 'tooShort',
32
+ 'tooLong',
33
+ 'rangeUnderflow',
34
+ 'rangeOverflow',
35
+ 'stepMismatch',
36
+ 'badInput',
37
+ ] as const;
38
+
39
+ const dirty = new WeakSet<Control>();
40
+ const submitting = new WeakSet<HTMLFormElement>();
41
+
42
+ function isControl(el: unknown): el is Control {
43
+ return el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement;
44
+ }
45
+
46
+ function enhancedForm(el: Element | null): HTMLFormElement | null {
47
+ const form = el instanceof HTMLFormElement ? el : (el as Control | null)?.form ?? null;
48
+ return form?.matches(SELECTOR) ? form : null;
49
+ }
50
+
51
+ /** Radios sharing a name validate (and show errors) as one control. */
52
+ function group(c: Control): Control[] {
53
+ if (c instanceof HTMLInputElement && c.type === 'radio' && c.name && c.form) {
54
+ return [...c.form.querySelectorAll<HTMLInputElement>(`input[type="radio"][name="${CSS.escape(c.name)}"]`)];
55
+ }
56
+ return [c];
57
+ }
58
+
59
+ function messageFor(c: Control): string {
60
+ for (const key of KEYS) {
61
+ if (c.validity[key]) {
62
+ const custom = c.dataset[`msg${key[0].toUpperCase()}${key.slice(1)}`];
63
+ return custom || c.validationMessage;
64
+ }
65
+ }
66
+ return c.validationMessage;
67
+ }
68
+
69
+ function errorElementFor(c: Control): HTMLElement | null {
70
+ const id = c.dataset.sinnErrorId;
71
+ if (id) return document.getElementById(id);
72
+ return c.closest('fieldset')?.querySelector<HTMLElement>(':scope > [data-sinn-error]') ?? null;
73
+ }
74
+
75
+ function toggleToken(el: Element, attr: string, token: string, on: boolean) {
76
+ const tokens = new Set((el.getAttribute(attr) ?? '').split(/\s+/).filter(Boolean));
77
+ if (on) tokens.add(token);
78
+ else tokens.delete(token);
79
+ if (tokens.size) el.setAttribute(attr, [...tokens].join(' '));
80
+ else el.removeAttribute(attr);
81
+ }
82
+
83
+ function setError(c: Control, message: string) {
84
+ const members = group(c);
85
+ for (const m of members) {
86
+ if (message) m.setAttribute('aria-invalid', 'true');
87
+ else m.removeAttribute('aria-invalid');
88
+ }
89
+
90
+ const el = errorElementFor(c);
91
+ if (!el) return;
92
+ const text = el.querySelector('[data-sinn-error-text]') ?? el;
93
+ text.textContent = message;
94
+ el.hidden = !message;
95
+
96
+ // Point aria-describedby at the error only while it has content.
97
+ // Group errors are described on the fieldset, field errors on the control.
98
+ if (el.id) {
99
+ const owners = el.parentElement instanceof HTMLFieldSetElement && !c.dataset.sinnErrorId ? [el.parentElement] : members;
100
+ for (const o of owners) toggleToken(o, 'aria-describedby', el.id, Boolean(message));
101
+ }
102
+ }
103
+
104
+ /** Returns true when valid. */
105
+ function validate(c: Control): boolean {
106
+ if (!c.willValidate) return true;
107
+ const valid = c.validity.valid;
108
+ setError(c, valid ? '' : messageFor(c));
109
+ return valid;
110
+ }
111
+
112
+ function controlsOf(form: HTMLFormElement): Control[] {
113
+ const seenRadioNames = new Set<string>();
114
+ const out: Control[] = [];
115
+ for (const el of form.elements) {
116
+ if (!isControl(el) || !el.willValidate) continue;
117
+ if (el instanceof HTMLInputElement && el.type === 'radio') {
118
+ if (seenRadioNames.has(el.name)) continue;
119
+ seenRadioNames.add(el.name);
120
+ }
121
+ out.push(el);
122
+ }
123
+ return out;
124
+ }
125
+
126
+ function enhance(root: ParentNode = document) {
127
+ for (const form of root.querySelectorAll<HTMLFormElement>(SELECTOR)) form.noValidate = true;
128
+ }
129
+
130
+ const FLAG = '__sinnFormValidation';
131
+ const w = window as unknown as Record<string, boolean>;
132
+
133
+ if (!w[FLAG]) {
134
+ w[FLAG] = true;
135
+ enhance();
136
+ document.addEventListener('astro:page-load', () => enhance());
137
+
138
+ document.addEventListener(
139
+ 'submit',
140
+ (event) => {
141
+ const form = enhancedForm(event.target as Element);
142
+ if (!form) return;
143
+
144
+ if (submitting.has(form)) {
145
+ event.preventDefault();
146
+ return;
147
+ }
148
+
149
+ form.dataset.sinnSubmitted = '';
150
+ const invalid = controlsOf(form).filter((c) => !validate(c));
151
+
152
+ if (invalid.length) {
153
+ event.preventDefault();
154
+ // Stop here (capture phase), so the page's own submit handlers only run for valid forms.
155
+ event.stopPropagation();
156
+ invalid[0].focus();
157
+ form.dispatchEvent(
158
+ new CustomEvent<SinnFormInvalidDetail>('sinn-form-invalid', {
159
+ bubbles: true,
160
+ composed: true,
161
+ detail: { controls: invalid },
162
+ }),
163
+ );
164
+ return;
165
+ }
166
+
167
+ },
168
+ true,
169
+ );
170
+
171
+ // Runs last (bubble phase on window). If no page handler took over with preventDefault
172
+ // (e.g. a fetch-based submit), the browser is navigating: lock the form against double submits.
173
+ window.addEventListener('submit', (event) => {
174
+ const form = enhancedForm(event.target as Element);
175
+ if (!form || event.defaultPrevented) return;
176
+ submitting.add(form);
177
+ form.setAttribute('aria-busy', 'true');
178
+ });
179
+
180
+ document.addEventListener('input', (event) => {
181
+ const c = event.target;
182
+ if (!isControl(c) || !enhancedForm(c)) return;
183
+ dirty.add(c);
184
+ if (c.getAttribute('aria-invalid') === 'true') validate(c);
185
+ });
186
+
187
+ document.addEventListener('change', (event) => {
188
+ const c = event.target;
189
+ if (!isControl(c) || !enhancedForm(c)) return;
190
+ dirty.add(c);
191
+ // Checkboxes, radios and selects commit on change, so check right away once touched.
192
+ if (c instanceof HTMLSelectElement || (c instanceof HTMLInputElement && (c.type === 'checkbox' || c.type === 'radio'))) {
193
+ validate(c);
194
+ }
195
+ });
196
+
197
+ document.addEventListener('focusout', (event) => {
198
+ const c = event.target;
199
+ if (!isControl(c)) return;
200
+ const form = enhancedForm(c);
201
+ if (!form) return;
202
+ if (dirty.has(c) || 'sinnSubmitted' in form.dataset) validate(c);
203
+ });
204
+
205
+ // Error-summary links: move focus to the field, not just scroll to it.
206
+ document.addEventListener('click', (event) => {
207
+ const link = (event.target as Element | null)?.closest?.('.sinn-form-error a[href^="#"]');
208
+ if (!(link instanceof HTMLAnchorElement)) return;
209
+ const target = document.getElementById(decodeURIComponent(link.hash.slice(1)));
210
+ if (!target) return;
211
+ event.preventDefault();
212
+ const label = target.id ? document.querySelector(`label[for="${CSS.escape(target.id)}"]`) : null;
213
+ (label ?? target).scrollIntoView({ block: 'center' });
214
+ target.focus({ preventScroll: true });
215
+ });
216
+
217
+ // Back/forward cache restores the page as it was mid-submit: unlock it.
218
+ window.addEventListener('pageshow', (event) => {
219
+ if (!event.persisted) return;
220
+ for (const form of document.querySelectorAll<HTMLFormElement>(SELECTOR)) {
221
+ submitting.delete(form);
222
+ form.removeAttribute('aria-busy');
223
+ }
224
+ });
225
+ }