Control Dark Mode
The dark mode is controlled by toggling the
d4r-dark class. To apply and remember
the user's preference, you can add the following script to the <head> of your
page:
1
2
3
4
5
6
7
8
9
10
11
12
13<script>
if (typeof window !== 'undefined') {
if (
localStorage['d4r-theme'] === 'dark' ||
(!('d4r-theme' in localStorage) &&
window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
document.documentElement.classList.add('d4r-dark');
} else {
document.documentElement.classList.remove('d4r-dark');
}
}
</script>You can then toggle the dark mode by adding or removing the
d4r-dark class and
storing the user's preference in localStorage:
1
2
3
4
5
6
7// Enable light mode
localStorage['d4r-theme'] = 'light'
document.documentElement.classList.remove('d4r-dark');
// Enable dark mode
localStorage['d4r-theme'] = 'dark'
document.documentElement.classList.add('d4r-dark');