Skip to content

Commit 417eaf1

Browse files
Mossakaclaude
andauthored
Remove theme toggle button and use browser's preferred color scheme (#16781)
The editor now follows the browser's prefers-color-scheme media query instead of providing a manual sun/moon toggle button. Primer CSS already supports data-color-mode="auto", so we just needed to remove the manual override and wire CodeMirror's theme to the media query listener. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2027cf6 commit 417eaf1

2 files changed

Lines changed: 16 additions & 55 deletions

File tree

docs/public/editor/editor.js

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ const errorBanner = $('errorBanner');
121121
const errorText = $('errorText');
122122
const warningBanner = $('warningBanner');
123123
const warningText = $('warningText');
124-
const themeToggle = $('themeToggle');
125124
const divider = $('divider');
126125
const panelEditor = $('panelEditor');
127126
const panelOutput = $('panelOutput');
@@ -138,38 +137,26 @@ let currentYaml = '';
138137
let pendingCompile = false;
139138

140139
// ---------------------------------------------------------------
141-
// Theme (uses Primer's data-color-mode)
140+
// Theme — follows browser's prefers-color-scheme automatically.
141+
// Primer CSS handles the page via data-color-mode="auto".
142+
// We only need to toggle the CodeMirror theme (oneDark vs default).
142143
// ---------------------------------------------------------------
143144
const editorThemeConfig = new Compartment();
144145
const outputThemeConfig = new Compartment();
146+
const darkMq = window.matchMedia('(prefers-color-scheme: dark)');
145147

146-
function getPreferredTheme() {
147-
const saved = localStorage.getItem('gh-aw-playground-theme');
148-
if (saved) return saved;
149-
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
148+
function isDark() {
149+
return darkMq.matches;
150150
}
151151

152-
function cmThemeFor(theme) {
153-
return theme === 'dark' ? oneDark : [];
152+
function cmThemeFor(dark) {
153+
return dark ? oneDark : [];
154154
}
155155

156-
function setTheme(theme) {
157-
document.documentElement.setAttribute('data-color-mode', theme);
158-
localStorage.setItem('gh-aw-playground-theme', theme);
159-
const sunIcon = themeToggle.querySelector('.icon-sun');
160-
const moonIcon = themeToggle.querySelector('.icon-moon');
161-
if (theme === 'dark') {
162-
sunIcon.style.display = 'block';
163-
moonIcon.style.display = 'none';
164-
} else {
165-
sunIcon.style.display = 'none';
166-
moonIcon.style.display = 'block';
167-
}
168-
169-
// Update CodeMirror themes
170-
const cmTheme = cmThemeFor(theme);
171-
editorView.dispatch({ effects: editorThemeConfig.reconfigure(cmTheme) });
172-
outputView.dispatch({ effects: outputThemeConfig.reconfigure(cmTheme) });
156+
function applyCmTheme() {
157+
const theme = cmThemeFor(isDark());
158+
editorView.dispatch({ effects: editorThemeConfig.reconfigure(theme) });
159+
outputView.dispatch({ effects: outputThemeConfig.reconfigure(theme) });
173160
}
174161

175162
// ---------------------------------------------------------------
@@ -182,7 +169,7 @@ const editorView = new EditorView({
182169
markdown(),
183170
EditorState.tabSize.of(2),
184171
indentUnit.of(' '),
185-
editorThemeConfig.of(cmThemeFor(getPreferredTheme())),
172+
editorThemeConfig.of(cmThemeFor(isDark())),
186173
keymap.of([{
187174
key: 'Mod-Enter',
188175
run: () => { doCompile(); return true; }
@@ -210,27 +197,13 @@ const outputView = new EditorView({
210197
yaml(),
211198
EditorState.readOnly.of(true),
212199
EditorView.editable.of(false),
213-
outputThemeConfig.of(cmThemeFor(getPreferredTheme())),
200+
outputThemeConfig.of(cmThemeFor(isDark())),
214201
],
215202
parent: outputMount,
216203
});
217204

218-
// ---------------------------------------------------------------
219-
// Apply initial theme + listen for changes
220-
// ---------------------------------------------------------------
221-
setTheme(getPreferredTheme());
222-
223-
themeToggle.addEventListener('click', () => {
224-
const current = document.documentElement.getAttribute('data-color-mode');
225-
setTheme(current === 'dark' ? 'light' : 'dark');
226-
});
227-
228-
// Listen for OS theme changes
229-
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
230-
if (!localStorage.getItem('gh-aw-playground-theme')) {
231-
setTheme(e.matches ? 'dark' : 'light');
232-
}
233-
});
205+
// Listen for OS theme changes and update CodeMirror accordingly
206+
darkMq.addEventListener('change', () => applyCmTheme());
234207

235208
// ---------------------------------------------------------------
236209
// Sample selector + deep-link loading

docs/public/editor/index.html

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,6 @@
160160
<span id="statusText">Loading...</span>
161161
</span>
162162

163-
<div class="d-flex flex-items-center gap-3 ml-auto">
164-
<button class="btn-octicon" id="themeToggle" title="Toggle theme" aria-label="Toggle dark mode">
165-
<!-- Sun icon (shown in dark mode) -->
166-
<svg class="icon-sun octicon" width="16" height="16" viewBox="0 0 16 16" fill="currentColor" style="display:none">
167-
<path d="M8 12a4 4 0 100-8 4 4 0 000 8zm0-1.5a2.5 2.5 0 110-5 2.5 2.5 0 010 5zm5.657-8.157a.75.75 0 010 1.06l-1.061 1.06a.75.75 0 01-1.06-1.06l1.06-1.06a.75.75 0 011.06 0zm-9.193 9.193a.75.75 0 010 1.06l-1.06 1.061a.75.75 0 11-1.061-1.06l1.06-1.061a.75.75 0 011.061 0zM8 0a.75.75 0 01.75.75v1.5a.75.75 0 01-1.5 0V.75A.75.75 0 018 0zM3 8a.75.75 0 01-.75.75H.75a.75.75 0 010-1.5h1.5A.75.75 0 013 8zm13 0a.75.75 0 01-.75.75h-1.5a.75.75 0 010-1.5h1.5A.75.75 0 0116 8zm-8 8a.75.75 0 01-.75-.75v-1.5a.75.75 0 011.5 0v1.5A.75.75 0 018 16zm-5.657-2.343a.75.75 0 010-1.06l1.06-1.061a.75.75 0 111.06 1.06l-1.06 1.06a.75.75 0 01-1.06 0zm12.728 0l-1.06-1.06a.75.75 0 011.06-1.061l1.06 1.06a.75.75 0 01-1.06 1.06z"/>
168-
</svg>
169-
<!-- Moon icon (shown in light mode) -->
170-
<svg class="icon-moon octicon" width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
171-
<path d="M9.598 1.591a.75.75 0 01.785-.175 7 7 0 11-8.967 8.967.75.75 0 01.961-.96 5.5 5.5 0 007.046-7.046.75.75 0 01.175-.786zm1.616 1.945a7 7 0 01-7.678 7.678 5.5 5.5 0 107.678-7.678z"/>
172-
</svg>
173-
</button>
174-
</div>
175163
</header>
176164

177165
<!-- Error Banner -->

0 commit comments

Comments
 (0)