Skip to content

Commit b412054

Browse files
committed
Fix flash of diff sidebar and toggle button during page load
Prevent the right drawer and toggle button from rendering during SSR/hydration by adding a mounted state check. Components only render after the client has mounted and session storage state has been properly read.
1 parent 3c06370 commit b412054

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/features/sidebar/view/SecondarySplitHeader.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ const ToggleDiffButton = ({
123123
isDiffAvailable: boolean
124124
}) => {
125125
const [isMac, setIsMac] = useState(false)
126+
// Prevent flash of button during SSR/hydration. Only render after client mount.
127+
const [mounted, setMounted] = useState(false)
126128
useEffect(() => {
129+
setMounted(true)
127130
// checkIsMac uses window so we delay the check.
128131
const timeout = window.setTimeout(() => {
129132
setIsMac(checkIsMac())
@@ -138,6 +141,7 @@ const ToggleDiffButton = ({
138141
: isDiffbarOpen
139142
? "Hide changes"
140143
: "Show changes"
144+
if (!mounted) return null
141145
return (
142146
<Box sx={{ display: isSidebarTogglable ? "block" : "none" }}>
143147

src/features/sidebar/view/internal/tertiary/RightContainer.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client'
22

3+
import { useState, useEffect } from "react"
34
import { SxProps } from "@mui/system"
45
import { Drawer as MuiDrawer } from "@mui/material"
56
import { useTheme } from "@mui/material/styles"
@@ -15,6 +16,13 @@ const RightContainer = ({
1516
onClose?: () => void
1617
children?: React.ReactNode
1718
}) => {
19+
// Prevent flash of drawer during SSR/hydration. Only render after client mount
20+
// when session storage state has been properly read.
21+
const [mounted, setMounted] = useState(false)
22+
useEffect(() => {
23+
setMounted(true)
24+
}, [])
25+
if (!mounted) return null
1826
return (
1927
<>
2028
<InnerRightContainer

0 commit comments

Comments
 (0)