Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 2b2bace

Browse files
committed
Apply color theme selector (GH-211)
1 parent 7bef040 commit 2b2bace

File tree

7 files changed

+278
-18
lines changed

7 files changed

+278
-18
lines changed

assets/icons/moon-svgrepo-com.svg

Lines changed: 45 additions & 0 deletions
Loading

assets/icons/sun-svgrepo-com.svg

Lines changed: 101 additions & 0 deletions
Loading

src/_static/js/theme.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// theme variables
2+
const themeAttribute = "data-theme"
3+
const theme = {
4+
light: "light",
5+
dark: "dark",
6+
}
7+
const userTheme = localStorage.getItem(themeAttribute)
8+
const isSystemThemeDark = window.matchMedia("(prefers-color-scheme: dark)").matches
9+
10+
11+
// initial theme check
12+
const checkTheme = () => {
13+
if (userTheme === theme.dark || (!userTheme && isSystemThemeDark)) {
14+
document.documentElement.setAttribute(themeAttribute, theme.dark)
15+
} else document.documentElement.setAttribute(themeAttribute, theme.light)
16+
}
17+
18+
19+
// manual theme select
20+
const swapTheme = () => {
21+
const currentTheme = document.documentElement.getAttribute(themeAttribute)
22+
const themeValue = currentTheme === theme.light ? theme.dark : theme.light
23+
localStorage.setItem(themeAttribute, themeValue)
24+
document.documentElement.setAttribute(themeAttribute, themeValue)
25+
}
26+
27+
28+
const swapThemeBtn = document.getElementById("swap-theme-btn");
29+
if (swapThemeBtn) swapThemeBtn.addEventListener("click", swapTheme)
30+
31+
document.addEventListener("DOMContentLoaded", () => {
32+
checkTheme()
33+
})

src/_static/scss/presentation-common.scss

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,101 @@
1010
==============
1111
*/
1212

13-
// todo: add colors palette
13+
@import "./colors";
14+
15+
:root[data-theme=light] {
16+
--background: #{$gray-50};
17+
--on-background: #{$on-gray-50};
18+
--surface: white;
19+
--on-surface: black;
20+
--primary: #{$yellow-600};
21+
--primary-accent: #{$yellow-a100};
22+
--on-primary: #{$on-yellow-600};
23+
--secondary: #{$blue-400};
24+
--secondary-accent: #{$blue-a100};
25+
--on-secondary: #{$on-blue-400};
26+
27+
#sun-icon {
28+
display: none;
29+
}
30+
}
31+
32+
:root[data-theme=dark] {
33+
--background: #{$gray-900};
34+
--on-background: #{$on-gray-900};
35+
--surface: #{$gray-700};
36+
--on-surface: #{$on-gray-700};
37+
--primary: #{$yellow-200};
38+
--primary-accent: #{$yellow-a200};
39+
--on-primary: #{$on-yellow-200};
40+
--secondary: #{$blue-200};
41+
--secondary-accent: #{$blue-a200};
42+
--on-secondary: #{$on-blue-200};
43+
44+
#moon-icon {
45+
display: none;
46+
}
47+
}
48+
49+
:root {
50+
background-color: var(--background);
51+
color: var(--on-background);
52+
53+
// color scheme selector
54+
#swap-theme-btn {
55+
pointer-events: all;
56+
cursor: pointer;
57+
58+
background: none;
59+
border: none;
60+
61+
position: absolute;
62+
top: 0;
63+
right: 0;
64+
margin: 1rem;
65+
66+
z-index: 1024;
67+
}
68+
69+
.theme-icon {
70+
width: 1rem;
71+
height: auto;
72+
73+
background:none;
74+
}
75+
}
1476

1577
/**
1678
Sizing and alignments
1779
=====================
1880
*/
1981

2082
// items sizing
21-
22-
html {
23-
font-size: 2.5rem;
83+
:root {
84+
font-size: 2.5rem; // base font size
2485

2586
body {
26-
font-size: 1em;
87+
font-size: 1em; // 100% from base font size
2788
}
2889

2990
small {
30-
font-size: 0.75em;
91+
font-size: 0.75em; // 75% from base font size
3192
}
3293

3394
h1 {
34-
font-size: 2em;
95+
font-size: 2em; // 200% from base font size
3596
}
3697

3798
h2 {
38-
font-size: 1.5em;
99+
font-size: 1.5em; // 150% from base font size
39100
}
40101

41102
h3 {
42-
font-size: 1.25em;
103+
font-size: 1.25em; // 125% from base font size
43104
}
44105
}
45106

46107
// align items and content
47-
48108
.text-start {
49109
text-align: start;
50110
}
@@ -71,19 +131,16 @@ html {
71131

72132
.fallback-message {
73133
font-family: sans-serif;
134+
font-size: 0.5em;
74135
line-height: 1.3;
75136

76137
width: 740px;
77-
padding: 20px;
78-
margin: 20px auto;
138+
padding: 10px 60px;
139+
margin: auto;
79140

80-
border: 1px solid #E4C652; // todo: set up color using palette
81141
border-radius: 10px;
82-
background: #EEDC94; // todo: set up colo using palette
83-
84-
p {
85-
margin-bottom: 10px;
86-
}
142+
background: var(--primary);
143+
color: var(--on-primary);
87144
}
88145

89146
.impress-supported {

src/conf.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
* Webpack entry point
33
*/
44

5+
// add JS modules
6+
import "./_static/js/theme"
7+
58
// add stylesheets to webpack bundle
69
import "./_static/scss/presentation-common.scss"
710
import "./_static/scss/presentation-normalization.scss"
811

912
// add assets/resources to webpack bundle
1013
import "../assets/icons/check-mark-svgrepo-com.svg"
1114
import "../assets/icons/cross-mark-svgrepo-com.svg"
15+
import "../assets/icons/moon-svgrepo-com.svg"
16+
import "../assets/icons/sun-svgrepo-com.svg"
1217

1318
// import and initialize impress.js presentation framework
1419
import "../assets/impress.js/js/impress"

src/rdbms/presentations/normalization/_index.hbs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919

2020
<body class="impress-not-supported">
2121

22+
{{!
23+
Presentation theme selector
24+
}}
25+
<div id="theme-selector" aria-label="select color scheme">
26+
<button id="swap-theme-btn">
27+
<img src="{{sunIcon}}" alt="sun icon" class="theme-icon" id="sun-icon">
28+
<img src="{{moonIcon}}" alt="moon icon" class="theme-icon" id="moon-icon">
29+
</button>
30+
</div>
31+
2232
{{!
2333
impress.js fallback message
2434

webpack.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ const HTMLWebpackPlugin = require("html-webpack-plugin")
55
const BASE_DIR = path.resolve(__dirname, "src")
66
const BUILD_DIR = path.resolve(__dirname, "_build", "webpack")
77
const ASSETS_DIR = path.resolve(__dirname, "assets")
8+
89
const commonHTMLWebpackPluginConfig = {
910
favicon: path.resolve(ASSETS_DIR, "favicon.ico"),
1011
inject: "body", // inject scripts at the bottom of the body
1112
}
1213

14+
const icons = {
15+
moonIcon: "../assets/moon-svgrepo-com.svg",
16+
sunIcon: "../assets/sun-svgrepo-com.svg",
17+
}
18+
1319
const config = {
1420
mode: "development",
1521
entry: path.resolve(BASE_DIR, "conf.js"),
@@ -29,6 +35,9 @@ const config = {
2935
...commonHTMLWebpackPluginConfig,
3036
template: path.resolve(BASE_DIR, "rdbms", "presentations", "normalization", "_index.hbs"),
3137
filename: path.resolve(BUILD_DIR, "normalization", "index.html"),
38+
templateParameters: {
39+
...icons,
40+
},
3241
}),
3342
],
3443
module: {

0 commit comments

Comments
 (0)