Initial commit for dark theme.
This commit is contained in:
parent
ac28e9824e
commit
c8f28d4287
4 changed files with 69 additions and 0 deletions
|
@ -115,6 +115,9 @@
|
||||||
{{ if .IsTranslated }}
|
{{ if .IsTranslated }}
|
||||||
{{ partial "navigators/lang-selector.html" . }}
|
{{ partial "navigators/lang-selector.html" . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
{{ if site.Params.darkTheme.enable }}
|
||||||
|
{{ partial "navigators/theme-selector.html" . }}
|
||||||
|
{{ end }}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
10
layouts/partials/navigators/theme-selector.html
Normal file
10
layouts/partials/navigators/theme-selector.html
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<li class="nav-item dropdown">
|
||||||
|
<a class="nav-link dropdown-toggle" href="#" id="themeSelector"
|
||||||
|
default-theme=" {{ site.Params.darkTheme.default }} " role="button"
|
||||||
|
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Theme</a>
|
||||||
|
<div class="dropdown-menu" aria-labelledby="themeSelector">
|
||||||
|
<a class="dropdown-item nav-link" href="#" onclick="enableLightTheme()">Light</a>
|
||||||
|
<a class="dropdown-item nav-link" href="#" onclick="enableDarkTheme()">Dark</a>
|
||||||
|
<a class="dropdown-item nav-link" href="#" onclick="useSystemTheme()">System</a>
|
||||||
|
</div>
|
||||||
|
</li>
|
|
@ -5,3 +5,6 @@
|
||||||
<script type="text/javascript" src="{{ "/js/navbar.js" | relURL }}"></script>
|
<script type="text/javascript" src="{{ "/js/navbar.js" | relURL }}"></script>
|
||||||
<script type="text/javascript" src="{{ "/js/plyr.js" | relURL }}"></script>
|
<script type="text/javascript" src="{{ "/js/plyr.js" | relURL }}"></script>
|
||||||
<script type="text/javascript" src="{{ "/js/main.js" | relURL }}"></script>
|
<script type="text/javascript" src="{{ "/js/main.js" | relURL }}"></script>
|
||||||
|
|
||||||
|
<script type="text/javascript" src="{{ "/js/node_modules/darkreader/darkreader.js" | relURL }}"></script>
|
||||||
|
<script type="text/javascript" src="{{ "/js/darkmode.js" | relURL }}"></script>
|
||||||
|
|
53
static/js/darkmode.js
Normal file
53
static/js/darkmode.js
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
function enableDarkTheme() {
|
||||||
|
localStorage.setItem('color-scheme', "dark");
|
||||||
|
DarkReader.enable({
|
||||||
|
brightness: 100,
|
||||||
|
contrast: 90,
|
||||||
|
sepia: 10
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function enableLightTheme() {
|
||||||
|
localStorage.setItem('color-scheme', "light");
|
||||||
|
DarkReader.disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
function useSystemTheme() {
|
||||||
|
localStorage.setItem('color-scheme', "system");
|
||||||
|
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
||||||
|
enableDarkTheme();
|
||||||
|
} else {
|
||||||
|
enableLightTheme();
|
||||||
|
}
|
||||||
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
|
||||||
|
if (e.matches) {
|
||||||
|
enableDarkTheme();
|
||||||
|
} else {
|
||||||
|
enableLightTheme();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize. Use dark as default.
|
||||||
|
function initializeColorScheme() {
|
||||||
|
// We're using the themeSelector attributes as a workaround for setting up
|
||||||
|
// the default coor scheme since we don't want to complicate this further by
|
||||||
|
// creating custom javascript output in Hugo.
|
||||||
|
themeSelector = document.getElementById("themeSelector");
|
||||||
|
defaultColorScheme = themeSelector.getAttribute('default-theme');
|
||||||
|
// If the user has already selected a preferred theme then use that instead
|
||||||
|
// of the default theme. Also, the default theme gets loaded to localStorage
|
||||||
|
// on the first visit.
|
||||||
|
if (!localStorage.getItem('color-scheme'))
|
||||||
|
localStorage.setItem('color-scheme', defaultColorScheme);
|
||||||
|
storedColorScheme = localStorage.getItem('color-scheme');
|
||||||
|
if (storedColorScheme == "light") {
|
||||||
|
enableLightTheme();
|
||||||
|
} else if (storedColorScheme == "system") {
|
||||||
|
useSystemTheme();
|
||||||
|
} else {
|
||||||
|
// Use dark theme as fallback since it is my default.
|
||||||
|
enableDarkTheme();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
initializeColorScheme()
|
Loading…
Add table
Add a link
Reference in a new issue