-
Notifications
You must be signed in to change notification settings - Fork 27
🎨 feat: implement theme toggle functionality and update styles #166
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| (function () { | ||
| var storageKey = 'pythonie-theme'; | ||
| var className = 'dark'; | ||
| var root = document.documentElement; | ||
| var toggleButton; | ||
|
|
||
| function getStoredTheme() { | ||
| try { | ||
| return window.localStorage ? localStorage.getItem(storageKey) : null; | ||
| } catch (e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function setStoredTheme(theme) { | ||
| try { | ||
| if (window.localStorage) { | ||
| localStorage.setItem(storageKey, theme); | ||
| } | ||
| } catch (e) { | ||
| /* ignore storage errors */ | ||
| } | ||
| } | ||
|
|
||
| function prefersDark() { | ||
| return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; | ||
| } | ||
|
|
||
| function getActiveTheme() { | ||
| return getStoredTheme() || (prefersDark() ? 'dark' : 'light'); | ||
| } | ||
|
|
||
| function applyTheme(theme) { | ||
| if (theme === 'dark') { | ||
| root.classList.add(className); | ||
| } else { | ||
| root.classList.remove(className); | ||
| theme = 'light'; | ||
| } | ||
| root.dataset.theme = theme; | ||
| updateToggle(theme); | ||
| } | ||
|
|
||
| function updateToggle(theme) { | ||
| if (!toggleButton) return; | ||
| var isDark = theme === 'dark'; | ||
| toggleButton.setAttribute('aria-pressed', isDark); | ||
| toggleButton.classList.toggle('is-dark', isDark); | ||
| var label = toggleButton.querySelector('[data-theme-label]'); | ||
| if (label) label.textContent = isDark ? 'Light' : 'Dark'; | ||
| } | ||
|
|
||
| function handleToggle() { | ||
| var isDark = root.classList.contains(className); | ||
| var nextTheme = isDark ? 'light' : 'dark'; | ||
| applyTheme(nextTheme); | ||
| setStoredTheme(nextTheme); | ||
| } | ||
|
|
||
| function initThemeToggle() { | ||
| toggleButton = document.querySelector('[data-theme-toggle]'); | ||
| if (!toggleButton) { | ||
| return; | ||
| } | ||
| toggleButton.addEventListener('click', handleToggle); | ||
| applyTheme(getActiveTheme()); | ||
| } | ||
|
|
||
| document.addEventListener('DOMContentLoaded', initThemeToggle); | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,24 @@ | ||
| {% load static %} | ||
|
|
||
| <section class="container"> | ||
| <header class="row" id="header"> | ||
| <header class="row header-bar" id="header"> | ||
| <!-- Begin grid --> | ||
| <a href="/"> | ||
| <h1><img src="{% static 'img/pythonie.png' %}" | ||
| class="logo img-rounded"/> | ||
| <span id="pyhead">Python Ireland</span></h1></a> | ||
| <a class="header-brand" href="/"> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for formatting! |
||
| <h1> | ||
| <img src="{% static 'img/pythonie.png' %}" class="logo img-rounded"/> | ||
| <span id="pyhead">Python Ireland</span> | ||
| </h1> | ||
| </a> | ||
| <div class="header-actions"> | ||
| <button type="button" | ||
| class="theme-toggle" | ||
| data-theme-toggle | ||
| aria-pressed="false" | ||
| aria-label="Toggle dark mode"> | ||
| <span class="theme-toggle-switch"></span> | ||
| <span class="theme-toggle-label" data-theme-label>Dark</span> | ||
| </button> | ||
| </div> | ||
| <!-- End grid --> | ||
| </header> | ||
| <!-- header --> | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using variables is a nice refactor even outside of outside of the new theme!