@@ -121,7 +121,6 @@ const errorBanner = $('errorBanner');
121121const errorText = $ ( 'errorText' ) ;
122122const warningBanner = $ ( 'warningBanner' ) ;
123123const warningText = $ ( 'warningText' ) ;
124- const themeToggle = $ ( 'themeToggle' ) ;
125124const divider = $ ( 'divider' ) ;
126125const panelEditor = $ ( 'panelEditor' ) ;
127126const panelOutput = $ ( 'panelOutput' ) ;
@@ -138,38 +137,26 @@ let currentYaml = '';
138137let 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// ---------------------------------------------------------------
143144const editorThemeConfig = new Compartment ( ) ;
144145const 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
0 commit comments