npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bholuvivek/use-theme-hook

v1.0.1

Published

A simple React hook for toggling light and dark themes.

Downloads

31

Readme

use-theme-hook

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.

Features

  • 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.

Installation

To install the use-theme-hook, run the following command:

npm install use-theme-hook
yarn add use-theme-hook

How to Use

1. Wrap Your App with the ThemeProvider

  • 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;

2. Use the useTheme Hook in Your Components

  • 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;

3. Apply CSS Based on the Theme

` To style your application based on the current theme, you can use CSS classes or data attributes applied to the body or root element.

Example: Styling with CSS

  • 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>
);

API Reference

useTheme

T - he useTheme hook provides the current theme and a function to toggle between light and dark themes.

Return Values

theme: string: The current theme, either "light" or "dark". toggleTheme: () => void: A function to toggle the theme between light and dark.

ThemeProvider

  • The ThemeProvider component provides the theme context to your entire application.

Props

children: ReactNode: The children components that will be wrapped by the theme provider.

Customization

  • You can further customize the hook to fit your application's needs:

Default Theme: Change the default theme in the useTheme.ts file.

Additional Styles: Apply additional styles based on the data-theme attribute or class names applied to the root element.

Contributing

  • Contributions are welcome! Please open an issue or submit a pull request for any improvements, bug fixes, or feature requests.

Key Sections Explained

  1. Installation: Instructions on how to install the package using npm or yarn.
  2. How to Use: Detailed steps on how to wrap your app with ThemeProvider and use the useTheme hook in your components.
  3. Styling: Explanation of how to apply CSS styles based on the theme, including a code snippet for CSS.
  4. API Reference: Describes the hook's return values and the ThemeProvider component.
  5. Customization: Tips on customizing the default behavior or appearance of the themes.
  6. Contributing: Invites the community to contribute to the project with issues or pull requests.
  7. 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