0Pricing
Electron Desktop App Development · レッスン

テーマ設定とダークモード対応

nativeThemeモジュールとCSS変数を使って、Electron UIにネイティブなライトテーマとダークテーマを追加します。既存のUI構築スキルもさらに活用できます。

「テーマ設定とダークモード対応」はCoddyKit上の無料Electron Desktop App Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはElectron Desktop App Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Electron Desktop App Developmentコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.

よくある質問

「テーマ設定とダークモード対応」レッスンは無料ですか?

はい。「テーマ設定とダークモード対応」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Electron Desktop App Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Electron Desktop App Developmentコースには全4レッスンが含まれています。

「テーマ設定とダークモード対応」で何を学びますか?

nativeThemeモジュールとCSS変数を使って、Electron UIにネイティブなライトテーマとダークテーマを追加します。既存のUI構築スキルもさらに活用できます。 ブラウザで直接実行するハンズオンコードでElectron Desktop App Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Electron Desktop App Developmentを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのElectron Desktop App Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「テーマ設定とダークモード対応」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このElectron Desktop App Developmentレッスンでコードを書いて実行できますか?

はい。すべてのElectron Desktop App Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. HTML、CSS、JavaScriptの統合
  2. フロントエンドフレームワークの利用
  3. レスポンシブなデスクトップデザイン
  4. テーマ設定とダークモード対応
← Electron Desktop App Developmentに戻る