Skip to content

Commit b5af774

Browse files
do not respect user color scheme if user has set a preference
also, the localAlternateSentinel is used dynamically so it needs to be updated when we detect an OS change so that the toggle works on the first click
1 parent cf69f72 commit b5af774

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/resources/formats/html/templates/quarto-html-before-body.ejs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@
184184
185185
<% if (respectUserColorScheme) { %>
186186
queryPrefersDark.addEventListener("change", e => {
187+
if(window.localStorage.getItem("quarto-color-scheme") !== null)
188+
return;
187189
const alternate = e.matches
188190
toggleColorMode(alternate);
191+
localAlternateSentinel = e.matches ? 'alternate' : 'default'; // this is used alongside local storage!
189192
toggleGiscusIfUsed(alternate, darkModeDefault);
190193
});
191194
<% } %>

tests/integration/playwright/tests/html-dark-mode-defaultdark.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,21 @@ test('Project specifies dark and light brands, dynamic respect-user-color-scheme
8787
await expect(locatr).toHaveCSS('background-color', blue);
8888
await check_toggle(page, false);
8989
});
90+
91+
92+
test('Project specifies dark and light brands, do not respect-user-color-scheme after toggling', async ({ page }) => {
93+
await page.goto('./html/dark-brand/project-dark/simple-respect-color-scheme.html');
94+
const locatr = await page.locator('body').first();
95+
await expect(locatr).toHaveClass(`fullcontent quarto-dark`);
96+
await expect(locatr).toHaveCSS('background-color', red);
97+
await check_toggle(page, true);
98+
99+
await check_backgrounds(page, 'quarto-dark', red, blue);
100+
await check_backgrounds(page, 'quarto-light', blue, red);
101+
await check_toggle(page, true);
102+
103+
await page.emulateMedia({ colorScheme: 'light' });
104+
await expect(locatr).toHaveClass(`fullcontent quarto-dark`);
105+
await expect(locatr).toHaveCSS('background-color', red);
106+
await check_toggle(page, true);
107+
});

0 commit comments

Comments
 (0)