Skip to content

Commit a05a619

Browse files
committed
fix(themer): memoize the applied theme, paint the body and guard the snapshot write
- Build the theme in a `useMemo` keyed on the options the reducer swaps, so opening, prerendering or splitting the sidebar no longer hands the Studio's styled-components context a new theme object - Put the applied theme's background back on `document.body`, which Safari tints its chrome from - Swallow `localStorage` failures when persisting the actor snapshot, like every other write in this module
1 parent 460c37f commit a05a619

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎plugins/@sanity/themer/src/tool/plugin.tsx‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {ThemeProvider} from '@sanity/ui'
2-
import {useReducer, use, useState} from 'react'
2+
import type {RootTheme} from '@sanity/ui/theme'
3+
import {use, useEffect, useMemo, useReducer, useState} from 'react'
34
import {browser} from 'react-dom'
4-
import {definePlugin, type LayoutProps} from 'sanity'
5+
import {definePlugin, type LayoutProps, useColorSchemeValue} from 'sanity'
56

67
import {buildTheme} from '../theme/buildTheme'
78
import type {BuildThemeOptions} from '../theme/options'
@@ -119,8 +120,17 @@ function ThemerProvider({
119120
// Split screen needs some space
120121
const split = state.split && !isTooSmallForSplitScreen
121122

123+
// The theme identity must be stable between renders: it feeds the
124+
// styled-components theme context for the whole Studio, and rebuilding it
125+
// would re-render everything. Keying on the options, which the reducer only
126+
// replaces when they change, keeps it stable across every other render.
127+
const theme = useMemo(
128+
() => (state.theme === null ? undefined : buildTheme(state.theme)),
129+
[state.theme],
130+
)
131+
122132
return (
123-
<ThemeProvider theme={(state.theme !== null ? buildTheme(state.theme) : null) ?? undefined}>
133+
<ThemeProvider theme={theme}>
124134
<PluginConfigContext value={config}>
125135
<ThemerLayout
126136
dispatch={dispatch}
@@ -133,6 +143,33 @@ function ThemerProvider({
133143
{children}
134144
</ThemerLayout>
135145
</PluginConfigContext>
146+
<BodyBackground theme={theme} />
136147
</ThemeProvider>
137148
)
138149
}
150+
151+
/**
152+
* The Studio paints the body with its configured theme from above this layout,
153+
* and Safari tints its chrome from the body — so the applied theme has to
154+
* reach the body too, in the scheme the Studio is showing.
155+
*/
156+
function BodyBackground({theme}: {theme: RootTheme | undefined}) {
157+
const scheme = useColorSchemeValue()
158+
159+
useEffect(() => {
160+
const background = theme?.v2?.color[scheme].default.bg
161+
162+
if (background === undefined) return undefined
163+
164+
const {style} = document.body
165+
const previous = style.backgroundColor
166+
167+
style.backgroundColor = background
168+
169+
return () => {
170+
style.backgroundColor = previous
171+
}
172+
}, [scheme, theme])
173+
174+
return null
175+
}

‎plugins/@sanity/themer/src/tool/storage.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,5 +320,9 @@ export function readPersistedSnapshot():
320320
export function writePersistedSnapshot(
321321
snapshot: ReturnType<ActorRefFrom<typeof themerMachine>['getPersistedSnapshot']>,
322322
): void {
323-
localStorage.setItem(PERSIST_SNAPSHOT_KEY, JSON.stringify(snapshot))
323+
try {
324+
localStorage.setItem(PERSIST_SNAPSHOT_KEY, JSON.stringify(snapshot))
325+
} catch {
326+
// Storage can be unavailable (e.g. private browsing) — the snapshot just won't persist
327+
}
324328
}

0 commit comments

Comments
 (0)