Coder Social home page Coder Social logo

chrisburnell / theme-selector Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 21 KB

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

Home Page: https://chrisburnell.github.io/theme-selector/demo.html

License: MIT License

JavaScript 37.85% HTML 62.15%
component components custom-element custom-elements customelement customelements web-component web-components webcomponent webcomponents

theme-selector's Introduction

theme-selector

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

Demo | Further reading

Examples

General usage example:

<script type="module" src="theme-selector.js"></script>

<theme-selector>
    <select autocomplete="off">
        <option value="light">
            Light
        </option>
        <option value="dark">
            Dark
        </option>
    </select>
</theme-selector>

<style>
    theme-selector:not(:defined) {
        display: none;
    }
</style>

Example using a custom storage key:

<script type="module" src="theme-selector.js"></script>

<theme-selector data-key="color-scheme">
    <select autocomplete="off">
        <option value="light">
            Light
        </option>
        <option value="dark">
            Dark
        </option>
    </select>
</theme-selector>

<style>
    theme-selector:not(:defined) {
        display: none;
    }
</style>

Example using a custom dark theme key:

<script type="module" src="theme-selector.js"></script>

<theme-selector data-dark-theme="white-on-black">
    <select autocomplete="off">
        <option value="black-on-white">
            Black on White
        </option>
        <option value="white-on-black">
            White on Black
        </option>
    </select>
</theme-selector>

<style>
    theme-selector:not(:defined) {
        display: none;
    }
</style>

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

<script type="module" src="theme-selector.js"></script>

<theme-selector data-transition-class="transitioning" data-transition-duration="100">
    <select autocomplete="off">
        <option value="light">
            Light
        </option>
        <option value="dark">
            Dark
        </option>
    </select>
</theme-selector>

<style>
    theme-selector:not(:defined) {
        display: none;
    }
</style>

Example targeting a specific element to apply the theme to:

<script type="module" src="theme-selector.js"></script>

<theme-selector data-root-element=".content">
    <select autocomplete="off">
        <option value="light">
            Light
        </option>
        <option value="dark">
            Dark
        </option>
    </select>
</theme-selector>

<style>
    theme-selector:not(:defined) {
        display: none;
    }
</style>

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: npm install @chrisburnell/theme-selector
  2. Download the source manually from GitHub into your project.
  3. Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)

Usage

Make sure you include the <script> in your project (choose one of these):

<!-- Host yourself -->
<script type="module" src="theme-selector.js"></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://www.unpkg.com/@chrisburnell/theme-selector/theme-selector.js"
></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://esm.sh/@chrisburnell/theme-selector"
></script>

Credit

With thanks to the following people:

theme-selector's People

Contributors

chrisburnell avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.