@once-ui-system/fx 3.0.0-beta.0 → 3.0.0-beta.3
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/dist/CelebrationFx.d.ts +24 -4
- package/dist/CelebrationFx.js +444 -272
- package/dist/GlitchFx.module.css +3 -0
- package/dist/HoloFx.module.css +3 -0
- package/dist/MatrixFx.js +7 -2
- package/dist/RevealFx.module.css +3 -0
- package/dist/ShineFx.module.css +3 -0
- package/dist/TiltFx.module.css +3 -0
- package/dist/WeatherFx.d.ts +25 -4
- package/dist/WeatherFx.js +471 -818
- package/dist/internal/FadingLetters.module.css +3 -0
- package/dist/internal/canvasStage.d.ts +61 -0
- package/dist/internal/canvasStage.js +163 -0
- package/dist/internal/cssColor.js +10 -1
- package/dist/internal/useControllableState.d.ts +14 -0
- package/dist/internal/useControllableState.js +35 -0
- package/package.json +3 -2
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
@layer properties, theme, base, once-ui, components, utilities;
|
|
2
|
+
@layer once-ui {
|
|
1
3
|
/*
|
|
2
4
|
* Every animated property here is one the compositor can run on its own:
|
|
3
5
|
* `transform` and `opacity`, nothing else.
|
|
@@ -222,3 +224,4 @@
|
|
|
222
224
|
animation: lightOut 1600ms linear forwards;
|
|
223
225
|
animation-delay: var(--exit-delay, 0s);
|
|
224
226
|
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The frame loop CelebrationFx and WeatherFx share: a 2D canvas sized to its
|
|
3
|
+
* container at the device's pixel ratio (capped at 2), redrawn with the time
|
|
4
|
+
* since the last frame in seconds, so motion runs at the same pace on a 60 Hz
|
|
5
|
+
* and a 120 Hz screen.
|
|
6
|
+
*
|
|
7
|
+
* The loop sleeps when there is nothing to draw (`frame` returns false), while
|
|
8
|
+
* the container is off screen, and after `stop()`. `wake()` starts it again;
|
|
9
|
+
* call it from events and effects, never from inside `frame`.
|
|
10
|
+
*/
|
|
11
|
+
export interface Stage {
|
|
12
|
+
readonly ctx: CanvasRenderingContext2D;
|
|
13
|
+
/** CSS pixels. */
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
/** Device pixels per CSS pixel, 1 to 2. */
|
|
17
|
+
dpr: number;
|
|
18
|
+
/** Whether the page behind is dark: glows add light there and stay flat on a light page. */
|
|
19
|
+
dark: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface StageOptions {
|
|
22
|
+
/** Draws one frame; `dt` is in seconds, at most 1/20 s. Returns whether to keep running. */
|
|
23
|
+
frame: (stage: Stage, dt: number) => boolean;
|
|
24
|
+
/** Runs after the canvas changes size, before the next frame. */
|
|
25
|
+
resize?: (stage: Stage) => void;
|
|
26
|
+
}
|
|
27
|
+
export interface StageHandle {
|
|
28
|
+
readonly stage: Stage;
|
|
29
|
+
wake: () => void;
|
|
30
|
+
/** Draws one frame now without starting the loop (the still frame under reduced motion). */
|
|
31
|
+
paint: () => void;
|
|
32
|
+
stop: () => void;
|
|
33
|
+
}
|
|
34
|
+
export declare function createStage(canvas: HTMLCanvasElement, container: HTMLElement, { frame, resize }: StageOptions): StageHandle | null;
|
|
35
|
+
/**
|
|
36
|
+
* A colour prop as a value the canvas can paint. Read through the same
|
|
37
|
+
* resolver as every other colour prop (`brand-weak`, `--x`, `#fff`, `oklch(…)`
|
|
38
|
+
* all work), then a `var(--token)` is looked up on `element`, because a canvas
|
|
39
|
+
* cannot paint `var()`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function canvasColor(element: Element, value: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Whether the page around `element` is dark, read from `--page-background`
|
|
44
|
+
* through a 1×1 canvas (any CSS colour syntax works). Unknown reads as dark.
|
|
45
|
+
*/
|
|
46
|
+
export declare function isDarkBackground(element: Element): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Calls `onChange` when the theme around the effect may have changed: a
|
|
49
|
+
* `data-*`, `class` or `style` change on the root element, or the system
|
|
50
|
+
* colour scheme. The effects read their colours again so a theme switch
|
|
51
|
+
* repaints them.
|
|
52
|
+
*/
|
|
53
|
+
export declare function watchTheme(onChange: () => void): () => void;
|
|
54
|
+
/** A random number in [min, max). */
|
|
55
|
+
export declare const rand: (min: number, max: number) => number;
|
|
56
|
+
/** A random whole number in [0, length). */
|
|
57
|
+
export declare const pick: (length: number) => number;
|
|
58
|
+
/**
|
|
59
|
+
* `intensity` as a whole number in [0, max]; NaN and negatives are 0.
|
|
60
|
+
*/
|
|
61
|
+
export declare const clampCount: (value: number, max: number) => number;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { cssColor } from "./cssColor.js";
|
|
2
|
+
const MAX_DT = 1 / 20;
|
|
3
|
+
export function createStage(canvas, container, { frame, resize }) {
|
|
4
|
+
const ctx = canvas.getContext("2d");
|
|
5
|
+
if (!ctx)
|
|
6
|
+
return null;
|
|
7
|
+
const stage = { ctx, width: 0, height: 0, dpr: 1, dark: true };
|
|
8
|
+
let raf;
|
|
9
|
+
let last;
|
|
10
|
+
let visible = true;
|
|
11
|
+
let stopped = false;
|
|
12
|
+
const measure = () => {
|
|
13
|
+
const rect = container.getBoundingClientRect();
|
|
14
|
+
const dpr = Math.min(2, Math.max(1, window.devicePixelRatio || 1));
|
|
15
|
+
if (rect.width === stage.width && rect.height === stage.height && dpr === stage.dpr)
|
|
16
|
+
return;
|
|
17
|
+
stage.width = rect.width;
|
|
18
|
+
stage.height = rect.height;
|
|
19
|
+
stage.dpr = dpr;
|
|
20
|
+
canvas.width = Math.round(rect.width * dpr);
|
|
21
|
+
canvas.height = Math.round(rect.height * dpr);
|
|
22
|
+
resize?.(stage);
|
|
23
|
+
};
|
|
24
|
+
const tick = (now) => {
|
|
25
|
+
raf = undefined;
|
|
26
|
+
if (stopped || !visible)
|
|
27
|
+
return;
|
|
28
|
+
const dt = last === undefined ? 1 / 60 : Math.min(MAX_DT, Math.max(0, (now - last) / 1000));
|
|
29
|
+
last = now;
|
|
30
|
+
ctx.setTransform(stage.dpr, 0, 0, stage.dpr, 0, 0);
|
|
31
|
+
ctx.clearRect(0, 0, stage.width, stage.height);
|
|
32
|
+
const keep = frame(stage, dt);
|
|
33
|
+
ctx.globalAlpha = 1;
|
|
34
|
+
ctx.globalCompositeOperation = "source-over";
|
|
35
|
+
// Asleep, the canvas keeps what this frame drew: nothing once everything
|
|
36
|
+
// has settled, the still frame under reduced motion.
|
|
37
|
+
if (keep)
|
|
38
|
+
raf = requestAnimationFrame(tick);
|
|
39
|
+
else
|
|
40
|
+
last = undefined;
|
|
41
|
+
};
|
|
42
|
+
const wake = () => {
|
|
43
|
+
if (stopped || !visible || raf !== undefined)
|
|
44
|
+
return;
|
|
45
|
+
last = undefined;
|
|
46
|
+
raf = requestAnimationFrame(tick);
|
|
47
|
+
};
|
|
48
|
+
const paint = () => {
|
|
49
|
+
if (stopped)
|
|
50
|
+
return;
|
|
51
|
+
ctx.setTransform(stage.dpr, 0, 0, stage.dpr, 0, 0);
|
|
52
|
+
ctx.clearRect(0, 0, stage.width, stage.height);
|
|
53
|
+
frame(stage, 0);
|
|
54
|
+
ctx.globalAlpha = 1;
|
|
55
|
+
ctx.globalCompositeOperation = "source-over";
|
|
56
|
+
};
|
|
57
|
+
measure();
|
|
58
|
+
// A new size clears the canvas; with the loop asleep (a still frame), draw it again.
|
|
59
|
+
const remeasure = () => {
|
|
60
|
+
const before = canvas.width * 10000 + canvas.height;
|
|
61
|
+
measure();
|
|
62
|
+
if (raf === undefined && !stopped && canvas.width * 10000 + canvas.height !== before)
|
|
63
|
+
paint();
|
|
64
|
+
};
|
|
65
|
+
let resizeObserver;
|
|
66
|
+
const onWindowResize = () => remeasure();
|
|
67
|
+
if (typeof ResizeObserver !== "undefined") {
|
|
68
|
+
resizeObserver = new ResizeObserver(() => remeasure());
|
|
69
|
+
resizeObserver.observe(container);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
window.addEventListener("resize", onWindowResize);
|
|
73
|
+
}
|
|
74
|
+
// Off screen, the loop sleeps. Until the observer says otherwise the effect
|
|
75
|
+
// counts as visible, so a browser (or test) that never reports still draws.
|
|
76
|
+
let intersection;
|
|
77
|
+
if (typeof IntersectionObserver !== "undefined") {
|
|
78
|
+
intersection = new IntersectionObserver((entries) => {
|
|
79
|
+
const entry = entries[entries.length - 1];
|
|
80
|
+
if (!entry)
|
|
81
|
+
return;
|
|
82
|
+
visible = entry.isIntersecting;
|
|
83
|
+
if (visible)
|
|
84
|
+
wake();
|
|
85
|
+
else if (raf !== undefined) {
|
|
86
|
+
cancelAnimationFrame(raf);
|
|
87
|
+
raf = undefined;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
intersection.observe(container);
|
|
91
|
+
}
|
|
92
|
+
const stop = () => {
|
|
93
|
+
stopped = true;
|
|
94
|
+
if (raf !== undefined)
|
|
95
|
+
cancelAnimationFrame(raf);
|
|
96
|
+
raf = undefined;
|
|
97
|
+
resizeObserver?.disconnect();
|
|
98
|
+
intersection?.disconnect();
|
|
99
|
+
window.removeEventListener("resize", onWindowResize);
|
|
100
|
+
};
|
|
101
|
+
return { stage, wake, paint, stop };
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* A colour prop as a value the canvas can paint. Read through the same
|
|
105
|
+
* resolver as every other colour prop (`brand-weak`, `--x`, `#fff`, `oklch(…)`
|
|
106
|
+
* all work), then a `var(--token)` is looked up on `element`, because a canvas
|
|
107
|
+
* cannot paint `var()`.
|
|
108
|
+
*/
|
|
109
|
+
export function canvasColor(element, value) {
|
|
110
|
+
const css = cssColor(value);
|
|
111
|
+
const token = /^var\((--[\w-]+)\)$/.exec(css);
|
|
112
|
+
if (!token)
|
|
113
|
+
return css;
|
|
114
|
+
const resolved = getComputedStyle(element).getPropertyValue(token[1]).trim();
|
|
115
|
+
return resolved || "currentColor";
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Whether the page around `element` is dark, read from `--page-background`
|
|
119
|
+
* through a 1×1 canvas (any CSS colour syntax works). Unknown reads as dark.
|
|
120
|
+
*/
|
|
121
|
+
export function isDarkBackground(element) {
|
|
122
|
+
const color = canvasColor(element, "page-background");
|
|
123
|
+
if (color === "currentColor")
|
|
124
|
+
return true;
|
|
125
|
+
const probe = document.createElement("canvas");
|
|
126
|
+
probe.width = 1;
|
|
127
|
+
probe.height = 1;
|
|
128
|
+
const ctx = probe.getContext("2d", { willReadFrequently: true });
|
|
129
|
+
if (!ctx || typeof ctx.getImageData !== "function")
|
|
130
|
+
return true;
|
|
131
|
+
ctx.fillStyle = color;
|
|
132
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
133
|
+
const data = ctx.getImageData(0, 0, 1, 1)?.data;
|
|
134
|
+
if (!data || data[3] === 0)
|
|
135
|
+
return true;
|
|
136
|
+
const luminance = (0.2126 * data[0] + 0.7152 * data[1] + 0.0722 * data[2]) / 255;
|
|
137
|
+
return luminance < 0.5;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Calls `onChange` when the theme around the effect may have changed: a
|
|
141
|
+
* `data-*`, `class` or `style` change on the root element, or the system
|
|
142
|
+
* colour scheme. The effects read their colours again so a theme switch
|
|
143
|
+
* repaints them.
|
|
144
|
+
*/
|
|
145
|
+
export function watchTheme(onChange) {
|
|
146
|
+
const root = document.documentElement;
|
|
147
|
+
const observer = typeof MutationObserver !== "undefined" ? new MutationObserver(() => onChange()) : undefined;
|
|
148
|
+
observer?.observe(root, { attributes: true, attributeFilter: ["class", "style", "data-theme"] });
|
|
149
|
+
const scheme = window.matchMedia?.("(prefers-color-scheme: dark)");
|
|
150
|
+
scheme?.addEventListener?.("change", onChange);
|
|
151
|
+
return () => {
|
|
152
|
+
observer?.disconnect();
|
|
153
|
+
scheme?.removeEventListener?.("change", onChange);
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/** A random number in [min, max). */
|
|
157
|
+
export const rand = (min, max) => min + Math.random() * (max - min);
|
|
158
|
+
/** A random whole number in [0, length). */
|
|
159
|
+
export const pick = (length) => Math.floor(Math.random() * length);
|
|
160
|
+
/**
|
|
161
|
+
* `intensity` as a whole number in [0, max]; NaN and negatives are 0.
|
|
162
|
+
*/
|
|
163
|
+
export const clampCount = (value, max) => Number.isFinite(value) ? Math.round(Math.min(max, Math.max(0, value))) : 0;
|
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
* `var(--brand-on-background-weak)`.
|
|
17
17
|
*
|
|
18
18
|
* With a `prefix` (the charts pass `data-`), a palette hue is that palette's
|
|
19
|
-
* token first: `blue` is `var(--data-blue)`, not the CSS colour `blue
|
|
19
|
+
* token first: `blue` is `var(--data-blue)`, not the CSS colour `blue`; and
|
|
20
|
+
* `brand` or `accent` is the theme's colour at the palette's step,
|
|
21
|
+
* `var(--function-brand-400)`.
|
|
20
22
|
*
|
|
21
23
|
* 2.x wrapped every value in `var(--…)`, so `#fff` became `var(--#fff)` and
|
|
22
24
|
* `brand-weak` became `var(--brand-weak)`, neither of which paints anything.
|
|
@@ -43,6 +45,8 @@ const NAMED = new Set(("aliceblue antiquewhite aqua aquamarine azure beige bisqu
|
|
|
43
45
|
"steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen").split(" "));
|
|
44
46
|
/** The `--data-*` palette the charts and gauges draw from. */
|
|
45
47
|
const DATA_HUES = new Set("blue indigo violet magenta pink red orange yellow moss green emerald aqua cyan gray contrast".split(" "));
|
|
48
|
+
/** The theme's own colours, which a chart takes as hues: `brand`, `accent`. */
|
|
49
|
+
const THEME_HUES = new Set(["brand", "accent"]);
|
|
46
50
|
const TONE_WEIGHT = /^(neutral|brand|accent|info|danger|warning|success)-(weak|medium|strong)$/;
|
|
47
51
|
/** A value CSS can paint as written, with no token lookup. */
|
|
48
52
|
export function isRawColor(value) {
|
|
@@ -57,6 +61,11 @@ export function cssColor(value, prefix = "") {
|
|
|
57
61
|
const v = value.trim();
|
|
58
62
|
if (prefix && DATA_HUES.has(v))
|
|
59
63
|
return `var(--${prefix}${v})`;
|
|
64
|
+
// In a chart, `brand` and `accent` are the theme's colours at the step the
|
|
65
|
+
// palette hues are drawn at, so they sit with them in light and dark. A bare
|
|
66
|
+
// tone was `var(--data-brand)` before, which no theme defines.
|
|
67
|
+
if (prefix && THEME_HUES.has(v))
|
|
68
|
+
return `var(--function-${v}-400)`;
|
|
60
69
|
if (isRawColor(v))
|
|
61
70
|
return v;
|
|
62
71
|
if (v.startsWith("--"))
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one value contract every stateful component follows in 3.0: state `X`
|
|
3
|
+
* is `X` / `defaultX` / `onXChange(next)`.
|
|
4
|
+
*
|
|
5
|
+
* The component is controlled when `value` is defined, never because a
|
|
6
|
+
* handler was passed. Uncontrolled, it keeps its own state starting from
|
|
7
|
+
* `defaultValue`. Either way `onChange` hears every change the component
|
|
8
|
+
* makes, and only those: a parent changing `value` does not call it back.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useControllableState<T>({ value, defaultValue, onChange, }: {
|
|
11
|
+
value: T | undefined;
|
|
12
|
+
defaultValue: T;
|
|
13
|
+
onChange?: (next: T) => void;
|
|
14
|
+
}): [T, (next: T | ((prev: T) => T)) => void];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// A copy of core's internal `src/internal/useControllableState.ts` (CelebrationFx and
|
|
2
|
+
// WeatherFx use it here); `__tests__/internal-parity.test.ts` holds the two word for word.
|
|
3
|
+
"use client";
|
|
4
|
+
import { useCallback, useRef, useState } from "react";
|
|
5
|
+
/**
|
|
6
|
+
* The one value contract every stateful component follows in 3.0: state `X`
|
|
7
|
+
* is `X` / `defaultX` / `onXChange(next)`.
|
|
8
|
+
*
|
|
9
|
+
* The component is controlled when `value` is defined, never because a
|
|
10
|
+
* handler was passed. Uncontrolled, it keeps its own state starting from
|
|
11
|
+
* `defaultValue`. Either way `onChange` hears every change the component
|
|
12
|
+
* makes, and only those: a parent changing `value` does not call it back.
|
|
13
|
+
*/
|
|
14
|
+
export function useControllableState({ value, defaultValue, onChange, }) {
|
|
15
|
+
const [own, setOwn] = useState(defaultValue);
|
|
16
|
+
const controlled = value !== undefined;
|
|
17
|
+
const current = controlled ? value : own;
|
|
18
|
+
const latest = useRef({ current, controlled, onChange });
|
|
19
|
+
latest.current = { current, controlled, onChange };
|
|
20
|
+
const set = useCallback((next) => {
|
|
21
|
+
const { current: prev, controlled: isControlled, onChange: notify } = latest.current;
|
|
22
|
+
const resolved = typeof next === "function" ? next(prev) : next;
|
|
23
|
+
if (Object.is(resolved, prev))
|
|
24
|
+
return;
|
|
25
|
+
// Uncontrolled, two changes in one event build on each other. Controlled,
|
|
26
|
+
// the parent's `value` stays the truth: a parent that declines a change
|
|
27
|
+
// (keeps `value` as it was) hears the same request again next time.
|
|
28
|
+
if (!isControlled) {
|
|
29
|
+
setOwn(resolved);
|
|
30
|
+
latest.current.current = resolved;
|
|
31
|
+
}
|
|
32
|
+
notify?.(resolved);
|
|
33
|
+
}, []);
|
|
34
|
+
return [current, set];
|
|
35
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@once-ui-system/fx",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.3",
|
|
4
4
|
"description": "Once UI effects: RevealFx, CountFx, MatrixFx and the other motion and decoration components, on top of @once-ui-system/core.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"once-ui",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://docs.once-ui.com",
|
|
24
24
|
"publishConfig": {
|
|
25
|
-
"access": "public"
|
|
25
|
+
"access": "public",
|
|
26
|
+
"tag": "next"
|
|
26
27
|
},
|
|
27
28
|
"type": "module",
|
|
28
29
|
"sideEffects": [
|