Skip to content
Closed
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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,37 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</tbody>
</table>


---

## 🌗 Dark/Light Theme Toggle

We’ve added a dark/light theme toggle to enhance user experience.

### 🔧 How It Works

- A toggle button (☀️/🌙) is available in the **NavBar**.
- Clicking it switches between **light** and **dark** mode.
- Theme preference is saved in `localStorage` so it persists across sessions.
- Tailwind CSS uses `darkMode: 'class'` (configured in `tailwind.config.js`).
- A custom `useDarkMode` hook manages toggling logic and class updates.

### 📂 Files Modified

- `src/common/NavBar.jsx` → Added toggle button
- `src/hooks/useDarkMode.js` → Custom hook for managing theme
- `src/index.jsx` → Ensures correct theme on initial load
- `tailwind.config.js` → Enabled `darkMode: 'class'`

### 📸 Preview

| Light Mode ☀️ | Dark Mode 🌙 |
|---------------|--------------|
| ![Light](https://dummyimage.com/300x150/ffffff/000000&text=Light+Mode) | ![Dark](https://dummyimage.com/300x150/000000/ffffff&text=Dark+Mode) |

---


Comment on lines +398 to +428
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are not required, you can remove.

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

Expand Down
30 changes: 22 additions & 8 deletions src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RouteDefs from 'common/routing/RouteDefs';
import { SearchContextProvider } from 'common/search/search-context';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { createRoot } from 'react-dom/client';
import reportWebVitals from './reportWebVitals';
import register from './registerServiceWorker';
Expand All @@ -27,33 +27,47 @@ const Index = () => {
language: []
});

const [theme, setTheme] = useState(localStorage.getItem('theme') || 'light');

// Effect to apply theme
useEffect(() => {
const root = document.documentElement;
if (theme === 'dark') {
root.classList.add('dark');
} else {
root.classList.remove('dark');
}
localStorage.setItem('theme', theme);
}, [theme]);

const toggleTheme = () => {
setTheme(prev => (prev === 'dark' ? 'light' : 'dark'));
};

const value = {
searchTerm,
setSearchTerm,
filterQuery,
setFilterQuery,
showShareModal,
setShowShareModal
setShowShareModal,
toggleTheme, // add toggle function to context if needed in NavBar
theme
};

return (
// <React.StrictMode>
<ErrorBoundry>
<SearchContextProvider value={value}>
<RouteDefs />
<Notification />
</SearchContextProvider>
</ErrorBoundry>
// </React.StrictMode>
);
};

const container = document.getElementById('root');
createRoot(container).render(<Index />);

// Makes the app to work offline and load faster
register();

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
Loading
Loading