-
Notifications
You must be signed in to change notification settings - Fork 119
Fix dark mode flicker docs #4250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
View your CI Pipeline Execution ↗ for commit 07a5a8f.
☁️ Nx Cloud last updated this comment at |
React.useEffect(() => { | ||
setIsMacOS(typeof window !== "undefined" && navigator && navigator?.platform.toUpperCase().includes("MAC")); | ||
}, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error came up after removing the delayed rendering as navigator is only available running client side.
Size Change: 0 B Total Size: 1.12 MB ℹ️ View Unchanged
|
@@ -68,7 +73,7 @@ const SiteHeaderSearch: React.FC = () => { | |||
Search | |||
</Text> | |||
</Box> | |||
{breakpointIndex === 0 ? null : ( | |||
{breakpointIndex === 0 || isMacOS === undefined ? null : ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not render the keyboard shortcuts in the search bar until we know what OS they are using
@@ -5,6 +5,16 @@ import { SimpleStorage } from "../utils/SimpleStorage"; | |||
|
|||
export type UseDarkModeReturn = [ValidThemeName, () => void, boolean]; | |||
|
|||
const setCookie = (name: string, value: any, days: number): void => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot use the NextJS cookies tool are this does not run client side and I couldn't get it to work except this way or we add a new cookie dependency
React.useEffect(() => { | ||
const localTheme = SimpleStorage.get("theme") as ValidThemeName; | ||
const cookieTheme = getCookie(themeCookieKey) as ValidThemeName; | ||
|
||
if (window.matchMedia?.("(prefers-color-scheme: dark)").matches && !localTheme) { | ||
if (window.matchMedia?.("(prefers-color-scheme: dark)").matches && !cookieTheme) { | ||
setMode(ValidThemes.DARK); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we are setting the default value from the cookie we don't need local storage anymore. We also don't want to use teh document cookie in this hook as we will still get the flickering issue. The reason this works now is that itis inside a useEffect hook and in functions so we know it would already be rendered in the client.
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 07a5a8f:
|
Paste
|
Project |
Paste
|
Branch Review |
fix-dark-mode-flicker-docs
|
Run status |
|
Run duration | 06m 42s |
Commit |
|
Committer | krisantrobus |
View all properties for this run ↗︎ |
Test results | |
---|---|
|
0
|
|
0
|
|
0
|
|
0
|
|
125
|
View all changes introduced in this branch ↗︎ |
The current problem was that we were using a default theme in the useDarkMode hook that was the light mode. Then once the component was rendered it ran the use effect hook to pull the value from local storage and then updated the theme.
This change captures the cookies at the request level to the server and passes the theme value we store in the cookies to the page to use.
Still see the flicker but I think this is due to SSG (static generated pages at build time). I'm continuing to look at it.