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

rethemeable

v5.0.1

Published

Themeable React component utilities

Downloads

20

Readme

Rethemeable

Rethemeable provides utilities for producing and consuming themeable React components.

It doesn't define the notion of theme. A theme can be a list of CSS class names or a set of inline style directives.

It doesn't define how components apply themes. It is up to component to decide what and when to apply CSS class names or inline styles.

Instead Rethemeable define a way to propagate theme through React component tree.

It's up to component authors to declare the theming contract for each component.

It's up to consumers to fulfil the theming contract for each component.

Installation

Install from npm:

% npm install rethemeable

Usage

Define a themeable component:

import { themeable } from 'rethemeable'
import React from 'react'

@themeable
class Button extends React.Component {

  render() {
    let theme = this.theme
    return (
      <button className={theme.self}>
        <i className={theme.icon} />
        {this.props.children}
      </button>
    )
  }
}

Now define a theme as a collection of styles for a set of React components. Styles for each component are isolated by an unique key within the theme:

import Button from 'widgets/Button'

let BootstrapTheme = {
  [Button.theme]: {
    self: 'btn btn-xs'
  }
}

And configure it via <ApplyTheme /> component:

import { ApplyTheme } from 'rethemeable'
import React from 'react'

React.render(
  <ApplyTheme theme={BootstrapTheme}>
    <div>
      <h1>This is an app</h1>
      <Button>See, I don't have theme prop passed explicitly</Button>
    </div>
  </ApplyTheme>
)

Component <Button /> will receive theme implicitly.

You can also pass theme directly to component via props if you need a more fine-grained control:

<Button theme={BootstrapTheme} />

If you don't want to use <ApplyTheme /> component but just apply some theme on a themeable component you can use theme function:

import { theme } from 'rethemeable'
import ThemeableButton from 'third-party-themeable-button'

let MyButton = theme(ThemeableButton, BootstrapTheme);

React.render(<MyButton>Hello, I'm themed!</MyButton>)

Rethemeable with CSS modules

Rethemeable approach works well with CSS modules, as you can compose your theme from a set of CSS modules:

import Button from 'widgets/Button'
import Modal from 'widgets/Button'

import ButtonTheme from './Button.css'
import ModalTheme from './Modal.css'

let Theme = {
  [Button.theme]: ButtonTheme,
  [ModalTheme.theme]: ModalTheme
}

Chunking theme and component separately, or when to pass themeKeys

It can be desirable to chunk (or split) bundled assets in production, especially when dealing with larger sites.

When chunking themeable components, the theme and themeable components must to be able to be loaded independently in order for a build pipeline (like Webpack) to properly optimize chunk size and contents.

Separating the definition of theme keys from a given themeable component allows the keys, theme, and components to be loaded if and whenever they are needed.

./key-registry.js

/** List of theme keys to connect classmaps with component themes */

export {
  Button: Symbol('Button'),
  Modal: Symbol('Modal')
}

./theme.js

/** Map themeKey Symbols to theme classmaps */

import { Button, Modal } from './key-registry';
import ButtonTheme from './button.css';
import ModalTheme from 'some-module/with-modal-styles.css';

const theme = {
  [Button]: ButtonTheme,
  [Modal]: ModalTheme
}

export { theme }

./Button.jsx

import { themeable } from 'rethemeable';
import { compAKey as themeKey } from './key-registry';

class Button {}

export default themeable(Button, {themeKey});

./Modal.jsx

import { themeable } from 'rethemeable';
import { compBKey as themeKey } from './key-registry';

class Modal {}

export default themeable(Modal, {themeKey});

./Layout.jsx

/** Provide theme to themeable components via context */

import Helmet from 'react-helmet';
import { theme } from './theme.js';
import { ApplyTheme } from 'rethemeable';

// map of asset paths extracted during build
import assets from 'assets-map.json';

class App {
  /*

  render(
    <ApplyTheme theme={theme}>
      <Helmet link={assets.theme.css} />
        {this.props.children}
    </ApplyTheme>
    )
}

Without chunks, all components are hard dependencies of a theme. Therefore they must be chunked together to preserve the link of Symbol to theme. This can result bloated chunks and/or tedious maintenance of themes.