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

@temporg/ui-themeable

v99.10.0

Published

A UI component library made by Rock Content

Downloads

38

Readme


category: packages

ui-themeable

npm  build-status  MIT License  Code of Conduct

The ui-themeable library is meant to be used along with a babel plugin to import CSS styles and generate theme variables. With this framework, each UI component can be used in isolation and support multiple themes, including dynamic themes provided at runtime, while still working within a system of components that use a shared global theme.

Motivation

  1. Two-tiered theme variable system: system-wide variables + component level variables. With this variable system, components can be themed, tested, and rendered in isolation from the rest of the system, and we can mitigate issues that may arise with system-wide theme updates.

  2. Runtime theme application and definition: to apply user/account level themes without using the CSS cascade.

  3. Prevent CSS Cascade bugs: All components should specify variants via props or component level theme variables only (no className or style overrides) with a clear API and should not rely on any external styles.

  4. Theme variables should be accessible in both JS and CSS.

  5. All component styles and variables should scoped to the component.

  6. Pre-render/server-side render support (inline critical CSS).

Installation

yarn add @temporg/ui-themeable

Usage

Make a UI component themeable:

// Button/index.js
import themeable from '@temporg/ui-themeable'

import styles from 'styles.css'
import theme from 'theme.js'

class Button extends React.Component {
  render () {
    return <button className={styles.root}>{this.props.children}</button>
  }
}
export default themeable(theme, styles)(Example)

Themeable components inject their themed styles into the document when they are mounted.

After the initial mount, a themeable component's theme can be configured explicitly via its theme prop or passed via React context using the ApplyTheme component.

Themeable components register themselves with the global theme registry when they are imported into the application, so you will need to be sure to import them before you mount your application so that the default themed styles can be generated and injected.

Defining variables

The themeable component transforms the JS variables defined in the theme.js file into CSS custom properties that are automatically scoped and applied to the component.

For example, to add a variable for the hover state of a Button component, the theme.js file might contain the following:

// Button/theme.js
export default function generator ({ colors }) {
  return (
    background: colors.backgroundMedium,
    color: colors.textDarkest,

    hoverColor: colors.textLightest,
    hoverBackground: colors.backgroundDarkest
  )
}

The arguments to the generator function are the global theme variables. In the above example, we've defined the default theme for the Button component.

The purpose of the generator function is to take the global variables and apply them as values to the functional component level variables. When coming up with names for the component level variables, try to make them describe how they are used in the component (vs describing the variable value).

Supporting multiple themes

If we want to make the Button transform the global theme variables differently with a another theme, (e.g. theme-rock-dark) we can make a generator for that theme:

// Button/theme.js
...
generator['theme-rock-dark'] = function ({ colors }) {
  return {
    background: colors.backgroundLightest
  }
}

This will override the default Button theme and use the global theme variable colors.textLightest for the value of its background theme variable instead of colors.tiara.

The rest of the variables will pick up from the default Button theme generator (applying the global theme variables from the theme-rock-dark theme).

Using theme variables in CSS

Note: Don't worry about scoping your CSS variables (the ui-themable library will take care of that for you):

.root {
  background: var(--background);
  color: var(--color);

  &:hover {
    background: var(--hoverBackground);
    color: var(--hoverColor);
  }
}

Using theme variables in JavaScript

Since the variables are defined in JS you can also access them in your component JS (e.g. this.theme.hoverColor) which will give you the theme values applied via React context with ApplyTheme or the theme prop (falling back to the defaults provided in the theme.js file).

How it works

The babel plugin does a few things:

  1. It uses the css-modules-require-hook to namespace the class names (configurable via themeable.config.js).
  2. It runs postcss on the contents of the theme.css file using plugins defined in postcss.config.js, plus postcss-themeable-styles.
  3. It converts the processed CSS string to a function that provides a JS template so that variable values from theme.js can be injected into the CSS for browsers that don't support CSS variables.

The ui-themable library will call the theme function and inject the resulting CSS string into the document when the component mounts. If the browser supports CSS variables, it will inject namespaced CSS variables into the CSS before adding it to the document.

e.g. The following is injected into the document for browsers with CSS var support:

.list__root {
  color: var(--list__color);
  background: var(--list__background);
}

:root {
  --list__color: #8893A2;
  --list__background: #FFFFFF;
}

Whereas if the browser does not support CSS variables:

.list__root {
  color: #8893A2;
  background: #FFFFFF;
}

The ui-themable library also supports runtime themes as follows:

For browsers that support CSS variables, it will add variables via the style attribute on the component root (when the theme is changed, either via the theme property or via React context using the ApplyTheme component).

<div style="--list-background: red">

For browsers that don't support CSS variables it will update the DOM like:

<div data-theme="XYZ">
  <style type="text/css">
    [data-theme="XYZ"].list__root {
      background: red;
    }
  </style>
</div>