@m3ui-vue/m3ui-vue 1.2.1 → 1.2.2
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/API.md +152 -11
- package/dist/{MMenuItem-DI351kIf.js → MMenuItem-D4Gx_l3F.js} +2 -2
- package/dist/{MMenuItem-DI351kIf.js.map → MMenuItem-D4Gx_l3F.js.map} +1 -1
- package/dist/{MSlider-BnEy5ieZ.js → MSlider-Dx_S3vJ9.js} +227 -221
- package/dist/{MSlider-BnEy5ieZ.js.map → MSlider-Dx_S3vJ9.js.map} +1 -1
- package/dist/{MTextField-BBmkaIbO.js → MTextField-Bz8JIsnl.js} +4 -4
- package/dist/{MTextField-BBmkaIbO.js.map → MTextField-Bz8JIsnl.js.map} +1 -1
- package/dist/api.json +1 -1
- package/dist/chart.js +1 -1
- package/dist/components/MMap.vue.d.ts +1 -1
- package/dist/components/MSlider.vue.d.ts +2 -2
- package/dist/composables/_effectCanvas.d.ts +23 -0
- package/dist/composables/_storageKeys.d.ts +9 -0
- package/dist/composables/themeInitScript.d.ts +2 -0
- package/dist/composables/useConfetti.d.ts +19 -8
- package/dist/composables/useFireworks.d.ts +37 -0
- package/dist/composables/useReactions.d.ts +32 -0
- package/dist/composables/viewportHints.d.ts +2 -0
- package/dist/directives/vSparkle.d.ts +27 -0
- package/dist/index.d.ts +7 -1
- package/dist/m3ui.js +5403 -5022
- package/dist/m3ui.js.map +1 -1
- package/dist/map.js +572 -572
- package/dist/map.js.map +1 -1
- package/dist/plugin.d.ts +10 -0
- package/dist/rich-text-editor.js +2 -2
- package/dist/{useThemeTokens-Bv-Uv8eX.js → useThemeTokens-y0HZpyhL.js} +23 -8
- package/dist/useThemeTokens-y0HZpyhL.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MSlider.test.ts +36 -0
- package/src/__tests__/plugin.test.ts +56 -1
- package/src/__tests__/ssr.test.ts +1 -1
- package/src/__tests__/themeInitScript.test.ts +10 -0
- package/src/__tests__/useConfetti.test.ts +33 -2
- package/src/__tests__/useFireworks.test.ts +104 -0
- package/src/__tests__/useReactions.test.ts +51 -0
- package/src/__tests__/vSparkle.test.ts +83 -0
- package/src/components/MEmojiSelector.vue +3 -2
- package/src/components/MMap.vue +3 -2
- package/src/components/MSlider.vue +22 -4
- package/src/components/MTextField.vue +8 -4
- package/src/composables/_colorUtils.ts +4 -2
- package/src/composables/_effectCanvas.ts +109 -0
- package/src/composables/_ssrState.ts +2 -2
- package/src/composables/_storageKeys.ts +28 -0
- package/src/composables/themeInitScript.ts +5 -2
- package/src/composables/useColorPalette.ts +36 -8
- package/src/composables/useConfetti.ts +84 -107
- package/src/composables/useFireworks.ts +360 -0
- package/src/composables/useReactions.ts +131 -0
- package/src/composables/useTheme.ts +21 -4
- package/src/composables/viewportHints.ts +6 -5
- package/src/directives/vSparkle.ts +180 -0
- package/src/index.ts +7 -1
- package/src/plugin.ts +21 -5
- package/dist/useThemeTokens-Bv-Uv8eX.js.map +0 -1
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
// Fireworks: rockets rise from the bottom of the screen and burst, in the theme's colors. A classic burst (a peony)
|
|
2
|
+
// throws its sparks out from the center in every direction, trailing streaks, then drag slows them and gravity pulls
|
|
3
|
+
// them down as they flicker out: simulated. A shaped one lays its sparks out along one of Material 3's expressive
|
|
4
|
+
// shapes (MShape's: a ring, a heart, a flower…): kinematic, so the shape stays readable while it opens and sags. Drawn
|
|
5
|
+
// on the effects' shared canvas (_effectCanvas) with useConfetti. Nothing happens under reduced motion.
|
|
6
|
+
import { getShape, type ShapeName } from 'shape-morph'
|
|
7
|
+
import { __resetEffectCanvas, reducedMotion, runLayer, themeColors, type EffectLayer } from './_effectCanvas'
|
|
8
|
+
|
|
9
|
+
export interface FireworksOptions {
|
|
10
|
+
/** How many rockets (default 6, launched over about a second and a half); with `duration`, rockets per second */
|
|
11
|
+
count?: number
|
|
12
|
+
/**
|
|
13
|
+
* What the bursts look like, one picked per burst: `'classic'` (sparks thrown out from the center that fall as they
|
|
14
|
+
* fade) or an MShape's name the sparks are laid out along (`'Circle'` is a ring). Default: mostly classic, with a
|
|
15
|
+
* ring, a heart, a flower… now and then
|
|
16
|
+
*/
|
|
17
|
+
shapes?: FireworkShape[]
|
|
18
|
+
/** Any CSS colors. Default: the theme's primary, secondary and tertiary roles and their containers */
|
|
19
|
+
colors?: string[]
|
|
20
|
+
/** Burst radius in px. Default: a sixth of the screen's smaller side, between 70 and 180 */
|
|
21
|
+
size?: number
|
|
22
|
+
/** Keep launching for this long, in ms (`count` then being rockets per second); `Infinity` runs until `stop()` */
|
|
23
|
+
duration?: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A burst's look: `'classic'`, or an MShape's name its sparks are laid out along. */
|
|
27
|
+
export type FireworkShape = 'classic' | ShapeName
|
|
28
|
+
|
|
29
|
+
/** Stops launching before `duration` ends; what's already in the air finishes. */
|
|
30
|
+
export type StopFireworks = () => void
|
|
31
|
+
|
|
32
|
+
const DEFAULT_SHAPES: FireworkShape[] = ['classic', 'classic', 'classic', 'classic', 'classic', 'classic', 'Circle', 'Heart', 'Flower', 'Sunny', 'Clover4Leaf']
|
|
33
|
+
const ROLES = ['primary', 'secondary', 'tertiary', 'primary-container', 'tertiary-container']
|
|
34
|
+
const SPARKS = 64
|
|
35
|
+
const CLASSIC_SPARKS = 90
|
|
36
|
+
// Sparks are slowed by DRAG (1/s) and pulled by GRAVITY (px/s²); a shaped burst instead opens over EXPAND seconds
|
|
37
|
+
// (ease out) while sagging SAG px/s². Every spark draws a streak through its last TAIL positions, kept every
|
|
38
|
+
// TAIL_STEP seconds whatever the frame rate (so ~0.27s of it); a rocket, its last TRAIL
|
|
39
|
+
const DRAG = 2.2
|
|
40
|
+
const GRAVITY = 110
|
|
41
|
+
const EXPAND = 0.9
|
|
42
|
+
const SAG = 70
|
|
43
|
+
const TAIL = 17
|
|
44
|
+
const TAIL_STEP = 1 / 60
|
|
45
|
+
const TRAIL = 10
|
|
46
|
+
|
|
47
|
+
interface Rocket {
|
|
48
|
+
x0: number
|
|
49
|
+
y0: number
|
|
50
|
+
x1: number
|
|
51
|
+
y1: number
|
|
52
|
+
flight: number
|
|
53
|
+
age: number
|
|
54
|
+
trail: number[]
|
|
55
|
+
color: string
|
|
56
|
+
burst: () => void
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface Burst {
|
|
60
|
+
cx: number
|
|
61
|
+
cy: number
|
|
62
|
+
radius: number
|
|
63
|
+
/** A shaped burst's sparks, as offsets from the center (see shapeOutline); a classic one has none */
|
|
64
|
+
offsets: Float32Array | null
|
|
65
|
+
x: Float32Array
|
|
66
|
+
y: Float32Array
|
|
67
|
+
vx: Float32Array
|
|
68
|
+
vy: Float32Array
|
|
69
|
+
/** The last TAIL positions of every spark, oldest first, the newest being where it is now: `[spark][step][x, y]` */
|
|
70
|
+
tail: Float32Array
|
|
71
|
+
/** Time since the tail last moved up a step */
|
|
72
|
+
tailClock: number
|
|
73
|
+
colors: string[]
|
|
74
|
+
age: number
|
|
75
|
+
life: number
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface Pending {
|
|
79
|
+
at: number
|
|
80
|
+
launch: () => void
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const outlines = new Map<string, Float32Array>()
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* `n` points evenly spaced along a shape's outline, as offsets from its center scaled so the farthest is at 1
|
|
87
|
+
* (`[x0, y0, x1, y1, …]`). Exported for tests.
|
|
88
|
+
*/
|
|
89
|
+
export function shapeOutline(shape: ShapeName, n = SPARKS): Float32Array {
|
|
90
|
+
const key = `${shape}:${n}`
|
|
91
|
+
let out = outlines.get(key)
|
|
92
|
+
if (out) return out
|
|
93
|
+
// The outline as a polyline (each cubic sampled), then n points at equal distances along it
|
|
94
|
+
const pts: number[] = []
|
|
95
|
+
for (const cubic of getShape(shape).cubics) {
|
|
96
|
+
const p = cubic.points
|
|
97
|
+
for (let i = 0; i < 16; i++) {
|
|
98
|
+
const t = i / 16
|
|
99
|
+
const u = 1 - t
|
|
100
|
+
const a = u * u * u, b = 3 * u * u * t, c = 3 * u * t * t, d = t * t * t
|
|
101
|
+
pts.push(a * p[0]! + b * p[2]! + c * p[4]! + d * p[6]!, a * p[1]! + b * p[3]! + c * p[5]! + d * p[7]!)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const count = pts.length / 2
|
|
105
|
+
const lengths = [0]
|
|
106
|
+
for (let i = 1; i <= count; i++) {
|
|
107
|
+
const j = i % count
|
|
108
|
+
lengths.push(lengths[i - 1]! + Math.hypot(pts[j * 2]! - pts[(i - 1) * 2]!, pts[j * 2 + 1]! - pts[(i - 1) * 2 + 1]!))
|
|
109
|
+
}
|
|
110
|
+
const total = lengths[count]!
|
|
111
|
+
// Centered on its bounding box (sampling isn't even along the outline, so an average would lean)
|
|
112
|
+
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity
|
|
113
|
+
for (let i = 0; i < count; i++) {
|
|
114
|
+
minX = Math.min(minX, pts[i * 2]!); maxX = Math.max(maxX, pts[i * 2]!)
|
|
115
|
+
minY = Math.min(minY, pts[i * 2 + 1]!); maxY = Math.max(maxY, pts[i * 2 + 1]!)
|
|
116
|
+
}
|
|
117
|
+
const cx = (minX + maxX) / 2
|
|
118
|
+
const cy = (minY + maxY) / 2
|
|
119
|
+
out = new Float32Array(n * 2)
|
|
120
|
+
let seg = 0
|
|
121
|
+
let max = 0
|
|
122
|
+
for (let k = 0; k < n; k++) {
|
|
123
|
+
const target = (k / n) * total
|
|
124
|
+
while (lengths[seg + 1]! < target) seg++
|
|
125
|
+
const f = (target - lengths[seg]!) / (lengths[seg + 1]! - lengths[seg]! || 1)
|
|
126
|
+
const j = (seg + 1) % count
|
|
127
|
+
const x = pts[seg * 2]! + (pts[j * 2]! - pts[seg * 2]!) * f - cx
|
|
128
|
+
const y = pts[seg * 2 + 1]! + (pts[j * 2 + 1]! - pts[seg * 2 + 1]!) * f - cy
|
|
129
|
+
out[k * 2] = x
|
|
130
|
+
out[k * 2 + 1] = y
|
|
131
|
+
max = Math.max(max, Math.hypot(x, y))
|
|
132
|
+
}
|
|
133
|
+
for (let i = 0; i < out.length; i++) out[i]! /= max || 1
|
|
134
|
+
outlines.set(key, out)
|
|
135
|
+
return out
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let rockets: Rocket[] = []
|
|
139
|
+
let bursts: Burst[] = []
|
|
140
|
+
let pending: Pending[] = []
|
|
141
|
+
let emitters: { until: number; rate: number; carry: number; launch: () => void }[] = []
|
|
142
|
+
|
|
143
|
+
const easeOut = (t: number) => 1 - (1 - t) ** 3
|
|
144
|
+
|
|
145
|
+
function createBurst(cx: number, cy: number, radius: number, colors: string[], offsets: Float32Array | null): Burst {
|
|
146
|
+
const n = offsets ? offsets.length / 2 : CLASSIC_SPARKS
|
|
147
|
+
const b: Burst = {
|
|
148
|
+
cx, cy, radius, colors, offsets, age: 0,
|
|
149
|
+
life: offsets ? 1.9 * (0.85 + Math.random() * 0.3) : 2 + Math.random() * 0.6,
|
|
150
|
+
x: new Float32Array(n).fill(cx),
|
|
151
|
+
y: new Float32Array(n).fill(cy),
|
|
152
|
+
vx: new Float32Array(n),
|
|
153
|
+
vy: new Float32Array(n),
|
|
154
|
+
tail: new Float32Array(n * TAIL * 2),
|
|
155
|
+
tailClock: 0,
|
|
156
|
+
}
|
|
157
|
+
for (let i = 0; i < n; i++) {
|
|
158
|
+
if (!offsets) {
|
|
159
|
+
// A sphere of sparks seen from the front: denser toward the rim, some flying toward or away from us. With drag
|
|
160
|
+
// alone a spark stops speed / DRAG away, so that's the radius
|
|
161
|
+
const angle = Math.random() * Math.PI * 2
|
|
162
|
+
const depth = Math.random() * 2 - 1
|
|
163
|
+
const speed = radius * DRAG * Math.sqrt(1 - depth * depth) * (0.92 + Math.random() * 0.12)
|
|
164
|
+
b.vx[i] = Math.cos(angle) * speed
|
|
165
|
+
b.vy[i] = Math.sin(angle) * speed
|
|
166
|
+
}
|
|
167
|
+
for (let k = 0; k < TAIL; k++) {
|
|
168
|
+
b.tail[(i * TAIL + k) * 2] = cx
|
|
169
|
+
b.tail[(i * TAIL + k) * 2 + 1] = cy
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return b
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function stepBurst(ctx: CanvasRenderingContext2D, b: Burst, dt: number) {
|
|
176
|
+
b.age += dt
|
|
177
|
+
const t = b.age
|
|
178
|
+
const n = b.x.length
|
|
179
|
+
const opened = b.radius * easeOut(Math.min(t / EXPAND, 1))
|
|
180
|
+
b.tailClock += dt
|
|
181
|
+
const shift = b.tailClock >= TAIL_STEP
|
|
182
|
+
if (shift) b.tailClock %= TAIL_STEP
|
|
183
|
+
for (let i = 0; i < n; i++) {
|
|
184
|
+
if (b.offsets) {
|
|
185
|
+
b.x[i] = b.cx + b.offsets[i * 2]! * opened
|
|
186
|
+
b.y[i] = b.cy + b.offsets[i * 2 + 1]! * opened + SAG * t * t
|
|
187
|
+
} else {
|
|
188
|
+
b.vx[i]! -= b.vx[i]! * DRAG * dt
|
|
189
|
+
b.vy[i]! += (GRAVITY - b.vy[i]! * DRAG) * dt
|
|
190
|
+
b.x[i]! += b.vx[i]! * dt
|
|
191
|
+
b.y[i]! += b.vy[i]! * dt
|
|
192
|
+
}
|
|
193
|
+
const base = i * TAIL * 2
|
|
194
|
+
if (shift) b.tail.copyWithin(base, base + 2, base + TAIL * 2)
|
|
195
|
+
b.tail[base + (TAIL - 1) * 2] = b.x[i]!
|
|
196
|
+
b.tail[base + (TAIL - 1) * 2 + 1] = b.y[i]!
|
|
197
|
+
}
|
|
198
|
+
// A flash where it bursts
|
|
199
|
+
if (t < 0.15) {
|
|
200
|
+
ctx.globalAlpha = 0.35 * (1 - t / 0.15)
|
|
201
|
+
ctx.fillStyle = b.colors[0]!
|
|
202
|
+
ctx.beginPath()
|
|
203
|
+
ctx.arc(b.cx, b.cy, b.radius * 0.25, 0, Math.PI * 2)
|
|
204
|
+
ctx.fill()
|
|
205
|
+
}
|
|
206
|
+
// Fades over the second half of its life, the sparks flickering out over the last third
|
|
207
|
+
const left = 1 - t / b.life
|
|
208
|
+
const fade = Math.max(0, Math.min(1, left * 2))
|
|
209
|
+
const flicker = left < 0.35
|
|
210
|
+
const shown = new Uint8Array(n)
|
|
211
|
+
for (let i = 0; i < n; i++) shown[i] = !flicker || Math.random() < 0.6 ? 1 : 0
|
|
212
|
+
// One path per color and streak segment, fainter and thinner toward the tail's end, then a bright head
|
|
213
|
+
b.colors.forEach((color, ci) => {
|
|
214
|
+
ctx.strokeStyle = color
|
|
215
|
+
ctx.fillStyle = color
|
|
216
|
+
for (let k = 1; k < TAIL; k++) {
|
|
217
|
+
const along = k / (TAIL - 1)
|
|
218
|
+
ctx.globalAlpha = fade * along * along
|
|
219
|
+
ctx.lineWidth = 0.8 + 1.6 * along
|
|
220
|
+
ctx.beginPath()
|
|
221
|
+
for (let i = ci; i < n; i += b.colors.length) {
|
|
222
|
+
if (!shown[i]) continue
|
|
223
|
+
const a = (i * TAIL + k - 1) * 2
|
|
224
|
+
ctx.moveTo(b.tail[a]!, b.tail[a + 1]!)
|
|
225
|
+
ctx.lineTo(b.tail[a + 2]!, b.tail[a + 3]!)
|
|
226
|
+
}
|
|
227
|
+
ctx.stroke()
|
|
228
|
+
}
|
|
229
|
+
ctx.globalAlpha = fade
|
|
230
|
+
ctx.beginPath()
|
|
231
|
+
for (let i = ci; i < n; i += b.colors.length) {
|
|
232
|
+
if (!shown[i]) continue
|
|
233
|
+
ctx.moveTo(b.x[i]! + 1.8, b.y[i]!)
|
|
234
|
+
ctx.arc(b.x[i]!, b.y[i]!, 1.8, 0, Math.PI * 2)
|
|
235
|
+
}
|
|
236
|
+
ctx.fill()
|
|
237
|
+
})
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const layer: EffectLayer = {
|
|
241
|
+
step(ctx, dt, dpr, now) {
|
|
242
|
+
emitters = emitters.filter((e) => e.until > now)
|
|
243
|
+
for (const e of emitters) {
|
|
244
|
+
e.carry += e.rate * dt
|
|
245
|
+
for (; e.carry >= 1; e.carry--) e.launch()
|
|
246
|
+
}
|
|
247
|
+
const due = pending.filter((p) => p.at <= now)
|
|
248
|
+
pending = pending.filter((p) => p.at > now)
|
|
249
|
+
for (const p of due) p.launch()
|
|
250
|
+
|
|
251
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0)
|
|
252
|
+
ctx.lineCap = 'round'
|
|
253
|
+
|
|
254
|
+
for (const r of rockets) {
|
|
255
|
+
r.age += dt
|
|
256
|
+
const t = easeOut(Math.min(r.age / r.flight, 1))
|
|
257
|
+
r.trail.push(r.x0 + (r.x1 - r.x0) * t, r.y0 + (r.y1 - r.y0) * t)
|
|
258
|
+
if (r.trail.length > TRAIL * 2) r.trail.splice(0, 2)
|
|
259
|
+
ctx.strokeStyle = r.color
|
|
260
|
+
ctx.lineWidth = 2.5
|
|
261
|
+
for (let i = 2; i < r.trail.length; i += 2) {
|
|
262
|
+
ctx.globalAlpha = i / r.trail.length
|
|
263
|
+
ctx.beginPath()
|
|
264
|
+
ctx.moveTo(r.trail[i - 2]!, r.trail[i - 1]!)
|
|
265
|
+
ctx.lineTo(r.trail[i]!, r.trail[i + 1]!)
|
|
266
|
+
ctx.stroke()
|
|
267
|
+
}
|
|
268
|
+
if (r.age >= r.flight) r.burst()
|
|
269
|
+
}
|
|
270
|
+
rockets = rockets.filter((r) => r.age < r.flight)
|
|
271
|
+
|
|
272
|
+
for (const b of bursts) stepBurst(ctx, b, dt)
|
|
273
|
+
bursts = bursts.filter((b) => b.age < b.life)
|
|
274
|
+
|
|
275
|
+
return rockets.length > 0 || bursts.length > 0 || pending.length > 0 || emitters.length > 0
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Launches fireworks: a few rockets, or with `duration` a show that keeps going (stop it early with the returned
|
|
281
|
+
* function). Browser only; does nothing under reduced motion.
|
|
282
|
+
*/
|
|
283
|
+
export function launchFireworks(options: FireworksOptions = {}): StopFireworks {
|
|
284
|
+
const noop = () => {}
|
|
285
|
+
if (typeof window === 'undefined' || reducedMotion()) return noop
|
|
286
|
+
const colors = options.colors?.length ? options.colors : themeColors(document.documentElement, ROLES)
|
|
287
|
+
const shapes = options.shapes?.length ? options.shapes : DEFAULT_SHAPES
|
|
288
|
+
const count = options.count ?? 6
|
|
289
|
+
const pick = <T,>(list: T[]) => list[Math.floor(Math.random() * list.length)]!
|
|
290
|
+
|
|
291
|
+
const launch = () => {
|
|
292
|
+
const w = window.innerWidth
|
|
293
|
+
const h = window.innerHeight
|
|
294
|
+
const x0 = w * (0.1 + Math.random() * 0.8)
|
|
295
|
+
const color = pick(colors)
|
|
296
|
+
const rocket: Rocket = {
|
|
297
|
+
x0,
|
|
298
|
+
y0: h + 10,
|
|
299
|
+
x1: x0 + (Math.random() - 0.5) * 120,
|
|
300
|
+
y1: h * (0.15 + Math.random() * 0.3),
|
|
301
|
+
flight: 0.9 + Math.random() * 0.4,
|
|
302
|
+
age: 0,
|
|
303
|
+
trail: [],
|
|
304
|
+
color,
|
|
305
|
+
burst() {
|
|
306
|
+
const shape = pick(shapes)
|
|
307
|
+
const radius = (options.size ?? Math.min(180, Math.max(70, Math.min(w, h) / 6))) * (0.8 + Math.random() * 0.4)
|
|
308
|
+
// Sometimes two colors, alternating spark by spark
|
|
309
|
+
const burstColors = Math.random() < 0.3 ? [color, pick(colors)] : [color]
|
|
310
|
+
if (shape === 'classic') {
|
|
311
|
+
bursts.push(createBurst(this.x1, this.y1, radius, burstColors, null))
|
|
312
|
+
return
|
|
313
|
+
}
|
|
314
|
+
const base = shapeOutline(shape)
|
|
315
|
+
// Turned at random, except a heart, which reads only upright
|
|
316
|
+
const angle = shape === 'Heart' ? 0 : Math.random() * Math.PI * 2
|
|
317
|
+
const cos = Math.cos(angle), sin = Math.sin(angle)
|
|
318
|
+
// A ring gets a second, smaller one inside it
|
|
319
|
+
const rings = shape === 'Circle' ? [1, 0.55] : [1]
|
|
320
|
+
const offsets = new Float32Array(base.length * rings.length)
|
|
321
|
+
rings.forEach((scale, k) => {
|
|
322
|
+
for (let i = 0; i < base.length; i += 2) {
|
|
323
|
+
offsets[k * base.length + i] = (base[i]! * cos - base[i + 1]! * sin) * scale
|
|
324
|
+
offsets[k * base.length + i + 1] = (base[i]! * sin + base[i + 1]! * cos) * scale
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
bursts.push(createBurst(this.x1, this.y1, radius, burstColors, offsets))
|
|
328
|
+
},
|
|
329
|
+
}
|
|
330
|
+
rockets.push(rocket)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (!runLayer(layer)) return noop
|
|
334
|
+
let emitter: (typeof emitters)[number] | null = null
|
|
335
|
+
const now = performance.now()
|
|
336
|
+
if (options.duration && options.duration > 0) {
|
|
337
|
+
emitter = { until: now + options.duration, rate: count, carry: 1, launch }
|
|
338
|
+
emitters.push(emitter)
|
|
339
|
+
} else {
|
|
340
|
+
// The first one right away, the rest over about 0.25s each
|
|
341
|
+
for (let i = 0; i < count; i++) pending.push({ at: now + (i === 0 ? 0 : Math.random() * count * 250), launch })
|
|
342
|
+
}
|
|
343
|
+
return () => {
|
|
344
|
+
if (emitter) emitter.until = 0
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Fireworks for a component: `launch()`, `launch({ shapes: ['classic'] })`, `launch({ shapes: ['Heart'] })`, `launch({ duration: 10000 })`. */
|
|
349
|
+
export function useFireworks() {
|
|
350
|
+
return { launch: launchFireworks }
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Stops every firework and removes the canvas. Internal, for tests; not exported from index.ts. */
|
|
354
|
+
export function __resetFireworks() {
|
|
355
|
+
rockets = []
|
|
356
|
+
bursts = []
|
|
357
|
+
pending = []
|
|
358
|
+
emitters = []
|
|
359
|
+
__resetEffectCanvas()
|
|
360
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Floating reactions: an emoji or a Material Symbol that pops up from a point and floats away swaying, the way
|
|
2
|
+
// reactions rise over a live stream or a video call. Each one is an element in a fixed layer over the page (created
|
|
3
|
+
// with the first, removed after the last; no pointer events, hidden from assistive technology), moved by a Web
|
|
4
|
+
// Animation, which the compositor runs. Under reduced motion it only fades in and out where it starts.
|
|
5
|
+
import { originPoint, reducedMotion, type EffectOrigin } from './_effectCanvas'
|
|
6
|
+
|
|
7
|
+
/** What floats up: any text (an emoji, `'+1'`), or a Material Symbol (`{ icon: 'favorite' }`, filled by default). */
|
|
8
|
+
export type ReactionContent = string | { icon: string; filled?: boolean; color?: string }
|
|
9
|
+
|
|
10
|
+
export interface ReactionOptions {
|
|
11
|
+
/** How many float up together, a little apart (default 1) */
|
|
12
|
+
count?: number
|
|
13
|
+
/** Size in px (default 32) */
|
|
14
|
+
size?: number
|
|
15
|
+
/** How far it rises, in px (default 260) */
|
|
16
|
+
distance?: number
|
|
17
|
+
/** How long it floats, in ms (default 2200) */
|
|
18
|
+
duration?: number
|
|
19
|
+
/** How far it sways to each side, in px (default 20) */
|
|
20
|
+
sway?: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Where a reaction starts: the pointer of an event, an element's center, or a point in viewport pixels */
|
|
24
|
+
export type ReactionOrigin = EffectOrigin
|
|
25
|
+
|
|
26
|
+
let layer: HTMLElement | null = null
|
|
27
|
+
let alive = 0
|
|
28
|
+
|
|
29
|
+
function ensureLayer() {
|
|
30
|
+
if (layer) return layer
|
|
31
|
+
layer = document.createElement('div')
|
|
32
|
+
layer.setAttribute('aria-hidden', 'true')
|
|
33
|
+
layer.style.cssText = 'position:fixed;inset:0;pointer-events:none;overflow:hidden;z-index:2147483000'
|
|
34
|
+
document.body.appendChild(layer)
|
|
35
|
+
return layer
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function release(el: HTMLElement) {
|
|
39
|
+
el.remove()
|
|
40
|
+
if (--alive <= 0 && layer) {
|
|
41
|
+
layer.remove()
|
|
42
|
+
layer = null
|
|
43
|
+
alive = 0
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const easeOut = (t: number) => 1 - (1 - t) ** 2
|
|
48
|
+
|
|
49
|
+
/** The path one reaction floats along, sampled for a linear Web Animation. */
|
|
50
|
+
function floatFrames(distance: number, sway: number, phase: number, drift: number): Keyframe[] {
|
|
51
|
+
const frames: Keyframe[] = []
|
|
52
|
+
const n = 30
|
|
53
|
+
for (let i = 0; i <= n; i++) {
|
|
54
|
+
const t = i / n
|
|
55
|
+
const y = -distance * easeOut(t)
|
|
56
|
+
const wave = Math.sin(phase + t * Math.PI * 2.4)
|
|
57
|
+
const x = sway * wave * (0.4 + 0.6 * t) + drift * t
|
|
58
|
+
// Pops in past its size, settles, and shrinks a little as it fades out
|
|
59
|
+
const scale = t < 0.12 ? 1.15 * easeOut(t / 0.12) : t < 0.22 ? 1.15 - 0.15 * ((t - 0.12) / 0.1) : 1 - 0.15 * ((t - 0.22) / 0.78)
|
|
60
|
+
const opacity = t < 0.08 ? t / 0.08 : t > 0.65 ? 1 - (t - 0.65) / 0.35 : 1
|
|
61
|
+
frames.push({
|
|
62
|
+
transform: `translate(-50%, -50%) translate(${x.toFixed(1)}px, ${y.toFixed(1)}px) rotate(${(wave * 10).toFixed(1)}deg) scale(${scale.toFixed(3)})`,
|
|
63
|
+
opacity: +opacity.toFixed(3),
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
return frames
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Sends a reaction floating up from a point. Browser only. */
|
|
70
|
+
export function fireReaction(origin: ReactionOrigin | undefined, content: ReactionContent, options: ReactionOptions = {}) {
|
|
71
|
+
if (typeof window === 'undefined') return
|
|
72
|
+
const point = originPoint(origin)
|
|
73
|
+
const size = options.size ?? 32
|
|
74
|
+
const count = Math.max(1, options.count ?? 1)
|
|
75
|
+
const reduced = reducedMotion()
|
|
76
|
+
const host = ensureLayer()
|
|
77
|
+
// An icon's default color is the theme's primary where the reaction starts (an MScheme's, inside one)
|
|
78
|
+
const iconColor = typeof content === 'object'
|
|
79
|
+
? content.color ?? (getComputedStyle(point.el).getPropertyValue('--color-primary').trim() || 'currentColor')
|
|
80
|
+
: undefined
|
|
81
|
+
|
|
82
|
+
for (let i = 0; i < count; i++) {
|
|
83
|
+
const el = document.createElement('span')
|
|
84
|
+
el.style.cssText = `position:absolute;left:${point.x}px;top:${point.y}px;line-height:1;font-size:${size}px;white-space:nowrap;opacity:0;will-change:transform,opacity`
|
|
85
|
+
if (typeof content === 'string') el.textContent = content
|
|
86
|
+
else {
|
|
87
|
+
el.className = 'material-symbols-outlined'
|
|
88
|
+
el.textContent = content.icon
|
|
89
|
+
el.style.color = iconColor!
|
|
90
|
+
if (content.filled !== false) el.style.fontVariationSettings = "'FILL' 1"
|
|
91
|
+
}
|
|
92
|
+
host.appendChild(el)
|
|
93
|
+
alive++
|
|
94
|
+
const duration = (options.duration ?? 2200) * (0.9 + Math.random() * 0.2)
|
|
95
|
+
const frames = reduced
|
|
96
|
+
? [
|
|
97
|
+
{ transform: 'translate(-50%, -50%)', opacity: 0 },
|
|
98
|
+
{ transform: 'translate(-50%, -50%)', opacity: 1, offset: 0.2 },
|
|
99
|
+
{ transform: 'translate(-50%, -50%)', opacity: 0 },
|
|
100
|
+
]
|
|
101
|
+
: floatFrames(
|
|
102
|
+
(options.distance ?? 260) * (0.8 + Math.random() * 0.4),
|
|
103
|
+
(options.sway ?? 20) * (0.7 + Math.random() * 0.6),
|
|
104
|
+
Math.random() * Math.PI * 2,
|
|
105
|
+
(Math.random() - 0.5) * 40,
|
|
106
|
+
)
|
|
107
|
+
const anim = el.animate(frames, { duration: reduced ? 1200 : duration, delay: i * 90, easing: 'linear', fill: 'both' })
|
|
108
|
+
let done = false
|
|
109
|
+
const end = () => {
|
|
110
|
+
if (!done) release(el)
|
|
111
|
+
done = true
|
|
112
|
+
}
|
|
113
|
+
anim.addEventListener('finish', end, { once: true })
|
|
114
|
+
anim.addEventListener('cancel', end, { once: true })
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Floating reactions for a component: `react(event, '🎉')` from the pointer, `react(el, { icon: 'favorite' })` from
|
|
120
|
+
* an element, `react(el, '👏', { count: 5 })` for a handful.
|
|
121
|
+
*/
|
|
122
|
+
export function useReactions() {
|
|
123
|
+
return { react: fireReaction }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Removes every reaction at once. Internal, for tests; not exported from index.ts. */
|
|
127
|
+
export function __resetReactions() {
|
|
128
|
+
layer?.remove()
|
|
129
|
+
layer = null
|
|
130
|
+
alive = 0
|
|
131
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ref, watchEffect, onMounted, onUnmounted } from 'vue'
|
|
2
2
|
import { themeTransition } from './_themeTransition'
|
|
3
3
|
import { initialState, readCookie, ssrValue, writeCookie } from './_ssrState'
|
|
4
|
-
import { THEME_KEY } from './_storageKeys'
|
|
4
|
+
import { THEME_KEY, onStoragePrefix, storageKey } from './_storageKeys'
|
|
5
5
|
|
|
6
6
|
export type Theme = 'light' | 'dark' | 'system'
|
|
7
7
|
|
|
@@ -11,7 +11,15 @@ export { THEME_KEY }
|
|
|
11
11
|
const inBrowser = typeof window !== 'undefined'
|
|
12
12
|
|
|
13
13
|
// localStorage, else the cookie createM3UI({ cookies }) keeps (cleared storage, or a theme a server set)
|
|
14
|
-
|
|
14
|
+
function readStored(): Theme {
|
|
15
|
+
if (!inBrowser) return 'system'
|
|
16
|
+
const key = storageKey(THEME_KEY)
|
|
17
|
+
let v: string | null = null
|
|
18
|
+
try { v = localStorage.getItem(key) } catch { /* storage blocked */ }
|
|
19
|
+
return ((v ?? readCookie(key)) as Theme | null) ?? 'system'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const theme = ref<Theme>(readStored())
|
|
15
23
|
|
|
16
24
|
let _appliedDark: boolean | null = null
|
|
17
25
|
|
|
@@ -30,6 +38,14 @@ function applyTheme(t: Theme) {
|
|
|
30
38
|
// Apply immediately on module load (before any component mounts) — no animation
|
|
31
39
|
if (inBrowser) applyTheme(theme.value)
|
|
32
40
|
|
|
41
|
+
// createM3UI({ storagePrefix }): this app's own stored theme, applied before anything renders — no animation
|
|
42
|
+
onStoragePrefix(() => {
|
|
43
|
+
if (!inBrowser) return
|
|
44
|
+
theme.value = readStored()
|
|
45
|
+
_appliedDark = null
|
|
46
|
+
applyTheme(theme.value)
|
|
47
|
+
})
|
|
48
|
+
|
|
33
49
|
export function useTheme() {
|
|
34
50
|
const mediaQuery = inBrowser ? window.matchMedia('(prefers-color-scheme: dark)') : null
|
|
35
51
|
|
|
@@ -39,8 +55,9 @@ export function useTheme() {
|
|
|
39
55
|
|
|
40
56
|
watchEffect(() => {
|
|
41
57
|
if (!inBrowser) return
|
|
42
|
-
|
|
43
|
-
|
|
58
|
+
const key = storageKey(THEME_KEY)
|
|
59
|
+
try { localStorage.setItem(key, theme.value) } catch { /* storage blocked */ }
|
|
60
|
+
writeCookie(key, theme.value)
|
|
44
61
|
applyTheme(theme.value)
|
|
45
62
|
})
|
|
46
63
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { VIEWPORT_KEY } from './_storageKeys'
|
|
1
|
+
import { VIEWPORT_KEY, storageKey } from './_storageKeys'
|
|
2
2
|
|
|
3
3
|
/** A request's headers as server frameworks hand them over: a `Headers`, or a plain object (Node's, h3's). */
|
|
4
4
|
export type RequestHeaders = Headers | Record<string, string | string[] | undefined>
|
|
@@ -33,8 +33,8 @@ function header(headers: RequestHeaders, name: string): string | undefined {
|
|
|
33
33
|
|
|
34
34
|
const size = (n: number) => Number.isFinite(n) && n > 0 && n < 20000
|
|
35
35
|
|
|
36
|
-
function fromCookie(cookies: string | undefined): ViewportSize | undefined {
|
|
37
|
-
const raw = cookies?.match(new RegExp(`(?:^|;\\s*)${VIEWPORT_KEY}=([^;]*)`))?.[1]
|
|
36
|
+
function fromCookie(cookies: string | undefined, prefix: string): ViewportSize | undefined {
|
|
37
|
+
const raw = cookies?.match(new RegExp(`(?:^|;\\s*)${storageKey(VIEWPORT_KEY, prefix)}=([^;]*)`))?.[1]
|
|
38
38
|
if (!raw) return undefined
|
|
39
39
|
let value = raw
|
|
40
40
|
try { value = decodeURIComponent(raw) } catch { /* as it is */ }
|
|
@@ -61,14 +61,15 @@ function guess(headers: RequestHeaders): ViewportSize | undefined {
|
|
|
61
61
|
* cookies: true })` keeps in the `m3-viewport` cookie (any visit after the first); the viewport client hints
|
|
62
62
|
* (Chromium, once the server asked for them with `VIEWPORT_HINTS`); or, on a first visit, a size standing for the
|
|
63
63
|
* kind of device (`Sec-CH-UA-Mobile`, then the user agent: a phone, a tablet or a desktop), unless `guess: false`.
|
|
64
|
+
* With `createM3UI({ storagePrefix })`, pass the same `storagePrefix` so the right cookie is read.
|
|
64
65
|
* Undefined when nothing tells: the layouts then render compact.
|
|
65
66
|
*
|
|
66
67
|
* ```ts
|
|
67
68
|
* createM3UI({ cookies: true, initialState: { viewport: viewportFromHeaders(request.headers) } })
|
|
68
69
|
* ```
|
|
69
70
|
*/
|
|
70
|
-
export function viewportFromHeaders(headers: RequestHeaders, options: { guess?: boolean } = {}): ViewportSize | undefined {
|
|
71
|
-
const cookie = fromCookie(header(headers, 'cookie'))
|
|
71
|
+
export function viewportFromHeaders(headers: RequestHeaders, options: { guess?: boolean; storagePrefix?: string } = {}): ViewportSize | undefined {
|
|
72
|
+
const cookie = fromCookie(header(headers, 'cookie'), options.storagePrefix ?? '')
|
|
72
73
|
if (cookie) return cookie
|
|
73
74
|
const width = Number(header(headers, 'sec-ch-viewport-width'))
|
|
74
75
|
if (size(width)) {
|