-
Notifications
You must be signed in to change notification settings - Fork 357
add dark mode toggle(fixes #3628) #3634
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
Changes from all commits
8ed3927
3f586a7
6739739
666685c
b67f576
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,31 @@ | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta name="fragment" content="!"> | ||
<link rel="shortcut icon" type="image/x-icon" href="../favicon.ico" /> | ||
<link rel="stylesheet" href="mdwiki/extlib/css/darkmode.css"> | ||
<meta charset="UTF-8"> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | ||
<script> | ||
|
||
const docElem = document.documentElement; | ||
const localStorageKey = 'mdwiki-dark-mode'; | ||
|
||
const savedDarkModePreference = localStorage.getItem(localStorageKey); | ||
let shouldBeDark = false; | ||
|
||
if (savedDarkModePreference === 'true') { | ||
shouldBeDark = true; | ||
} else if (savedDarkModePreference === null) { | ||
|
||
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { | ||
shouldBeDark = true; | ||
} | ||
} | ||
|
||
|
||
if (shouldBeDark) { | ||
docElem.classList.add('dark-mode'); | ||
} | ||
</script> | ||
<style type="text/css"> | ||
/* hide the main content while we assemble everything */ | ||
.md-hidden-load { display: none; } | ||
|
@@ -260,6 +284,78 @@ | |
|
||
</head> | ||
<body> | ||
<button id="darkModeToggle" | ||
style="position: fixed; top: 6px; right: 60px; z-index: 9999; border-radius: 100px; padding: 10px 10px; cursor: pointer; font-size: 18px; transition: all 0.3s ease;border:none"> | ||
<span id="toggleIcon">🌙</span> | ||
</button> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | ||
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. This is a duplicate script addition, let's remove this one and keep the one in |
||
|
||
<script> | ||
$(document).ready(function () { | ||
var $html = $('html'); | ||
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. Looks like you use this reference for checking if dark mode is set in code, and the body class is used in CSS. I think it'd be a little cleaner to just apply the |
||
var $body = $('body'); | ||
var $navbar = $('#md-main-navbar'); | ||
var $darkModeToggleBtn = $('#darkModeToggle'); | ||
var $darkModeToggleNavbarLink = $('#darkModeToggleNavbar'); | ||
var $toggleIcon = $('#toggleIcon'); | ||
var localStorageKey = 'mdwiki-dark-mode'; | ||
|
||
|
||
function setDarkMode(isDark) { | ||
|
||
if (isDark) { | ||
$html.addClass('dark-mode'); | ||
$body.addClass('dark-mode'); | ||
$navbar.removeClass('navbar-default').addClass('navbar-inverse'); | ||
$darkModeToggleBtn.css({ | ||
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. Let's try to move this to the CSS. |
||
'background-color': '#002f4a', | ||
}); | ||
$toggleIcon.text('☀️'); | ||
} else { | ||
$html.removeClass('dark-mode'); | ||
$body.removeClass('dark-mode'); | ||
$navbar.removeClass('navbar-inverse').addClass('navbar-default'); | ||
$darkModeToggleBtn.css({ | ||
'background-color': '#2c3f51', | ||
}); | ||
$toggleIcon.text('🌙'); | ||
} | ||
localStorage.setItem(localStorageKey, isDark); | ||
} | ||
|
||
|
||
|
||
var initialIsDark = $html.hasClass('dark-mode'); | ||
setDarkMode(initialIsDark); | ||
|
||
$darkModeToggleBtn.on('click', function () { | ||
|
||
setDarkMode(!$html.hasClass('dark-mode')); | ||
}); | ||
|
||
|
||
$darkModeToggleNavbarLink.on('click', function (e) { | ||
e.preventDefault(); | ||
setDarkMode(!$html.hasClass('dark-mode')); | ||
}); | ||
$darkModeToggleBtn.hover( | ||
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. This should be doable with CSS, also. |
||
function () { | ||
if ($html.hasClass('dark-mode')) { | ||
$(this).css('background-color', '#004a75'); | ||
} else { | ||
$(this).css('background-color', '#1A252E'); | ||
} | ||
}, | ||
function () { | ||
if ($html.hasClass('dark-mode')) { | ||
$(this).css('background-color', '#002f4a'); | ||
} else { | ||
$(this).css('background-color', '#2c3f51'); | ||
} | ||
} | ||
); | ||
}); | ||
</script> | ||
<noscript> | ||
This website requires Javascript to be enabled. Please turn on Javascript | ||
and reload the page. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
body.dark-mode { | ||
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. Just a semantic comment, but since this isn't an external library let's put this in a new |
||
background-color: #1a1a1a; | ||
color: #bbbbbb; | ||
} | ||
|
||
body.dark-mode .navbar { | ||
background-color: #002f4a; | ||
border-color: #444; | ||
} | ||
|
||
body.dark-mode .navbar a { | ||
color: #bbbbbb; | ||
} | ||
|
||
body.dark-mode .panel, | ||
body.dark-mode .well { | ||
background-color: #282828; | ||
border-color: #444; | ||
color: #e0e0e0; | ||
} | ||
|
||
body.dark-mode h1, | ||
body.dark-mode h2, | ||
body.dark-mode h3, | ||
body.dark-mode h4, | ||
body.dark-mode h5, | ||
body.dark-mode h6 { | ||
color: #bbbbbb; | ||
} | ||
|
||
body.dark-mode .mdle { | ||
background-color: #000000; | ||
} | ||
|
||
body.dark-mode .dropdown-menu { | ||
background-color: #000000; | ||
} | ||
|
||
body.dark-mode .list-group-item { | ||
background-color: #1a1a1a; | ||
} | ||
|
||
body.dark-mode .dropdown-open { | ||
background-color: #006fb1; | ||
} | ||
|
||
body.dark-mode .panel.panel-default { | ||
border: solid; | ||
border-width: 1.8px; | ||
border-color: gray; | ||
} | ||
|
||
body.dark-mode .list-group-item { | ||
border: solid; | ||
border-width: 1.8px; | ||
border-color: gray; | ||
} |
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.
Extra space here.