Skip to content

Commit

Permalink
Fix theme flash on paste website (#4025)
Browse files Browse the repository at this point in the history
* feat(website): fix flash of default theme

* feat(website): add changeset

* feat(website): remove changeset
  • Loading branch information
PixeledCode authored Aug 7, 2024
1 parent 933daf6 commit fb4cbc5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/paste-website/src/hooks/useDarkMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 => {
Expand All @@ -36,7 +36,7 @@ export const useDarkMode = (): UseDarkModeReturn => {
React.useEffect(() => {
const localTheme = SimpleStorage.get("theme") as ValidThemeName;

if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches && !localTheme) {
if (window.matchMedia?.("(prefers-color-scheme: dark)").matches && !localTheme) {
setMode(ValidThemes.DARK);
} else if (localTheme && isValidTheme(localTheme)) {
setTheme(localTheme);
Expand Down
18 changes: 12 additions & 6 deletions packages/paste-website/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,18 @@ const App = ({ Component, pageProps }: AppProps): React.ReactElement => {
disableAnimations={inCypress()}
cacheProviderProps={{ key: "next" }}
>
<DarkModeContext.Provider value={{ theme, toggleMode, componentMounted }}>
<PreviewThemeContext.Provider value={{ theme: previewTheme, selectTheme: setPreviewTheme }}>
<Component {...pageProps} />
{cookiesAccepted === null && <CookieConsent onAccept={handleCookieAccept} onReject={handleCookieReject} />}
</PreviewThemeContext.Provider>
</DarkModeContext.Provider>
{componentMounted ? (
<DarkModeContext.Provider value={{ theme, toggleMode, componentMounted }}>
<PreviewThemeContext.Provider value={{ theme: previewTheme, selectTheme: setPreviewTheme }}>
<Component {...pageProps} />
{cookiesAccepted === null && (
<CookieConsent onAccept={handleCookieAccept} onReject={handleCookieReject} />
)}
</PreviewThemeContext.Provider>
</DarkModeContext.Provider>
) : (
<></>
)}
</Theme.Provider>
</>
);
Expand Down

0 comments on commit fb4cbc5

Please sign in to comment.