feat(webui): smooth theme changes and sun-moon transitions (#6450)
* feat(webui): smooth theme changes and sun-moon transitions * test(webui): respect theme cooldown in diagram checks
This commit is contained in:
1 parent
4f7c4f0885
commit
3ede000ad4
12 files changed
+305
-17
No files matched your search
@@ -200,8 +200,10 @@ test("published Pele renders Chinese wrapping and mixed measured styles across w
|
|||||||
}
|
}
|
||||||
for (const theme of ["light", "dark"]) {
|
for (const theme of ["light", "dark"]) {
|
||||||
if (await page.evaluate(() => document.documentElement.classList.contains("dark")) !== (theme === "dark")) {
|
if (await page.evaluate(() => document.documentElement.classList.contains("dark")) !== (theme === "dark")) {
|
||||||
|
await expect(page.locator("html")).not.toHaveAttribute("data-theme-transition");
|
||||||
const previous = await image.getAttribute("src");
|
const previous = await image.getAttribute("src");
|
||||||
await page.getByRole("button", { name: "Toggle theme" }).click();
|
await page.getByRole("button", { name: "Toggle theme" }).click();
|
||||||
|
await expect.poll(() => page.evaluate(() => document.documentElement.classList.contains("dark"))).toBe(theme === "dark");
|
||||||
await expect(image).not.toHaveAttribute("src", previous!);
|
await expect(image).not.toHaveAttribute("src", previous!);
|
||||||
}
|
}
|
||||||
await image.scrollIntoViewIfNeeded();
|
await image.scrollIntoViewIfNeeded();
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import { QueuedGuidanceFixture } from "./queued-guidance-fixture";
|
|||||||
import { WorkspaceAlignmentFixture } from "./workspace-alignment-fixture";
|
import { WorkspaceAlignmentFixture } from "./workspace-alignment-fixture";
|
||||||
import { ComposerActionFixture } from "./composer-action-fixture";
|
import { ComposerActionFixture } from "./composer-action-fixture";
|
||||||
import { MobileChatLayoutFixture } from "./mobile-chat-layout-fixture";
|
import { MobileChatLayoutFixture } from "./mobile-chat-layout-fixture";
|
||||||
|
import { ThemeToggleFixture } from "./theme-toggle-fixture";
|
||||||
import { DisplayMathFixture } from "./display-math-fixture";
|
import { DisplayMathFixture } from "./display-math-fixture";
|
||||||
import { AttachmentLayoutFixture } from "./attachment-layout-fixture";
|
import { AttachmentLayoutFixture } from "./attachment-layout-fixture";
|
||||||
|
|
||||||
@@ -214,6 +215,8 @@ await initializeI18n();
|
|||||||
createRoot(document.getElementById("root")!).render(<TooltipProvider>
|
createRoot(document.getElementById("root")!).render(<TooltipProvider>
|
||||||
{parameters.get("case") === "attachments"
|
{parameters.get("case") === "attachments"
|
||||||
? <AttachmentLayoutFixture />
|
? <AttachmentLayoutFixture />
|
||||||
|
: parameters.get("case") === "theme-toggle"
|
||||||
|
? <ClientProvider client={railClient} token="fixture"><ThemeToggleFixture /></ClientProvider>
|
||||||
: parameters.get("case") === "mobile-chat-layout"
|
: parameters.get("case") === "mobile-chat-layout"
|
||||||
? <ClientProvider client={railClient} token="fixture"><MobileChatLayoutFixture parameters={parameters} /></ClientProvider>
|
? <ClientProvider client={railClient} token="fixture"><MobileChatLayoutFixture parameters={parameters} /></ClientProvider>
|
||||||
: parameters.get("case") === "composer-action"
|
: parameters.get("case") === "composer-action"
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { ThreadComposer } from "@/components/thread/ThreadComposer";
|
||||||
|
import { ThreadHeader } from "@/components/thread/ThreadHeader";
|
||||||
|
import { ThemeProvider, useTheme } from "@/hooks/useTheme";
|
||||||
|
|
||||||
|
export function ThemeToggleFixture() {
|
||||||
|
const { theme, toggle } = useTheme();
|
||||||
|
return (
|
||||||
|
<ThemeProvider theme={theme}>
|
||||||
|
<main className="thread-workspace relative flex h-screen flex-col justify-center gap-8 bg-background px-6">
|
||||||
|
<ThreadHeader title="Theme preview" hideTitle theme={theme} onToggleTheme={toggle} onToggleSidebar={() => {}} />
|
||||||
|
<div className="workbench-body bg-background">
|
||||||
|
<section className="workbench-pane flex flex-col gap-8 bg-background">
|
||||||
|
<h1 data-testid="hero-greeting" className="text-center text-4xl text-foreground">Where should we start?</h1>
|
||||||
|
<ThreadComposer variant="hero" onSend={() => {}} />
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</ThemeProvider>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
import { expect, test } from "@playwright/test";
|
||||||
|
|
||||||
|
for (const width of [1440, 390]) {
|
||||||
|
test.describe(`theme toggle at ${width}px`, () => {
|
||||||
|
test.use({ viewport: { width, height: 844 } });
|
||||||
|
|
||||||
|
test("morphs in place, ignores rapid reversals, and stays keyboard accessible", async ({ page }) => {
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
localStorage.setItem("nanobot.locale", "en");
|
||||||
|
if (!localStorage.getItem("nanobot-webui.theme")) localStorage.setItem("nanobot-webui.theme", "light");
|
||||||
|
});
|
||||||
|
await page.goto("/browser/fixture.html?case=theme-toggle");
|
||||||
|
const toggle = page.getByRole("button", { name: "Toggle theme from header", exact: true });
|
||||||
|
const icon = toggle.locator("svg");
|
||||||
|
await expect(icon).toHaveAttribute("data-theme", "light");
|
||||||
|
expect(await icon.evaluate(node => node.getAnimations({ subtree: true }).length)).toBe(0);
|
||||||
|
const initialBox = await toggle.boundingBox();
|
||||||
|
|
||||||
|
await toggle.click();
|
||||||
|
await expect(page.locator("html")).toHaveClass(/dark/);
|
||||||
|
const intermediate = await icon.evaluate(async node => {
|
||||||
|
await new Promise(requestAnimationFrame);
|
||||||
|
const transitions = node.getAnimations({ subtree: true });
|
||||||
|
for (const transition of transitions) {
|
||||||
|
transition.pause();
|
||||||
|
transition.currentTime = 100;
|
||||||
|
}
|
||||||
|
const core = getComputedStyle(node.querySelector(":scope > circle")!);
|
||||||
|
const rays = getComputedStyle(node.querySelector(".theme-toggle-rays")!);
|
||||||
|
const result = { radius: parseFloat(core.r), opacity: parseFloat(rays.opacity), count: transitions.length };
|
||||||
|
for (const transition of transitions) transition.play();
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
expect(intermediate.count).toBeGreaterThan(0);
|
||||||
|
expect(intermediate.radius).toBeGreaterThan(5);
|
||||||
|
expect(intermediate.radius).toBeLessThan(8);
|
||||||
|
expect(intermediate.opacity).toBeGreaterThan(0);
|
||||||
|
expect(intermediate.opacity).toBeLessThan(1);
|
||||||
|
expect(await toggle.boundingBox()).toEqual(initialBox);
|
||||||
|
|
||||||
|
await toggle.click();
|
||||||
|
await expect(icon).toHaveAttribute("data-theme", "dark");
|
||||||
|
const input = page.getByRole("textbox");
|
||||||
|
await input.fill("Typing stays available during the theme change");
|
||||||
|
await expect(input).toHaveValue("Typing stays available during the theme change");
|
||||||
|
await expect.poll(() => page.locator("html").getAttribute("data-theme-transition")).toBeNull();
|
||||||
|
await expect(icon).toHaveAttribute("data-theme", "dark");
|
||||||
|
await toggle.focus();
|
||||||
|
await page.keyboard.press("Space");
|
||||||
|
await expect(icon).toHaveAttribute("data-theme", "light");
|
||||||
|
await expect(page.locator("html")).not.toHaveClass(/dark/);
|
||||||
|
await expect(toggle).toBeFocused();
|
||||||
|
await expect.poll(() => page.locator("html").getAttribute("data-theme-transition")).toBeNull();
|
||||||
|
expect(await toggle.boundingBox()).toEqual(initialBox);
|
||||||
|
await page.reload();
|
||||||
|
await expect(icon).toHaveAttribute("data-theme", "light");
|
||||||
|
expect(await icon.evaluate(node => node.getAnimations({ subtree: true }).length)).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test("reduced motion keeps the canvas fade but switches the icon directly", async ({ page }) => {
|
||||||
|
await page.emulateMedia({ reducedMotion: "reduce" });
|
||||||
|
await page.addInitScript(() => {
|
||||||
|
localStorage.setItem("nanobot.locale", "en");
|
||||||
|
localStorage.setItem("nanobot-webui.theme", "dark");
|
||||||
|
});
|
||||||
|
await page.goto("/browser/fixture.html?case=theme-toggle");
|
||||||
|
const toggle = page.getByRole("button", { name: "Toggle theme from header", exact: true });
|
||||||
|
const icon = toggle.locator("svg");
|
||||||
|
const core = icon.locator(":scope > circle");
|
||||||
|
await expect(core).toHaveCSS("r", "5px");
|
||||||
|
await toggle.click();
|
||||||
|
await expect(page.locator("html")).not.toHaveClass(/dark/);
|
||||||
|
await expect(core).toHaveCSS("r", "8px");
|
||||||
|
expect(await icon.evaluate(node => node.getAnimations({ subtree: true }).length)).toBe(0);
|
||||||
|
const middle = await page.locator(".workbench-pane").evaluate(async node => {
|
||||||
|
await new Promise(requestAnimationFrame);
|
||||||
|
const transitions = node.getAnimations();
|
||||||
|
for (const transition of transitions) {
|
||||||
|
transition.pause();
|
||||||
|
transition.currentTime = 140;
|
||||||
|
}
|
||||||
|
const color = getComputedStyle(node).backgroundColor;
|
||||||
|
for (const transition of transitions) transition.play();
|
||||||
|
return color;
|
||||||
|
});
|
||||||
|
const red = Number(middle.match(/[\d.]+/)![0]);
|
||||||
|
expect(red).toBeGreaterThan(48);
|
||||||
|
expect(red).toBeLessThan(255);
|
||||||
|
await expect.poll(() => page.locator("html").getAttribute("data-theme-transition")).toBeNull();
|
||||||
|
await toggle.click();
|
||||||
|
await expect(core).toHaveCSS("r", "5px");
|
||||||
|
expect(await icon.evaluate(node => node.getAnimations({ subtree: true }).length)).toBe(0);
|
||||||
|
});
|
||||||
+3
-6
@@ -8,7 +8,8 @@ import {
|
|||||||
useState,
|
useState,
|
||||||
type ReactNode,
|
type ReactNode,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { ArrowRight, ChevronDown, Eye, EyeOff, Moon, ShieldCheck, Sun, X } from "lucide-react";
|
import { ArrowRight, ChevronDown, Eye, EyeOff, ShieldCheck, X } from "lucide-react";
|
||||||
|
import { ThemeToggleIcon } from "@/components/icons/ThemeToggleIcon";
|
||||||
import { Trans, useTranslation } from "react-i18next";
|
import { Trans, useTranslation } from "react-i18next";
|
||||||
import { channelUiPresentation } from "@/channel-plugins/registry";
|
import { channelUiPresentation } from "@/channel-plugins/registry";
|
||||||
import { StarPrompt } from "@/components/StarPrompt";
|
import { StarPrompt } from "@/components/StarPrompt";
|
||||||
@@ -2844,11 +2845,7 @@ function Shell({
|
|||||||
onClick={toggle}
|
onClick={toggle}
|
||||||
className="h-8 w-8 rounded-full text-muted-foreground/85 hover:bg-accent/40 hover:text-foreground"
|
className="h-8 w-8 rounded-full text-muted-foreground/85 hover:bg-accent/40 hover:text-foreground"
|
||||||
>
|
>
|
||||||
{theme === "dark" ? (
|
<ThemeToggleIcon theme={theme} />
|
||||||
<Sun className="h-4 w-4" />
|
|
||||||
) : (
|
|
||||||
<Moon className="h-4 w-4" />
|
|
||||||
)}
|
|
||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
.theme-toggle-mask {
|
||||||
|
mask-type: luminance;
|
||||||
|
forced-color-adjust: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-toggle-core {
|
||||||
|
r: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-toggle-cutout {
|
||||||
|
transform: translate(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-toggle-rays {
|
||||||
|
transform-origin: 12px 12px;
|
||||||
|
transform: rotate(0deg) scale(1);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-toggle-icon[data-theme="light"] .theme-toggle-core {
|
||||||
|
r: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-toggle-icon[data-theme="light"] .theme-toggle-cutout {
|
||||||
|
transform: translate(-7px, 11px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-toggle-icon[data-theme="light"] .theme-toggle-rays {
|
||||||
|
transform: rotate(-30deg) scale(0.6);
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-reduced-motion: no-preference) {
|
||||||
|
.theme-toggle-core {
|
||||||
|
transition: r 280ms cubic-bezier(0.2, 0.8, 0.2, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-toggle-cutout {
|
||||||
|
transition: transform 280ms cubic-bezier(0.2, 0.8, 0.2, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-toggle-rays {
|
||||||
|
transition: transform 280ms cubic-bezier(0.2, 0.8, 0.2, 1), opacity 160ms ease-out;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { useId } from "react";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
import "./ThemeToggleIcon.css";
|
||||||
|
|
||||||
|
export function ThemeToggleIcon({ theme, className }: {
|
||||||
|
theme: "light" | "dark";
|
||||||
|
className?: string;
|
||||||
|
}) {
|
||||||
|
const id = useId();
|
||||||
|
const cutoutId = `theme-cutout-${id}`;
|
||||||
|
const discId = `theme-disc-${id}`;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
fill="none"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="2"
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
aria-hidden="true"
|
||||||
|
className={cn("theme-toggle-icon h-4 w-4", className)}
|
||||||
|
data-theme={theme}
|
||||||
|
>
|
||||||
|
<defs>
|
||||||
|
<mask id={cutoutId} maskUnits="userSpaceOnUse" x="-2" y="-2" width="28" height="28" className="theme-toggle-mask">
|
||||||
|
<rect x="-2" y="-2" width="28" height="28" fill="white" stroke="none" />
|
||||||
|
<circle className="theme-toggle-cutout" cx="24" cy="-4" r="8" fill="black" stroke="none" />
|
||||||
|
</mask>
|
||||||
|
<clipPath id={discId}>
|
||||||
|
<circle className="theme-toggle-core" cx="12" cy="12" r="8" />
|
||||||
|
</clipPath>
|
||||||
|
</defs>
|
||||||
|
<circle className="theme-toggle-core" cx="12" cy="12" r="8" mask={`url(#${cutoutId})`} />
|
||||||
|
<g clipPath={`url(#${discId})`}>
|
||||||
|
<circle className="theme-toggle-cutout" cx="24" cy="-4" r="8" />
|
||||||
|
</g>
|
||||||
|
<path className="theme-toggle-rays" d="M12 2v2m0 16v2M4.93 4.93l1.42 1.42m11.3 11.3 1.42 1.42M2 12h2m16 0h2M4.93 19.07l1.42-1.42m11.3-11.3 1.42-1.42" />
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
TemporaryChatIcon,
|
TemporaryChatIcon,
|
||||||
} from "@/components/icons/product-icons";
|
} from "@/components/icons/product-icons";
|
||||||
import { Menu, Moon, Sun } from "lucide-react";
|
import { Menu } from "lucide-react";
|
||||||
|
import { ThemeToggleIcon } from "@/components/icons/ThemeToggleIcon";
|
||||||
import { type ReactNode } from "react";
|
import { type ReactNode } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
@@ -195,11 +196,7 @@ function ThemeButton({
|
|||||||
className,
|
className,
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{theme === "dark" ? (
|
<ThemeToggleIcon theme={theme} />
|
||||||
<Sun className="h-4 w-4" />
|
|
||||||
) : (
|
|
||||||
<Moon className="h-4 w-4" />
|
|
||||||
)}
|
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
:root[data-theme-transition="dark"] {
|
||||||
|
--theme-ink-delay: 180ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
:root[data-theme-transition="light"] {
|
||||||
|
--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);
|
||||||
|
}
|
||||||
|
|
||||||
|
:root[data-theme-transition] .thread-composer-surface {
|
||||||
|
transition: background-color 280ms linear, box-shadow 280ms linear, opacity 160ms ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
:root[data-theme-transition] .thread-composer-surface textarea::placeholder {
|
||||||
|
transition: color 0s linear var(--theme-ink-delay);
|
||||||
|
}
|
||||||
@@ -4,9 +4,11 @@ import {
|
|||||||
useCallback,
|
useCallback,
|
||||||
useContext,
|
useContext,
|
||||||
useEffect,
|
useEffect,
|
||||||
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
type ReactNode,
|
type ReactNode,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
import "./theme-transition.css";
|
||||||
|
|
||||||
type Theme = "light" | "dark";
|
type Theme = "light" | "dark";
|
||||||
const STORAGE_KEY = "nanobot-webui.theme";
|
const STORAGE_KEY = "nanobot-webui.theme";
|
||||||
@@ -50,6 +52,15 @@ export function useTheme(): {
|
|||||||
return "light";
|
return "light";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const switchTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
|
useEffect(() => () => {
|
||||||
|
if (switchTimer.current !== null) {
|
||||||
|
clearTimeout(switchTimer.current);
|
||||||
|
switchTimer.current = null;
|
||||||
|
delete document.documentElement.dataset.themeTransition;
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
applyTheme(theme);
|
applyTheme(theme);
|
||||||
try {
|
try {
|
||||||
@@ -59,10 +70,18 @@ export function useTheme(): {
|
|||||||
}
|
}
|
||||||
}, [theme]);
|
}, [theme]);
|
||||||
|
|
||||||
const setTheme = useCallback((t: Theme) => setThemeState(t), []);
|
const setTheme = useCallback((next: Theme) => {
|
||||||
|
if (next === theme || switchTimer.current !== null) return;
|
||||||
|
document.documentElement.dataset.themeTransition = next;
|
||||||
|
setThemeState(next);
|
||||||
|
switchTimer.current = setTimeout(() => {
|
||||||
|
delete document.documentElement.dataset.themeTransition;
|
||||||
|
switchTimer.current = null;
|
||||||
|
}, 600);
|
||||||
|
}, [theme]);
|
||||||
const toggle = useCallback(
|
const toggle = useCallback(
|
||||||
() => setThemeState((t) => (t === "dark" ? "light" : "dark")),
|
() => setTheme(theme === "dark" ? "light" : "dark"),
|
||||||
[],
|
[setTheme, theme],
|
||||||
);
|
);
|
||||||
return { theme, toggle, setTheme };
|
return { theme, toggle, setTheme };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4373,7 +4373,7 @@ describe("App layout", () => {
|
|||||||
|
|
||||||
await waitFor(() => expect(connectSpy).toHaveBeenCalled());
|
await waitFor(() => expect(connectSpy).toHaveBeenCalled());
|
||||||
|
|
||||||
fireEvent.click(screen.getByRole("button", { name: "Toggle theme from header" }));
|
fireEvent.click(await screen.findByRole("button", { name: "Toggle theme from header" }));
|
||||||
expect(toggleThemeSpy).toHaveBeenCalledTimes(1);
|
expect(toggleThemeSpy).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
fireEvent.keyDown(screen.getByRole("separator", { name: "Resize sidebar" }), { key: "Enter" });
|
fireEvent.keyDown(screen.getByRole("separator", { name: "Resize sidebar" }), { key: "Enter" });
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { act, renderHook } from "@testing-library/react";
|
import { act, renderHook } from "@testing-library/react";
|
||||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import { useTheme } from "@/hooks/useTheme";
|
import { useTheme } from "@/hooks/useTheme";
|
||||||
|
|
||||||
describe("useTheme", () => {
|
describe("useTheme", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
localStorage.removeItem("nanobot-webui.theme");
|
localStorage.removeItem("nanobot-webui.theme");
|
||||||
document.documentElement.classList.remove("dark");
|
document.documentElement.classList.remove("dark");
|
||||||
|
|
||||||
@@ -17,6 +18,9 @@ describe("useTheme", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
vi.clearAllTimers();
|
||||||
|
vi.useRealTimers();
|
||||||
|
delete document.documentElement.dataset.themeTransition;
|
||||||
document.querySelector('meta[name="theme-color"]')?.remove();
|
document.querySelector('meta[name="theme-color"]')?.remove();
|
||||||
document.documentElement.classList.remove("dark");
|
document.documentElement.classList.remove("dark");
|
||||||
localStorage.removeItem("nanobot-webui.theme");
|
localStorage.removeItem("nanobot-webui.theme");
|
||||||
@@ -36,4 +40,39 @@ describe("useTheme", () => {
|
|||||||
expect(themeColor?.content).toBe("#ffffff");
|
expect(themeColor?.content).toBe("#ffffff");
|
||||||
expect(localStorage.getItem("nanobot-webui.theme")).toBe("light");
|
expect(localStorage.getItem("nanobot-webui.theme")).toBe("light");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("ignores rapid reversals for 600ms without replaying them later", () => {
|
||||||
|
localStorage.setItem("nanobot-webui.theme", "light");
|
||||||
|
const { result } = renderHook(useTheme);
|
||||||
|
expect(document.documentElement).not.toHaveAttribute("data-theme-transition");
|
||||||
|
|
||||||
|
act(() => result.current.toggle());
|
||||||
|
expect(result.current.theme).toBe("dark");
|
||||||
|
expect(document.documentElement).toHaveAttribute("data-theme-transition", "dark");
|
||||||
|
act(() => {
|
||||||
|
result.current.toggle();
|
||||||
|
result.current.setTheme("light");
|
||||||
|
vi.advanceTimersByTime(599);
|
||||||
|
});
|
||||||
|
act(() => result.current.toggle());
|
||||||
|
expect(result.current.theme).toBe("dark");
|
||||||
|
expect(localStorage.getItem("nanobot-webui.theme")).toBe("dark");
|
||||||
|
|
||||||
|
act(() => vi.advanceTimersByTime(1));
|
||||||
|
expect(document.documentElement).not.toHaveAttribute("data-theme-transition");
|
||||||
|
act(() => vi.advanceTimersByTime(600));
|
||||||
|
expect(result.current.theme).toBe("dark");
|
||||||
|
act(() => result.current.toggle());
|
||||||
|
expect(result.current.theme).toBe("light");
|
||||||
|
expect(localStorage.getItem("nanobot-webui.theme")).toBe("light");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("releases the temporary transition styles when its owner unmounts", () => {
|
||||||
|
const { result, unmount } = renderHook(useTheme);
|
||||||
|
act(() => result.current.toggle());
|
||||||
|
expect(document.documentElement).toHaveAttribute("data-theme-transition");
|
||||||
|
unmount();
|
||||||
|
expect(document.documentElement).not.toHaveAttribute("data-theme-transition");
|
||||||
|
expect(vi.getTimerCount()).toBe(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user