Skip to content

Commit a4dcf6c

Browse files
committed
fix(themer): restore the configured theme, and size the sidebar header
The reducer's theme started out null while the stored theme was applied, so null meant both 'not synced yet' and 'the configured theme'; the layout fell back to a mount-time snapshot and could never show the configured theme again. Resolve the applied theme when the reducer initializes instead. Pass the measured Studio navbar height through ResizableSidebar so the sidebar header lines up with the navbar again.
1 parent 0e4098b commit a4dcf6c

3 files changed

Lines changed: 34 additions & 22 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ function writeStoredWidth(width: number): void {
4747
* @internal
4848
*/
4949
export default function ResizableSidebar({
50+
navbarHeight,
5051
overlay,
5152
split,
52-
}: {overlay: boolean} & Pick<ToolReducerState, 'split'>) {
53+
}: {navbarHeight: number | null; overlay: boolean} & Pick<ToolReducerState, 'split'>) {
5354
const [width, setWidth] = useState(readStoredWidth)
5455
const [dragging, setDragging] = useState(false)
5556
const drag = useRef<{pointerId: number; startX: number; startWidth: number} | null>(null)
@@ -126,7 +127,7 @@ export default function ResizableSidebar({
126127
/>
127128
)}
128129
<Box className={content} height="fill" overflow="hidden">
129-
<ThemerSidebar split={split} />
130+
<ThemerSidebar navbarHeight={navbarHeight} split={split} />
130131
</Box>
131132
</Card>
132133
</Layer>

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,9 @@ export function ThemerLayout({
144144
() => resolveThemes(stored, baseOptions),
145145
[stored, baseOptions],
146146
)
147-
const [initialOptions] = useState(() => {
148-
const {active} = resolveThemes(selectStoredState(actorRef.getSnapshot()), baseOptions)
149-
return active.source === 'config' ? null : active.options
150-
})
151147
const theme = useMemo(
152-
() =>
153-
currentThemeOptions !== null
154-
? buildTheme(currentThemeOptions)
155-
: initialOptions !== null
156-
? buildTheme(initialOptions)
157-
: null,
158-
[currentThemeOptions, initialOptions],
148+
() => (currentThemeOptions === null ? null : buildTheme(currentThemeOptions)),
149+
[currentThemeOptions],
159150
)
160151

161152
// The Studio paints the body with its configured theme from above this
@@ -242,7 +233,13 @@ export function ThemerLayout({
242233
update={viewTransitionClasses.sidebar.update}
243234
exit={viewTransitionClasses.sidebar.exit}
244235
>
245-
{prerender && <ResizableSidebar overlay={isMobile} split={split} />}
236+
{prerender && (
237+
<ResizableSidebar
238+
navbarHeight={navbarHeight}
239+
overlay={isMobile}
240+
split={split}
241+
/>
242+
)}
246243
</ViewTransition>
247244
</Activity>
248245
</Flex>

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,32 @@ import {
99
ToolIsOpenContext,
1010
ToolSplitIsOpenContext,
1111
} from './context'
12-
import {toolReducer} from './reducer'
12+
import {toolReducer, type ToolReducerState} from './reducer'
13+
import {readStoredState} from './storage'
1314
import {ThemerLayout} from './ThemerLayout'
1415
import {ThemerNavbar} from './ThemerNavbar'
16+
import {resolveThemes} from './themes'
1517
import {useIsMobile} from './useIsMobile'
1618
import {useIsTooSmallForSplitScreen} from './useIsTooSmallForSplitScreen'
1719

20+
/**
21+
* The tool starts out with the theme the last session left applied, so that
22+
* `theme: null` only ever means the Studio's configured theme — picking that
23+
* one back restores it.
24+
*/
25+
function createInitialState(): ToolReducerState {
26+
// The same (not yet configurable) base options the layout resolves with
27+
const {active} = resolveThemes(readStoredState(), {})
28+
29+
return {
30+
prerender: false,
31+
prerenderSplitScreen: false,
32+
open: false,
33+
split: false,
34+
theme: active.source === 'config' ? null : active.options,
35+
}
36+
}
37+
1838
/**
1939
* Options for the {@link themerTool} plugin.
2040
*
@@ -100,13 +120,7 @@ function ThemerLayoutProvider({
100120
children: React.ReactNode
101121
config: Required<PluginConfig>
102122
}) {
103-
const [state, dispatch] = useReducer(toolReducer, {
104-
prerender: false,
105-
prerenderSplitScreen: false,
106-
open: false,
107-
split: false,
108-
theme: null,
109-
})
123+
const [state, dispatch] = useReducer(toolReducer, undefined, createInitialState)
110124
const isMobile = useIsMobile()
111125
const isTooSmallForSplitScreen = useIsTooSmallForSplitScreen()
112126
// @TODO Themer does not yet support mobile

0 commit comments

Comments
 (0)