The Dark mode toggle component theme for Cecil adds an icon button to switch a website between light and dark color schemes.
- Icon button: a moon in light mode, a sun in dark mode
- Follows the system preference by default, and keeps up with its changes
- Remembers the user choice (only when it differs from the system preference)
- No flash of the wrong scheme on page load
- Framework agnostic: sets a
darkclass, adata-themeattribute and thecolor-schemeproperty on<html>(works with Tailwind CSS, Bootstrap, Pico CSS or plain CSS) - JavaScript API and
colorschemechangeevent - Accessible:
aria-pressedstate, translatable label (English and French included) - No dependencies, vanilla JavaScript
- Minimal customizable style, through CSS custom properties
composer require cecil/theme-darkmodetoggleOr download the latest archive and uncompress its contents in
themes/darkmodetoggle.
Add darkmodetoggle in the theme section of the config.yml:
theme:
- darkmodetoggleAdd the following line in the HTML <head> of the main template (as early as possible, to avoid a flash):
{{ include('partials/darkmodetoggle-head.html.twig', {site}, with_context = false) }}Then add the button where you want it to be displayed (in the header for example):
{{ include('partials/darkmodetoggle.html.twig') }}The button can be included several times, and accepts optional variables:
{{ include('partials/darkmodetoggle.html.twig', {class: 'ml-2', label: 'Night mode'}) }}In dark mode, the <html> element looks like this:
<html class="dark" data-theme="dark" style="color-scheme: dark;">And in light mode:
<html data-theme="light" style="color-scheme: light;">So the dark styles of the website can be written with:
- Tailwind CSS: the
dark:variant, with@custom-variant dark (&:where(.dark, .dark *));(v4) ordarkMode: 'class'(v3) - Bootstrap: set the
classoption to an empty string and target[data-theme="dark"], or rely on thecolorschemechangeevent to setdata-bs-theme - Plain CSS:
:root[data-theme="dark"] { … }
Default options:
darkmodetoggle:
storage_key: theme # localStorage key of the user preference
class: dark # class added to <html> in dark mode (empty to disable)
style: true # minimal button style (false to style it yourself)The button style can be customized with the following CSS custom properties:
.darkmodetoggle {
--darkmodetoggle-size: 1.5rem; /* icon size */
--darkmodetoggle-padding: .25rem;
--darkmodetoggle-radius: .375rem;
--darkmodetoggle-color: currentColor;
--darkmodetoggle-background: transparent;
}Available CSS classes: .darkmodetoggle (button), .darkmodetoggle-icon-dark (moon icon) and .darkmodetoggle-icon-light (sun icon).
window.colorScheme.current(); // 'light' or 'dark'
window.colorScheme.set('dark');
window.colorScheme.toggle();
document.addEventListener('colorschemechange', (event) => {
console.log(event.detail.scheme); // 'light' or 'dark'
});Any element with the data-darkmodetoggle attribute toggles the color scheme when clicked.
The button label and title are translatable (English and French included):
# translations/messages.<locale>.yml
"Dark mode": "Mode sombre"
"Toggle dark mode": "Basculer le mode sombre"The demo directory contains a website using the theme (with Pico CSS). To run it locally:
php demo/link.php
php cecil.phar serve demo
demo/link.phplinks the repository asdemo/themes/darkmodetoggle, so the demo uses the theme under development.
Dark mode toggle is a free software distributed under the terms of the MIT license.
