File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ import { useState , useEffect } from "react" ;
2+
3+ const ThemeToggle = ( ) => {
4+ const [ theme , setTheme ] = useState ( "dark" ) ;
5+
6+ useEffect ( ( ) => {
7+ // if the theme isn't set, use the user's system preference
8+ const savedTheme = localStorage . getItem ( "theme" ) ;
9+ if ( savedTheme ) {
10+ setTheme ( savedTheme ) ;
11+ document . documentElement . setAttribute ( "data-theme" , savedTheme ) ;
12+ } else if ( window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches ) {
13+ setTheme ( "dark" ) ;
14+ document . documentElement . setAttribute ( "data-theme" , "dark" ) ;
15+ } else {
16+ setTheme ( "light" ) ;
17+ document . documentElement . setAttribute ( "data-theme" , "light" ) ;
18+ }
19+ } ) ;
20+
21+ const toggleTheme = ( ) => {
22+ const newTheme = theme === "dark" ? "light" : "dark" ;
23+ setTheme ( newTheme ) ;
24+ localStorage . setItem ( "theme" , newTheme ) ;
25+ document . documentElement . setAttribute ( "data-theme" , newTheme ) ;
26+ } ;
27+
28+ return (
29+ < button onClick = { toggleTheme } className = "button" aria-label = "Toggle theme" >
30+ { theme === "dark" ? "🌞" : "🌚" }
31+ </ button >
32+ ) ;
33+ } ;
34+
35+ export default ThemeToggle ;
Original file line number Diff line number Diff line change @@ -2,13 +2,15 @@ import { GitHubIcon } from "../components/Icons";
22import LinkButton from "../components/LinkButton" ;
33import Logo from "../components/Logo" ;
44import SearchInput from "../components/SearchInput" ;
5+ import ThemeToggle from "../components/ThemeToggle" ;
56
67const Header = ( ) => {
78 return (
89 < header className = "header" >
910 < Logo />
1011 < nav className = "primary-nav" >
1112 < SearchInput />
13+ < ThemeToggle />
1214 < LinkButton
1315 href = "https://github.com/dostonnabotov/quicksnip/blob/main/CONTRIBUTING.md"
1416 target = "_blank"
Original file line number Diff line number Diff line change 1919 --clr-neutral-500 : # 3c3c3c ;
2020 --clr-neutral-700 : # 2a2a2a ;
2121 --clr-neutral-900 : # 1c1c1c ;
22+ --clr-neutral-200-light : # 1c1c1c ;
23+ --clr-neutral-300-light : # 2a2a2a ;
24+ --clr-neutral-500-light : # b3b3b3 ;
25+ --clr-neutral-700-light : # e0e0e0 ;
26+ --clr-neutral-900-light : # ffffff ;
2227
2328 --clr-accent : # 00b4b8 ;
2429
5560
5661 --br-md : 0.5rem ;
5762 --br-lg : 0.75rem ;
63+
64+ transition :
65+ background-color 0.3s ease,
66+ color 0.3s ease;
67+ }
68+
69+ [data-theme = "light" ] {
70+ --bg-primary : var (--clr-neutral-900-light );
71+ --bg-secondary : var (--clr-neutral-700-light );
72+ --text-primary : var (--clr-neutral-200-light );
73+ --text-secondary : var (--clr-neutral-300-light );
74+ --text-dark : var (--clr-neutral-900-light );
75+ --border-color : var (--clr-neutral-500-light );
5876}
5977
6078/*------------------------------------*\
You can’t perform that action at this time.
0 commit comments