瀏覽代碼

fix: switch theme at once on Safari, where the reveal flashed the glass

WebKit (Safari, and every browser on iOS, Chrome included) paints a view
transition's snapshots without backdrop blur, so the glass nav and cards
went clear for the length of the circular reveal and then snapped back.
WebKit browsers now switch the theme instantly; Chromium, whose
snapshots keep the blur, keeps the reveal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QEPoSHwHCYMNpm2iKxY9hL
Claude 1 月之前
父節點
當前提交
eb1793b17c
共有 1 個文件被更改,包括 16 次插入 和 1 次删除
  1. 16 1
      lib/theme-transition.ts

+ 16 - 1
lib/theme-transition.ts

@@ -8,6 +8,17 @@ type ViewTransitionDoc = Document & {
   startViewTransition?: (update: () => void) => { ready: Promise<void> };
 };
 
+/**
+ * WebKit (Safari, and every browser on iOS) paints view-transition snapshots
+ * without backdrop blur, so the glass nav and cards turned clear for the
+ * length of the reveal and flashed back. There the theme switches at once.
+ */
+const snapshotsDropGlass = () => {
+  const ua = navigator.userAgent;
+
+  return /AppleWebKit/.test(ua) && !/(Chrome|Chromium|Edg|OPR)\//.test(ua);
+};
+
 /**
  * Switches the theme with a circular reveal that grows from `origin`
  * (usually the button that was clicked). Falls back to an instant switch.
@@ -24,7 +35,11 @@ export function switchTheme(
     flushSync(() => setTheme(next));
   };
 
-  if (!doc.startViewTransition || prefersReducedMotion()) {
+  if (
+    !doc.startViewTransition ||
+    prefersReducedMotion() ||
+    snapshotsDropGlass()
+  ) {
     apply();
 
     return;