@patterninc/pattern-ui 0.0.0-main.202610062317.51bfdacb98b3 → 0.0.0-main.202610070652.6ff70dca923d

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,4 +1,4 @@
1
- import { createThemeAnimation, updateThemeStyles } from "./theme-transition.js";
1
+ import { startThemeTransition } from "./theme-transition.js";
2
2
  import { c } from "react/compiler-runtime";
3
3
  import { jsx } from "react/jsx-runtime";
4
4
  import React, { createContext, useContext, useEffect } from "react";
@@ -58,9 +58,7 @@ function ThemeProvider(t0) {
58
58
  let t4;
59
59
  if ($[4] !== setTernaryDarkMode) {
60
60
  t4 = (newTheme) => {
61
- const animationCss = createThemeAnimation("bottom-up", false);
62
- updateThemeStyles(animationCss);
63
- document.startViewTransition(() => {
61
+ startThemeTransition(() => {
64
62
  applyThemeClass(resolveEffectiveTheme(newTheme) === "dark");
65
63
  setTernaryDarkMode(newTheme);
66
64
  });
@@ -1,7 +1,17 @@
1
1
  //#region src/utils/theme-transition.ts
2
2
  const STYLE_ID = "pattern-ui-theme-transition-styles";
3
+ /**
4
+ * Whether the theme change can be animated: the View Transition API must be available and the user
5
+ * must not have requested reduced motion.
6
+ */
7
+ function canAnimateThemeChange() {
8
+ if (typeof document === "undefined" || typeof window === "undefined") return false;
9
+ if (typeof document.startViewTransition !== "function") return false;
10
+ if (typeof window.matchMedia !== "function") return false;
11
+ return !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
12
+ }
3
13
  /** Returns clip-path from/to values for the given direction. */
4
- const getRectangleClipPath = (direction) => {
14
+ function getRectangleClipPath(direction) {
5
15
  switch (direction) {
6
16
  case "bottom-up": return {
7
17
  from: "polygon(0% 100%, 100% 100%, 100% 100%, 0% 100%)",
@@ -40,12 +50,12 @@ const getRectangleClipPath = (direction) => {
40
50
  to: "polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%)"
41
51
  };
42
52
  }
43
- };
53
+ }
44
54
  /**
45
55
  * Generates the CSS string for the View Transition API animation. The returned CSS should be
46
56
  * injected into the document with `updateThemeStyles`.
47
57
  */
48
- const createThemeAnimation = (start = "bottom-up", blur = false) => {
58
+ function createThemeAnimation(start = "bottom-up", blur = false) {
49
59
  const clipPath = getRectangleClipPath(start);
50
60
  const suffix = `${start}${blur ? "-blur" : ""}`;
51
61
  return `
@@ -92,12 +102,12 @@ const createThemeAnimation = (start = "bottom-up", blur = false) => {
92
102
  }
93
103
  }
94
104
  `;
95
- };
105
+ }
96
106
  /**
97
107
  * Injects (or updates) the theme transition CSS into the document `<head>`. Call this before
98
108
  * `document.startViewTransition` so the animation is ready.
99
109
  */
100
- const updateThemeStyles = (css) => {
110
+ function updateThemeStyles(css) {
101
111
  if (typeof window === "undefined") return;
102
112
  let el = document.getElementById(STYLE_ID);
103
113
  if (!el) {
@@ -106,7 +116,7 @@ const updateThemeStyles = (css) => {
106
116
  document.head.appendChild(el);
107
117
  }
108
118
  el.textContent = css;
109
- };
119
+ }
110
120
  /**
111
121
  * High-level helper: injects the animation CSS, then runs `callback` inside
112
122
  * `document.startViewTransition` (if available) so the theme change animates.
@@ -123,12 +133,16 @@ const updateThemeStyles = (css) => {
123
133
  * class on `<html>`).
124
134
  * @param options Optional direction and blur settings.
125
135
  */
126
- const startThemeTransition = (callback, options = {}) => {
136
+ function startThemeTransition(callback, options = {}) {
127
137
  const { direction = "bottom-up", blur = false } = options;
138
+ if (!canAnimateThemeChange()) {
139
+ if (typeof document !== "undefined") document.getElementById(STYLE_ID)?.remove();
140
+ callback();
141
+ return;
142
+ }
128
143
  updateThemeStyles(createThemeAnimation(direction, blur));
129
- if (typeof document !== "undefined" && "startViewTransition" in document) document.startViewTransition(callback);
130
- else callback();
131
- };
144
+ document.startViewTransition(callback);
145
+ }
132
146
 
133
147
  //#endregion
134
148
  export { createThemeAnimation, startThemeTransition, updateThemeStyles };
@@ -19,13 +19,18 @@ function useExpandableHeight() {
19
19
  let frameId = requestAnimationFrame(() => {
20
20
  frameId = requestAnimationFrame(measure);
21
21
  });
22
+ let resizeFrameId = null;
22
23
  const resizeObserver = new ResizeObserver(() => {
23
- if (contentRef.current) requestAnimationFrame(measure);
24
+ if (contentRef.current) {
25
+ if (resizeFrameId !== null) cancelAnimationFrame(resizeFrameId);
26
+ resizeFrameId = requestAnimationFrame(measure);
27
+ }
24
28
  });
25
29
  resizeObserver.observe(element);
26
30
  return () => {
27
31
  resizeObserver.disconnect();
28
32
  if (frameId !== null) cancelAnimationFrame(frameId);
33
+ if (resizeFrameId !== null) cancelAnimationFrame(resizeFrameId);
29
34
  };
30
35
  }, []);
31
36
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patterninc/pattern-ui",
3
- "version": "0.0.0-main.202610062317.51bfdacb98b3",
3
+ "version": "0.0.0-main.202610070652.6ff70dca923d",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/patterninc/pattern-exp.git",
@@ -249,10 +249,6 @@
249
249
  "types": "./src/components/ElementComment/ElementComment.tsx",
250
250
  "default": "./dist/components/ElementComment/ElementComment.js"
251
251
  },
252
- "./element-component": {
253
- "types": "./src/components/ElementComponent/ElementComponent.tsx",
254
- "default": "./dist/components/ElementComponent/ElementComponent.js"
255
- },
256
252
  "./feedback-mode": {
257
253
  "types": "./src/components/FeedbackWidget/FeedbackMode.tsx",
258
254
  "default": "./dist/components/FeedbackWidget/FeedbackMode.js"
@@ -614,6 +610,10 @@
614
610
  "types": "./src/ai-elements/ThinkingLoader/thinking-loader.tsx",
615
611
  "default": "./dist/ai-elements/ThinkingLoader/thinking-loader.js"
616
612
  },
613
+ "./thumbnail": {
614
+ "types": "./src/components/Thumbnail/Thumbnail.tsx",
615
+ "default": "./dist/components/Thumbnail/Thumbnail.js"
616
+ },
617
617
  "./title-pill": {
618
618
  "types": "./src/components/TitlePill/TitlePill.tsx",
619
619
  "default": "./dist/components/TitlePill/TitlePill.js"
@@ -1,7 +1,7 @@
1
1
  import React, { createContext, useContext, useEffect } from 'react'
2
2
  import { useTernaryDarkMode } from 'usehooks-ts'
3
3
 
4
- import { createThemeAnimation, updateThemeStyles } from './theme-transition'
4
+ import { startThemeTransition } from './theme-transition'
5
5
 
6
6
  export type Theme = 'light' | 'dark' | 'system'
7
7
 
@@ -62,10 +62,7 @@ export function ThemeProvider({ children }: ThemeProviderProps): React.ReactElem
62
62
  }, [isDarkMode])
63
63
 
64
64
  const setTheme = (newTheme: Theme): void => {
65
- const animationCss = createThemeAnimation('bottom-up', false)
66
- updateThemeStyles(animationCss)
67
-
68
- document.startViewTransition(() => {
65
+ startThemeTransition(() => {
69
66
  // Apply the theme class synchronously inside the transition callback so the
70
67
  // View Transition API can capture the change.
71
68
  const newEffectiveTheme = resolveEffectiveTheme(newTheme)
@@ -28,8 +28,19 @@ export interface ThemeTransitionOptions {
28
28
 
29
29
  const STYLE_ID = 'pattern-ui-theme-transition-styles'
30
30
 
31
+ /**
32
+ * Whether the theme change can be animated: the View Transition API must be available and the user
33
+ * must not have requested reduced motion.
34
+ */
35
+ function canAnimateThemeChange(): boolean {
36
+ if (typeof document === 'undefined' || typeof window === 'undefined') return false
37
+ if (typeof document.startViewTransition !== 'function') return false
38
+ if (typeof window.matchMedia !== 'function') return false
39
+ return !window.matchMedia('(prefers-reduced-motion: reduce)').matches
40
+ }
41
+
31
42
  /** Returns clip-path from/to values for the given direction. */
32
- const getRectangleClipPath = (direction: AnimationStart): { from: string; to: string } => {
43
+ function getRectangleClipPath(direction: AnimationStart): { from: string; to: string } {
33
44
  switch (direction) {
34
45
  case 'bottom-up':
35
46
  return {
@@ -83,7 +94,7 @@ const getRectangleClipPath = (direction: AnimationStart): { from: string; to: st
83
94
  * Generates the CSS string for the View Transition API animation. The returned CSS should be
84
95
  * injected into the document with `updateThemeStyles`.
85
96
  */
86
- export const createThemeAnimation = (start: AnimationStart = 'bottom-up', blur = false): string => {
97
+ export function createThemeAnimation(start: AnimationStart = 'bottom-up', blur = false): string {
87
98
  const clipPath = getRectangleClipPath(start)
88
99
  const suffix = `${start}${blur ? '-blur' : ''}`
89
100
 
@@ -137,7 +148,7 @@ export const createThemeAnimation = (start: AnimationStart = 'bottom-up', blur =
137
148
  * Injects (or updates) the theme transition CSS into the document `<head>`. Call this before
138
149
  * `document.startViewTransition` so the animation is ready.
139
150
  */
140
- export const updateThemeStyles = (css: string): void => {
151
+ export function updateThemeStyles(css: string): void {
141
152
  if (typeof window === 'undefined') return
142
153
 
143
154
  let el = document.getElementById(STYLE_ID) as HTMLStyleElement | null
@@ -165,17 +176,19 @@ export const updateThemeStyles = (css: string): void => {
165
176
  * class on `<html>`).
166
177
  * @param options Optional direction and blur settings.
167
178
  */
168
- export const startThemeTransition = (
179
+ export function startThemeTransition(
169
180
  callback: () => void,
170
181
  options: ThemeTransitionOptions = {},
171
- ): void => {
182
+ ): void {
172
183
  const { direction = 'bottom-up', blur = false } = options
173
184
 
174
- updateThemeStyles(createThemeAnimation(direction, blur))
175
-
176
- if (typeof document !== 'undefined' && 'startViewTransition' in document) {
177
- document.startViewTransition(callback)
178
- } else {
185
+ if (!canAnimateThemeChange()) {
186
+ // Drop CSS injected by an earlier animated switch so it can't animate later transitions
187
+ if (typeof document !== 'undefined') document.getElementById(STYLE_ID)?.remove()
179
188
  callback()
189
+ return
180
190
  }
191
+
192
+ updateThemeStyles(createThemeAnimation(direction, blur))
193
+ document.startViewTransition(callback)
181
194
  }
@@ -25,9 +25,11 @@ export function useExpandableHeight() {
25
25
  frameId = requestAnimationFrame(measure)
26
26
  })
27
27
 
28
+ let resizeFrameId: number | null = null
28
29
  const resizeObserver = new ResizeObserver(() => {
29
30
  if (contentRef.current) {
30
- requestAnimationFrame(measure)
31
+ if (resizeFrameId !== null) cancelAnimationFrame(resizeFrameId)
32
+ resizeFrameId = requestAnimationFrame(measure)
31
33
  }
32
34
  })
33
35
 
@@ -36,6 +38,7 @@ export function useExpandableHeight() {
36
38
  return () => {
37
39
  resizeObserver.disconnect()
38
40
  if (frameId !== null) cancelAnimationFrame(frameId)
41
+ if (resizeFrameId !== null) cancelAnimationFrame(resizeFrameId)
39
42
  }
40
43
  }, [])
41
44