Overview
The js-toggle-dark-mode is a smart JavaScript application that automatically inherits the user’s prefers-color-scheme and allows them to override it and store their preference in their browser storage. This enables site-wide preference for the user across multiple visits.
Features
- Automatic inheritance of the user’s prefers-color-scheme
- Ability for the user to override the color scheme preference
- Storage of the user’s preference in their browser storage
- Site-wide preference across multiple visits
Installation
To install the js-toggle-dark-mode on your own site, follow these steps:
- Copy the three
<link rel="stylesheet">declarations from the top of theindex.htmlfile. The first two declarations for Bootstrap and Font Awesome are optional and only needed for styling the toggle button. - Copy the
<div id="theme-toggler">line from the file, which displays the toggle button if the user’s browser supports the functionality. - Copy the
<script src>declaration from the bottom of theindex.htmlfile. - Copy the
jsandcssfolders. - Edit the
css/light-theme.cssfile with your preferred light theme styles. - Edit the
css/dark-theme.cssfile with your preferred dark theme styles.
Summary
The js-toggle-dark-mode is a JavaScript application that allows users to control the color scheme of a website based on their preference. It automatically inherits the user’s preferred color scheme, but also allows them to override it and store their preference in their browser storage. This enables site-wide preference and ensures that the user’s chosen color scheme persists across multiple visits.