fix(web): reload the installed iOS app when the theme changes
An iOS home-screen app only reads theme-color on page load, so updating the meta in place left the status bar with the previous theme's color. Reload when the resolved theme changes; the pre-paint script applies the stored theme before the first paint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -47,6 +47,15 @@ function systemTheme(): ResolvedTheme {
|
|||||||
return window.matchMedia(DARK_QUERY).matches ? 'dark' : 'light'
|
return window.matchMedia(DARK_QUERY).matches ? 'dark' : 'light'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveTheme(theme: Theme): ResolvedTheme {
|
||||||
|
return theme === 'system' ? systemTheme() : theme
|
||||||
|
}
|
||||||
|
|
||||||
|
/** iOS home-screen app (`navigator.standalone` only exists in iOS Safari). */
|
||||||
|
function isIosStandalone() {
|
||||||
|
return 'standalone' in navigator && navigator.standalone === true
|
||||||
|
}
|
||||||
|
|
||||||
export function ThemeProvider({ children }: { children: ReactNode }) {
|
export function ThemeProvider({ children }: { children: ReactNode }) {
|
||||||
const [theme, setThemeState] = useState<Theme>(readStoredTheme)
|
const [theme, setThemeState] = useState<Theme>(readStoredTheme)
|
||||||
const [systemDark, setSystemDark] = useState(() => systemTheme() === 'dark')
|
const [systemDark, setSystemDark] = useState(() => systemTheme() === 'dark')
|
||||||
@@ -68,14 +77,22 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
|
|||||||
?.setAttribute('content', THEME_COLORS[resolvedTheme])
|
?.setAttribute('content', THEME_COLORS[resolvedTheme])
|
||||||
}, [resolvedTheme])
|
}, [resolvedTheme])
|
||||||
|
|
||||||
const setTheme = useCallback((next: Theme) => {
|
const setTheme = useCallback(
|
||||||
setThemeState(next)
|
(next: Theme) => {
|
||||||
try {
|
setThemeState(next)
|
||||||
window.localStorage.setItem(STORAGE_KEY, next)
|
try {
|
||||||
} catch {
|
window.localStorage.setItem(STORAGE_KEY, next)
|
||||||
// Ignore storage failures: the theme still applies for this session.
|
} catch {
|
||||||
}
|
// Ignore storage failures: the theme still applies for this session.
|
||||||
}, [])
|
}
|
||||||
|
|
||||||
|
// An installed iOS app only reads `theme-color` when the page loads, so
|
||||||
|
// the status bar keeps the old color until a reload. The stored theme is
|
||||||
|
// applied again before the first paint by the script in `index.html`.
|
||||||
|
if (isIosStandalone() && resolveTheme(next) !== resolvedTheme) window.location.reload()
|
||||||
|
},
|
||||||
|
[resolvedTheme],
|
||||||
|
)
|
||||||
|
|
||||||
const value = useMemo(
|
const value = useMemo(
|
||||||
() => ({ theme, resolvedTheme, setTheme }),
|
() => ({ theme, resolvedTheme, setTheme }),
|
||||||
|
|||||||
Reference in New Issue
Block a user