Skip to content

Commit 9779f0d

Browse files
committed
Delay nav highlight until icon layout settles
1 parent ed57ae1 commit 9779f0d

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/components/nav-card.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default function NavCard() {
7878
const [hoveredIndex, setHoveredIndex] = useState<number>(0)
7979
const [highlightStyle, setHighlightStyle] = useState<React.CSSProperties | null>(null)
8080
const [animateIconsHighlight, setAnimateIconsHighlight] = useState(false)
81+
const [highlightReady, setHighlightReady] = useState(false)
8182
const itemRefs = useRef<Array<HTMLAnchorElement | null>>([])
8283
const iconContainerRef = useRef<HTMLDivElement | null>(null)
8384
const previousFormRef = useRef<'full' | 'mini' | 'icons' | null>(null)
@@ -131,16 +132,34 @@ export default function NavCard() {
131132
const previousForm = previousFormRef.current
132133
if (form !== 'icons') {
133134
setAnimateIconsHighlight(false)
135+
setHighlightReady(true)
134136
previousFormRef.current = form
135137
return
136138
}
137139

138-
setAnimateIconsHighlight(previousForm === 'icons')
140+
if (previousForm !== 'icons') {
141+
setAnimateIconsHighlight(false)
142+
setHighlightReady(false)
143+
const frame = window.requestAnimationFrame(() => {
144+
setHighlightReady(true)
145+
})
146+
previousFormRef.current = form
147+
return () => window.cancelAnimationFrame(frame)
148+
}
149+
150+
setAnimateIconsHighlight(true)
151+
setHighlightReady(true)
139152
previousFormRef.current = form
140153
}, [form, pathname])
141154

155+
142156
useLayoutEffect(() => {
143157
if (form === 'icons') {
158+
if (!highlightReady) {
159+
setHighlightStyle(null)
160+
return
161+
}
162+
144163
const activeItem = itemRefs.current[hoveredIndex]
145164
const container = iconContainerRef.current
146165
if (!activeItem || !container) {
@@ -165,7 +184,8 @@ export default function NavCard() {
165184
width: '100%',
166185
height: itemHeight
167186
})
168-
}, [form, hoveredIndex, itemHeight, maxSM, maxXS, pathname, size.width])
187+
}, [form, hoveredIndex, itemHeight, maxSM, maxXS, pathname, size.width, highlightReady])
188+
169189

170190
if (maxSM) position = { x: center.x - size.width / 2, y: 16 }
171191

@@ -207,6 +227,7 @@ export default function NavCard() {
207227
if (form === 'icons') setHoveredIndex(activeIndex ?? 0)
208228
}}>
209229
{highlightStyle &&
230+
highlightReady &&
210231
(form === 'icons' ? (
211232
<motion.div
212233
className='pointer-events-none absolute max-w-[230px] rounded-full border'

0 commit comments

Comments
 (0)