- Plugins
- Theme Switch
Plugins
Tailwind CSS Theme Switch Plugin API
Headless, unstyled Theme Switch plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Theme Switch plugin via npm, or skip this step if you are already using Preline UI as a package.
npm i @preline/theme-switch
Toggling dark mode manually
To enable manual toggling of dark mode instead of relying on the operating system preference, use Tailwind's @custom-variant strategy:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
...
How you add the dark class to the html element is up to you, but a common approach is to use a bit of JS that reads a preference from somewhere (like localStorage) and updates the DOM accordingly.
For more information, visit Tailwind CSS Dark Mode
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<button type="button" class="hs-dark-mode hs-dark-mode-active:hidden block" data-hs-theme-click-value="dark">
Dark
</button>
<button type="button" class="hs-dark-mode hs-dark-mode-active:inline-flex hidden" data-hs-theme-click-value="light">
Light
</button>
Methods
The HSThemeSwitch object is contained within the global window object
Destroy instance.
const { element } = HSThemeSwitch.getInstance('#theme-switch', true);
const destroyBtn = document.querySelector('#destroy-btn');
destroyBtn.addEventListener('click', () => {
element.destroy();
});
Events
When appearance was changed.
window.addEventListener('on-hs-appearance-change', () => {
...your code
});
Demo examples
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.