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

color-scheme-switcher

v0.0.3

Published

A library to select color scheme either manually or automatically.

Downloads

41

Readme

Color Scheme Switcher

NPM
Version License Minzipped
Size Dependencies Dependents

Description

This package facilitates toggling the color scheme of a react app between dark and light mode.

This can be done either manually or automatically.

If done automatically, it can either follow the system color scheme, or follow daylight in the user's location.

This library is careful not to request geolocation permissions, which could be intrusive to users, unless it will be used.

Installation

npm i color-scheme-switcher

Note: react and react-dom are peer dependencies.

Usage

Provider Options

| Option | Type | Optional? | Default | Description | | ------------------------------- | -------------------------- | --------- | ------- | ------------------------------------------------------------------------------------------------------- | | defaultColorSchemeIsLight | boolean | Yes | false | This sets whether the default color scheme is light mode or dark mode before the page has fully loaded. | | setColorSchemeIsLightCallback | (value: boolean) => void | Yes | None | This is a function which will be run whenever the color scheme changes. |

Integration

See src/examples for other examples.

  1. In your _app.tsx (or similar) file, wrap children with the color-scheme-switcher provider.

    Note: if your component library's theme provider can use the colorSchemeIsLight boolean from this library directly, wrap the theme provider in a component that calls the useColorSchemeSwitcher hook and pass it in directly (not demonstrated here).

    import { ColorSchemeSwitcherProvider } from "color-scheme-switcher";
    import { SomeComponentLibraryProvider } from "some-component-library";
    
    const App: AppType = ({ Component, pageProps }) => {
      const defaultColorSchemeIsLight = true;
      const [colorScheme, setColorScheme] = useState<"light" | "dark">(
        defaultColorSchemeIsLight ? "light" : "dark"
      );
    
      const setColorSchemeIsLightCallback = (colorSchemeIsLight: boolean) => {
        setColorScheme(colorSchemeIsLight ? "light" : "dark");
      };
    
      return (
        <ColorSchemeSwitcherProvider
          defaultColorSchemeIsLight={defaultColorSchemeIsLight}
          setColorSchemeIsLightCallback={setColorSchemeIsLightCallback}
        >
          <SomeComponentLibraryProvider colorScheme={colorScheme}>
            <Component {...pageProps} />
          </SomeComponentLibraryProvider>
        </ColorSchemeSwitcherProvider>
      );
    };
    
    export default App;
  2. In a component or page (or similar), use the context from this package.

    import { useColorSchemeSwitcher } from "color-scheme-switcher";
    import { Checkbox } from "@/components";
    
    interface HomePageProps {}
    
    const HomePage: React.FC<HomePageProps> = () => {
      const {
        colorSchemeIsLight,
        colorSchemeIsManual,
        colorSchemeFollowsSun,
        setColorSchemeIsLight,
        setColorSchemeIsManual,
        setColorSchemeFollowsSun,
      } = useColorSchemeSwitcher();
    
      return (
        <div
          sx={{
            color: colorSchemeIsLight ? "black" : "white",
            backgroundColor: colorSchemeIsLight ? "white" : "black",
          }}
        >
          <Checkbox
            label="Color Scheme is Manual"
            value={colorSchemeIsManual}
            setValue={setColorSchemeIsManual}
          />
    
          {colorSchemeIsManual ? (
            <Checkbox
              label="Color Scheme is Light"
              value={colorSchemeIsLight}
              setValue={setColorSchemeIsLight}
            />
          ) : (
            <Checkbox
              label="Color Scheme Follows Sun"
              value={colorSchemeFollowsSun}
              setValue={setColorSchemeFollowsSun}
            />
          )}
    
          <p>{`Color scheme is light? ${colorSchemeIsLight ? "yes" : "no"}`}</p>
          <p>{`Color scheme is manual? ${
            colorSchemeIsManual ? "yes" : "no"
          }`}</p>
          <p>{`Color scheme follows sun? ${
            colorSchemeFollowsSun ? "yes" : "no"
          }`}</p>
        </div>
      );
    };
    
    export default HomePage;

To-Do

  • Document exported utils.
  • Add options for changing other defaults.

Development

  1. Fork this repository.

  2. Install development dependencies.

    npm i -D
  3. Start build and test watchers.

    npm run build:watch
    npm run test:watch
  4. Modify code.

  5. Ensure all tests pass.

  6. Push code to your fork.

  7. Submit a merge request into the development branch of this repository.