fix(webui): crossfade theme changes as one page (#6459)
* fix(webui): crossfade theme changes as one page * test(webui): align selection highlight motion checks Follow the shared CSS transition contract instead of obsolete Tailwind utilities. Verify normal geometry easing and reduced-motion theme paint in the browser without changing accepted theme behavior or relaxing CI gates.
This commit is contained in:
1 parent
4701218719
commit
83b6112843
20 files changed
+538
-67
No files matched your search
@@ -6,25 +6,68 @@
|
||||
--theme-ink-delay: 100ms;
|
||||
}
|
||||
|
||||
/* Keep text from dissolving into a matching gray while the canvas changes. */
|
||||
:root[data-theme-transition] :is(
|
||||
body,
|
||||
.thread-workspace,
|
||||
.workbench-body,
|
||||
.workbench-pane,
|
||||
main.bg-background,
|
||||
.bg-sidebar,
|
||||
.text-sidebar-muted-foreground,
|
||||
[data-testid="thread-header"] > div,
|
||||
[data-testid="hero-greeting"]
|
||||
) {
|
||||
transition: background-color 280ms linear, border-color 280ms linear, color 0s linear var(--theme-ink-delay);
|
||||
/* Paint owners opt in; components with local motion compose these lists instead. */
|
||||
:root[data-theme-transition] {
|
||||
--theme-paint-duration: 280ms;
|
||||
--theme-paint-transition: background-color var(--theme-paint-duration) linear, border-color var(--theme-paint-duration) linear, box-shadow var(--theme-paint-duration) linear;
|
||||
--theme-ink-transition: color 0s linear var(--theme-ink-delay);
|
||||
}
|
||||
|
||||
:root[data-theme-transition] .thread-composer-surface {
|
||||
transition: background-color 280ms linear, box-shadow 280ms linear, opacity 160ms ease-out;
|
||||
:root[data-theme-transition] [data-theme-surface] {
|
||||
transition: var(--theme-paint-transition);
|
||||
}
|
||||
|
||||
/* Delay only explicitly colored ink, not every inheriting layout wrapper. */
|
||||
:root[data-theme-transition] [data-theme-ink] {
|
||||
transition: var(--theme-ink-transition);
|
||||
}
|
||||
|
||||
:root[data-theme-transition] :is(body, [data-theme-surface][data-theme-ink]) {
|
||||
transition: var(--theme-paint-transition), var(--theme-ink-transition);
|
||||
}
|
||||
|
||||
:root[data-theme-transition] .thread-composer-surface textarea::placeholder {
|
||||
transition: color 0s linear var(--theme-ink-delay);
|
||||
}
|
||||
|
||||
/* Blend complete palettes, without a second layer of per-surface paint interpolation. */
|
||||
:root[data-theme-snapshot] {
|
||||
--theme-paint-duration: 0s;
|
||||
--theme-ink-delay: 0s;
|
||||
}
|
||||
|
||||
:root[data-theme-snapshot]::view-transition {
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
:root[data-theme-snapshot]::view-transition-group(root) {
|
||||
animation: none;
|
||||
}
|
||||
|
||||
/* Complementary alpha adds to one, rather than exposing an empty frame between themes. */
|
||||
:root[data-theme-snapshot]::view-transition-image-pair(root) {
|
||||
isolation: isolate;
|
||||
}
|
||||
|
||||
:root[data-theme-snapshot]::view-transition-old(root),
|
||||
:root[data-theme-snapshot]::view-transition-new(root) {
|
||||
mix-blend-mode: plus-lighter;
|
||||
}
|
||||
|
||||
:root[data-theme-snapshot]::view-transition-old(root) {
|
||||
animation: theme-crossfade-out 200ms ease-in-out both;
|
||||
}
|
||||
|
||||
:root[data-theme-snapshot]::view-transition-new(root) {
|
||||
animation: theme-crossfade-in 200ms ease-in-out both;
|
||||
}
|
||||
|
||||
@keyframes theme-crossfade-out {
|
||||
from { opacity: 1; }
|
||||
to { opacity: 0; }
|
||||
}
|
||||
|
||||
@keyframes theme-crossfade-in {
|
||||
from { opacity: 0; }
|
||||
to { opacity: 1; }
|
||||
}
|
||||
+57
-16
@@ -8,6 +8,7 @@ import {
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { flushSync } from "react-dom";
|
||||
import "./theme-transition.css";
|
||||
|
||||
import { useMediaQuery } from "@/hooks/useMediaQuery";
|
||||
@@ -26,6 +27,10 @@ function readStored(): ThemePreference {
|
||||
}
|
||||
}
|
||||
|
||||
function resolveTheme(preference: ThemePreference, systemDark: boolean): Theme {
|
||||
return preference === "system" ? (systemDark ? "dark" : "light") : preference;
|
||||
}
|
||||
|
||||
function applyTheme(theme: Theme): void {
|
||||
const root = document.documentElement;
|
||||
if (theme === "dark") root.classList.add("dark");
|
||||
@@ -47,15 +52,19 @@ export function useTheme(): {
|
||||
} {
|
||||
const [preference, setPreference] = useState(readStored);
|
||||
const systemDark = useMediaQuery("(prefers-color-scheme: dark)");
|
||||
const theme: Theme = preference === "system" ? (systemDark ? "dark" : "light") : preference;
|
||||
const theme = resolveTheme(preference, systemDark);
|
||||
|
||||
const switchTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const viewTransition = useRef<ViewTransition | null>(null);
|
||||
const pendingPreference = useRef<{ value: ThemePreference } | null>(null);
|
||||
useEffect(() => () => {
|
||||
if (switchTimer.current !== null) {
|
||||
clearTimeout(switchTimer.current);
|
||||
switchTimer.current = null;
|
||||
delete document.documentElement.dataset.themeTransition;
|
||||
}
|
||||
if (switchTimer.current !== null) clearTimeout(switchTimer.current);
|
||||
switchTimer.current = null;
|
||||
pendingPreference.current = null;
|
||||
viewTransition.current?.skipTransition();
|
||||
viewTransition.current = null;
|
||||
delete document.documentElement.dataset.themeTransition;
|
||||
delete document.documentElement.dataset.themeSnapshot;
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -66,32 +75,64 @@ export function useTheme(): {
|
||||
const refresh = (event: StorageEvent) => {
|
||||
if (event.key !== null && event.key !== STORAGE_KEY) return;
|
||||
if (event.storageArea && event.storageArea !== window.localStorage) return;
|
||||
setPreference(readStored());
|
||||
const value = readStored();
|
||||
if (pendingPreference.current) pendingPreference.current.value = value;
|
||||
else setPreference(value);
|
||||
};
|
||||
window.addEventListener("storage", refresh);
|
||||
return () => window.removeEventListener("storage", refresh);
|
||||
}, []);
|
||||
|
||||
const setTheme = useCallback((value: ThemePreference) => {
|
||||
if (value === preference) return;
|
||||
const next = value === "system" ? (systemDark ? "dark" : "light") : value;
|
||||
// Changing preference without changing the palette must not start or block a transition.
|
||||
if (next !== theme) {
|
||||
if (switchTimer.current !== null) return;
|
||||
document.documentElement.dataset.themeTransition = next;
|
||||
const pending = pendingPreference.current;
|
||||
const selected = pending?.value ?? preference;
|
||||
if (value === selected) return;
|
||||
const next = resolveTheme(value, systemDark);
|
||||
const changesPalette = next !== resolveTheme(selected, systemDark);
|
||||
const root = document.documentElement;
|
||||
// Same-palette choices, such as switching to System, must remain selectable.
|
||||
if (changesPalette) {
|
||||
if (switchTimer.current !== null || viewTransition.current !== null) return;
|
||||
root.dataset.themeTransition = next;
|
||||
switchTimer.current = setTimeout(() => {
|
||||
delete document.documentElement.dataset.themeTransition;
|
||||
switchTimer.current = null;
|
||||
if (viewTransition.current === null) delete root.dataset.themeTransition;
|
||||
}, 600);
|
||||
}
|
||||
setPreference(value);
|
||||
|
||||
if (pending) {
|
||||
// A native capture defers the React commit; retain the latest accepted preference.
|
||||
pending.value = value;
|
||||
} else if (changesPalette && document.startViewTransition
|
||||
&& !window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
|
||||
const update = { value };
|
||||
pendingPreference.current = update;
|
||||
root.dataset.themeSnapshot = "";
|
||||
const transition = document.startViewTransition(() => {
|
||||
if (pendingPreference.current !== update) return;
|
||||
pendingPreference.current = null;
|
||||
flushSync(() => setPreference(update.value));
|
||||
});
|
||||
viewTransition.current = transition;
|
||||
// Browser-skipped captures still apply their update and must release the owner.
|
||||
void transition.ready.catch(() => {});
|
||||
const release = () => {
|
||||
if (viewTransition.current !== transition) return;
|
||||
viewTransition.current = null;
|
||||
delete root.dataset.themeSnapshot;
|
||||
if (switchTimer.current === null) delete root.dataset.themeTransition;
|
||||
};
|
||||
void transition.finished.then(release, release);
|
||||
} else {
|
||||
setPreference(value);
|
||||
}
|
||||
// Only user choices persist. Resolved system changes and other tabs never write back.
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, value);
|
||||
} catch {
|
||||
// Browser-only preferences should never block the interface.
|
||||
}
|
||||
}, [preference, systemDark, theme]);
|
||||
}, [preference, systemDark]);
|
||||
const toggle = useCallback(
|
||||
() => setTheme(theme === "dark" ? "light" : "dark"),
|
||||
[setTheme, theme],
|
||||
|
||||
Reference in new issue
Block a user