@vvedantb/ui 0.51.0 → 0.53.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.
@@ -32,6 +32,7 @@ export { BulkActionBar } from "./ui/bulk-action-bar.js";
32
32
  export { CopyLinkMenuItem } from "./ui/copy-link-menu-item.js";
33
33
  export { ToggleSearch } from "./ui/toggle-search.js";
34
34
  export { MotionProvider } from "./motion/motion-provider.js";
35
+ export { useReducedMotionSetting } from "./motion/reduced-motion.js";
35
36
  export { MarqueeOnHover } from "./ui/marquee-on-hover.js";
36
37
  export { RelativeDateTime } from "./ui/relative-date-time.js";
37
38
  export { ImageLightbox } from "./ui/image-lightbox.js";
package/dist/src/index.js CHANGED
@@ -28,6 +28,7 @@ export { BulkActionBar } from "./ui/bulk-action-bar.js";
28
28
  export { CopyLinkMenuItem } from "./ui/copy-link-menu-item.js";
29
29
  export { ToggleSearch } from "./ui/toggle-search.js";
30
30
  export { MotionProvider } from "./motion/motion-provider.js";
31
+ export { useReducedMotionSetting } from "./motion/reduced-motion.js";
31
32
  export { MarqueeOnHover } from "./ui/marquee-on-hover.js";
32
33
  export { RelativeDateTime } from "./ui/relative-date-time.js";
33
34
  export { ImageLightbox } from "./ui/image-lightbox.js";
@@ -1,7 +1,7 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { Button } from "../ui/button.js";
3
3
  import { pageTitleClass } from "../ui/page-title.js";
4
- import { useMediaQuery } from "../hooks/use-media-query.js";
4
+ import { useReducedMotionSetting } from "../motion/reduced-motion.js";
5
5
  import { cn } from "../utils/cn.js";
6
6
  import { landingShellClass, productHeroSocialProofClass } from "./marketing-classes.js";
7
7
  import { PROJECT_V_HOME_URL } from "./projectV.js";
@@ -14,7 +14,7 @@ function UserCountProof(t0) {
14
14
  const {
15
15
  count
16
16
  } = t0;
17
- const reducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
17
+ const reducedMotion = useReducedMotionSetting();
18
18
  const t1 = !reducedMotion && "animate-pulse";
19
19
  let t2;
20
20
  if ($[0] !== t1) {
@@ -265,23 +265,22 @@ html:has(#top) {
265
265
  }
266
266
  }
267
267
 
268
+ /* Reduced motion follows the root `MotionProvider reducedMotion` setting
269
+ (`html[data-reduced-motion]`), as in motion.css. */
268
270
  @media (prefers-reduced-motion: reduce) {
269
- html:has(#top) {
271
+ html:not([data-reduced-motion="never"]):has(#top) {
270
272
  scroll-behavior: auto;
271
273
  }
272
274
 
273
- .landing-graph-dash,
274
- .landing-preview-pulse,
275
- .landing-pulse-dot,
276
- .landing-ambient-drift,
277
- .landing-ambient-node-a,
278
- .landing-ambient-node-b,
279
- .landing-ambient-node-c,
280
- .landing-ambient-node-d,
281
- .landing-ambient-node-e,
282
- .landing-ambient-node-f,
283
- .landing-ambient-node-g,
284
- .landing-ambient-node-h {
275
+ html:not([data-reduced-motion="never"]) :is(.landing-graph-dash, .landing-preview-pulse, .landing-pulse-dot, .landing-ambient-drift, .landing-ambient-node-a, .landing-ambient-node-b, .landing-ambient-node-c, .landing-ambient-node-d, .landing-ambient-node-e, .landing-ambient-node-f, .landing-ambient-node-g, .landing-ambient-node-h) {
285
276
  animation: none !important;
286
277
  }
287
278
  }
279
+
280
+ html[data-reduced-motion="always"]:has(#top) {
281
+ scroll-behavior: auto;
282
+ }
283
+
284
+ html[data-reduced-motion="always"] :is(.landing-graph-dash, .landing-preview-pulse, .landing-pulse-dot, .landing-ambient-drift, .landing-ambient-node-a, .landing-ambient-node-b, .landing-ambient-node-c, .landing-ambient-node-d, .landing-ambient-node-e, .landing-ambient-node-f, .landing-ambient-node-g, .landing-ambient-node-h) {
285
+ animation: none !important;
286
+ }
@@ -1,6 +1,8 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { LazyMotion, MotionConfig, domMax } from "motion/react";
3
- import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { useLayoutEffect } from "react";
4
+ import { ReducedMotionContext } from "./reduced-motion.js";
5
+
4
6
  /**
5
7
  * The app-wide Motion setup that `m.*` components (in this package and the
6
8
  * app) need: one lazy feature bundle under `strict`, so a stray `motion.*`
@@ -10,10 +12,13 @@ import { jsx as _jsx } from "react/jsx-runtime";
10
12
  * (`layoutId` shared highlights); `domAnimation` is smaller if the app has
11
13
  * none. Under `strict` a missing feature silently skips the animation.
12
14
  * `reducedMotion` defaults to `"user"` (follow the OS setting); pass `"never"`
13
- * to always animate.
15
+ * to always animate. It governs the whole package, not only `m.*`: dialogs,
16
+ * sheets, the `motion-reduce:` variant and motion.css read it too, through
17
+ * `html[data-reduced-motion]` (stamped here, kept after unmount) and context.
14
18
  */
19
+ import { jsx as _jsx } from "react/jsx-runtime";
15
20
  export function MotionProvider(t0) {
16
- const $ = _c(6);
21
+ const $ = _c(12);
17
22
  const {
18
23
  children,
19
24
  features: t1,
@@ -22,29 +27,56 @@ export function MotionProvider(t0) {
22
27
  const features = t1 === undefined ? domMax : t1;
23
28
  const reducedMotion = t2 === undefined ? "user" : t2;
24
29
  let t3;
25
- if ($[0] !== children || $[1] !== reducedMotion) {
26
- t3 = /*#__PURE__*/_jsx(MotionConfig, {
30
+ let t4;
31
+ if ($[0] !== reducedMotion) {
32
+ t3 = () => {
33
+ document.documentElement.dataset.reducedMotion = reducedMotion;
34
+ };
35
+ t4 = [reducedMotion];
36
+ $[0] = reducedMotion;
37
+ $[1] = t3;
38
+ $[2] = t4;
39
+ } else {
40
+ t3 = $[1];
41
+ t4 = $[2];
42
+ }
43
+ useLayoutEffect(t3, t4);
44
+ let t5;
45
+ if ($[3] !== children || $[4] !== reducedMotion) {
46
+ t5 = /*#__PURE__*/_jsx(MotionConfig, {
27
47
  reducedMotion: reducedMotion,
28
48
  children: children
29
49
  });
30
- $[0] = children;
31
- $[1] = reducedMotion;
32
- $[2] = t3;
50
+ $[3] = children;
51
+ $[4] = reducedMotion;
52
+ $[5] = t5;
33
53
  } else {
34
- t3 = $[2];
54
+ t5 = $[5];
35
55
  }
36
- let t4;
37
- if ($[3] !== features || $[4] !== t3) {
38
- t4 = /*#__PURE__*/_jsx(LazyMotion, {
56
+ let t6;
57
+ if ($[6] !== features || $[7] !== t5) {
58
+ t6 = /*#__PURE__*/_jsx(LazyMotion, {
39
59
  features: features,
40
60
  strict: true,
41
- children: t3
61
+ children: t5
62
+ });
63
+ $[6] = features;
64
+ $[7] = t5;
65
+ $[8] = t6;
66
+ } else {
67
+ t6 = $[8];
68
+ }
69
+ let t7;
70
+ if ($[9] !== reducedMotion || $[10] !== t6) {
71
+ t7 = /*#__PURE__*/_jsx(ReducedMotionContext, {
72
+ value: reducedMotion,
73
+ children: t6
42
74
  });
43
- $[3] = features;
44
- $[4] = t3;
45
- $[5] = t4;
75
+ $[9] = reducedMotion;
76
+ $[10] = t6;
77
+ $[11] = t7;
46
78
  } else {
47
- t4 = $[5];
79
+ t7 = $[11];
48
80
  }
49
- return t4;
81
+ return t7;
50
82
  }
@@ -0,0 +1,50 @@
1
+ import { c as _c } from "react/compiler-runtime";
2
+ import { createContext, use } from "react";
3
+ import { useMediaQuery } from "../hooks/use-media-query.js";
4
+
5
+ /**
6
+ * Whether the package's own motion follows the OS "reduce motion" setting.
7
+ * Set once at the root with `MotionProvider reducedMotion` (the same option as
8
+ * Motion's `MotionConfig` and HeroUI's provider):
9
+ * - `"user"` (default): follow `prefers-reduced-motion`.
10
+ * - `"always"`: always reduce.
11
+ * - `"never"`: always animate; the app owns its own "motion off" switch.
12
+ *
13
+ * `MotionProvider` stamps it on `html[data-reduced-motion]` for CSS and for
14
+ * non-React code, and provides it through context for components.
15
+ */
16
+
17
+ export const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)";
18
+ export const ReducedMotionContext = /*#__PURE__*/createContext("user");
19
+ function resolve(setting, osReduces) {
20
+ if (setting === "never") return false;
21
+ if (setting === "always") return true;
22
+ return osReduces;
23
+ }
24
+ function documentSetting() {
25
+ const value = document.documentElement.dataset.reducedMotion;
26
+ return value === "always" || value === "never" ? value : "user";
27
+ }
28
+
29
+ /** Outside React (event handlers, DOM helpers): reads `html[data-reduced-motion]`. */
30
+ export function prefersReducedMotion() {
31
+ if (typeof window === "undefined") return false;
32
+ return resolve(documentSetting(), window.matchMedia(REDUCED_MOTION_QUERY).matches);
33
+ }
34
+
35
+ /** In a component: the root setting from `MotionProvider`, then the OS query. */
36
+ export function useReducedMotionSetting() {
37
+ const $ = _c(3);
38
+ const setting = use(ReducedMotionContext);
39
+ const osReduces = useMediaQuery(REDUCED_MOTION_QUERY);
40
+ let t0;
41
+ if ($[0] !== osReduces || $[1] !== setting) {
42
+ t0 = resolve(setting, osReduces);
43
+ $[0] = osReduces;
44
+ $[1] = setting;
45
+ $[2] = t0;
46
+ } else {
47
+ t0 = $[2];
48
+ }
49
+ return t0;
50
+ }
@@ -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,11 +1,12 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import * as React from "react";
3
+ import { prefersReducedMotion } from "../motion/reduced-motion.js";
3
4
 
4
5
  /** Phones, plus short landscape viewports such as 844×390. */
5
6
  export const COMPACT_OVERLAY_QUERY = "(max-width: 639px), (max-height: 500px)";
6
- export function prefersReducedMotion() {
7
- return typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
8
- }
7
+
8
+ /** The root `MotionProvider reducedMotion` setting, then the OS query. */
9
+ export { prefersReducedMotion };
9
10
  export function useCompactOverlay() {
10
11
  const $ = _c(2);
11
12
  const [compact, setCompact] = React.useState(false);
@@ -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
@@ -106,22 +106,35 @@
106
106
  }
107
107
  }
108
108
 
109
+ /*
110
+ * Reduced motion follows the root `MotionProvider reducedMotion` setting
111
+ * (`html[data-reduced-motion]`): the OS query unless it is `"never"`, and
112
+ * always when it is `"always"`.
113
+ */
109
114
  @media (prefers-reduced-motion: reduce) {
110
- .ui-page-enter,
111
- .ui-enter,
112
- .ui-enter-up,
113
- .ui-stagger > * {
115
+ html:not([data-reduced-motion="never"]) :is(.ui-page-enter, .ui-enter, .ui-enter-up, .ui-stagger > *) {
114
116
  animation-duration: 1ms;
115
117
  animation-name: ui-fade-only;
116
118
  }
117
119
 
118
- ::view-transition-group(*),
119
- ::view-transition-old(*),
120
- ::view-transition-new(*) {
120
+ html:not([data-reduced-motion="never"])::view-transition-group(*),
121
+ html:not([data-reduced-motion="never"])::view-transition-old(*),
122
+ html:not([data-reduced-motion="never"])::view-transition-new(*) {
121
123
  animation: none !important;
122
124
  }
123
125
  }
124
126
 
127
+ html[data-reduced-motion="always"] :is(.ui-page-enter, .ui-enter, .ui-enter-up, .ui-stagger > *) {
128
+ animation-duration: 1ms;
129
+ animation-name: ui-fade-only;
130
+ }
131
+
132
+ html[data-reduced-motion="always"]::view-transition-group(*),
133
+ html[data-reduced-motion="always"]::view-transition-old(*),
134
+ html[data-reduced-motion="always"]::view-transition-new(*) {
135
+ animation: none !important;
136
+ }
137
+
125
138
  /*
126
139
  * PageMotionProvider `disabled` sets `html[data-page-motion="off"]`. Page and
127
140
  * route enters, list stagger, `animate-in` / `animate-out`, the modal and
@@ -10,6 +10,20 @@
10
10
  * --motion-ease-emphasized: cubic-bezier(0.2, 0.8, 0.2, 1)
11
11
  */
12
12
 
13
+ /* `motion-reduce:` follows the root `MotionProvider reducedMotion` setting
14
+ (`html[data-reduced-motion]`), not only the OS: `"never"` turns it off,
15
+ `"always"` turns it on, `"user"` (or no provider) keeps the media query. */
16
+ @custom-variant motion-reduce {
17
+ @media (prefers-reduced-motion: reduce) {
18
+ html:not([data-reduced-motion="never"]) & {
19
+ @slot;
20
+ }
21
+ }
22
+ html[data-reduced-motion="always"] & {
23
+ @slot;
24
+ }
25
+ }
26
+
13
27
  /* Extends sub-40px controls to a 40x40px tap target without changing layout.
14
28
  Only force `relative` when the element is otherwise static: absolute /
15
29
  fixed / sticky already form a containing block for ::after. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/ui",
3
- "version": "0.51.0",
3
+ "version": "0.53.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",
package/src/client.ts CHANGED
@@ -43,6 +43,7 @@ export { CopyLinkMenuItem } from "./ui/copy-link-menu-item";
43
43
  export { ToggleSearch } from "./ui/toggle-search";
44
44
  export type { ToggleSearchProps, ToggleSearchVariant } from "./ui/toggle-search";
45
45
  export { MotionProvider } from "./motion/motion-provider";
46
+ export { useReducedMotionSetting, type ReducedMotion } from "./motion/reduced-motion";
46
47
  export { MarqueeOnHover } from "./ui/marquee-on-hover";
47
48
  export { RelativeDateTime } from "./ui/relative-date-time";
48
49
  export type { RelativeDateTimeProps } from "./ui/relative-date-time";
package/src/index.ts CHANGED
@@ -39,6 +39,7 @@ export { CopyLinkMenuItem } from "./ui/copy-link-menu-item";
39
39
  export { ToggleSearch } from "./ui/toggle-search";
40
40
  export type { ToggleSearchProps, ToggleSearchVariant } from "./ui/toggle-search";
41
41
  export { MotionProvider } from "./motion/motion-provider";
42
+ export { useReducedMotionSetting, type ReducedMotion } from "./motion/reduced-motion";
42
43
  export { MarqueeOnHover } from "./ui/marquee-on-hover";
43
44
  export { RelativeDateTime } from "./ui/relative-date-time";
44
45
  export type { RelativeDateTimeProps } from "./ui/relative-date-time";
@@ -1,7 +1,7 @@
1
1
  import type { ComponentProps, MouseEventHandler, ReactNode } from "react";
2
2
  import { Button } from "../ui/button";
3
3
  import { pageTitleClass } from "../ui/page-title";
4
- import { useMediaQuery } from "../hooks/use-media-query";
4
+ import { useReducedMotionSetting } from "../motion/reduced-motion";
5
5
  import { cn } from "../utils/cn";
6
6
  import { landingShellClass, productHeroSocialProofClass } from "./marketing-classes";
7
7
  import { PROJECT_V_HOME_URL } from "./projectV";
@@ -63,7 +63,7 @@ export type ProductHeroProps = {
63
63
  const titleClass = `text-balance text-4xl sm:text-5xl lg:text-6xl ${pageTitleClass.replace(/\btext-xl\s*/, "")}`;
64
64
 
65
65
  function UserCountProof({ count }: { count: number }) {
66
- const reducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
66
+ const reducedMotion = useReducedMotionSetting();
67
67
  return (
68
68
  <p className={productHeroSocialProofClass} data-testid="user-count">
69
69
  <span data-testid="user-count-dot" aria-hidden="true" className={cn("size-2 rounded-full bg-success", !reducedMotion && "animate-pulse")} />
@@ -265,23 +265,22 @@ html:has(#top) {
265
265
  }
266
266
  }
267
267
 
268
+ /* Reduced motion follows the root `MotionProvider reducedMotion` setting
269
+ (`html[data-reduced-motion]`), as in motion.css. */
268
270
  @media (prefers-reduced-motion: reduce) {
269
- html:has(#top) {
271
+ html:not([data-reduced-motion="never"]):has(#top) {
270
272
  scroll-behavior: auto;
271
273
  }
272
274
 
273
- .landing-graph-dash,
274
- .landing-preview-pulse,
275
- .landing-pulse-dot,
276
- .landing-ambient-drift,
277
- .landing-ambient-node-a,
278
- .landing-ambient-node-b,
279
- .landing-ambient-node-c,
280
- .landing-ambient-node-d,
281
- .landing-ambient-node-e,
282
- .landing-ambient-node-f,
283
- .landing-ambient-node-g,
284
- .landing-ambient-node-h {
275
+ html:not([data-reduced-motion="never"]) :is(.landing-graph-dash, .landing-preview-pulse, .landing-pulse-dot, .landing-ambient-drift, .landing-ambient-node-a, .landing-ambient-node-b, .landing-ambient-node-c, .landing-ambient-node-d, .landing-ambient-node-e, .landing-ambient-node-f, .landing-ambient-node-g, .landing-ambient-node-h) {
285
276
  animation: none !important;
286
277
  }
287
278
  }
279
+
280
+ html[data-reduced-motion="always"]:has(#top) {
281
+ scroll-behavior: auto;
282
+ }
283
+
284
+ html[data-reduced-motion="always"] :is(.landing-graph-dash, .landing-preview-pulse, .landing-pulse-dot, .landing-ambient-drift, .landing-ambient-node-a, .landing-ambient-node-b, .landing-ambient-node-c, .landing-ambient-node-d, .landing-ambient-node-e, .landing-ambient-node-f, .landing-ambient-node-g, .landing-ambient-node-h) {
285
+ animation: none !important;
286
+ }
@@ -1,5 +1,6 @@
1
1
  import { LazyMotion, MotionConfig, domMax, type FeatureBundle } from "motion/react";
2
- import type { ReactNode } from "react";
2
+ import { useLayoutEffect, type ReactNode } from "react";
3
+ import { ReducedMotionContext, type ReducedMotion } from "./reduced-motion";
3
4
 
4
5
  /**
5
6
  * The app-wide Motion setup that `m.*` components (in this package and the
@@ -10,7 +11,9 @@ import type { ReactNode } from "react";
10
11
  * (`layoutId` shared highlights); `domAnimation` is smaller if the app has
11
12
  * none. Under `strict` a missing feature silently skips the animation.
12
13
  * `reducedMotion` defaults to `"user"` (follow the OS setting); pass `"never"`
13
- * to always animate.
14
+ * to always animate. It governs the whole package, not only `m.*`: dialogs,
15
+ * sheets, the `motion-reduce:` variant and motion.css read it too, through
16
+ * `html[data-reduced-motion]` (stamped here, kept after unmount) and context.
14
17
  */
15
18
  export function MotionProvider({
16
19
  children,
@@ -19,11 +22,17 @@ export function MotionProvider({
19
22
  }: {
20
23
  children: ReactNode;
21
24
  features?: FeatureBundle;
22
- reducedMotion?: "user" | "always" | "never";
25
+ reducedMotion?: ReducedMotion;
23
26
  }) {
27
+ useLayoutEffect(() => {
28
+ document.documentElement.dataset.reducedMotion = reducedMotion;
29
+ }, [reducedMotion]);
30
+
24
31
  return (
25
- <LazyMotion features={features} strict>
26
- <MotionConfig reducedMotion={reducedMotion}>{children}</MotionConfig>
27
- </LazyMotion>
32
+ <ReducedMotionContext value={reducedMotion}>
33
+ <LazyMotion features={features} strict>
34
+ <MotionConfig reducedMotion={reducedMotion}>{children}</MotionConfig>
35
+ </LazyMotion>
36
+ </ReducedMotionContext>
28
37
  );
29
38
  }
@@ -0,0 +1,43 @@
1
+ import { createContext, use } from "react";
2
+ import { useMediaQuery } from "../hooks/use-media-query";
3
+
4
+ /**
5
+ * Whether the package's own motion follows the OS "reduce motion" setting.
6
+ * Set once at the root with `MotionProvider reducedMotion` (the same option as
7
+ * Motion's `MotionConfig` and HeroUI's provider):
8
+ * - `"user"` (default): follow `prefers-reduced-motion`.
9
+ * - `"always"`: always reduce.
10
+ * - `"never"`: always animate; the app owns its own "motion off" switch.
11
+ *
12
+ * `MotionProvider` stamps it on `html[data-reduced-motion]` for CSS and for
13
+ * non-React code, and provides it through context for components.
14
+ */
15
+ export type ReducedMotion = "user" | "always" | "never";
16
+
17
+ export const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)";
18
+
19
+ export const ReducedMotionContext = createContext<ReducedMotion>("user");
20
+
21
+ function resolve(setting: ReducedMotion, osReduces: boolean): boolean {
22
+ if (setting === "never") return false;
23
+ if (setting === "always") return true;
24
+ return osReduces;
25
+ }
26
+
27
+ function documentSetting(): ReducedMotion {
28
+ const value = document.documentElement.dataset.reducedMotion;
29
+ return value === "always" || value === "never" ? value : "user";
30
+ }
31
+
32
+ /** Outside React (event handlers, DOM helpers): reads `html[data-reduced-motion]`. */
33
+ export function prefersReducedMotion(): boolean {
34
+ if (typeof window === "undefined") return false;
35
+ return resolve(documentSetting(), window.matchMedia(REDUCED_MOTION_QUERY).matches);
36
+ }
37
+
38
+ /** In a component: the root setting from `MotionProvider`, then the OS query. */
39
+ export function useReducedMotionSetting(): boolean {
40
+ const setting = use(ReducedMotionContext);
41
+ const osReduces = useMediaQuery(REDUCED_MOTION_QUERY);
42
+ return resolve(setting, osReduces);
43
+ }
@@ -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,14 +1,11 @@
1
1
  import * as React from "react";
2
+ import { prefersReducedMotion } from "../motion/reduced-motion";
2
3
 
3
4
  /** Phones, plus short landscape viewports such as 844×390. */
4
5
  export const COMPACT_OVERLAY_QUERY = "(max-width: 639px), (max-height: 500px)";
5
6
 
6
- export function prefersReducedMotion(): boolean {
7
- return (
8
- typeof window !== "undefined" &&
9
- window.matchMedia("(prefers-reduced-motion: reduce)").matches
10
- );
11
- }
7
+ /** The root `MotionProvider reducedMotion` setting, then the OS query. */
8
+ export { prefersReducedMotion };
12
9
 
13
10
  export function useCompactOverlay(): boolean {
14
11
  const [compact, setCompact] = React.useState(false);
@@ -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
@@ -106,22 +106,35 @@
106
106
  }
107
107
  }
108
108
 
109
+ /*
110
+ * Reduced motion follows the root `MotionProvider reducedMotion` setting
111
+ * (`html[data-reduced-motion]`): the OS query unless it is `"never"`, and
112
+ * always when it is `"always"`.
113
+ */
109
114
  @media (prefers-reduced-motion: reduce) {
110
- .ui-page-enter,
111
- .ui-enter,
112
- .ui-enter-up,
113
- .ui-stagger > * {
115
+ html:not([data-reduced-motion="never"]) :is(.ui-page-enter, .ui-enter, .ui-enter-up, .ui-stagger > *) {
114
116
  animation-duration: 1ms;
115
117
  animation-name: ui-fade-only;
116
118
  }
117
119
 
118
- ::view-transition-group(*),
119
- ::view-transition-old(*),
120
- ::view-transition-new(*) {
120
+ html:not([data-reduced-motion="never"])::view-transition-group(*),
121
+ html:not([data-reduced-motion="never"])::view-transition-old(*),
122
+ html:not([data-reduced-motion="never"])::view-transition-new(*) {
121
123
  animation: none !important;
122
124
  }
123
125
  }
124
126
 
127
+ html[data-reduced-motion="always"] :is(.ui-page-enter, .ui-enter, .ui-enter-up, .ui-stagger > *) {
128
+ animation-duration: 1ms;
129
+ animation-name: ui-fade-only;
130
+ }
131
+
132
+ html[data-reduced-motion="always"]::view-transition-group(*),
133
+ html[data-reduced-motion="always"]::view-transition-old(*),
134
+ html[data-reduced-motion="always"]::view-transition-new(*) {
135
+ animation: none !important;
136
+ }
137
+
125
138
  /*
126
139
  * PageMotionProvider `disabled` sets `html[data-page-motion="off"]`. Page and
127
140
  * route enters, list stagger, `animate-in` / `animate-out`, the modal and
@@ -10,6 +10,20 @@
10
10
  * --motion-ease-emphasized: cubic-bezier(0.2, 0.8, 0.2, 1)
11
11
  */
12
12
 
13
+ /* `motion-reduce:` follows the root `MotionProvider reducedMotion` setting
14
+ (`html[data-reduced-motion]`), not only the OS: `"never"` turns it off,
15
+ `"always"` turns it on, `"user"` (or no provider) keeps the media query. */
16
+ @custom-variant motion-reduce {
17
+ @media (prefers-reduced-motion: reduce) {
18
+ html:not([data-reduced-motion="never"]) & {
19
+ @slot;
20
+ }
21
+ }
22
+ html[data-reduced-motion="always"] & {
23
+ @slot;
24
+ }
25
+ }
26
+
13
27
  /* Extends sub-40px controls to a 40x40px tap target without changing layout.
14
28
  Only force `relative` when the element is otherwise static: absolute /
15
29
  fixed / sticky already form a containing block for ::after. */