Skip to content
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

feat: implement adaptive day/night browser bar theme 🚀🔥 #1558

Merged
merged 6 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions components/ThemeToggler/themeToggler.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
import { useState, useEffect } from 'react'
import { useTheme } from 'next-themes'
import { HiSun, HiMoon } from 'react-icons/hi'
import { useState, useEffect } from 'react';
import { useTheme } from 'next-themes';
import { HiSun, HiMoon } from 'react-icons/hi';
import { Helmet, HelmetProvider } from 'react-helmet-async';

export function ThemeToggler() {
const { resolvedTheme, setTheme } = useTheme()
const [mounted, setMounted] = useState(false)
const { resolvedTheme, setTheme } = useTheme();
const [mounted, setMounted] = useState(false);

useEffect(() => {
setMounted(true)
}, [])
setMounted(true);
}, []);

if (!mounted) {
return null
return null;
}

const handleThemeToggle = () => {
setTheme(resolvedTheme === 'dark' ? 'light' : 'dark')
setTheme(resolvedTheme === 'dark' ? 'light' : 'dark');
}

const iconProps = {
className: 'hover:text-theme-primary transition duration-300 ease-in-out',
size: '1.5rem',
}

const themeColor = resolvedTheme === 'dark' ? '#101623' : '#F5F3FF';

return (
<button
onClick={handleThemeToggle}
title={`Toggle dark mode (current state: ${resolvedTheme})`}
aria-label="Toggle dark mode"
>
{resolvedTheme === 'dark' ? (
<HiSun
{...iconProps}
className="text-light-primary hover:text-theme-primary transition duration-300 ease-in-out"
/>
) : (
<HiMoon
{...iconProps}
className="text-text-secondary hover:text-theme-primary transition duration-300 ease-in-out"
<HelmetProvider>
<Helmet>
<meta name="theme-color" content={themeColor} />
<meta name="msapplication-navbutton-color" content={themeColor} />
<meta name="msapplication-TileColor" content={themeColor} />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta
name="apple-mobile-web-app-status-bar-style"
content={themeColor}
/>
)}
</button>
)
</Helmet>
<button
onClick={handleThemeToggle}
title={`Toggle dark mode (current state: ${resolvedTheme})`}
>
{resolvedTheme === 'dark' ? (
<HiSun
{...iconProps}
className="text-light-primary hover:text-theme-primary transition duration-300 ease-in-out"
/>
) : (
<HiMoon
{...iconProps}
className="text-text-secondary hover:text-theme-primary transition duration-300 ease-in-out"
/>
)}
</button>
</HelmetProvider>
);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"react": "18.2.0",
"react-autosuggest": "^10.1.0",
"react-dom": "18.2.0",
"react-helmet-async": "^1.3.0",
"react-icons": "^4.8.0",
"react-spinners": "^0.13.8",
"react-tooltip": "^5.20.0",
Expand Down
42 changes: 37 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.