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

varial

v1.0.0

Published

React theming using CSS variables

Downloads

3

Readme

Varial

Varial provides UI theme support for React apps using CSS variables.

varial.init({
  variants: ['light', 'dark'],
  styles: { background: { light: 'white', dark: 'black' } },
});

function App() {
  const { variant, setVariant } = useVariant();
  const onClick = () => setVariant(variant === 'light' ? 'dark' : 'light');
  return (
    <button style={{ backgroundColor: 'var(--background)' }} onClick={onClick}>
      Toggle theme
    </button>
  );
}

ReactDOM.render(
  <VariantProvider>
    <App />
  </VariantProvider>,
  document.getElementById('root'),
);

Features

  • Multi-theme support using CSS variables – integrates with your preferred UI toolkit
  • Automatic user dark preference detection
  • Theme switch with React Context
  • Smooth initial theme load with pre-rendered React pages – avoid the "white flash"

Install

npm install varial

Initialization

The library must be initialized by providing a styles object that contains a mapping of global CSS variables as well as a variants array containing the names of all variants used in styles. Both regular key-value pairs as well as different values for each theme variant can be provided.

import varial from "varial";

const variants = ['light', 'dark'];

const styles = {
  greyDark: "hsl(215, 22%, 15%)",
  greyLight: "hsl(210, 17%, 98%)",
  background: {
    light: "var(--greyLight)",
    dark: "var(--greyDark)",
  },
  text: {
    light: "black",
    dark: "white",
  },
};

varial.init({ variants, styles, defaultVariant: "dark" });

This will create the following global CSS variables:

{
    --greyDark: hsl(215, 22%, 15%);
    --greyLight: hsl(210, 17%, 98%);
    --background: var(--greyDark);
    --text: white;
}

Calling varial.init is the bare minimum of using this library. Rest of the functionalities are optional.

Theme Control

The library exposes the current theme using React Context. Wrap your App with a VariantProvider:

import varial, { VariantProvider } from "react-varial";

varial.init({ styles });

ReactDOM.render(
  <VariantProvider>
    <App />
  </VariantProvider>
  document.getElementById("root")
);

Access and change theme in function components with the useVariant hook:

import { useVariant } from "react-varial";

function App() {
  const { variant, setVariant } = useVariant();
  return (
    <button onClick={setVariant(variant === 'light' ? 'dark' : 'light')}>
      Toggle theme
    </button>
  );
}

setVariant updates all the CSS variables created using your styles object with the values of the given variant.

Automatic Darkmode

Automatic darkmode is supported if your styles object contains variants named light and dark. Color preference detection can be enabled with the detectColorScheme option:

varial.init({ styles, detectColorScheme: true });

The color scheme will update real-time if the user changes their color preference in OS settings.

Avoid the "White Flash" With SSR

Regular SPAs that are not server rendered suffer from a brief flash of white on the initial load of the page. This can be somewhat jarring especially if the page has a dark theme. The flash happens because the HTML page is empty when it arrives to the browser, and it gets populated with markup only after some code has been executed.

Pre-rendered apps have existing HTML already when they arrive to the browser, so we can leverage some well timed scripting to update the page theme to match the user's preference before rendering anything.

The library provides a mechanism for injecting a script to the beginning of your page that will update the CSS variables to the selected variant before any other code is executed. That way your pre-rendered and contentful page will have the user's preferred color scheme on the very first render.

For example with Next.js, place ThemeScript as the first element of body in your custom Document:

import Document, { Head, Html, Main, NextScript } from "next/document";
import { ThemeScript } from "react-varial";

class MyDocument extends Document {
  /* ... */
  render() {
    return (
      <Html>
        <Head />
        <body>
          <ThemeScript />
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}