@furystack/shades 12.3.0 → 12.4.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.
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Configuration for the View Transition API integration.
3
+ * When provided as an object, allows specifying transition types for CSS targeting
4
+ * via the `:active-view-transition-type()` pseudo-class.
5
+ */
6
+ export type ViewTransitionConfig = {
7
+ types?: string[]
8
+ }
9
+
10
+ /**
11
+ * Wraps a DOM update in `document.startViewTransition()` when the View Transition API
12
+ * is available and `config` is truthy. Falls back to calling `update()` directly otherwise.
13
+ *
14
+ * Returns the `updateCallbackDone` promise when a transition is started, allowing callers
15
+ * that need to wait for the DOM update (e.g. to run lifecycle hooks) to `await` the result.
16
+ * Returns `undefined` when no transition is used (the update runs synchronously).
17
+ *
18
+ * @param config - The view transition configuration (boolean or object with types)
19
+ * @param update - The synchronous DOM update callback
20
+ * @returns A promise that resolves after the update callback completes inside the transition, or `undefined`
21
+ */
22
+ export const maybeViewTransition = (
23
+ config: boolean | ViewTransitionConfig | undefined,
24
+ update: () => void,
25
+ ): Promise<void> | undefined => {
26
+ if (config && document.startViewTransition) {
27
+ const types = typeof config === 'object' && config.types?.length ? config.types : undefined
28
+ const transition = types ? document.startViewTransition({ update, types }) : document.startViewTransition(update)
29
+ return transition.updateCallbackDone
30
+ } else {
31
+ update()
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Keeps a "displayed" copy of `value` that is updated through a view transition
37
+ * whenever the value changes and `shouldTransition` returns true.
38
+ *
39
+ * When the transition is skipped (config is falsy or `shouldTransition` returns false),
40
+ * the displayed value is updated synchronously without an animation.
41
+ *
42
+ * @param useState - The component's `useState` hook
43
+ * @param key - Unique state key for caching the displayed value
44
+ * @param value - The latest source value (e.g. from `useObservable` or derived from props)
45
+ * @param config - View transition configuration forwarded to `maybeViewTransition`
46
+ * @param shouldTransition - Predicate that decides whether a value change warrants a transition.
47
+ * Defaults to `() => true` (always transition).
48
+ * @returns The currently displayed value
49
+ */
50
+ export const transitionedValue = <T>(
51
+ useState: <S>(key: string, initialValue: S) => [S, (v: S) => void],
52
+ key: string,
53
+ value: T,
54
+ config: boolean | ViewTransitionConfig | undefined,
55
+ shouldTransition: (prev: T, next: T) => boolean = () => true,
56
+ ): T => {
57
+ const [displayed, setDisplayed] = useState(key, value)
58
+ if (value !== displayed) {
59
+ if (shouldTransition(displayed, value)) {
60
+ void maybeViewTransition(config, () => setDisplayed(value))
61
+ } else {
62
+ setDisplayed(value)
63
+ }
64
+ }
65
+ return displayed
66
+ }