-
Notifications
You must be signed in to change notification settings - Fork 161
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
Darkmode for single file #90
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a20fcf1
Common css instead of css replacement
olibu 0036b31
Theme moved into own js file as of performance issues
olibu a4e050d
Merge remote-tracking branch 'upstream/esm' into fix/darkmode
olibu b0b0670
Theme support for single file
olibu 56c3ad5
theme-color set for iOS devices
olibu 1b55372
theme.js added to lint
olibu 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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 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 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,41 @@ | ||
const selectTheme = document.getElementById('theme-select'); | ||
// set dark theme depending on OS settings | ||
function setTheme(theme) { | ||
if (!selectTheme) { | ||
console.log('Themes are currently not working with single file version.'); | ||
return; | ||
} | ||
if (theme == 'os') { | ||
let prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)'); | ||
if (prefersDarkScheme.matches) { | ||
theme = 'dark'; | ||
} else { | ||
theme = 'light'; | ||
} | ||
} | ||
document.documentElement.style['color-scheme'] = theme; | ||
document.querySelector('html').setAttribute('data-theme', theme); | ||
// set tthe theme-color for iOS devices | ||
let bgColor = getComputedStyle(document.documentElement).getPropertyValue('--main-bg-color'); | ||
let metaThemeColor = document.querySelector('meta[name=theme-color]'); | ||
metaThemeColor.setAttribute('content', bgColor); | ||
} | ||
// activate selected theme | ||
let theme = 'os'; | ||
const localStorageTheme = localStorage.getItem('theme'); | ||
if (localStorageTheme) { | ||
theme = localStorageTheme; | ||
} | ||
if (theme == 'light') { | ||
selectTheme.selectedIndex = 2; | ||
} else if (theme == 'dark') { | ||
selectTheme.selectedIndex = 1; | ||
} | ||
setTheme(theme); | ||
// add handler to theme selction element | ||
if (selectTheme) { | ||
selectTheme.addEventListener ('change', function () { | ||
localStorage.setItem('theme', selectTheme.value); | ||
setTheme(selectTheme.value); | ||
}); | ||
} |
This file contains 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
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.
@olibu is this necessary?
(and it is correct when OS theme is light? or would cause flickering on the other way?)
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.
Thinking about it, I'd say it's fine as it is.
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.
I verified it in dark and light mode to work without flickering. However, I set dark as the default. When testing I recognized that the dark flash is not as disturbing as the light flash.
Flickering stopped by separating the function into the theme.js