Dark Mode

Overview

Automatically update the native interfaces

“Native interfaces” include the file picker, window border, dialogs, context menus, and more - anything where the UI comes from your operating system and not from your app. The default behavior is to opt into this automatic theming from the OS.

Automatically update your own interfaces

If your app has its own dark mode, you should toggle it on and off in sync with the system’s dark mode setting. You can do this by using the prefers-color-scheme CSS media query.

Manually update your own interfaces

If you want to manually switch between light/dark modes, you can do this by setting the desired mode in the themeSource property of the nativeTheme module. This property’s value will be propagated to your Renderer process. Any CSS rules related to prefers-color-scheme will be updated accordingly.

macOS settings

In macOS 10.14 Mojave, Apple introduced a new system-wide dark mode for all macOS computers. If your Electron app has a dark mode, you can make it follow the system-wide dark mode setting using the nativeTheme api.

In macOS 10.15 Catalina, Apple introduced a new “automatic” dark mode option for all macOS computers. In order for the nativeTheme.shouldUseDarkColors and Tray APIs to work correctly in this mode on Catalina, you need to use Electron >=7.0.0, or set NSRequiresAquaSystemAppearance to false in your Info.plist file for older versions. Both Electron Packager and Electron Forge have a darwinDarkModeSupport option to automate the Info.plist changes during app build time.

If you wish to opt-out while using Electron > 8.0.0, you must set the NSRequiresAquaSystemAppearance key in the Info.plist file to true. Please note that Electron 8.0.0 and above will not let you opt-out of this theming, due to the use of the macOS 10.14 SDK.

Example

This example demonstrates an Electron application that derives its theme colors from the nativeTheme. Additionally, it provides theme toggle and reset controls using IPC channels.

docs/fiddles/features/dark-mode (27.0.1)Open in Fiddle

  • main.js
  • preload.js
  • index.html
  • renderer.js
  • styles.css
  1. const { app, BrowserWindow, ipcMain, nativeTheme } = require('electron')
  2. const path = require('node:path')
  3. function createWindow () {
  4. const win = new BrowserWindow({
  5. width: 800,
  6. height: 600,
  7. webPreferences: {
  8. preload: path.join(__dirname, 'preload.js')
  9. }
  10. })
  11. win.loadFile('index.html')
  12. }
  13. ipcMain.handle('dark-mode:toggle', () => {
  14. if (nativeTheme.shouldUseDarkColors) {
  15. nativeTheme.themeSource = 'light'
  16. } else {
  17. nativeTheme.themeSource = 'dark'
  18. }
  19. return nativeTheme.shouldUseDarkColors
  20. })
  21. ipcMain.handle('dark-mode:system', () => {
  22. nativeTheme.themeSource = 'system'
  23. })
  24. app.whenReady().then(() => {
  25. createWindow()
  26. app.on('activate', () => {
  27. if (BrowserWindow.getAllWindows().length === 0) {
  28. createWindow()
  29. }
  30. })
  31. })
  32. app.on('window-all-closed', () => {
  33. if (process.platform !== 'darwin') {
  34. app.quit()
  35. }
  36. })
  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld('darkMode', {
  3. toggle: () => ipcRenderer.invoke('dark-mode:toggle'),
  4. system: () => ipcRenderer.invoke('dark-mode:system')
  5. })
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Hello World!</title>
  6. <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  7. <link rel="stylesheet" type="text/css" href="./styles.css">
  8. </head>
  9. <body>
  10. <h1>Hello World!</h1>
  11. <p>Current theme source: <strong id="theme-source">System</strong></p>
  12. <button id="toggle-dark-mode">Toggle Dark Mode</button>
  13. <button id="reset-to-system">Reset to System Theme</button>
  14. <script src="renderer.js"></script>
  15. </body>
  16. </body>
  17. </html>
  1. document.getElementById('toggle-dark-mode').addEventListener('click', async () => {
  2. const isDarkMode = await window.darkMode.toggle()
  3. document.getElementById('theme-source').innerHTML = isDarkMode ? 'Dark' : 'Light'
  4. })
  5. document.getElementById('reset-to-system').addEventListener('click', async () => {
  6. await window.darkMode.system()
  7. document.getElementById('theme-source').innerHTML = 'System'
  8. })
  1. :root {
  2. color-scheme: light dark;
  3. }
  4. @media (prefers-color-scheme: dark) {
  5. body { background: #333; color: white; }
  6. }
  7. @media (prefers-color-scheme: light) {
  8. body { background: #ddd; color: black; }
  9. }

How does this work?

Starting with the index.html file:

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Hello World!</title>
  6. <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  7. <link rel="stylesheet" type="text/css" href="./styles.css">
  8. </head>
  9. <body>
  10. <h1>Hello World!</h1>
  11. <p>Current theme source: <strong id="theme-source">System</strong></p>
  12. <button id="toggle-dark-mode">Toggle Dark Mode</button>
  13. <button id="reset-to-system">Reset to System Theme</button>
  14. <script src="renderer.js"></script>
  15. </body>
  16. </body>
  17. </html>

And the styles.css file:

styles.css

  1. @media (prefers-color-scheme: dark) {
  2. body { background: #333; color: white; }
  3. }
  4. @media (prefers-color-scheme: light) {
  5. body { background: #ddd; color: black; }
  6. }

The example renders an HTML page with a couple elements. The <strong id="theme-source"> element shows which theme is currently selected, and the two <button> elements are the controls. The CSS file uses the prefers-color-scheme media query to set the <body> element background and text colors.

The preload.js script adds a new API to the window object called darkMode. This API exposes two IPC channels to the renderer process, 'dark-mode:toggle' and 'dark-mode:system'. It also assigns two methods, toggle and system, which pass messages from the renderer to the main process.

preload.js

  1. const { contextBridge, ipcRenderer } = require('electron')
  2. contextBridge.exposeInMainWorld('darkMode', {
  3. toggle: () => ipcRenderer.invoke('dark-mode:toggle'),
  4. system: () => ipcRenderer.invoke('dark-mode:system')
  5. })

Now the renderer process can communicate with the main process securely and perform the necessary mutations to the nativeTheme object.

The renderer.js file is responsible for controlling the <button> functionality.

renderer.js

  1. document.getElementById('toggle-dark-mode').addEventListener('click', async () => {
  2. const isDarkMode = await window.darkMode.toggle()
  3. document.getElementById('theme-source').innerHTML = isDarkMode ? 'Dark' : 'Light'
  4. })
  5. document.getElementById('reset-to-system').addEventListener('click', async () => {
  6. await window.darkMode.system()
  7. document.getElementById('theme-source').innerHTML = 'System'
  8. })

Using addEventListener, the renderer.js file adds 'click' event listeners to each button element. Each event listener handler makes calls to the respective window.darkMode API methods.

Finally, the main.js file represents the main process and contains the actual nativeTheme API.

  1. const { app, BrowserWindow, ipcMain, nativeTheme } = require('electron')
  2. const path = require('node:path')
  3. const createWindow = () => {
  4. const win = new BrowserWindow({
  5. width: 800,
  6. height: 600,
  7. webPreferences: {
  8. preload: path.join(__dirname, 'preload.js')
  9. }
  10. })
  11. win.loadFile('index.html')
  12. ipcMain.handle('dark-mode:toggle', () => {
  13. if (nativeTheme.shouldUseDarkColors) {
  14. nativeTheme.themeSource = 'light'
  15. } else {
  16. nativeTheme.themeSource = 'dark'
  17. }
  18. return nativeTheme.shouldUseDarkColors
  19. })
  20. ipcMain.handle('dark-mode:system', () => {
  21. nativeTheme.themeSource = 'system'
  22. })
  23. }
  24. app.whenReady().then(() => {
  25. createWindow()
  26. app.on('activate', () => {
  27. if (BrowserWindow.getAllWindows().length === 0) {
  28. createWindow()
  29. }
  30. })
  31. })
  32. app.on('window-all-closed', () => {
  33. if (process.platform !== 'darwin') {
  34. app.quit()
  35. }
  36. })

The ipcMain.handle methods are how the main process responds to the click events from the buttons on the HTML page.

The 'dark-mode:toggle' IPC channel handler method checks the shouldUseDarkColors boolean property, sets the corresponding themeSource, and then returns the current shouldUseDarkColors property. Looking back on the renderer process event listener for this IPC channel, the return value from this handler is utilized to assign the correct text to the <strong id='theme-source'> element.

The 'dark-mode:system' IPC channel handler method assigns the string 'system' to the themeSource and returns nothing. This also corresponds with the relative renderer process event listener as the method is awaited with no return value expected.

Run the example using Electron Fiddle and then click the “Toggle Dark Mode” button; the app should start alternating between a light and dark background color.

Dark Mode