Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Light/Dark mode switch refactor: toggle on <html>, use CSS vars #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 65 additions & 111 deletions sass/_theme.scss
Original file line number Diff line number Diff line change
@@ -1,149 +1,103 @@
$foreground: #222222;
$background: #eeeeee;
$secondary: gray;
$tertiary: #dddddd;
$accent: #3d3cba;

$foreground-dark: #eeeeee;
$background-dark: #161616;
$secondary-dark: #999999;
$tertiary-dark: #444444;
$accent-dark: #959bf0;

@mixin light-theme {
color: $foreground;
background-color: $background;

.secondary {
color: $secondary;
}

a, a:link, a:visited {
color: $accent;
}

a:hover {
color: darken($accent, 30%);
}

blockquote {
border-left: 2px solid $secondary;
}
// MARK: Variables

code {
background-color: $tertiary;
@media (prefers-color-scheme: light) {
:root.light {
--foreground: #222222;
--background: orange;
--secondary: gray;
--tertiary: #dddddd;
--accent: #3d3cba;
}
}

pre code {
background-color: transparent;
@media (prefers-color-scheme: dark) {
:root.dark {
--foreground: #eeeeee;
--background: orange;
--secondary: #999999;
--tertiary: #444444;
--accent: #959bf0;
}
}

.footnote-definition sup {
color: $secondary;
}

table {
th, td {
border-color: darken($tertiary, 5%);
}
// MARK: Light/dark config

thead, tr:nth-child(even) {
background-color: lighten($tertiary, 10%);
}
}
.dark-mode-buttons {
position: absolute;
top: 1em;
right: 1em;
}

@mixin dark-theme {
color: $foreground-dark;
background-color: $background-dark;
.dark-mode-button {
border: none;
background-color: transparent;

.secondary {
color: $secondary-dark;
&:hover {
cursor: pointer;
}
}

a, a:link, a:visited {
color: $accent-dark;
}

a:hover {
color: lighten($accent-dark, 10%);
}
// MARK: Styles

blockquote {
border-left: 2px solid $secondary-dark;
}
body {
color: var(--foreground);
background-color: var(--background);

code {
background-color: $tertiary-dark;
}

pre code {
background-color: transparent;
}

.footnote-definition sup {
color: $secondary-dark;
&:not(.dark-mode) {
#dark-mode-on {
display: inline;
}
#dark-mode-off {
display: none;
}
}

table {
th, td {
border-color: $tertiary-dark;
&.dark-mode {
#dark-mode-on {
display: none;
}

thead, tr:nth-child(even) {
background-color: darken($tertiary-dark, 15%);
#dark-mode-off {
display: inline;
}
}
}

@media (prefers-color-scheme: light) {
body {
@include light-theme;
}
.secondary {
color: var(--secondary);
}

@media (prefers-color-scheme: dark) {
body {
@include dark-theme;
}
a, a:link, a:visited {
color: var(--accent);
}

.dark-mode-buttons {
position: absolute;

top: 1em;
right: 1em;
a:hover {
color: color-mix(in hsl, var(--accent) 30%, black);
}

.dark-mode-button {
border: none;
background-color: transparent;

&:hover {
cursor: pointer;
}
blockquote {
border-left: 2px solid var(--secondary);
}

body:not(.dark-mode) {
@include light-theme;

#dark-mode-on {
display: inline;
}
code {
background-color: var(--tertiary);
}

#dark-mode-off {
display: none;
}
pre code {
background-color: transparent;
}

body.dark-mode {
@include dark-theme;
.footnote-definition sup {
color: var(--secondary);
}

#dark-mode-on {
display: none;
table {
th, td {
border-color: color-mix(in hsl, var(--tertiary) 5%, black);
}

#dark-mode-off {
display: inline;
thead, tr:nth-child(even) {
background-color: color-mix(in hsl, var(--tertiary) 10%, white);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rough approximation (old vs. new)

background-color: lighten($tertiary, 10%);
background-color: color-mix(in hsl, var(--tertiary) 10%, white);

Relative color syntax isn't supported on FFox yet :( so this feels like an okay workaround.

}
}