themeio
TypeScript icon, indicating that this package has built-in type declarations

0.5.5 • Public • Published

npm Package Coverage Status

themeio

Effortless theme switching for your web apps.

themeio allows you to change the style of your app (or parts of it) based on various strategies, e.g. according to the system theme or based on a schedule. It optionally integrates with storage backends to persist a user's preferred theme.

themeio builds on 3 components to do its work, which can be customized to your requirements:

  • a ThemeStrategy determines when to use which theme
  • a ThemeRenderer applies the determined theme to your app
  • a ThemeStore optionally persists the selected theme

By default, themes are rendered by toggling classes on the document.body element.

If you simply want to switch between a light and a dark theme, you can follow the quick start example.

Installation

Use npm or your package manager of choice to install themeio:

npm install --save themeio

Quick Start

The following code shows an example for switching between a dark and a light theme:

<body>
  <script src="node_modules/themeio/dist/default.min.js"></script>
  <button>Toggle Theme</button>
</body>
body.themeio-light {
  background: #fff;
  color: #000;
}

body.themeio-dark {
  background: #000;
  color: #fff;
}
document.querySelector('button').addEventListener('click', () => {
  window.themeio.toggle() // <-- toggle the currently active theme
});

Configuration

themeio ships with various strategies, stores and renderers to choose from. If you can't find a suitable implementation, you can always add your own. Refer to the documentation below for more details.

Strategies

Strategies define when themeio chooses which theme. The following strategies ship with themeio.

You can follow the links to each strategy for details and examples.

If none of the available strategies suits your needs, you can define your own by implementing the ThemeStrategy interface.

Renderers

Renderers define how to apply a theme to your app. In most cases, this means making changes to the DOM.

Stores

Stores define how to store a theme when it is changed manually. This is useful for keeping the same theme across sessions.

Package Sidebar

Install

npm i themeio

Weekly Downloads

9

Version

0.5.5

License

MIT

Unpacked Size

31.2 kB

Total Files

42

Last publish

Collaborators

  • kevlatus