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

Add light and dark theme using context and custom hooks #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"devDependencies": {
"@vitejs/plugin-react": "^4.3.4",
"autoprefixer": "^10.4.20",
"clsx": "^2.1.1",
"postcss": "^8.5.1",
"prop-types": "^15.8.1",
"tailwindcss": "^3.4.17",
Expand Down
13 changes: 11 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { Projects } from './components';
import clsx from 'clsx';
import { Header, Projects } from './components';

import { projects } from './data';
import { useTheme } from './hooks';

export default function App() {
const { isDarkMode } = useTheme();
return (
<div className="container mx-auto w-full flex justify-center py-6">
<div
className={clsx(
'container mx-auto w-full flex flex-col gap-8 justify-center py-6',
isDarkMode && 'bg-gray-800'
)}
>
<Header />
<Projects projects={projects} />
</div>
);
Expand Down
24 changes: 24 additions & 0 deletions src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import clsx from 'clsx';

import { useTheme } from '../hooks';

export const Header = () => {
const { isDarkMode, toggleTheme } = useTheme();

return (
<header
className={clsx(
'flex justify-between items-center p-4',
isDarkMode ? 'app-dark' : 'app-light'
)}
>
<h1 className="text-xl font-bold">React JS Fall 2024</h1>
<button
onClick={toggleTheme}
className="px-4 py-2 border rounded-md text-sm font-medium"
>
{isDarkMode ? 'Light Mode' : 'Dark Mode'}
</button>
</header>
);
};
1 change: 1 addition & 0 deletions src/components/index.jsx
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './project/Projects';
export * from './Header';
10 changes: 9 additions & 1 deletion src/components/project/Project.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import clsx from 'clsx';
import { useState } from 'react';

import { useTheme } from '../../hooks';
import { ProjectPropType } from '../propTypesDefs';

export const Project = ({ project }) => {
const { isDarkMode } = useTheme();
const [selectedContributor, setSelectedContributor] = useState();

return (
Expand All @@ -12,7 +15,12 @@ export const Project = ({ project }) => {
{project.contributors.map((contributor) => (
<button
key={contributor}
className="text-sm font-medium text-gray-500 hover:text-gray-700 border border-gray-200 rounded-md px-4 py-2"
className={clsx(
'text-sm font-medium text-gray-500 hover:text-gray-700 border border-gray-200 rounded-md px-4 py-2',
isDarkMode
? 'text-gray-300 hover:text-gray-400 border-gray-600'
: 'text-gray-500 hover:text-gray-700 border-gray-200'
)}
onClick={() => setSelectedContributor(contributor)}
>
{contributor}
Expand Down
12 changes: 11 additions & 1 deletion src/components/project/Projects.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import clsx from 'clsx';
import PropTypes from 'prop-types';

import { ProjectPropType } from '../propTypesDefs';

import { useTheme } from '../../hooks';

import { Project } from './Project';

export const Projects = ({ projects }) => {
const { isDarkMode } = useTheme();

return (
<div className="container space-y-6">
<div
className={clsx(
'container space-y-6',
isDarkMode ? 'app-dark' : 'app-light'
)}
>
{projects.map((project) => (
<Project key={project.name} project={project} />
))}
Expand Down
26 changes: 26 additions & 0 deletions src/contexts/ThemeContext.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createContext, useState } from 'react';
import PropTypes from 'prop-types';

export const ThemeContext = createContext({
isDarkMode: false,
toggleTheme: () => {},
});

export const ThemeProvider = ({ children }) => {
const [isDarkMode, setIsDarkMode] = useState(false);

const toggleTheme = () => {
setIsDarkMode(!isDarkMode);
// document.documentElement.classList.toggle('dark', !isDarkMode);
};

return (
<ThemeContext.Provider value={{ isDarkMode, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
};

ThemeProvider.propTypes = {
children: PropTypes.node.isRequired,
};
1 change: 1 addition & 0 deletions src/contexts/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ThemeContext';
1 change: 1 addition & 0 deletions src/hooks/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useTheme';
6 changes: 6 additions & 0 deletions src/hooks/useTheme.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useContext } from 'react';
import { ThemeContext } from '../contexts';

export const useTheme = () => {
return useContext(ThemeContext);
};
10 changes: 10 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

.app-dark {
background-color: rgb(31 41 55 / var(--tw-bg-opacity, 1));
color: rgb(243 244 246 / var(--tw-text-opacity, 1)) !important;
}

.app-light {
background-color: rgb(243 244 246 / var(--tw-bg-opacity, 1));
color: rgb(17 24 39 / var(--tw-text-opacity, 1)) !important;
}
5 changes: 4 additions & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

import { ThemeProvider } from './contexts';
import App from './App';

import './index.css';

const root = createRoot(document.getElementById('react-app-root'));
root.render(
<StrictMode>
<App />
<ThemeProvider>
<App />
</ThemeProvider>
</StrictMode>
);
1 change: 1 addition & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('tailwindcss').Config} */
export default {
darkMode: 'class', // Enable dark mode
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {},
Expand Down