@baybreezy/docd 0.3.4 → 0.3.5
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.
|
@@ -25,8 +25,33 @@
|
|
|
25
25
|
<script lang="ts" setup>
|
|
26
26
|
import { buttonStyles } from "../Ui/Button.vue";
|
|
27
27
|
|
|
28
|
+
type ThemeTransitionVariant =
|
|
29
|
+
| "circle"
|
|
30
|
+
| "square"
|
|
31
|
+
| "triangle"
|
|
32
|
+
| "diamond"
|
|
33
|
+
| "hexagon"
|
|
34
|
+
| "rectangle"
|
|
35
|
+
| "star";
|
|
36
|
+
|
|
37
|
+
const props = withDefaults(
|
|
38
|
+
defineProps<{
|
|
39
|
+
duration?: number;
|
|
40
|
+
variant?: ThemeTransitionVariant;
|
|
41
|
+
/** When true, the transition expands from the viewport center instead of the button center. */
|
|
42
|
+
fromCenter?: boolean;
|
|
43
|
+
}>(),
|
|
44
|
+
{
|
|
45
|
+
duration: 400,
|
|
46
|
+
variant: "circle",
|
|
47
|
+
fromCenter: false,
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
|
|
28
51
|
const colorMode = useColorMode();
|
|
29
|
-
const
|
|
52
|
+
const toggler = useTemplateRef("toggler");
|
|
53
|
+
const { width: viewportWidthRef, height: viewportHeightRef } = useWindowSize();
|
|
54
|
+
|
|
30
55
|
const iconName = computed(() => {
|
|
31
56
|
switch (colorMode.value) {
|
|
32
57
|
case "light":
|
|
@@ -38,49 +63,214 @@
|
|
|
38
63
|
}
|
|
39
64
|
});
|
|
40
65
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
66
|
+
// CSS custom properties driving the ::view-transition-group/new(root) rules below.
|
|
67
|
+
const vtDuration = useCssVar("--theme-toggle-vt-duration");
|
|
68
|
+
const vtClipFrom = useCssVar("--theme-vt-clip-from");
|
|
69
|
+
|
|
70
|
+
let isTransitioning = false;
|
|
71
|
+
let activeAnimation: Animation | null = null;
|
|
72
|
+
|
|
73
|
+
function cancelAnimation() {
|
|
74
|
+
activeAnimation?.cancel();
|
|
75
|
+
activeAnimation = null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function resetTransitionState() {
|
|
79
|
+
isTransitioning = false;
|
|
80
|
+
delete document.documentElement.dataset.themeVt;
|
|
81
|
+
vtDuration.value = null;
|
|
82
|
+
vtClipFrom.value = null;
|
|
83
|
+
cancelAnimation();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
tryOnScopeDispose(() => {
|
|
87
|
+
cancelAnimation();
|
|
88
|
+
if (document.documentElement.dataset.themeVt !== "active") return;
|
|
89
|
+
delete document.documentElement.dataset.themeVt;
|
|
90
|
+
vtDuration.value = null;
|
|
91
|
+
vtClipFrom.value = null;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
function polygonCollapsed(point: string, vertexCount: number): string {
|
|
95
|
+
return `polygon(${Array.from({ length: vertexCount }, () => point).join(", ")})`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// All coordinates are percentages of the snapshot reference box: Chrome renders absolute px
|
|
99
|
+
// clip-path coordinates on ::view-transition-new(root) unscaled on fractional display scales
|
|
100
|
+
// for the first transition after load, so px values land at the wrong position.
|
|
101
|
+
function getThemeTransitionClipPaths(
|
|
102
|
+
variant: ThemeTransitionVariant,
|
|
103
|
+
cx: number,
|
|
104
|
+
cy: number,
|
|
105
|
+
maxRadius: number,
|
|
106
|
+
viewportWidth: number,
|
|
107
|
+
viewportHeight: number
|
|
108
|
+
): [string, string] {
|
|
109
|
+
const toX = (x: number) => `${(x / viewportWidth) * 100}%`;
|
|
110
|
+
const toY = (y: number) => `${(y / viewportHeight) * 100}%`;
|
|
111
|
+
const point = (x: number, y: number) => `${toX(x)} ${toY(y)}`;
|
|
112
|
+
// circle() percentage radii resolve against hypot(w, h) / sqrt(2) of the reference box.
|
|
113
|
+
const toRadius = (r: number) =>
|
|
114
|
+
`${(r / (Math.hypot(viewportWidth, viewportHeight) / Math.SQRT2)) * 100}%`;
|
|
115
|
+
|
|
116
|
+
switch (variant) {
|
|
117
|
+
case "circle":
|
|
118
|
+
return [
|
|
119
|
+
`circle(0% at ${point(cx, cy)})`,
|
|
120
|
+
`circle(${toRadius(maxRadius)} at ${point(cx, cy)})`,
|
|
121
|
+
];
|
|
122
|
+
case "square": {
|
|
123
|
+
const halfW = Math.max(cx, viewportWidth - cx);
|
|
124
|
+
const halfH = Math.max(cy, viewportHeight - cy);
|
|
125
|
+
const halfSide = Math.max(halfW, halfH) * 1.05;
|
|
126
|
+
const end = [
|
|
127
|
+
point(cx - halfSide, cy - halfSide),
|
|
128
|
+
point(cx + halfSide, cy - halfSide),
|
|
129
|
+
point(cx + halfSide, cy + halfSide),
|
|
130
|
+
point(cx - halfSide, cy + halfSide),
|
|
131
|
+
].join(", ");
|
|
132
|
+
return [polygonCollapsed(point(cx, cy), 4), `polygon(${end})`];
|
|
133
|
+
}
|
|
134
|
+
case "triangle": {
|
|
135
|
+
const scale = maxRadius * 2.2;
|
|
136
|
+
const dx = (Math.sqrt(3) / 2) * scale;
|
|
137
|
+
const verts = [
|
|
138
|
+
point(cx, cy - scale),
|
|
139
|
+
point(cx + dx, cy + 0.5 * scale),
|
|
140
|
+
point(cx - dx, cy + 0.5 * scale),
|
|
141
|
+
].join(", ");
|
|
142
|
+
return [polygonCollapsed(point(cx, cy), 3), `polygon(${verts})`];
|
|
143
|
+
}
|
|
144
|
+
case "diamond": {
|
|
145
|
+
// Slightly larger than the view-transition circle radius so axis-aligned coverage matches the circle reveal.
|
|
146
|
+
const R = maxRadius * Math.SQRT2;
|
|
147
|
+
const end = [
|
|
148
|
+
point(cx, cy - R),
|
|
149
|
+
point(cx + R, cy),
|
|
150
|
+
point(cx, cy + R),
|
|
151
|
+
point(cx - R, cy),
|
|
152
|
+
].join(", ");
|
|
153
|
+
return [polygonCollapsed(point(cx, cy), 4), `polygon(${end})`];
|
|
154
|
+
}
|
|
155
|
+
case "hexagon": {
|
|
156
|
+
const R = maxRadius * Math.SQRT2;
|
|
157
|
+
const verts: string[] = [];
|
|
158
|
+
for (let i = 0; i < 6; i++) {
|
|
159
|
+
const a = -Math.PI / 2 + (i * Math.PI) / 3;
|
|
160
|
+
verts.push(point(cx + R * Math.cos(a), cy + R * Math.sin(a)));
|
|
161
|
+
}
|
|
162
|
+
return [polygonCollapsed(point(cx, cy), 6), `polygon(${verts.join(", ")})`];
|
|
163
|
+
}
|
|
164
|
+
case "rectangle": {
|
|
165
|
+
const halfW = Math.max(cx, viewportWidth - cx);
|
|
166
|
+
const halfH = Math.max(cy, viewportHeight - cy);
|
|
167
|
+
const end = [
|
|
168
|
+
point(cx - halfW, cy - halfH),
|
|
169
|
+
point(cx + halfW, cy - halfH),
|
|
170
|
+
point(cx + halfW, cy + halfH),
|
|
171
|
+
point(cx - halfW, cy + halfH),
|
|
172
|
+
].join(", ");
|
|
173
|
+
return [polygonCollapsed(point(cx, cy), 4), `polygon(${end})`];
|
|
174
|
+
}
|
|
175
|
+
case "star": {
|
|
176
|
+
// Small overscan so the last frames never leave a 1px seam before the transition group ends.
|
|
177
|
+
const R = maxRadius * Math.SQRT2 * 1.03;
|
|
178
|
+
const innerRatio = 0.42;
|
|
179
|
+
const starPolygon = (radius: number) => {
|
|
180
|
+
const verts: string[] = [];
|
|
181
|
+
for (let i = 0; i < 5; i++) {
|
|
182
|
+
const outerA = -Math.PI / 2 + (i * 2 * Math.PI) / 5;
|
|
183
|
+
verts.push(point(cx + radius * Math.cos(outerA), cy + radius * Math.sin(outerA)));
|
|
184
|
+
const innerA = outerA + Math.PI / 5;
|
|
185
|
+
verts.push(
|
|
186
|
+
point(
|
|
187
|
+
cx + radius * innerRatio * Math.cos(innerA),
|
|
188
|
+
cy + radius * innerRatio * Math.sin(innerA)
|
|
189
|
+
)
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
return `polygon(${verts.join(", ")})`;
|
|
193
|
+
};
|
|
194
|
+
const startR = Math.max(2, R * 0.025);
|
|
195
|
+
return [starPolygon(startR), starPolygon(R)];
|
|
196
|
+
}
|
|
197
|
+
default:
|
|
198
|
+
return [
|
|
199
|
+
`circle(0% at ${point(cx, cy)})`,
|
|
200
|
+
`circle(${toRadius(maxRadius)} at ${point(cx, cy)})`,
|
|
201
|
+
];
|
|
47
202
|
}
|
|
48
|
-
|
|
203
|
+
}
|
|
49
204
|
|
|
50
205
|
const toggleTheme = () => {
|
|
51
|
-
const button =
|
|
52
|
-
if (!button) return;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const viewportWidth =
|
|
57
|
-
const viewportHeight =
|
|
206
|
+
const button = toggler.value;
|
|
207
|
+
if (!button || isTransitioning || document.documentElement.dataset.themeVt === "active") return;
|
|
208
|
+
|
|
209
|
+
// innerWidth/innerHeight (not visualViewport): percentages must resolve against the
|
|
210
|
+
// snapshot reference box, which includes classic scrollbars.
|
|
211
|
+
const viewportWidth = viewportWidthRef.value;
|
|
212
|
+
const viewportHeight = viewportHeightRef.value;
|
|
213
|
+
|
|
214
|
+
let x: number;
|
|
215
|
+
let y: number;
|
|
216
|
+
if (props.fromCenter) {
|
|
217
|
+
x = viewportWidth / 2;
|
|
218
|
+
y = viewportHeight / 2;
|
|
219
|
+
} else {
|
|
220
|
+
const { top, left, width, height } = button.getBoundingClientRect();
|
|
221
|
+
x = left + width / 2;
|
|
222
|
+
y = top + height / 2;
|
|
223
|
+
}
|
|
224
|
+
|
|
58
225
|
const maxRadius = Math.hypot(Math.max(x, viewportWidth - x), Math.max(y, viewportHeight - y));
|
|
59
|
-
|
|
226
|
+
|
|
227
|
+
const newTheme = colorMode.value === "dark" ? "light" : "dark";
|
|
60
228
|
const applyTheme = () => {
|
|
229
|
+
// Toggle the class synchronously so the View Transitions API snapshots the new theme
|
|
230
|
+
// inside the startViewTransition callback, then hand persistence to color-mode.
|
|
61
231
|
document.documentElement.classList.toggle("dark", newTheme === "dark");
|
|
232
|
+
document.documentElement.classList.toggle("light", newTheme === "light");
|
|
62
233
|
colorMode.preference = newTheme;
|
|
63
234
|
};
|
|
235
|
+
|
|
64
236
|
if (typeof document.startViewTransition !== "function") {
|
|
65
237
|
applyTheme();
|
|
66
238
|
return;
|
|
67
239
|
}
|
|
240
|
+
|
|
241
|
+
const clipPath = getThemeTransitionClipPaths(
|
|
242
|
+
props.variant,
|
|
243
|
+
x,
|
|
244
|
+
y,
|
|
245
|
+
maxRadius,
|
|
246
|
+
viewportWidth,
|
|
247
|
+
viewportHeight
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
document.documentElement.dataset.themeVt = "active";
|
|
251
|
+
vtDuration.value = `${props.duration}ms`;
|
|
252
|
+
// Pin the collapsed clip-path via CSS so Firefox does not paint the new theme unclipped
|
|
253
|
+
// between snapshot and the ready.then() JS animation.
|
|
254
|
+
vtClipFrom.value = clipPath[0];
|
|
255
|
+
|
|
256
|
+
isTransitioning = true;
|
|
68
257
|
const transition = document.startViewTransition(applyTheme);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
},
|
|
258
|
+
transition.finished.finally(resetTransitionState).catch(() => {});
|
|
259
|
+
|
|
260
|
+
transition.ready
|
|
261
|
+
.then(() => {
|
|
262
|
+
activeAnimation = document.documentElement.animate(
|
|
263
|
+
{ clipPath },
|
|
76
264
|
{
|
|
77
265
|
duration: props.duration,
|
|
78
|
-
|
|
266
|
+
// Star: linear avoids easing overshoot that fights polygon interpolation at t→1.
|
|
267
|
+
easing: props.variant === "star" ? "linear" : "ease-in-out",
|
|
268
|
+
fill: "forwards",
|
|
79
269
|
pseudoElement: "::view-transition-new(root)",
|
|
80
270
|
}
|
|
81
271
|
);
|
|
82
|
-
})
|
|
83
|
-
|
|
272
|
+
})
|
|
273
|
+
.catch(() => {});
|
|
84
274
|
};
|
|
85
275
|
</script>
|
|
86
276
|
<style>
|
|
@@ -95,4 +285,10 @@
|
|
|
95
285
|
::view-transition-old(root) {
|
|
96
286
|
z-index: 1;
|
|
97
287
|
}
|
|
288
|
+
html[data-theme-vt="active"]::view-transition-group(root) {
|
|
289
|
+
animation-duration: var(--theme-toggle-vt-duration, 400ms);
|
|
290
|
+
}
|
|
291
|
+
html[data-theme-vt="active"]::view-transition-new(root) {
|
|
292
|
+
clip-path: var(--theme-vt-clip-from);
|
|
293
|
+
}
|
|
98
294
|
</style>
|