components

Tailwind CSS Theme Controller

If a checkbox or radio input with the theme-controller class is present on the page, the page's theme will be set to the value of that input.

Class Name
Type
Description
theme-controllerComponentFor a checkbox or radio button input intended to toggle the page theme.

The Theme Controller changes the theme using only CSS. You can use JavaScript to save the input state to the server or localStorage if you want it to persist across page refreshes.

Use the theme-controller component class with a checkbox input to create a theme switcher that toggles between the default theme and the theme specified in the value.

Create a theme controller using a switch based on the example provided below.

The example below shows how to implement a theme controller using a switch component.

The example below illustrates how to create a theme controller using a switch combined with text.

The example provided shows how to build a theme controller using a switch that features icons.

The example below shows how to build a theme controller using a switch with personalized colors.

Create a theme controller using a radio input based on the example below.

The example below shows how to create a theme controller using a radio button.

Here’s a ready-to-use example of a theme controller utilizing a dropdown menu.

Below are the comprehensively outlined steps you can follow to seamlessly integrate theme-change into your project for efficient theme switching functionality with storing in local storage.

  • 1
    Step 1: Install theme-change

    Install theme-change via npm.

    npm i theme-change --save

  • 2
    Step 2: Include it in javascript file.

    To integrate theme-change, add the following code in your javascript file.

    import { themeChange } from 'theme-change'
    themeChange()
    You can also use a CDN link.

    Include the following <script> tag near the end of your </body> section:

    <script src="https://cdn.jsdelivr.net/npm/theme-change@2.0.2/index.js"></script>

  • 3
    Step 3: Basic use case of theme-change.

    The below example showcase the toggle functionality using the switch component.

    <input type="checkbox" data-toggle-theme="luxury" class="switch" />

The theme-change plugin offers three primary attributes to manage themes dynamically:

  1. data-set-theme="luxury"
    Use this attribute to set the theme directly for components such as buttons, radio buttons, and dropdowns. The theme is applied upon user interaction (e.g., clicking a button).

  2. data-choose-theme
    This attribute is designed for select components. It dynamically applies the theme based on the selected option from a dropdown menu.

  3. data-toggle-theme="soft"
    Ideal for toggle components like checkboxes and switches. This attribute allows users to switch between themes interactively by toggling the element.

It also provides the data-act-class="ACTIVECLASS" attribute, which applies the specified ACTIVECLASS to the element when it is clicked or toggled.

For a live demonstration and detailed example of using FlyonUI with the theme-change plugin, check out this StackBlitz example.