Skip to content

Commit 6c6e452

Browse files
a2937raisedadead
authored andcommitted
feat: create a functional dark mode
1 parent 4cbcb3f commit 6c6e452

File tree

9 files changed

+203
-111
lines changed

9 files changed

+203
-111
lines changed

config/i18n/locales/english/translations.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"donate": "Donate",
55
"load-more-articles": "Load More Articles",
66
"menu": "Menu",
7-
"learn": "Curriculum"
7+
"learn": "Curriculum",
8+
"toggle-dark-mode": "Night Mode"
89
},
910
"search": {
1011
"label": "Search",

cypress/e2e/english/landing/landing.cy.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const selectors = {
1212
postPublishedTime: "[data-test-label='post-published-time']",
1313
banner: "[data-test-label='banner']",
1414
dropDownMenu: "[data-test-label='header-menu']",
15-
toggleDropDownMenuButton: "[data-test-label='header-menu-button']"
15+
toggleDropDownMenuButton: "[data-test-label='header-menu-button']",
16+
darkModeButton: "[data-test-label='dark-mode-button']"
1617
};
1718

1819
describe('Landing (Hashnode sourced)', () => {
@@ -37,6 +38,44 @@ describe('Landing (Hashnode sourced)', () => {
3738
);
3839
});
3940

41+
const visit = (darkAppearance: boolean) =>
42+
cy.visit('/', {
43+
onBeforeLoad(win) {
44+
cy.stub(win, 'matchMedia')
45+
.withArgs('(prefers-color-scheme: dark)')
46+
.returns({
47+
matches: darkAppearance
48+
});
49+
}
50+
});
51+
52+
it('The dark mode button should be able to change the theme to dark mode from light mode', function () {
53+
visit(false);
54+
cy.get(selectors.toggleDropDownMenuButton).click();
55+
cy.get(selectors.darkModeButton).click();
56+
57+
cy.get('body', { timeout: 1000 }).should('have.class', 'dark-mode');
58+
});
59+
60+
it('The dark mode button should be able to change the theme to light mode from dark mode', function () {
61+
visit(true);
62+
cy.get(selectors.toggleDropDownMenuButton).click();
63+
cy.get(selectors.darkModeButton).click();
64+
65+
cy.get('body', { timeout: 1000 }).should('not.have.class', 'dark-mode');
66+
});
67+
68+
it('The theme should be set to dark and update the value in localStorage to dark', function () {
69+
cy.clearLocalStorage();
70+
cy.clearCookies();
71+
cy.get(selectors.toggleDropDownMenuButton).click();
72+
cy.get(selectors.darkModeButton).click();
73+
cy.window().then(win => {
74+
expect(win.localStorage.getItem('theme')).to.equal('dark');
75+
});
76+
visit(false);
77+
});
78+
4079
// Because all templates readers see use `default.njk` as a base,
4180
// we can test the favicon here
4281
it('should have a favicon', () => {

src/_includes/assets/css/global.css

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,15 @@ html {
267267
}
268268
body {
269269
overflow-x: hidden;
270-
color: var(--gray90);
270+
color: var(--primary-color);
271271
font-family: var(--font-family-sans-serif);
272272
font-size: 1.5rem;
273273
line-height: 1.6em;
274274
font-weight: 400;
275275
font-style: normal;
276276
letter-spacing: 0;
277277
text-rendering: optimizeLegibility;
278-
background: var(--gray00);
278+
background: var(--primary-background);
279279

280280
-webkit-font-smoothing: antialiased;
281281
-moz-osx-font-smoothing: grayscale;
@@ -286,7 +286,7 @@ body {
286286
::selection {
287287
text-shadow: none;
288288
background: var(--dark-blue);
289-
color: var(--gray00);
289+
color: var(--primary-background);
290290
}
291291

292292
hr {
@@ -297,7 +297,7 @@ hr {
297297
padding: 0;
298298
height: 1px;
299299
border: 0;
300-
border-top: 1px solid var(--gray75);
300+
border-top: 1px solid var(--quaternary-color);
301301
}
302302

303303
audio,
@@ -363,7 +363,7 @@ dt {
363363
float: left;
364364
margin: 0 20px 0 0;
365365
width: 120px;
366-
color: var(--gray90);
366+
color: var(--primary-color);
367367
font-weight: 500;
368368
text-align: right;
369369
}
@@ -376,7 +376,7 @@ dd {
376376
blockquote {
377377
margin: 1.5em 0;
378378
padding: 0 1.6em;
379-
border-left: var(--gray10) 0.5em solid;
379+
border-left: var(--tertiary-background) 0.5em solid;
380380
}
381381

382382
blockquote p {

0 commit comments

Comments
 (0)