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

react-color-theme

v1.0.3

Published

Simple hook-based color theming for React with TypeScript support

Downloads

221

Readme

react-color-theme

Simple hook-based color theming for React with TypeScript support. Define your own color themes and use ThemeProvider in order to choose one of the themes in your app. Then fetch the current theme using useTheme hook.

How to use?

  1. Create a file for defining themes theme.jsx, set up themes and export them.
// ./features/theme.jsx
import { createTheming } from 'react-color-theme';

export const [ThemeProvider, useTheme, themes] = createTheming(
  {
    background: '#282836',
    foreground: '#3e3e4a',
    text: '#fff',
    primary: '#fc6',
    secondary: '#4f6bab',
  },
  {
    dark: {
      background: '#282836',
      foreground: '#3e3e4a',
      text: '#fff',
    },
    light: {
      background: '#fff',
      foreground: '#eee',
      text: '#333',
    },
  }
);
  • The first argument is the default theme, it must contain default values for all possible colors.
  • The second argument is the object containing all named themes. A named theme might miss some colors, in this case a corresponding value from the default theme will be used.
  1. Use ThemeProvider in your App.jsx file:
// ./App.jsx
import React, { useEffect, useState } from 'react';
import { ThemeProvider } from './features/theme';
import { MyComponent } from './components/MyComponent';

export const App = () => {
  const [themeName, setThemeName] = useState('main');

  useEffect(() => {
    // Your theme name determining logic
    setThemeName('light');
  }, []);

  return (
    <ThemeProvider value={themeName}>
      {/* ... */}

      <MyComponent />

      {/* ... */}
    </ThemeProvider>
  );
};
  1. Then in your component fetch your theme using useTheme hook:
// ./components/MyComponent.jsx
import React from 'react';
import { useTheme } from './features/theme';

export const MyComponent = () => {
  const theme = useTheme();

  return (
    <div style={{ backgroundColor: theme.background }}>
      <h2 style={{ color: theme.primary }}>This is a title</h2>
      <p style={{ color: theme.text }}>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Quas
        reprehenderit officiis alias quam recusandae dolores sunt placeat.
      </p>
    </div>
  );
};

And that is it! Switching from dark to light modes should be as easy as changing themeName state of the App component.

Are there any problems?

  • Unfortunately, if you call useTheme in the same file where you render the theme provider it will not work properly. Because at the moment the hook is invoked, theme name is not yet set, so it will fallback to default theme and warn you in console about this case.

    It is recommended to refactor your code to avoid needing to use themes in the file where the theme provider is rendered, but if it is not possible, you can pass theme name directly to useTheme hook as a parameter. So a fix would be:

export const App = () => {
  const [themeName, setThemeName] = useState('main');
  const theme = useTheme(themeName); // <- Here we haven't yet initialized ThemeProvider, so we have to pass theme name to the hook

  useEffect(() => {
    // Your theme name determining logic
    setThemeName('light');
  }, []);

  return <ThemeProvider value={themeName}>{/* ... */}</ThemeProvider>;
};

Do you have TypeScript support?

  • Indeed we do! Defining your themes made easy with type hints from your IDE. Also ThemeProvider expects to get a union of theme names, so in order to pass a string to it be sure to convert it to one of themes keys first. You can use keyof typeof themes type to do that, this general themes object is exposed for simplifying work with types. So when working with theme name you can do something like that:
import { themes } from './features/theme';

type ThemeName = keyof typeof themes;

const App: FC = () => {
  const [themeName, setThemeName] = useState<ThemeName>('main');

  let name: ThemeName = 'dark';
  name = 'light';
  setThemeName(name);

  return <ThemeProvider value={themeName}>{/* ... */}</ThemeProvider>;
};