Skip to content

Commit

Permalink
feat/#393 added dark-light theme toggle button
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-AkshatGupta committed Mar 4, 2024
1 parent b54e15c commit cbf9097
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 10 deletions.
27 changes: 27 additions & 0 deletions apps/host/src/components/ThemeWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import styles from '@/styles.module.scss';
import { ReactElement, createContext, useState } from 'react';

type Theme = 'light' | 'dark';

interface ThemeContextType {
theme: Theme;
toggleTheme: (theme: Theme) => void;
}

export const ThemeContext = createContext<ThemeContextType>({
theme: 'light',
toggleTheme: () => {},
});

export const ThemeWrapper = ({ children }: { children: ReactElement }) => {
const [theme, setTheme] = useState<Theme>('light');

const toggleTheme = (theme: Theme): void => setTheme(theme === 'light' ? 'dark' : 'light');
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
<div className={styles.containerWrapper} data-theme={theme}>
{children}
</div>
</ThemeContext.Provider>
);
};
11 changes: 9 additions & 2 deletions apps/host/src/components/common/navbar/navbar.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
padding: 1rem;
margin-bottom: 1rem;
background-color: var(--bg-navbar);
box-shadow: 0 1px 3px 0 var(--shadow-navbar), 0 1px 2px -1px var(--shadow-navbar);
box-shadow:
0 1px 3px 0 var(--shadow-navbar),
0 1px 2px -1px var(--shadow-navbar);
transition: all 150ms ease-in-out;

--icon-container-size: 26px;
Expand Down Expand Up @@ -46,7 +48,6 @@
height: var(--icon-container-size);
color: var(--text-body);


& > svg {
width: var(--github-svg-size);
height: var(--github-svg-size);
Expand All @@ -70,3 +71,9 @@
}
}
}

.themeButton {
border: none;
color: var(--text-link);
background-color: inherit;
}
8 changes: 8 additions & 0 deletions apps/host/src/components/common/navbar/navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { useContext } from 'react';
import styles from './navbar.module.scss';
import { logo } from '@fmc/assets/images';
import { ThemeContext } from './../../ThemeWrapper';
import { Sun } from 'lucide-react';
import { Moon } from 'lucide-react';

function Navbar({ children, title }: { children?: React.ReactNode; title?: string }) {
const { theme, toggleTheme } = useContext(ThemeContext);
return (
<nav className={styles.navbar}>
<a className={styles.logo} href="/frontend-mini-challenges/">
Expand All @@ -24,6 +29,9 @@ function Navbar({ children, title }: { children?: React.ReactNode; title?: strin
/>
</svg>
</a>
<button className={styles.themeButton} onClick={() => toggleTheme(theme)}>
{theme === 'light' ? <Sun /> : <Moon />}
</button>
</div>
</nav>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
opacity: 0.7;
transition: all 0.2s ease;
}
.searchInput:focus, .searchInput:hover {
.searchInput:focus,
.searchInput:hover {
border-color: var(--text-link);
& + .searchIcon {
opacity: 1;
Expand Down Expand Up @@ -84,9 +85,11 @@
padding: 10px;
overflow: hidden;
background-color: white;
border: 1px solid rgb(0 0 0 / 15%);
box-shadow: 0 6px 8px 0 rgb(0 0 0 / 15%);
border: 1px solid var(--card-shadow);
box-shadow: 0 6px 8px 0 var(--card-shadow);
transition: all 0.2s ease-in-out;
background-color: var(--bg-body);
color: var(--text-body);

&:hover {
box-shadow: 0 6px 8px 0 rgb(0 0 0 / 30%);
Expand Down Expand Up @@ -137,7 +140,7 @@
align-items: center;
justify-content: center;
font-size: 0.8rem;
color: black;
color: var(--text-body);

& > img {
width: 25px;
Expand All @@ -148,7 +151,7 @@
}

.name {
color: black;
color: var(--text-body);
}

&.medium h3 {
Expand Down
5 changes: 4 additions & 1 deletion apps/host/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import HomePage from './pages/home';
import LeaderboardPage from './pages/leaderboard';
import './index.css';
import Challenges from './pages/challenges';
import { ThemeWrapper } from './components/ThemeWrapper';

const router = createHashRouter([
{
Expand All @@ -23,6 +24,8 @@ const router = createHashRouter([

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
<ThemeWrapper>
<RouterProvider router={router} />
</ThemeWrapper>
</React.StrictMode>
);
7 changes: 7 additions & 0 deletions apps/host/src/styles.module.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.containerWrapper {
width: 100%;
height: 100%;
background-color: var(--bg-body);
color: var(--text-body);
}

.container {
max-width: min(90%, 1200px);
margin: 0 auto;
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

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

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
"workspaces": [
"apps/*",
"shared/*"
]
],
"dependencies": {
"lucide": "^0.344.0",
"lucide-react": "^0.344.0"
}
}
11 changes: 10 additions & 1 deletion shared/styles/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,30 @@

/* Links */
--text-link: blue;

/* Challenge Card */
--card-shadow: rgb(0 0 0 / 15%);
}

/* Dark override for above variables */

:root.dark {
[data-theme='dark'] {
/* Default body colors */
--bg-body: #030712;
--text-body: #e5e7eb;

--bg-navbar: #030712;
--shadow-navbar: rgba(150 150 150 / 0.2);

/* Footer-specific colors (same as navbar) */
--bg-footer: var(--bg-body);
--shadow-footer: #1f2937;

/* Links */
--text-link: #6366f1;

/* Challenge Card */
--card-shadow: rgb(150 150 150/ 20%);

color-scheme: dark;
}

0 comments on commit cbf9097

Please sign in to comment.