@vvedantb/ui 0.51.0 → 0.52.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.
@@ -1,5 +1,67 @@
1
1
  import { prefersReducedMotion } from "./overlay-a11y.js";
2
2
  const closeTimers = new WeakMap();
3
+
4
+ /*
5
+ * Grow from the trigger (macOS-style): a modal opened by a press grows out of
6
+ * the pressed point and shrinks back into it on close. Opened from the
7
+ * keyboard, it grows out of the focused control. With neither (a shortcut, an
8
+ * async open), it keeps the centred `--modal-scale` zoom.
9
+ */
10
+ const TRIGGER_SCALE = 0.4;
11
+ /** A press older than this did not open the modal (it was an async open). */
12
+ const PRESS_WINDOW_MS = 500;
13
+ let lastPress = null;
14
+
15
+ // From module load, not the first modal: the press that opens the first
16
+ // modal comes before any modal connects.
17
+ if (typeof document !== "undefined") {
18
+ document.addEventListener("pointerdown", event => {
19
+ lastPress = {
20
+ x: event.clientX,
21
+ y: event.clientY,
22
+ at: performance.now()
23
+ };
24
+ }, true);
25
+ }
26
+ function triggerPoint(el) {
27
+ const press = lastPress;
28
+ lastPress = null;
29
+ if (press && performance.now() - press.at < PRESS_WINDOW_MS) return press;
30
+ const active = document.activeElement;
31
+ if (active instanceof HTMLElement && active !== document.body && !el.contains(active)) {
32
+ const r = active.getBoundingClientRect();
33
+ if (r.width > 0) return {
34
+ x: r.left + r.width / 2,
35
+ y: r.top + r.height / 2
36
+ };
37
+ }
38
+ return null;
39
+ }
40
+
41
+ /**
42
+ * Points the surface's transform origin at the trigger and widens its zoom.
43
+ * Skips instant surfaces (`--modal-scale: 1`, the command palette). Measured
44
+ * from the box centre, which a centred scale does not move.
45
+ */
46
+ function aimAtTrigger(el) {
47
+ if (getComputedStyle(el).getPropertyValue("--modal-scale").trim() === "1") {
48
+ return;
49
+ }
50
+ const point = triggerPoint(el);
51
+ // No layout (jsdom, a hidden host): nothing to aim from.
52
+ if (point === null || el.offsetWidth === 0) return;
53
+ const r = el.getBoundingClientRect();
54
+ const left = r.left + r.width / 2 - el.offsetWidth / 2;
55
+ const top = r.top + r.height / 2 - el.offsetHeight / 2;
56
+ // Measuring fixed the 0.96 start as the before-change style, so jump to
57
+ // the trigger start without a transition, or it would tween there first.
58
+ el.style.transition = "none";
59
+ el.style.transformOrigin = `${point.x - left}px ${point.y - top}px`;
60
+ el.style.setProperty("--modal-scale", String(TRIGGER_SCALE));
61
+ el.style.setProperty("--modal-scale-close", String(TRIGGER_SCALE));
62
+ void el.offsetWidth;
63
+ el.style.transition = "";
64
+ }
3
65
  function readModalCloseMs() {
4
66
  const raw = getComputedStyle(document.documentElement).getPropertyValue("--modal-close-dur");
5
67
  const parsed = parseFloat(raw);
@@ -45,6 +107,7 @@ function primeModalSurface(el) {
45
107
  openModalSurface(el);
46
108
  return;
47
109
  }
110
+ aimAtTrigger(el);
48
111
  requestAnimationFrame(() => {
49
112
  openModalSurface(el);
50
113
  });
@@ -73,7 +136,9 @@ function disconnectModalSurface(el) {
73
136
  // wire radix data, state → is, open / is, closing on the dialog surface
74
137
  // call from the content ref callback so portal, mounted nodes are never missed
75
138
  export function connectModalSurface(el) {
76
- if (el.getAttribute("data-state") === "open") {
139
+ // A content re-render can re-run the ref (a new `ref` prop): a surface
140
+ // that is already open stays put, or it replayed its entrance mid-view.
141
+ if (el.getAttribute("data-state") === "open" && !el.classList.contains("is-open")) {
77
142
  primeModalSurface(el);
78
143
  }
79
144
  const observer = new MutationObserver(() => {
@@ -1,6 +1,7 @@
1
1
  /*
2
2
  * @vvedantb/ui/tokens/css/modal.css: .t-modal open / close transition used by
3
- * @vvedantb/ui Dialog (transitions.dev pattern).
3
+ * @vvedantb/ui Dialog (transitions.dev pattern). `modalTransition.ts` aims
4
+ * the transform origin at the trigger and sets a wider inline --modal-scale.
4
5
  *
5
6
  * Reads --modal-open-dur, --modal-close-dur, --modal-scale,
6
7
  * --modal-scale-close and --modal-ease. tokens.css sets them on :root; set
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/ui",
3
- "version": "0.51.0",
3
+ "version": "0.52.0",
4
4
  "license": "MIT",
5
5
  "description": "Shared React 19 UI for web apps: one main JavaScript entry (primitives, shell pieces, app helpers including the Convex and Clerk provider, VITE env parsing and ensure-user, artifact viewers over the @vvedantb/server artifacts core, native tokens), third-party brand logos (also @vvedantb/ui/logos), motion-driven animated SVG icons (@vvedantb/ui/icons/animated), a client entry without @vvedantb/server, convex or Clerk for Chrome extensions, design-token CSS, shared Tailwind v4 CSS, a Tailwind preset and packaged Instrument Sans fonts.",
6
6
  "type": "module",
@@ -2,6 +2,70 @@ import { prefersReducedMotion } from "./overlay-a11y";
2
2
 
3
3
  const closeTimers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>();
4
4
 
5
+ /*
6
+ * Grow from the trigger (macOS-style): a modal opened by a press grows out of
7
+ * the pressed point and shrinks back into it on close. Opened from the
8
+ * keyboard, it grows out of the focused control. With neither (a shortcut, an
9
+ * async open), it keeps the centred `--modal-scale` zoom.
10
+ */
11
+ const TRIGGER_SCALE = 0.4;
12
+ /** A press older than this did not open the modal (it was an async open). */
13
+ const PRESS_WINDOW_MS = 500;
14
+ let lastPress: { x: number; y: number; at: number } | null = null;
15
+
16
+ // From module load, not the first modal: the press that opens the first
17
+ // modal comes before any modal connects.
18
+ if (typeof document !== "undefined") {
19
+ document.addEventListener(
20
+ "pointerdown",
21
+ (event) => {
22
+ lastPress = { x: event.clientX, y: event.clientY, at: performance.now() };
23
+ },
24
+ true,
25
+ );
26
+ }
27
+
28
+ function triggerPoint(el: HTMLElement): { x: number; y: number } | null {
29
+ const press = lastPress;
30
+ lastPress = null;
31
+ if (press && performance.now() - press.at < PRESS_WINDOW_MS) return press;
32
+ const active = document.activeElement;
33
+ if (
34
+ active instanceof HTMLElement &&
35
+ active !== document.body &&
36
+ !el.contains(active)
37
+ ) {
38
+ const r = active.getBoundingClientRect();
39
+ if (r.width > 0) return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
40
+ }
41
+ return null;
42
+ }
43
+
44
+ /**
45
+ * Points the surface's transform origin at the trigger and widens its zoom.
46
+ * Skips instant surfaces (`--modal-scale: 1`, the command palette). Measured
47
+ * from the box centre, which a centred scale does not move.
48
+ */
49
+ function aimAtTrigger(el: HTMLElement): void {
50
+ if (getComputedStyle(el).getPropertyValue("--modal-scale").trim() === "1") {
51
+ return;
52
+ }
53
+ const point = triggerPoint(el);
54
+ // No layout (jsdom, a hidden host): nothing to aim from.
55
+ if (point === null || el.offsetWidth === 0) return;
56
+ const r = el.getBoundingClientRect();
57
+ const left = r.left + r.width / 2 - el.offsetWidth / 2;
58
+ const top = r.top + r.height / 2 - el.offsetHeight / 2;
59
+ // Measuring fixed the 0.96 start as the before-change style, so jump to
60
+ // the trigger start without a transition, or it would tween there first.
61
+ el.style.transition = "none";
62
+ el.style.transformOrigin = `${point.x - left}px ${point.y - top}px`;
63
+ el.style.setProperty("--modal-scale", String(TRIGGER_SCALE));
64
+ el.style.setProperty("--modal-scale-close", String(TRIGGER_SCALE));
65
+ void el.offsetWidth;
66
+ el.style.transition = "";
67
+ }
68
+
5
69
  function readModalCloseMs(): number {
6
70
  const raw = getComputedStyle(document.documentElement).getPropertyValue(
7
71
  "--modal-close-dur",
@@ -50,6 +114,7 @@ function primeModalSurface(el: HTMLElement): void {
50
114
  openModalSurface(el);
51
115
  return;
52
116
  }
117
+ aimAtTrigger(el);
53
118
  requestAnimationFrame(() => {
54
119
  openModalSurface(el);
55
120
  });
@@ -79,7 +144,12 @@ function disconnectModalSurface(el: HTMLElement): void {
79
144
  // wire radix data, state → is, open / is, closing on the dialog surface
80
145
  // call from the content ref callback so portal, mounted nodes are never missed
81
146
  export function connectModalSurface(el: HTMLElement): () => void {
82
- if (el.getAttribute("data-state") === "open") {
147
+ // A content re-render can re-run the ref (a new `ref` prop): a surface
148
+ // that is already open stays put, or it replayed its entrance mid-view.
149
+ if (
150
+ el.getAttribute("data-state") === "open" &&
151
+ !el.classList.contains("is-open")
152
+ ) {
83
153
  primeModalSurface(el);
84
154
  }
85
155
 
@@ -1,6 +1,7 @@
1
1
  /*
2
2
  * @vvedantb/ui/tokens/css/modal.css: .t-modal open / close transition used by
3
- * @vvedantb/ui Dialog (transitions.dev pattern).
3
+ * @vvedantb/ui Dialog (transitions.dev pattern). `modalTransition.ts` aims
4
+ * the transform origin at the trigger and sets a wider inline --modal-scale.
4
5
  *
5
6
  * Reads --modal-open-dur, --modal-close-dur, --modal-scale,
6
7
  * --modal-scale-close and --modal-ease. tokens.css sets them on :root; set