Skip to content

Commit e945529

Browse files
committed
fix(themer): restore configured theme options, body tint, reduced motion and split preview behaviour
- thread the documented themerTool({config}) options back through to ThemerLayout as baseOptions, instead of hardcoding {config: {}} - start the tool reducer's theme as null so the first paint inherits the Studio's configured theme instead of building the stock one - put the applied theme's background back on document.body, which Safari tints its chrome from - keep the navbar color wheel still for prefers-reduced-motion: reduce - drive the color wheel stories through the icon's imperative handle and its busy loop, which replaced the progress prop - end the split preview when the sidebar closes - drop the split copy in from the top where the copies stack on small screens, instead of sliding it in from the side
1 parent fcb2aaf commit e945529

8 files changed

Lines changed: 100 additions & 74 deletions

File tree

‎dev/storybook/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"@vanilla-extract/css": "catalog:",
3232
"@vitejs/plugin-react": "^6.1.1",
3333
"@vitest/browser-playwright": "^4.1.11",
34-
"motion": "catalog:",
3534
"oxc-transform-react": "catalog:",
3635
"playwright": "^1.63.0",
3736
"react": "catalog:",
Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import {Button, Flex, Text} from '@sanity/ui'
22
import type {Meta, StoryObj} from '@storybook/react-vite'
3-
import {animate, useMotionValue} from 'motion/react'
4-
import {useEffect} from 'react'
3+
import {useRef} from 'react'
54

65
import AnimatedColorWheelIcon from '../../../../../plugins/@sanity/themer/src/tool/AnimatedColorWheelIcon'
7-
import {ANIMATION_DURATION} from '../../../../../plugins/@sanity/themer/src/tool/colorWheel'
86

97
interface WheelArgs {
10-
progress: number
8+
busy: boolean
119
}
1210

1311
/**
@@ -21,54 +19,38 @@ const meta: Meta<WheelArgs> = {
2119
export default meta
2220
type Story = StoryObj<WheelArgs>
2321

24-
function NavbarButton() {
25-
const progress = useMotionValue(0)
26-
27-
const play = () => {
28-
// A run in progress plays out, like on the Studio navbar
29-
if (progress.isAnimating()) return
30-
31-
animate(progress, [0, 1], {duration: ANIMATION_DURATION, ease: 'linear'})
32-
}
22+
function NavbarButton({busy}: WheelArgs) {
23+
const icon = useRef<{play: () => void}>(null)
3324

3425
return (
3526
<Flex align="center" gap={3}>
3627
<Button
28+
aria-busy={busy}
3729
aria-label="Themer"
38-
icon={<AnimatedColorWheelIcon progress={progress} />}
30+
icon={<AnimatedColorWheelIcon busy={busy} ref={icon} />}
3931
mode="bleed"
40-
onMouseEnter={play}
32+
// A run in progress plays out, like on the Studio navbar
33+
// oxlint-disable-next-line storybook/context-in-play-function -- the icon's imperative handle, not another story's play function
34+
onMouseEnter={() => icon.current?.play()}
35+
selected={busy}
4136
/>
4237
<Text muted size={1}>
43-
Hover the button to play the animation
38+
{busy ? 'The wheel laps until the code arrives' : 'Hover the button to play the animation'}
4439
</Text>
4540
</Flex>
4641
)
4742
}
4843

4944
/** The navbar toggle, which plays the animation on hover */
5045
export const Default: Story = {
51-
render: () => <NavbarButton />,
52-
}
53-
54-
function WheelAtProgress(props: {progress: number}) {
55-
const progress = useMotionValue(props.progress)
56-
57-
useEffect(() => {
58-
progress.set(props.progress)
59-
}, [progress, props.progress])
60-
61-
return (
62-
<Text size={4}>
63-
<AnimatedColorWheelIcon progress={progress} />
64-
</Text>
65-
)
46+
args: {busy: false},
47+
render: (args) => <NavbarButton busy={args.busy} />,
6648
}
6749

68-
/** The wheel at any point of the animation, from 0 to 1 */
69-
export const Progress: Story = {
70-
args: {progress: 0.5},
71-
argTypes: {progress: {control: {type: 'range', min: 0, max: 1, step: 0.01}}},
72-
parameters: {controls: {include: ['progress']}},
73-
render: (args) => <WheelAtProgress progress={args.progress} />,
50+
/** The toggle while the sidebar's code loads, where the wheel laps until it has arrived */
51+
export const Busy: Story = {
52+
args: {busy: true},
53+
argTypes: {busy: {control: {type: 'boolean'}}},
54+
parameters: {controls: {include: ['busy']}},
55+
render: (args) => <NavbarButton busy={args.busy} />,
7456
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {usePrefersReducedMotion} from '@sanity/ui'
12
import {motion, MotionValue, useTransform, animate, useMotionValue} from 'motion/react'
23
import {useEffect, useImperativeHandle} from 'react'
34

@@ -76,7 +77,10 @@ export default function AnimatedColorWheelIcon({
7677
ref: React.Ref<{play: () => void}>
7778
}) {
7879
const progress = useMotionValue(0)
79-
const rotate = useTransform(progress, (value) => wheelRotation(value))
80+
const prefersReducedMotion = usePrefersReducedMotion()
81+
const rotate = useTransform(progress, (value) =>
82+
prefersReducedMotion ? 0 : wheelRotation(value),
83+
)
8084

8185
useImperativeHandle(
8286
ref,

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ function sameView(a: ThemerView, b: ThemerView): boolean {
9292
}
9393

9494
interface ThemerLayoutProps extends ToolReducerState {
95+
/** The `buildTheme` options the Studio's configured theme was generated from */
96+
baseOptions: BuildThemeOptions
9597
children: React.ReactNode
9698
}
9799

@@ -124,14 +126,14 @@ interface ThemerLayoutProps extends ToolReducerState {
124126
* @internal
125127
*/
126128
export function ThemerLayout({
129+
baseOptions,
127130
children,
128131
open,
129132
prerender,
130133
prerenderSplitScreen,
131134
split,
132135
theme: currentThemeOptions,
133136
}: ThemerLayoutProps) {
134-
const baseOptions = {config: {}}
135137
const [input] = useState<ThemerInput>(() => ({baseOptions, stored: readStoredState()}))
136138
const [snapshot, send, actorRef] = useActor(themerMachine, {input})
137139
const stored = useSelector(actorRef, selectStoredState, sameStoredState)
@@ -192,6 +194,21 @@ export function ThemerLayout({
192194
// const scheme = useDeferredValue(useColorSchemeValue())
193195
const oppositeScheme: ThemeColorSchemeKey = scheme === 'dark' ? 'light' : 'dark'
194196

197+
useEffect(() => {
198+
const background = theme?.v2?.color[scheme].default.bg
199+
200+
if (background === undefined) return undefined
201+
202+
const {style} = document.body
203+
const previous = style.backgroundColor
204+
205+
style.backgroundColor = background
206+
207+
return () => {
208+
style.backgroundColor = previous
209+
}
210+
}, [scheme, theme])
211+
195212
// The machine releases the image of a palette as it is replaced or its
196213
// theme deleted; whatever it still holds goes with the layout — the actor
197214
// keeps its last snapshot once stopped
@@ -234,6 +251,10 @@ export function ThemerLayout({
234251
],
235252
)
236253

254+
// Side by side the split copy comes in from the side it sits on; stacked
255+
// on small screens it comes in from the top
256+
const splitscreen = viewTransitionClasses.splitscreen[mobile ? 'column' : 'row']
257+
237258
// What the view transitions do this render, from what the machine says
238259
const classes = reduceMotion
239260
? {
@@ -278,9 +299,9 @@ export function ThemerLayout({
278299
<Activity mode={split && open ? 'visible' : 'hidden'}>
279300
<ViewTransition
280301
default="none"
281-
enter={viewTransitionClasses.splitscreen.enter}
282-
update={viewTransitionClasses.splitscreen.update}
283-
exit={viewTransitionClasses.splitscreen.exit}
302+
enter={splitscreen.enter}
303+
update={splitscreen.update}
304+
exit={splitscreen.exit}
284305
// enter={classes.copyEnter}
285306
// exit={classes.copyExit}
286307
// onEnter={transitioned}

‎plugins/@sanity/themer/src/tool/ViewTransitions.css.ts‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,35 @@ const resizeStudioTransition = createViewTransitionType(
120120
'resizeStudio',
121121
)
122122

123+
/**
124+
* The split copy's classes for one layout direction: it comes in from the
125+
* edge it sits on — beside the Studio, or above it where the copies stack
126+
*/
127+
function createSplitscreenClasses(enterAnimation: string, exitAnimation: string, debugId: string) {
128+
return {
129+
enter: {
130+
[viewTransitionTypes['split-creen:open']]: createViewTransitionType(
131+
{new: {animationName: enterAnimation}},
132+
`${debugId}In`,
133+
),
134+
debug: redOutlines,
135+
default: 'none',
136+
},
137+
update: {
138+
[viewTransitionTypes.crossfade]: 'auto',
139+
default: 'none',
140+
},
141+
exit: {
142+
[viewTransitionTypes['split-creen:close']]: createViewTransitionType(
143+
{old: {animationName: exitAnimation}},
144+
`${debugId}Out`,
145+
),
146+
debug: redOutlines,
147+
default: 'none',
148+
},
149+
} as const satisfies Record<string, ViewTransitionClass>
150+
}
151+
123152
const animationDuration = '5s'
124153
export const viewTransitionClasses = {
125154
[viewTransitionTypes.open]: createViewTransitionType({
@@ -172,27 +201,9 @@ export const viewTransitionClasses = {
172201
},
173202
} as const satisfies Record<string, ViewTransitionClass>,
174203
splitscreen: {
175-
enter: {
176-
[viewTransitionTypes['split-creen:open']]: createViewTransitionType(
177-
{new: {animationName: slideIn}},
178-
'slideIn',
179-
),
180-
debug: redOutlines,
181-
default: 'none',
182-
},
183-
update: {
184-
[viewTransitionTypes.crossfade]: 'auto',
185-
default: 'none',
186-
},
187-
exit: {
188-
[viewTransitionTypes['split-creen:close']]: createViewTransitionType(
189-
{old: {animationName: slideOut}},
190-
'slideOut',
191-
),
192-
debug: redOutlines,
193-
default: 'none',
194-
},
195-
} as const satisfies Record<string, ViewTransitionClass>,
204+
row: createSplitscreenClasses(slideIn, slideOut, 'splitSlide'),
205+
column: createSplitscreenClasses(dropIn, dropOut, 'splitDrop'),
206+
},
196207
studio: {
197208
enter: {
198209
debug: redOutlines,

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {ThemerNavbar} from './ThemerNavbar'
2323
*
2424
* @alpha
2525
*/
26-
export interface ThemerToolOptions {
26+
export interface ThemerToolOptions extends PluginConfig {
2727
/**
2828
* The `buildTheme` options that the Studio's configured theme was generated
2929
* from — the themer starts editing from these, so pass the same object that
@@ -69,10 +69,12 @@ export interface ThemerToolOptions {
6969
*
7070
* @alpha
7171
*/
72-
export const themerTool = definePlugin<PluginConfig | void>((options) => {
73-
const {title = 'Themer'} = options ?? {}
72+
export const themerTool = definePlugin<ThemerToolOptions | void>((options) => {
73+
// No options generate the stock theme, which is what a Studio without a
74+
// `theme` in its config gets
75+
const {config: baseOptions = {}, title = 'Themer'} = options ?? {}
7476

75-
const ThemerLayoutProvider = defineThemerLayout({title})
77+
const ThemerLayoutProvider = defineThemerLayout({title}, baseOptions)
7678

7779
return {
7880
name: '@sanity/themer/tool',
@@ -91,18 +93,24 @@ function useIsMobile() {
9193
return index <= 2
9294
}
9395

94-
function defineThemerLayout(config: Required<PluginConfig>) {
96+
function defineThemerLayout(config: Required<PluginConfig>, baseOptions: BuildThemeOptions) {
9597
return function DefinedThemerLayoutProvider(props: LayoutProps) {
9698
'use memo'
9799

98-
return <ThemerLayoutProvider config={config}>{props.renderDefault(props)}</ThemerLayoutProvider>
100+
return (
101+
<ThemerLayoutProvider baseOptions={baseOptions} config={config}>
102+
{props.renderDefault(props)}
103+
</ThemerLayoutProvider>
104+
)
99105
}
100106
}
101107

102108
function ThemerLayoutProvider({
109+
baseOptions,
103110
children,
104111
config,
105112
}: {
113+
baseOptions: BuildThemeOptions
106114
children: React.ReactNode
107115
config: Required<PluginConfig>
108116
}) {
@@ -112,7 +120,7 @@ function ThemerLayoutProvider({
112120
prerenderSplitScreen: false,
113121
open: false,
114122
split: false,
115-
theme: undefined,
123+
theme: null,
116124
})
117125

118126
return (
@@ -122,6 +130,7 @@ function ThemerLayoutProvider({
122130
<ToolIsOpenContext value={state.open}>
123131
<BusyTransitionContext value={busy}>
124132
<ThemerLayout
133+
baseOptions={baseOptions}
125134
prerender={state.prerender}
126135
prerenderSplitScreen={state.prerenderSplitScreen}
127136
split={state.split}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ export function toolReducer(state: ToolReducerState, action: ToolReducerAction):
4545
open: !state.open,
4646
prerender: true,
4747
}
48+
// The split preview belongs to a sidebar session: closing the sidebar
49+
// ends the split with it
4850
case 'close':
4951
return state.open
5052
? {
5153
...state,
5254
open: false,
55+
split: false,
5356
}
5457
: state
5558
case 'split-screen:prerender':

‎pnpm-lock.yaml‎

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)