@keystone-sites/marketing-design-system 1.0.21 → 1.0.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keystone-sites/marketing-design-system",
3
- "version": "1.0.21",
3
+ "version": "1.0.22",
4
4
  "description": "Keystone's marketing design system: tokens, the tick grid, type, primitives, site chrome, and the landing kit. Source-shipped CSS and TSX for Next.js consumers.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -38,7 +38,6 @@
38
38
  flex-direction: column;
39
39
  box-sizing: border-box;
40
40
  width: 100%;
41
- max-width: 28rem;
42
41
  max-height: calc(100dvh - var(--space-2xl) * 2);
43
42
  overflow-y: auto;
44
43
  border: 1px solid var(--color-border-050);
@@ -60,6 +59,9 @@
60
59
  .modal-sheet[data-size="xs"] .modal-body {
61
60
  padding-block: calc(var(--space-4xl) + var(--space-xs)) calc(var(--space-3xl) + var(--space-xs));
62
61
  }
62
+ .modal-sheet[data-size="sm"] {
63
+ max-width: 30rem;
64
+ }
63
65
  .modal-sheet[data-size="md"] {
64
66
  max-width: 36rem;
65
67
  }
@@ -3,7 +3,8 @@
3
3
  /** Modal: a centred sheet over a dimmed ground. Locks page scroll, traps
4
4
  * Tab, closes on Escape and on the backdrop, and returns focus to the
5
5
  * element that opened it. Renders into the body so grid geometry stays
6
- * untouched. Styles live in modal.css. */
6
+ * untouched; a body that changes size while open (a step, a new `size`)
7
+ * tweens the sheet to it. Styles live in modal.css. */
7
8
 
8
9
  import { useEffect, useRef, type ReactNode } from "react";
9
10
  import { createPortal } from "react-dom";
@@ -15,7 +16,7 @@ interface ModalProps {
15
16
  /** Names the dialog for assistive technology. */
16
17
  label: string;
17
18
  onClose: () => void;
18
- /** Sheet width: xs for a sign-in, sm for prompts, md for forms, lg for records. */
19
+ /** Sheet width: xs for a prompt, sm for a sign-in, md for forms, lg for records. */
19
20
  size?: "xs" | "sm" | "md" | "lg";
20
21
  /** The page behind: lightened to paper, or dimmed to ink and blurred
21
22
  * under a sheet on the page ground with a long soft drop. */
@@ -25,6 +26,30 @@ interface ModalProps {
25
26
  children: ReactNode;
26
27
  }
27
28
 
29
+ interface Box {
30
+ width: number;
31
+ height: number;
32
+ }
33
+
34
+ const same = (a: Box, b: Box) => a.width === b.width && a.height === b.height;
35
+
36
+ // Clipped while it moves: a growing sheet is briefly shorter than its
37
+ // content, and that is no cause for a scrollbar. The cap moves with the
38
+ // width, which it would otherwise hold back.
39
+ const frame = ({ width, height }: Box): Keyframe => ({
40
+ width: `${width}px`,
41
+ maxWidth: `${width}px`,
42
+ height: `${height}px`,
43
+ overflowY: "hidden",
44
+ });
45
+
46
+ /** A CSS time as the Web Animations API wants it. The computed token
47
+ * may come back in either unit (a bundler shortens `250ms` to `.25s`). */
48
+ function milliseconds(time: string): number {
49
+ const t = time.trim();
50
+ return t.endsWith("ms") ? parseFloat(t) : parseFloat(t) * 1000;
51
+ }
52
+
28
53
  export function Modal({
29
54
  label,
30
55
  onClose,
@@ -34,6 +59,7 @@ export function Modal({
34
59
  children,
35
60
  }: ModalProps) {
36
61
  const sheetRef = useRef<HTMLDivElement>(null);
62
+ const bodyRef = useRef<HTMLDivElement>(null);
37
63
  const openerRef = useRef<Element | null>(null);
38
64
 
39
65
  useEffect(() => {
@@ -48,6 +74,79 @@ export function Modal({
48
74
  };
49
75
  }, []);
50
76
 
77
+ // The body changing size (a step, a new `size`) moves the sheet
78
+ // smoothly: as the new content lands, the sheet tweens from the box it
79
+ // stood in to its new one. Reduced motion snaps; so does the first
80
+ // measure.
81
+ useEffect(() => {
82
+ const sheet = sheetRef.current;
83
+ const body = bodyRef.current;
84
+ if (
85
+ !sheet ||
86
+ !body ||
87
+ typeof ResizeObserver === "undefined" ||
88
+ typeof sheet.animate !== "function"
89
+ )
90
+ return;
91
+ const reduced = window.matchMedia("(prefers-reduced-motion: reduce)");
92
+ const box = (): Box => ({ width: sheet.offsetWidth, height: sheet.offsetHeight });
93
+ let flight: { tween: Animation; hold: Animation; from: Box; to: Box } | null = null;
94
+ const fly = (from: Box, to: Box, at = 0) => {
95
+ const style = getComputedStyle(sheet);
96
+ const timing = {
97
+ duration: milliseconds(style.getPropertyValue("--motion-drawer-duration")),
98
+ easing: style.getPropertyValue("--motion-drawer-ease").trim(),
99
+ };
100
+ // The body keeps its settled width, centred, while the sheet moves
101
+ // around it: its lines land wrapped as they will stay, and the
102
+ // observer stays quiet until the content itself changes again.
103
+ // Measured before the sheet is set moving, which would carry it.
104
+ const held: Keyframe = {
105
+ width: `${body.offsetWidth}px`,
106
+ boxSizing: "border-box",
107
+ alignSelf: "center",
108
+ };
109
+ const tween = sheet.animate([frame(from), frame(to)], timing);
110
+ const hold = body.animate([held, held], timing);
111
+ tween.currentTime = at;
112
+ hold.currentTime = at;
113
+ tween.onfinish = () => {
114
+ flight = null;
115
+ };
116
+ flight = { tween, hold, from, to };
117
+ };
118
+ // The sheet's box as last settled (its layout box, untouched by the
119
+ // entrance's transform): by the time the observer runs, layout
120
+ // already holds the new one.
121
+ let last: Box | null = null;
122
+ const ro = new ResizeObserver(() => {
123
+ if (!flight) {
124
+ const from = last;
125
+ last = box();
126
+ if (from && !same(from, last) && !reduced.matches) fly(from, last);
127
+ return;
128
+ }
129
+ // Content changing under way: lift the tween to see where the sheet
130
+ // would now settle, and either put it back or turn it toward the
131
+ // new end.
132
+ const { tween, hold, from, to } = flight;
133
+ const at = Number(tween.currentTime);
134
+ const now = box();
135
+ tween.cancel();
136
+ hold.cancel();
137
+ flight = null;
138
+ last = box();
139
+ if (same(last, to)) fly(from, to, at);
140
+ else fly(now, last);
141
+ });
142
+ ro.observe(body);
143
+ return () => {
144
+ ro.disconnect();
145
+ flight?.tween.cancel();
146
+ flight?.hold.cancel();
147
+ };
148
+ }, []);
149
+
51
150
  useEffect(() => {
52
151
  const onKey = (event: KeyboardEvent) => {
53
152
  if (event.key === "Escape") {
@@ -76,7 +175,9 @@ export function Modal({
76
175
  <button type="button" className="modal-close" aria-label="Close" onClick={onClose}>
77
176
  <IconGalleryClose />
78
177
  </button>
79
- <div className="modal-body">{children}</div>
178
+ <div ref={bodyRef} className="modal-body">
179
+ {children}
180
+ </div>
80
181
  {foot ? <div className="modal-foot">{foot}</div> : null}
81
182
  </div>
82
183
  </div>,