Skip to content

Commit 13417e9

Browse files
committed
fix(themer): keep the Studio resizing on sidebar toggles, hide the toggle on mobile
Opening or closing the sidebar while the split preview was on added a `split-creen:*` type next to `open`/`close`. React joins every matching class in a `ViewTransition` type map, so the Studio picked up the split's blur fade on top of its resize class and dissolved instead of resizing. The sidebar's own types now carry the split copy's enter and exit as well. The navbar toggle also stayed visible below the media index where the layout forces the sidebar closed, so pressing it only spun the icon and left state behind that a later resize could act on. It is kept out of the navbar there, on the same breakpoint the layout gates `open` on.
1 parent 5d136dd commit 13417e9

5 files changed

Lines changed: 56 additions & 16 deletions

File tree

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import {ColorWheelIcon} from '@sanity/icons/ColorWheel'
2-
import {Box, Button, Text} from '@sanity/ui'
2+
import {Box, Button, Text, useMediaIndex} from '@sanity/ui'
33
import {Tooltip} from '@sanity/ui/tooltip'
44
import {lazy, startTransition, Suspense, use, useRef, useTransition, ViewTransition} from 'react'
55
import {type NavbarProps} from 'sanity'
66
import type {NavbarAction} from 'sanity/_dangerously_use_private_internals_that_do_not_follow_semver'
77

88
import {addThemerTransitionType} from './addThemerTransitionType'
9-
import {PluginConfigContext, ToolDispatchContext, ToolIsOpenContext, ToolSplitIsOpenContext} from './context'
9+
import {
10+
PluginConfigContext,
11+
SIDEBAR_MIN_MEDIA_INDEX,
12+
ToolDispatchContext,
13+
ToolIsOpenContext,
14+
} from './context'
1015

1116
import {viewTransitionClasses} from './ViewTransitions.css'
1217

@@ -17,17 +22,17 @@ function ThemerNavbarButton() {
1722
const iconRef = useRef<{spin: () => void}>(null)
1823
const dispatch = use(ToolDispatchContext)
1924
const open = use(ToolIsOpenContext)
20-
const split = use(ToolSplitIsOpenContext)
2125

26+
// The sidebar's own type only — a split type would also pick the Studio's
27+
// blur fade out of `viewTransitionClasses.studio.update`, which belongs to
28+
// toggling the split in place; the split copy comes along on these types too
2229
const handleOpen = () =>
2330
startBusyTransition(() => {
24-
if(split) addThemerTransitionType('split-creen:open')
2531
addThemerTransitionType('open')
2632
dispatch({type: 'open'})
2733
})
2834
const handleClose = () =>
2935
startBusyTransition(() => {
30-
if(split) addThemerTransitionType('split-creen:close')
3136
addThemerTransitionType('close')
3237
dispatch({type: 'close'})
3338
})
@@ -78,6 +83,9 @@ function ThemerNavbarButton() {
7883

7984
export function ThemerNavbar(props: NavbarProps) {
8085
'use memo'
86+
// The sidebar stays closed where there is no room for it, so the toggle
87+
// stays out of the navbar there instead of doing nothing when pressed
88+
const isMobile = useMediaIndex() < SIDEBAR_MIN_MEDIA_INDEX
8189
const button = {
8290
location: 'topbar',
8391
name: 'themer-topbar',
@@ -89,6 +97,8 @@ export function ThemerNavbar(props: NavbarProps) {
8997
} satisfies NavbarAction
9098
return props.renderDefault({
9199
...props,
92-
__internal_actions: [...(props.__internal_actions ?? []), button],
100+
__internal_actions: isMobile
101+
? props.__internal_actions
102+
: [...(props.__internal_actions ?? []), button],
93103
})
94104
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ export function ThemerSidebar({split}: Pick<ToolReducerState, 'split'>) {
103103
mode="bleed"
104104
onClick={() =>
105105
startTransition(() => {
106-
if(split) addThemerTransitionType('split-creen:close')
107106
addThemerTransitionType('close')
108107
dispatch({type: 'close'})
109108
})

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,31 @@ const resizeStudioTransition = createViewTransitionType(
118118

119119
const splitScreenFilter = 'blur(20px)'
120120

121+
// The split copy slides in from the side and out to it — whether the split itself
122+
// is toggling, or the sidebar it comes and goes with is opening or closing
123+
const splitScreenEnterTransition = createViewTransitionType(
124+
{
125+
new: {
126+
animationName: keyframes({
127+
'from': {transform: 'translateX(-100%)', opacity: 0},
128+
'10%': {opacity: 0},
129+
}),
130+
},
131+
},
132+
'slideIn',
133+
)
134+
const splitScreenExitTransition = createViewTransitionType(
135+
{
136+
old: {
137+
animationName: keyframes({
138+
'to': {transform: 'translateX(-100%)', opacity: 0},
139+
'90%': {opacity: 0},
140+
}),
141+
},
142+
},
143+
'slideOut',
144+
)
145+
121146
const animationDuration = '5s'
122147
export const viewTransitionClasses = {
123148
[viewTransitionTypes.open]: createViewTransitionType({
@@ -175,10 +200,8 @@ export const viewTransitionClasses = {
175200
} as const satisfies Record<string, ViewTransitionClass>,
176201
splitscreen: {
177202
enter: {
178-
[viewTransitionTypes['split-creen:open']]: createViewTransitionType(
179-
{new: {animationName: keyframes({from: {transform: 'translateX(-100%)', opacity: 0}, '10%': {opacity: 0}})}},
180-
'slideIn',
181-
),
203+
[viewTransitionTypes['split-creen:open']]: splitScreenEnterTransition,
204+
[viewTransitionTypes.open]: splitScreenEnterTransition,
182205
debug: redOutlines,
183206
default: 'none',
184207
},
@@ -187,10 +210,8 @@ export const viewTransitionClasses = {
187210
default: 'none',
188211
},
189212
exit: {
190-
[viewTransitionTypes['split-creen:close']]: createViewTransitionType(
191-
{old: {animationName: keyframes({to: {transform: 'translateX(-100%)', opacity: 0}, '90%': {opacity: 0}})}},
192-
'slideOut',
193-
),
213+
[viewTransitionTypes['split-creen:close']]: splitScreenExitTransition,
214+
[viewTransitionTypes.close]: splitScreenExitTransition,
194215
debug: redOutlines,
195216
default: 'none',
196217
},

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ export function useThemer(): ThemerContextValue {
6262
return context
6363
}
6464

65+
/**
66+
* The narrowest media index the sidebar shows at — themer does not support
67+
* mobile yet, so below it the Studio keeps the whole width and the navbar
68+
* leaves its toggle out rather than offering one that opens nothing.
69+
*
70+
* @internal
71+
*/
72+
export const SIDEBAR_MIN_MEDIA_INDEX = 3
73+
6574
/** @internal */
6675
export const ToolIsOpenContext = createContext<boolean>(false)
6776
/** @internal */

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
BusyTransitionContext,
88
type PluginConfig,
99
PluginConfigContext,
10+
SIDEBAR_MIN_MEDIA_INDEX,
1011
StartBusyTransitionContext,
1112
ToolDispatchContext,
1213
ToolIsOpenContext,
@@ -111,7 +112,7 @@ function ThemerLayoutProvider({
111112
theme: null,
112113
})
113114
const index = useMediaIndex()
114-
const isMobile = index <= 2
115+
const isMobile = index < SIDEBAR_MIN_MEDIA_INDEX
115116
// @TODO Themer does not yet support mobile
116117
const open = state.open && !isMobile
117118
// Split screen needs some space

0 commit comments

Comments
 (0)