Skip to content

Commit 87f61ee

Browse files
committed
Added dark mode toggle
1 parent 6d8c08a commit 87f61ee

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

assets/theme-toggle.svg

Lines changed: 3 additions & 0 deletions
Loading

index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<h1>Python Australia Community</h1>
1414
<p class="intro">Python Australia Community is a community-run organisation dedicated to bringing together Python enthusiasts from across the country. We aim to connect Python-appreciators with local meetups, events, and initiatives to help grow and connect the Python community in Australia.</p>
1515
</div>
16+
<button class="theme-toggle" type="button" title="Toggle theme" aria-label="Toggle theme" onClick="toggleTheme()">
17+
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="1.5rem" height="1.5rem" fill="currentColor" viewBox="0 0 32 32">
18+
<path d="M16 .5C7.4.5.5 7.4.5 16S7.4 31.5 16 31.5 31.5 24.6 31.5 16 24.6.5 16 .5zm0 28.1V3.4C23 3.4 28.6 9 28.6 16S23 28.6 16 28.6z"></path>
19+
</svg>
20+
</button>
1621
</header>
1722

1823
<section class="discord-promo">
@@ -125,5 +130,6 @@ <h3>Gold Coast Python Meetup</h3>
125130
</div>
126131
</div>
127132
</main>
133+
<script src="./js/theme_switcher.js"></script>
128134
</body>
129135
</html>

js/theme_switcher.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Apply a given theme/color-scheme
2+
function setTheme(themeName) {
3+
localStorage.setItem('theme', themeName)
4+
document.documentElement.className = themeName
5+
}
6+
7+
// Toggle between light and dark theme
8+
function toggleTheme() {
9+
if (localStorage.getItem('theme') === 'theme-dark') {
10+
setTheme('theme-light')
11+
} else {
12+
setTheme('theme-dark')
13+
}
14+
}
15+
16+
// Set the theme on initial load
17+
(function () {
18+
if (localStorage.getItem('theme') === 'theme-dark') {
19+
setTheme('theme-dark')
20+
} else {
21+
setTheme('theme-light')
22+
}
23+
})();
24+
25+
// Listen for changes to the dark mode media query and set the theme to match
26+
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)').onchange = (e) => {
27+
if (e.matches) {
28+
setTheme('theme-dark')
29+
} else {
30+
setTheme('theme-light')
31+
}
32+
}

style/style.css

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
:root {
22
color-scheme: light dark;
3+
--bright-blue: color-mix(in oklab, var(--python-blue), white 50%);
34
--python-blue: #3776a9;
45
--dark-blue: color-mix(in oklab, var(--python-blue), black 20%);
56
--darker-blue: color-mix(in oklab, var(--python-blue), black 45%);
@@ -14,6 +15,14 @@
1415
--dark-card: #494c51;
1516
}
1617

18+
.theme-light {
19+
color-scheme: light;
20+
}
21+
22+
.theme-dark {
23+
color-scheme: dark;
24+
}
25+
1726
body {
1827
font-family: 'Segoe UI', Arial, sans-serif;
1928
margin: 0;
@@ -111,7 +120,7 @@ header {
111120
color: #fff;
112121
text-align: center;
113122

114-
img {
123+
> img {
115124
padding: 20px;
116125
margin-left: -76px;
117126
margin-top: -76px;
@@ -149,6 +158,28 @@ header {
149158
}
150159
}
151160

161+
.theme-toggle {
162+
background: none;
163+
border: none;
164+
align-self: flex-start;
165+
padding: 1rem;
166+
color: light-dark(var(--light-text), var(--bright-blue));
167+
cursor: pointer;
168+
transition: color 0.2s ease;
169+
}
170+
171+
.theme-toggle:hover {
172+
color: var(--python-yellow);
173+
}
174+
175+
.theme-toggle svg {
176+
transition: transform 0.3s ease;
177+
}
178+
179+
.theme-dark .theme-toggle>svg {
180+
transform: rotate(180deg);
181+
}
182+
152183
.intro {
153184
font-size: 1.2rem;
154185
max-width: 600px;

0 commit comments

Comments
 (0)