Theme toggle
Let people switch between light and dark. Momo follows the system setting by default; a toggle or a choice of "Light / Dark / System" overrides it and is remembered on every page.
Toggle
A button with data-momo-theme-toggle flips the mode. Put a moon and a sun in it: .theme-icon-light shows in light mode, .theme-icon-dark in dark. Add data-momo-theme-dark="dark-black" (or "dark-split") to switch to another dark instead.
<button class="btn btn-icon" type="button" data-momo-theme-toggle aria-label="Dark mode"><i class="mi mi-moon theme-icon-light" aria-hidden="true"></i><i class="mi mi-sun theme-icon-dark" aria-hidden="true"></i></button>
<button class="btn" type="button" data-momo-theme-toggle><i class="mi mi-moon theme-icon-light" aria-hidden="true"></i><i class="mi mi-sun theme-icon-dark" aria-hidden="true"></i> Switch mode</button>Light, dark or system
Buttons with data-momo-theme="light", "dark", "dark-black", "dark-split" (see three darks) or "system"; the chosen one gets aria-pressed="true".
<div class="btn-row" role="group" aria-label="Color mode">
<button class="btn btn-sm" type="button" data-momo-theme="light"><i class="mi mi-sun" aria-hidden="true"></i> Light</button>
<button class="btn btn-sm" type="button" data-momo-theme="dark"><i class="mi mi-moon" aria-hidden="true"></i> Dark</button>
<button class="btn btn-sm" type="button" data-momo-theme="system"><i class="mi mi-monitor" aria-hidden="true"></i> System</button>
</div>No flash on load
Put this one line in the <head>, before the stylesheet, so a page opened in dark mode never flashes white first:
<script>try{var m=localStorage.getItem('momo-mode');if(m)document.documentElement.setAttribute('data-momo-mode',m)}catch(e){}</script>
Notes
- The choice is kept in the reader's browser as
momo-mode; "System" forgets it and follows the device again. - From script:
Momo.theme.get()is"light"or"dark",Momo.theme.set('dark')changes it, and amomo:themeevent fires. - To swap a picture or logo by mode, use state variants:
dark:hiddenandhidden dark:block. - The toggle button says whether dark mode is on (
aria-pressed); give an icon-only one a name likearia-label="Dark mode".