A simple React hook for toggling between light and dark themes. This hook allows you to easily implement theme switching functionality in your React applications with minimal setup. It handles theme persistence using localStorage
and automatically applies the selected theme to your application.
- Toggle between Light and Dark themes.
-
Automatic theme persistence using
localStorage
. - Easy integration with any React or React TypeScript application.
- Customizable: Style your application based on the theme attribute.
To install the use-theme-hook
, run the following command:
npm install use-theme-hook
yarn add use-theme-hook
- To start using the theme hook, wrap your application's root component with the ThemeProvider component. This will provide the theme context to your entire application.
// App.tsx
import React from 'react';
import { ThemeProvider } from 'use-theme-hook'; // Import the ThemeProvider from the package
const App: React.FC = () => {
return (
<ThemeProvider>
{/* Your application's components go here */}
<h1>Welcome to My Themed Application</h1>
</ThemeProvider>
);
};
export default App;
- Inside any of your components, you can use the useTheme hook to access the current theme and toggle functionality.
import React from 'react';
import { useTheme } from 'use-theme-hook'; // Import the useTheme hook from the package
const MainContent: React.FC = () => {
const { theme, toggleTheme } = useTheme(); // Destructure theme and toggleTheme from the hook
return (
<div>
<h2>Current Theme: {theme}</h2>
<button onClick={toggleTheme}>
Switch to {theme === 'light' ? 'Dark' : 'Light'} Theme
</button>
</div>
);
};
export default MainContent;
` To style your application based on the current theme, you can use CSS classes or data attributes applied to the body or root element.
- Create a CSS file (e.g., styles.css) with styles for light and dark themes:
/* theme.css */
/* Light Theme */
body[data-theme='light'] {
background-color: #ffffff;
color: #000000;
}
/* Dark Theme */
body[data-theme='dark'] {
background-color: #121212;
color: #ffffff;
}
## main file
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './theme.css'; // Import the theme styles
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
T - he useTheme hook provides the current theme and a function to toggle between light and dark themes.
theme: string: The current theme, either "light" or "dark". toggleTheme: () => void: A function to toggle the theme between light and dark.
- The ThemeProvider component provides the theme context to your entire application.
children: ReactNode: The children components that will be wrapped by the theme provider.
- You can further customize the hook to fit your application's needs:
Additional Styles: Apply additional styles based on the data-theme attribute or class names applied to the root element.
- Contributions are welcome! Please open an issue or submit a pull request for any improvements, bug fixes, or feature requests.
- Installation: Instructions on how to install the package using npm or yarn.
-
How to Use: Detailed steps on how to wrap your app with
ThemeProvider
and use theuseTheme
hook in your components. - Styling: Explanation of how to apply CSS styles based on the theme, including a code snippet for CSS.
-
API Reference: Describes the hook's return values and the
ThemeProvider
component. - Customization: Tips on customizing the default behavior or appearance of the themes.
- Contributing: Invites the community to contribute to the project with issues or pull requests.
- License: States the license type, typically MIT, which is common for open-source packages.
This README serves as a comprehensive guide for users, making it