Theming and Dark Mode Support
Add native light and dark theming to your Electron UI using the nativeTheme module and CSS variables, complementing your existing UI-building skills.
Theming and Dark Mode Support is a free Electron Desktop App Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Electron Desktop App Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Theming Matters
Desktop users expect apps to respect their system theme. A polished Electron app switches between light and dark automatically.
The nativeTheme Module
Electron exposes nativeTheme in the main process to read and control the OS appearance.
const { nativeTheme } = require('electron');
console.log(nativeTheme.shouldUseDarkColors);Detecting Dark Mode
shouldUseDarkColors returns a boolean telling you the active appearance. Use it to pick initial styles.
function themeName(isDark) {
return isDark ? 'dark' : 'light';
}
console.log(themeName(true));Reacting to Changes
Listen for the updated event so your UI follows when the user switches their system theme live.
nativeTheme.on('updated', () => {
const dark = nativeTheme.shouldUseDarkColors;
win.webContents.send('theme-changed', dark);
});CSS Variables for Themes
Define colors as CSS custom properties so switching themes only swaps a class on the root element.
:root {
--bg: #ffffff;
--fg: #111111;
}
.dark {
--bg: #111111;
--fg: #f5f5f5;
}Applying the Theme Class
In the renderer, toggle a class based on the IPC message from the main process.
function applyTheme(isDark) {
document.body.classList.toggle('dark', isDark);
}Forcing a Theme
Let users override the system with nativeTheme.themeSource, which accepts 'system', 'light', or 'dark'.
function setSource(choice) {
const valid = ['system', 'light', 'dark'];
return valid.includes(choice) ? choice : 'system';
}
console.log(setSource('dark'));Persisting User Choice
Save the user's theme preference so it survives restarts. A small config file or localStorage works well.
Theming Native Elements
The window title bar and menus can also follow the theme on platforms that support it, for a seamless look.
Avoiding Flash of Wrong Theme
Read the theme before the window shows, then reveal it on ready-to-show to avoid a jarring color flash.
Accessible Color Choices
Ensure both themes meet contrast ratios. Dark mode is not just inverted colors; test readability.
Quick Check
Test your theming knowledge.
Recap
You learned to support light and dark themes using nativeTheme, CSS variables, live updates via IPC, user overrides with themeSource, and persistence of preferences.
Frequently asked questions
Is the “Theming and Dark Mode Support” lesson free?
Yes — the full text of “Theming and Dark Mode Support” is free to read here on the web, and the Electron Desktop App Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Electron Desktop App Development course, upgrade to CoddyKit PRO.
What will I learn in “Theming and Dark Mode Support”?
Add native light and dark theming to your Electron UI using the nativeTheme module and CSS variables, complementing your existing UI-building skills. You practise Electron Desktop App Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Electron Desktop App Development?
No prior experience is required. Electron Desktop App Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Theming and Dark Mode Support” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Electron Desktop App Development lesson?
Yes. Every Electron Desktop App Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integrating HTML, CSS, JavaScript
- Using Frontend Frameworks
- Responsive Desktop Design
- Theming and Dark Mode Support