-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: Light theme flash during page load (#8004)
* FIX: Light theme flash during page load * Invoke theme script inline
- Loading branch information
1 parent
8eddc07
commit 709d1a8
Showing
5 changed files
with
105 additions
and
107 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** @jsxRuntime classic */ | ||
/** @jsx jsx */ | ||
import { Fragment, useState, useEffect, HTMLAttributes } from 'react'; | ||
import { jsx } from '@emotion/react'; | ||
|
||
import { COLORS } from '../lib/TOKENS'; | ||
import { LightMode } from './icons/LightMode'; | ||
import { DarkMode } from './icons/DarkMode'; | ||
|
||
function ModeIcon({ theme }: { theme: 'light' | 'dark' }) { | ||
if (theme === 'dark') { | ||
return <LightMode css={{ height: 'var(--space-xlarge)' }} />; | ||
} | ||
|
||
return <DarkMode css={{ height: 'var(--space-xlarge)' }} />; | ||
} | ||
|
||
export function ThemeToggle(props: HTMLAttributes<HTMLButtonElement>) { | ||
/* | ||
We don't want to render the toggle during server rendering | ||
because Next will always server render the light mode toggle and hydrate the light mode toggle | ||
even if the theme is dark mode based on system preference. | ||
So we render the toggle only on the client | ||
*/ | ||
const [theme, setTheme] = useState<keyof typeof COLORS | null>(null); | ||
|
||
useEffect(() => { | ||
const currentTheme = document.documentElement.getAttribute('data-theme') as 'light' | 'dark'; | ||
setTheme(currentTheme); | ||
}, [setTheme]); | ||
|
||
const handleThemeChange = () => { | ||
const newTheme = theme === 'dark' ? 'light' : 'dark'; | ||
if (newTheme === 'dark') { | ||
document.documentElement.setAttribute('data-theme', 'dark'); | ||
} else { | ||
document.documentElement.setAttribute('data-theme', 'light'); | ||
} | ||
setTheme(newTheme); | ||
localStorage.setItem('theme', newTheme); | ||
}; | ||
|
||
return ( | ||
<Fragment> | ||
<button | ||
key={theme} | ||
onClick={handleThemeChange} | ||
css={{ | ||
display: 'inline-flex', | ||
appearance: 'none', | ||
background: 'transparent', | ||
boxShadow: 'none', | ||
border: '0 none', | ||
borderRadius: '100%', | ||
lineHeight: 1, | ||
padding: 0, | ||
margin: 0, | ||
color: 'var(--muted)', | ||
cursor: 'pointer', | ||
transition: 'color 0.3s ease', | ||
'&:hover, &:focus': { | ||
color: 'var(--link)', | ||
}, | ||
}} | ||
{...props} | ||
> | ||
{theme === null ? <span css={{ width: 24 }} /> : <ModeIcon theme={theme} />} | ||
</button> | ||
</Fragment> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
709d1a8
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.
Successfully deployed to the following URLs:
keystone-next-docs – ./
keystone-6.vercel.app
demo.keystonejs.com
keystone-next-docs-git-main-thinkmill.vercel.app
keystone-next-docs-thinkmill.vercel.app
www.keystonejs.com
keystonejs.com
next.keystonejs.com