logo polydile logo polydile en modo oscuro dile-components

dile-light-mode-switch

Web Component to toggle between light and dark themes using a switch and an icon. It allows dynamically changing the theme by toggling a class on the <html> element.

Installation

npm i @dile/ui

Usage

Import the component.

import '@dile/ui/components/light-mode-switch/light-mode-switch.js';

Use the component.

<dile-light-mode-switch></dile-light-mode-switch>

Properties

At the time of component initialization, it will look for the class defined by the darkModeClass property to set the value of the boolean property darkMode. If it finds the class on the <html> tag, it will initialize darkMode as true; if it does not find it, then darkMode will be initialized as false.

Methods

The component also provides a method to controls the component state programmatically.

Custom Events

CSS Custom Properties

Custom property Description Default
--dile-light-mode-switch-light-state-color Switch knob color in light mode #303030
--dile-light-mode-switch-dark-state-color Switch knob color in dark mode #e5e5e5
--dile-light-mode-switch-light-state-bar-color Switch bar color in light mode #dddddd
--dile-light-mode-switch-dark-state-bar-color Switch bar color in dark mode #0e0e0e
--dile-light-mode-switch-light-state-icon-color Icon color in light mode #303030
--dile-light-mode-switch-dark-state-icon-color Icon color in dark mode #e5e5e5
--dile-light-mode-switch-light-state-focus-color Border color when the element has the focus in light mode #3d91e5
--dile-light-mode-switch-dark-state-focus-color Border color when the element has the focus in light mode #ff9

dile-light-mode-switch demos

Default usage

<script type="module">
  import '@dile/ui/components/light-mode-switch/light-mode-switch.js';
</script>

<dile-light-mode-switch></dile-light-mode-switch>

Styled switch

<style>
  .styled {
    --dile-light-mode-switch-light-state-color: #555;
    --dile-light-mode-switch-dark-state-color: #fff;
    --dile-light-mode-switch-light-state-bar-color: #bbb;
    --dile-light-mode-switch-dark-state-bar-color: #369;
    --dile-light-mode-switch-light-state-icon-color: #666;
    --dile-light-mode-switch-dark-state-icon-color: #eee;
  }
</style>

<dile-light-mode-switch class="styled"></dile-light-mode-switch>