Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/chrisburnell/theme-selector

A Web Component to set a site-wide theme preference using a native select element, saved to local storage.
https://github.com/chrisburnell/theme-selector

component components custom-element custom-elements customelement customelements web-component web-components webcomponent webcomponents

Last synced: 3 days ago
JSON representation

A Web Component to set a site-wide theme preference using a native select element, saved to local storage.

Awesome Lists containing this project

README

        

# `theme-selector`

A Web Component for setting a website theme using a select element.

**[Demo](https://chrisburnell.github.io/theme-selector/demo.html)** | **[Further reading](https://chrisburnell.com/theme-selector/)**

## Examples

General usage example:

```html



Light


Dark

theme-selector:not(:defined) {
display: none;
}

```

Example using a custom storage key:

```html



Light


Dark

theme-selector:not(:defined) {
display: none;
}

```

Example using a custom dark theme key:

```html



Black on White


White on Black

theme-selector:not(:defined) {
display: none;
}

```

Example with a custom transition class and duration to keep the class applied:

```html



Light


Dark

theme-selector:not(:defined) {
display: none;
}

```

Example targeting a specific element to apply the theme to:

```html



Light


Dark

theme-selector:not(:defined) {
display: none;
}

```

## Features

This Web Component allows you to use a select element to control user-preferred theming across your website by setting a data attribute against the root element (or a defined selector query). Additionally saves the preference to local storage so that it can be applied on new page loads and maintained between user visits.

## Installation

You have a few options (choose one of these):

1. Install via [npm](https://www.npmjs.com/package/@chrisburnell/theme-selector): `npm install @chrisburnell/theme-selector`
1. [Download the source manually from GitHub](https://github.com/chrisburnell/theme-selector/releases) into your project.
1. Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)

### Usage

Make sure you include the `` in your project (choose one of these):

```html
<!-- Host yourself -->
<script type="module" src="theme-selector.js">
```

```html

```

```html

```

## Credit

With thanks to the following people:

- [David Darnes](https://darn.es) for creating this [Web Component repo template](https://github.com/daviddarnes/component-template)
- [Andy Bell](https://piccalil.li/) for writing the article, [Create a user controlled dark or light mode](https://piccalil.li/tutorial/create-a-user-controlled-dark-or-light-mode/), which served as the initial inspiration and basis for building my own theming setup and eventually became this Web Component