Execute needed js functions uppon ready

This commit is contained in:
Sagi Dayan 2019-07-30 17:06:49 -04:00
parent aadf911ff2
commit 573d2de139
2 changed files with 30 additions and 21 deletions

View file

@ -1,16 +1,21 @@
// Mobile menu
const menuTrigger = document.querySelector(".menu-trigger");
const menu = document.querySelector(".menu");
const mobileQuery = getComputedStyle(document.body).getPropertyValue("--phoneWidth");
(function() {
const menuTrigger = document.querySelector('.menu-trigger');
const menu = document.querySelector('.menu');
const mobileQuery =
getComputedStyle(document.body).getPropertyValue('--phoneWidth');
const isMobile = () => window.matchMedia(mobileQuery).matches;
const isMobileMenu = () => {
menuTrigger && menuTrigger.classList.toggle("hidden", !isMobile());
menu && menu.classList.toggle("hidden", isMobile());
menuTrigger && menuTrigger.classList.toggle('hidden', !isMobile());
menu && menu.classList.toggle('hidden', isMobile());
};
isMobileMenu();
menuTrigger && menuTrigger.addEventListener("click", () => menu && menu.classList.toggle("hidden"));
menuTrigger &&
menuTrigger.addEventListener(
'click', () => menu && menu.classList.toggle('hidden'));
window.addEventListener("resize", isMobileMenu);
window.addEventListener('resize', isMobileMenu);
})();

View file

@ -1,23 +1,27 @@
// Toggle theme
const getTheme = window.localStorage && window.localStorage.getItem("theme");
const themeToggle = document.querySelector(".theme-toggle");
const isDark = getTheme === "dark";
var metaThemeColor = document.querySelector("meta[name=theme-color]");
(function() {
const getTheme = window.localStorage && window.localStorage.getItem('theme');
const themeToggle = document.querySelector('.theme-toggle');
const isDark = getTheme === 'dark';
var metaThemeColor = document.querySelector('meta[name=theme-color]');
if (getTheme !== null) {
document.body.classList.toggle("dark-theme", isDark);
isDark ? metaThemeColor.setAttribute("content", "#252627") : metaThemeColor.setAttribute("content", "#fafafa");
document.body.classList.toggle('dark-theme', isDark);
isDark ? metaThemeColor.setAttribute('content', '#252627') :
metaThemeColor.setAttribute('content', '#fafafa');
}
themeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark-theme");
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-theme');
window.localStorage &&
window.localStorage.setItem(
"theme",
document.body.classList.contains("dark-theme") ? "dark" : "light",
);
document.body.classList.contains("dark-theme") ?
metaThemeColor.setAttribute("content", "#252627") : metaThemeColor.setAttribute("content", "#fafafa");
window.localStorage.setItem(
'theme',
document.body.classList.contains('dark-theme') ? 'dark' : 'light',
);
document.body.classList.contains('dark-theme') ?
metaThemeColor.setAttribute('content', '#252627') :
metaThemeColor.setAttribute('content', '#fafafa');
;
});
})();