-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(website): add dark mode toggle (#14064)
- Loading branch information
Showing
6 changed files
with
363 additions
and
272 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/website/components/ThemeToggle/ThemeToggle.module.css
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,48 @@ | ||
/* source: https://github.com/drizzle-team/drizzle-orm-docs */ | ||
.container { | ||
cursor: pointer; | ||
margin: 8px; | ||
user-select: none; | ||
width: 36px; | ||
height: 24px; | ||
position: relative; | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
html[class~="dark"] .line { | ||
background-color: rgba(249, 250, 251, 0.1); | ||
} | ||
|
||
.line { | ||
height: 10px; | ||
width: 32px; | ||
border-radius: 5px; | ||
background-color: rgba(0, 0, 0, 0.05); | ||
} | ||
|
||
html[class~="dark"] .circle { | ||
background-color: #484848; | ||
} | ||
|
||
.circle { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
width: 20px; | ||
height: 20px; | ||
border-radius: 10px; | ||
background-color: rgb(229, 231, 235); | ||
position: absolute; | ||
top: 50%; | ||
transform: translate(-50%, -50%); | ||
transition: all ease-in-out 0.1s; | ||
} | ||
|
||
.dark { | ||
left: 72%; | ||
} | ||
|
||
.light { | ||
left: 16%; | ||
} |
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,78 @@ | ||
// source: https://github.com/drizzle-team/drizzle-orm-docs | ||
import React, { useEffect, useState } from "react"; | ||
import { useTheme } from "next-themes"; | ||
import styles from "./ThemeToggle.module.css"; | ||
|
||
const ThemeToggle = () => { | ||
const [isDark, setIsDark] = useState<boolean | null>(null); | ||
const { theme, setTheme } = useTheme(); | ||
|
||
useEffect(() => { | ||
setIsDark(theme === "dark"); | ||
}, [theme]); | ||
|
||
useEffect(() => { | ||
if (theme === "system") { | ||
const isLight = document.documentElement.classList.contains("light"); | ||
setTheme(isLight ? "light" : "dark"); | ||
} | ||
}, []); | ||
|
||
const toggleTheme = () => { | ||
setTheme(theme === "light" ? "dark" : "light"); | ||
}; | ||
return ( | ||
<div className={styles.container} onClick={toggleTheme}> | ||
{isDark !== null && ( | ||
<> | ||
<div className={styles.line} /> | ||
<div | ||
className={`${styles.circle} ${ | ||
isDark ? styles.dark : styles.light | ||
}`} | ||
> | ||
{isDark ? ( | ||
<div> | ||
<svg | ||
fill="none" | ||
viewBox="2 2 20 20" | ||
width="12" | ||
height="12" | ||
stroke="currentColor" | ||
> | ||
<path | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth="2" | ||
fill="currentColor" | ||
d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" | ||
/> | ||
</svg> | ||
</div> | ||
) : ( | ||
<div> | ||
<svg | ||
fill="none" | ||
viewBox="3 3 18 18" | ||
width="12" | ||
height="12" | ||
stroke="currentColor" | ||
> | ||
<path | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
strokeWidth="2" | ||
fill="currentColor" | ||
d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" | ||
/> | ||
</svg> | ||
</div> | ||
)} | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ThemeToggle; |
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
Oops, something went wrong.