Dark Mode it's not new to Apple's design system, and it has been something we've been wanting to add since the first version of Puppertino. And this is now a strong part of Puppertino, to use Dark Mode, you will need to get the CSS of Dark Mode or download the full CSS
Dark mode it's pretty straight forward to use. And it comes in two flavors, automatic and manual toggling. The automatic toggling uses the media-prefers-scheme
to get the user's desired theme and apply automatically. Of course, this doesn't work in some (mosts) of Linux Operating systems, and that's what the manual version is aimed for. It's also really useful if you want your users to select their own theme.
This is how it looks on practice (All current components support dark mode):
You can experience the Dark Mode in all of the Puppertino's website by clicking This button
Usage:
<body class="p-auto-dark-mode"></body> <!-- Automatic -->
<body class="p-dark-theme"></body> <!-- Manual -->
1. Initializing PuppertinoThemeMan
To get started, initialize PuppertinoThemeMan
by calling the init()
function. This setup allows you to customize theme behavior, including auto-detection based on system preferences.
puppertinoThemeMan.init({
autoDetect: true, // Automatically detect system theme
darkThemeClass: 'p-dark-mode' // Your custom dark theme class (optional)
});
PuppertinoThemeMan
will adjust based on the system's light or dark mode.
Easily switch between light and dark themes using the toggle()
function. This function will apply the correct theme class and store the user’s selection for future visits.
puppertinoThemeMan.toggle();
When this function is called, PuppertinoThemeMan will:
localStorage
to remember the preference across sessions.If enabled in the init()
options, PuppertinoThemeMan can automatically switch to light or dark mode based on the user’s system preferences. This ensures a seamless experience aligned with the user’s device settings.
PuppertinoThemeMan synchronizes theme changes across multiple tabs. If a user changes their theme in one tab, the new theme will automatically apply to all open tabs in the same browser.
No extra configuration is needed—this behavior is built into PuppertinoThemeMan, ensuring a unified user experience.
You can check whether the dark theme is active by using the isDarkThemeActive() function. This function returns true if the dark theme is currently applied.
const isDarkMode = puppertinoThemeMan.isDarkThemeActive();
This allows you to tailor additional features based on the active theme.
How does this all work? Simple, Puppertino is based on CSS variables, and when you toggle this class you are basically overriding all the colors in CSS variables for the dark themed ones, making it really easy to change all the colors, and also create your own themes if you need to do so. Keep in mind that some variables have been reused to cut down sizes.