Fix browser compatibility: guard requestIdleCallback and startViewTransition (#1464)

This commit is contained in:
Konsti Wohlwend 2026-05-21 14:10:51 -07:00 committed by GitHub
parent b8fc04bdbd
commit 3b2e991c78
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View File

@ -22,6 +22,11 @@ export default function ThemeToggle() {
return;
}
if (typeof document.startViewTransition !== "function") {
setTheme(nextTheme);
return;
}
document.documentElement.classList.add("vt-disable-transitions");
const transition = document.startViewTransition(() => {

View File

@ -5,10 +5,15 @@ export function useWaitForIdle(min = 0, max = 5000) {
useEffect(() => {
let cancelled = false;
setTimeout(() => {
requestIdleCallback(() => {
const cb = () => {
if (cancelled) return;
setHasWaited(true);
}, { timeout: max - min });
};
if (typeof requestIdleCallback === "function") {
requestIdleCallback(cb, { timeout: max - min });
} else {
setTimeout(cb, max - min);
}
}, min);
return () => {
cancelled = true;