Add button to toggle dark mode

This commit is contained in:
Eduardo Gomez 2019-11-12 17:48:45 +01:00
parent b5215a58aa
commit cec8c3ffba
10 changed files with 127 additions and 9 deletions

View file

@ -35,5 +35,6 @@
"Among with no break line": "Among with ", "Among with no break line": "Among with ",
"Type to search": "Type to search", "Type to search": "Type to search",
"No results for your search, try something different.": "No results for your search, try something different.", "No results for your search, try something different.": "No results for your search, try something different.",
"JavaScript license information": "JavaScript license information" "JavaScript license information": "JavaScript license information",
"Toggle dark mode": "Toggle dark mode"
} }

View file

@ -35,5 +35,6 @@
"Among with no break line": "Junto con ", "Among with no break line": "Junto con ",
"Type to search": "Escribe para buscar", "Type to search": "Escribe para buscar",
"No results for your search, try something different.": "No hay resultados para tu búsqueda, prueba algo diferente.", "No results for your search, try something different.": "No hay resultados para tu búsqueda, prueba algo diferente.",
"JavaScript license information": "Información de licencia JavaScript" "JavaScript license information": "Información de licencia JavaScript",
"Toggle dark mode": "Cambiar modo oscuro"
} }

View file

@ -54,5 +54,5 @@
"desktop": "assets/screenshot-desktop.jpg", "desktop": "assets/screenshot-desktop.jpg",
"mobile": "assets/screenshot-mobile.jpg" "mobile": "assets/screenshot-mobile.jpg"
}, },
"version": "0.5.8" "version": "0.5.9"
} }

View file

@ -99,9 +99,15 @@ This header template is shared across all the pages.
</li> </li>
</ul> </ul>
</nav> </nav>
<div class="m-nav__right">
<button class="m-icon-button in-menu-main js-open-search" aria-label="Open search"> <button class="m-icon-button in-menu-main js-open-search" aria-label="Open search">
<span class="icon-search"></span> <span class="icon-search"></span>
</button> </button>
<div class="m-toggle-darkmode js-tooltip" data-tippy-content="{{t "Toggle dark mode"}}">
<input type="checkbox" class="js-toggle-darkmode">
<div></div>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -33,6 +33,8 @@ $(document).ready(() => {
const $inputSearch = $('.js-input-search') const $inputSearch = $('.js-input-search')
const $searchResults = $('.js-search-results') const $searchResults = $('.js-search-results')
const $searchNoResults = $('.js-no-results') const $searchNoResults = $('.js-no-results')
const $toggleDarkMode = $('.js-toggle-darkmode')
const currentTheme = localStorage.getItem('theme')
let fuse = null let fuse = null
let submenuIsOpen = false let submenuIsOpen = false
@ -163,6 +165,16 @@ $(document).ready(() => {
} }
}) })
$toggleDarkMode.change(() => {
if ($toggleDarkMode.is(':checked')) {
$('html').attr('data-theme', 'dark')
localStorage.setItem('theme', 'dark')
} else {
$('html').attr('data-theme', 'light')
localStorage.setItem('theme', 'light')
}
})
$(window).click((e) => { $(window).click((e) => {
if (submenuIsOpen) { if (submenuIsOpen) {
if ($submenuOption && !$submenuOption.contains(e.target)) { if ($submenuOption && !$submenuOption.contains(e.target)) {
@ -172,6 +184,14 @@ $(document).ready(() => {
} }
}) })
if (currentTheme) {
$('html').attr('data-theme', currentTheme)
if (currentTheme === 'dark') {
$toggleDarkMode.attr('checked', true)
}
}
var headerElement = document.querySelector('.js-header') var headerElement = document.querySelector('.js-header')
if (headerElement) { if (headerElement) {
@ -192,7 +212,7 @@ $(document).ready(() => {
infinite: false, infinite: false,
mobileFirst: true, mobileFirst: true,
variableWidth: true, variableWidth: true,
rtl: isRTL rtl: isRTL()
}) })
} }

View file

@ -33,6 +33,7 @@
@import "components/header/recent-articles"; @import "components/header/recent-articles";
@import "components/header/recent-article"; @import "components/header/recent-article";
@import "components/header/tags"; @import "components/header/tags";
@import "components/header/toggle-darkmode";
@import "components/hero/hero"; @import "components/hero/hero";
@import "components/hero/avatar"; @import "components/hero/avatar";
@import "components/hero/title"; @import "components/hero/title";

View file

@ -1,4 +1,5 @@
:root { :root,
[data-theme="light"] {
--background-color: #fff; --background-color: #fff;
--primary-foreground-color: #4a4a4a; --primary-foreground-color: #4a4a4a;
--secondary-foreground-color: #000; --secondary-foreground-color: #000;
@ -15,10 +16,10 @@
--submenu-shadow-color-opacity: 0.25; --submenu-shadow-color-opacity: 0.25;
--button-shadow-color-normal: rgba(108, 108, 108, 0.2); --button-shadow-color-normal: rgba(108, 108, 108, 0.2);
--button-shadow-color-hover: rgba(108, 108, 108, 0.3); --button-shadow-color-hover: rgba(108, 108, 108, 0.3);
--toggle-darkmode-button-color: #fff;
} }
@media (prefers-color-scheme: dark) { [data-theme="dark"] {
:root {
--background-color: #111; --background-color: #111;
--primary-foreground-color: #ccc; --primary-foreground-color: #ccc;
--secondary-foreground-color: #fff; --secondary-foreground-color: #fff;
@ -35,5 +36,27 @@
--submenu-shadow-color-opacity: 0.55; --submenu-shadow-color-opacity: 0.55;
--button-shadow-color-normal: rgba(10, 10, 10, 0.5); --button-shadow-color-normal: rgba(10, 10, 10, 0.5);
--button-shadow-color-hover: rgba(10, 10, 10, 0.5); --button-shadow-color-hover: rgba(10, 10, 10, 0.5);
--toggle-darkmode-button-color: #efd114;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--background-color: #111;
--primary-foreground-color: #ccc;
--secondary-foreground-color: #fff;
--primary-subtle-color: #2c2fe6;
--secondary-subtle-color: #141920;
--titles-color: #b4b4b4;
--link-color: #2c2fe6;
--primary-border-color: #1d1d1d;
--secondary-border-color: #0f0f0f;
--article-shadow-normal: 0 4px 5px 5px rgba(0,0,0,0.1);
--article-shadow-hover: 0 4px 5px 10px rgba(0,0,0,0.1);
--transparent-background-color: rgba(0,0,0,0.99);
--footer-background-color: #080808;
--submenu-shadow-color-opacity: 0.55;
--button-shadow-color-normal: rgba(10, 10, 10, 0.5);
--button-shadow-color-hover: rgba(10, 10, 10, 0.5);
--toggle-darkmode-button-color: #efd114;
} }
} }

View file

@ -35,9 +35,11 @@
.m-menu__main { .m-menu__main {
padding: 50px 0 5px; padding: 50px 0 5px;
min-height: 100%;
@include respond-to('medium') { @include respond-to('medium') {
padding: 0; padding: 0;
min-height: 0;
} }
} }

View file

@ -53,6 +53,12 @@
} }
} }
.m-nav__right {
display: flex;
align-items: center;
justify-content: flex-end;
}
.rtl { .rtl {
.m-nav__left ul li { .m-nav__left ul li {
@include respond-to('medium') { @include respond-to('medium') {

View file

@ -0,0 +1,58 @@
.m-toggle-darkmode {
position: fixed;
right: $mobile-space;
bottom: $mobile-space;
width: 22px;
height: 22px;
border-radius: 50%;
overflow: hidden;
@include respond-to('medium') {
position: relative;
right: auto;
bottom: auto;
margin-left: 20px;
}
input {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
outline: 0;
border: 0;
opacity: 0;
cursor: pointer;
z-index: 2;
@extend .no-appearance;
}
input::-moz-focus-inner {
border: 0;
}
div {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 50%;
overflow: hidden;
background-color: $titles;
z-index: 1;
&:after {
content: '';
width: 10px;
height: 10px;
border-radius: 50%;
background-color: var(--toggle-darkmode-button-color);
@include transition(background-color linear 0.2s);
}
}
}