How to add a toggle button for dark mode with Biff and Tailwind
By default, Tailwind's dark mode classes will take effect automatically if the user's operating system is already set to dark mode. Follow these instructions if you would also like to provide a toggle button so the user can override the operating system setting.
Set darkMode: 'class'
in tailwind.config.js
:
module.exports = {
darkMode: 'class',
// ...
}
Add the provided darkmode.js
file to resources/darkmode.js
. Inline it into your HTML so that it runs immediately when the page loads:
(defn some-page [ctx]
(biff/base-html
(merge ctx {:base/head [[:script (biff/unsafe (slurp (io/resource "darkmode.js")))]]})
...))
Then add a button to your UI that calls toggleDarkMode()
:
[:button {:onclick "toggleDarkMode()"} "Toggle"]