Skip to content

Commit

Permalink
Improve the dark mode switch
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiesigner committed Nov 12, 2019
1 parent cec8c3f commit 0a3e0a0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion partials/hero.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ for it, and apply those styles to the <header> tag.
{{/contentFor}}

<section class="m-hero with-picture" data-aos="fade">
<div class="m-hero__picture in-post {{#is "post"}}in-post{{/is}}"></div>
<div class="m-hero__picture {{#is "post"}}in-post{{/is}}"></div>
{{else}}
<section class="m-hero no-picture {{#is "post"}}in-post{{/is}}" data-aos="fade">
{{/if}}
15 changes: 10 additions & 5 deletions src/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import AOS from 'aos'
import Fuse from 'fuse.js'
import {
isRTL,
formatDate
formatDate,
isDarkMode
} from './helpers'

cssVars({})
Expand All @@ -34,7 +35,7 @@ $(document).ready(() => {
const $searchResults = $('.js-search-results')
const $searchNoResults = $('.js-no-results')
const $toggleDarkMode = $('.js-toggle-darkmode')
const currentTheme = localStorage.getItem('theme')
const currentSavedTheme = localStorage.getItem('theme')

let fuse = null
let submenuIsOpen = false
Expand Down Expand Up @@ -184,14 +185,18 @@ $(document).ready(() => {
}
})

if (currentTheme) {
$('html').attr('data-theme', currentTheme)
if (currentSavedTheme) {
$('html').attr('data-theme', currentSavedTheme)

if (currentTheme === 'dark') {
if (currentSavedTheme === 'dark') {
$toggleDarkMode.attr('checked', true)
}
}

if (isDarkMode()) {
$toggleDarkMode.attr('checked', true)
}

var headerElement = document.querySelector('.js-header')

if (headerElement) {
Expand Down
6 changes: 6 additions & 0 deletions src/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export const isMobile = (width = '768px') => {
return window.matchMedia(`(max-width: ${width})`).matches
}

export const isDarkMode = () => {
const darkModeMatcher = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)')

return darkModeMatcher && darkModeMatcher.matches
}

export const formatDate = (date) => {
if (date) {
return new Date(date).toLocaleDateString(
Expand Down
2 changes: 1 addition & 1 deletion src/sass/components/header/_menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
top: 0;
left: 0;
width: 100%;
height: 100vh;
height: 100%;
opacity: 0;
overflow-y: auto;
z-index: 2;
Expand Down
8 changes: 7 additions & 1 deletion src/sass/components/header/_toggle-darkmode.scss
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
.m-toggle-darkmode {
position: fixed;
right: $mobile-space;
bottom: $mobile-space;
bottom: 45px;
width: 22px;
height: 22px;
border-radius: 50%;
overflow: hidden;
z-index: 2;

@include respond-to('medium') {
position: relative;
right: auto;
bottom: auto;
margin-left: 20px;
z-index: auto;
}

input {
Expand Down Expand Up @@ -56,3 +58,7 @@
}
}
}

_:-ms-fullscreen, :root .m-toggle-darkmode {
display: none;
}

0 comments on commit 0a3e0a0

Please sign in to comment.