-
Notifications
You must be signed in to change notification settings - Fork 119
feat(docs): fix theme flicker #4271
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
Changes from all commits
5a99865
263a235
da31b85
20b1045
3f2f3f5
edba90d
94cbc73
f526059
79b6fb9
990534e
84cefa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import type { ValueOf } from "@twilio-paste/types"; | ||
import { parseCookies, setCookie } from "nookies"; | ||
import * as React from "react"; | ||
|
||
import { SimpleStorage } from "../utils/SimpleStorage"; | ||
|
||
export type UseDarkModeReturn = [ValidThemeName, () => void, boolean]; | ||
export type UseDarkModeReturn = [ValidThemeName | undefined, () => void, boolean]; | ||
|
||
const ValidThemes = { | ||
DEFAULT: "twilio", | ||
|
@@ -13,15 +12,16 @@ const ValidThemes = { | |
type ValidThemeName = ValueOf<typeof ValidThemes>; | ||
|
||
const isValidTheme = (themeName: ValidThemeName): boolean => { | ||
return themeName === ValidThemes.DEFAULT || themeName === ValidThemes.DARK; | ||
return Object.values(ValidThemes).includes(themeName); | ||
}; | ||
|
||
export const useDarkMode = (): UseDarkModeReturn => { | ||
const [theme, setTheme] = React.useState<ValidThemeName>(ValidThemes.DEFAULT); | ||
const [theme, setTheme] = React.useState<ValidThemeName>(); | ||
const [componentMounted, setComponentMounted] = React.useState(false); | ||
|
||
const setMode = (mode: ValidThemeName): void => { | ||
SimpleStorage.set("theme", mode); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to use local storage, all done via cookies now |
||
setCookie(null, "paste-docs-theme", mode, { path: "/" }); | ||
document.body.dataset.theme = mode; | ||
setTheme(mode); | ||
}; | ||
|
||
|
@@ -34,7 +34,7 @@ export const useDarkMode = (): UseDarkModeReturn => { | |
}; | ||
|
||
React.useEffect(() => { | ||
const localTheme = SimpleStorage.get("theme") as ValidThemeName; | ||
const localTheme = parseCookies()["paste-docs-theme"] as ValidThemeName; | ||
|
||
if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches && !localTheme) { | ||
setMode(ValidThemes.DARK); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { datadogRum } from "@datadog/browser-rum"; | ||
import "@twilio-paste/design-tokens/dist/themes/twilio-dark/tokens.data-theme.css"; | ||
import "@twilio-paste/design-tokens/dist/themes/twilio/tokens.custom-properties.css"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By using the |
||
import { Theme } from "@twilio-paste/theme"; | ||
import type { AppProps } from "next/app"; | ||
import Head from "next/head"; | ||
|
@@ -22,7 +24,7 @@ const App = ({ Component, pageProps }: AppProps): React.ReactElement => { | |
const router = useRouter(); | ||
const localStorageKey = "cookie-consent-accepted"; | ||
const [theme, toggleMode, componentMounted] = useDarkMode(); | ||
const [previewTheme, setPreviewTheme] = React.useState("twilio"); | ||
const [previewTheme, setPreviewTheme] = React.useState<string | undefined>(); | ||
const [cookiesAccepted, setCookiesAccepted] = React.useState<null | string>(); | ||
|
||
React.useEffect(() => { | ||
|
@@ -111,7 +113,7 @@ const App = ({ Component, pageProps }: AppProps): React.ReactElement => { | |
</> | ||
)} | ||
<Theme.Provider | ||
theme={theme} | ||
useCSSVariables={true} | ||
customBreakpoints={SITE_BREAKPOINTS} | ||
disableAnimations={inCypress()} | ||
cacheProviderProps={{ key: "next" }} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,22 @@ class _Document extends Document { | |
<link rel="apple-touch-icon" sizes="512x512" href="/icons/icon-512x512.png" /> | ||
</Head> | ||
<body> | ||
<script | ||
type="text/javascript" | ||
// eslint-disable-next-line react/no-danger | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
let parts = typeof document !== "undefined" && document?.cookie.split("paste-docs-theme="); | ||
let cookie = parts.length == 2 ?parts?.pop().split(";").shift() : null; | ||
if(cookie){ | ||
document.body.dataset.theme = cookie; | ||
} | ||
else if(window !== "undefined" && window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches){ | ||
document.body.dataset.theme = "twilio-dark"; | ||
} | ||
`, | ||
}} | ||
/> | ||
Comment on lines
+36
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried doing this using the Next Script element but I didn't have any luck, flicker was there just shorted. Doing it this way was the only thing to ge tit to work |
||
<noscript key="noscript">This app works best with JavaScript enabled.</noscript> | ||
<Main /> | ||
<NextScript /> | ||
|
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 am now delaying the Live code preview rendering until we have correctly identified the theme. I could not fix the Theme provider with CSS variables as it's nested an needs to cahgne independent of the actual app. This was the only workaround to not getting the previews to do the flicker. Won't effect SEO