thien.me icon indicating copy to clipboard operation
thien.me copied to clipboard

CLS in navbar during page change

Open BretHudson opened this issue 3 years ago • 3 comments

https://user-images.githubusercontent.com/1000405/154853052-170258cc-7b8d-496c-9be6-5caca98fb935.mp4

The navbar is experiencing CLS on page change. The source of this FOUC seems to be that the light/dark mode toggle is not visible during the initial render of a new page.

There are a few approaches to fixing this. Let me know if you have any questions!

BretHudson avatar Feb 20 '22 16:02 BretHudson

yea. I started noticing the flash. I thought it was because of framer-motion. I would love your help on fixing this. Thank you so much.

thienjs avatar Feb 20 '22 17:02 thienjs

Without looking at your code, I would assume there's something causing the toggle to only render after it is mounted. Due to this, when the toggle component is mounted, it adds it to the DOM and the navbar's flex recalculates to accommodate the newly placed element.

One simple solution would be to put a dummy icon/empty div in place of the toggle with the size of the icon when it hasn't yet been mounted, to "reserve" that space in the DOM before the actual toggle gets rendered.

I also just updated the title because I'm realizing it's not FOUC but CLS.

BretHudson avatar Feb 20 '22 21:02 BretHudson

Hey Bret, I tried a few things. This is what I came up with for now. Let me know what you think. Thank you so much for the help.

thienjs avatar Mar 01 '22 01:03 thienjs