右上のボタンでテーマを切り替えてみてください。設定はブラウザに保存されます。
テーマ: -
保存状態: -
システム設定: -
すべてのコンポーネントが自動的にテーマに対応します
テーマを切り替えると、背景色とテキスト色が自動的に変わります。
<html lang="ja" data-theme="light">
<script>
// シンプルなトグル
function toggleTheme() {
const html = document.documentElement;
const current = html.getAttribute('data-theme');
const newTheme = current === 'dark' ? 'light' : 'dark';
html.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
}
// テーマを設定
function setTheme(theme) {
if (theme === 'auto') {
// システム設定に従う
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
theme = prefersDark ? 'dark' : 'light';
localStorage.removeItem('theme'); // auto設定を保存
}
document.documentElement.setAttribute('data-theme', theme);
if (theme !== 'auto') {
localStorage.setItem('theme', theme);
}
}
// ページ読み込み時にテーマを復元
const savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
</script>
<button class="theme-toggle" onclick="toggleTheme()">テーマ切り替え</button>
ページ読み込み時のテーマフラッシュを防ぐため、<head>の最初にスクリプトを配置:
<head>
<!-- 最初に実行してフラッシュを防ぐ -->
<script>
(function() {
const savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
})();
</script>
<meta charset="UTF-8">
<link rel="stylesheet" href="dist/asagiri.min.css">
</head>
<script>
// システムのダークモード設定を検出
function getPreferredTheme() {
const saved = localStorage.getItem('theme');
if (saved) return saved;
// システム設定を確認
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
// システム設定の変更を監視
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
// ユーザーが手動設定していない場合のみ変更
if (!localStorage.getItem('theme')) {
const newTheme = e.matches ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', newTheme);
}
});
// 初期化
const theme = getPreferredTheme();
document.documentElement.setAttribute('data-theme', theme);
</script>
A: localStorage.setItem('theme', newTheme) を実行しているか確認してください。
A: テーマ復元スクリプトを<head>の最初に配置してください(CSSより前)。
A: そのコンポーネントがCSS変数(var(--color-*))を使用しているか確認してください。
A: CSS変数を上書きすることで簡単にカスタマイズできます:
[data-theme="dark"] {
--color-primary: #your-color;
--color-bg: #your-bg-color;
}