Skip to content
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

Feature/dark mode #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions assets/css/bootstrap-cyborg.min.css

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,9 @@
.mime-part {
padding: 10px;
}

/*************/
/* Dark mode */
#github.theme-dark {
filter: grayscale(1) invert(1);
}
47 changes: 47 additions & 0 deletions assets/js/theme-toggler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const LOCAL_STORAGE_KEY = 'mailhog-theme';
const LOCAL_STORAGE_DATA = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY));

const DARK_THEME_PATH = 'css/bootstrap-cyborg.min.css';
const LIGHT_THEME_PATH = 'css/bootstrap-3.3.2.min.css';

const GITHUB_LOGO = document.getElementById('github');
const DARK_STYLE_LINK = document.getElementById('theme-dark');
const LIGHT_STYLE_LINK = document.getElementById('theme-light');
const THEME_TOGGLER = document.getElementById('theme-toggler');

let isDark = LOCAL_STORAGE_DATA && LOCAL_STORAGE_DATA.isDark;
if (isDark) {
setTheme(isDark)
}

function toggleTheme() {
isDark = !isDark;

setTheme(isDark);
storeTheme(isDark);
}

function storeTheme(isDark) {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify({isDark}));
}

function setTheme(isDark) {
title = 'Switch beetwen dark and light mode';
if (isDark) {
GITHUB_LOGO.setAttribute('class', 'theme-dark');

DARK_STYLE_LINK.setAttribute('href', DARK_THEME_PATH);
LIGHT_STYLE_LINK.setAttribute('href', '');

THEME_TOGGLER.innerHTML = '🌙 Dark';
THEME_TOGGLER.setAttribute('title', title + ' (currently dark mode)');
} else {
GITHUB_LOGO.setAttribute('class', '');

DARK_STYLE_LINK.setAttribute('href', '');
LIGHT_STYLE_LINK.setAttribute('href', LIGHT_THEME_PATH);

THEME_TOGGLER.innerHTML = '☀️ Light';
THEME_TOGGLER.setAttribute('title', title + ' (currently light mode)');
}
}
9 changes: 7 additions & 2 deletions assets/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<link rel="icon" type="image/png" href="images/hog.png">

<link rel="stylesheet" href="css/jquery-ui-1.10.4-smoothness.css">
<link rel="stylesheet" href="css/bootstrap-3.3.2.min.css">
<link rel="stylesheet" href="css/bootstrap-3.3.2.min.css" id="theme-light">
<link rel="stylesheet" href="" id="theme-dark">
<link rel="stylesheet" href="css/style.css">
</head>
<body ng-controller="MailCtrl" ng-click="keepopen=false">
Expand Down Expand Up @@ -46,9 +47,12 @@
</form>

<ul class="nav navbar-nav navbar-right">
<li>
<a href="javascript:void()" id="theme-toggler" onclick="toggleTheme()" title="Switch beetwen dark and light mode (currently light mode)">☀️ Light</a>
</li>
<li>
<a target="_blank" href="https://github.com/mailhog/MailHog">
<img src="images/github.png" height="16" alt="GitHub"> GitHub
<img id="github" src="images/github.png" height="16" alt="GitHub"> GitHub
</a>
</li>
</ul>
Expand Down Expand Up @@ -144,6 +148,7 @@ <h3>Jim</h3>
<script src="js/filesize-3.1.2.min.js"></script>
<script src="js/strutil.js"></script>
<script src="js/controllers.js"></script>
<script src="js/theme-toggler.js"></script>
<script>
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
Expand Down