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

cleaned up some duplicate codes, and refactored it #129

Open
wants to merge 1 commit into
base: main
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
4 changes: 2 additions & 2 deletions src/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
--width-large: 1300px;
}

[data-theme="dark"] {
[data-theme='dark'] {
--primary-color: #ffcd42;
--secondary-color: #ffd35c;
--bg-primary: #000000;
Expand Down Expand Up @@ -68,7 +68,7 @@ a {
body {
background: var(--bg-primary);
color: var(--text-color);
font-family: "Raleway", sans-serif;
font-family: 'Raleway', sans-serif;
line-height: 1.5;
}

Expand Down
80 changes: 33 additions & 47 deletions src/js/script.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,53 @@
const hamburger = document.querySelector(".hamburger");
const navMenu = document.querySelector(".nav-menu");
const hamburger = document.querySelector('.hamburger');
const navMenu = document.querySelector('.nav-menu');
const navLink = document.querySelectorAll('.nav-link');

hamburger.addEventListener("click", mobileMenu);
const handleMobileMenu = () => {
hamburger.classList.toggle('active');
navMenu.classList.toggle('active');
};

function mobileMenu() {
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
}
hamburger.addEventListener('click', handleMobileMenu);

// Close navbar when link is clicked
const navLink = document.querySelectorAll(".nav-link");

navLink.forEach((n) => n.addEventListener("click", closeMenu));

function closeMenu() {
hamburger.classList.remove("active");
navMenu.classList.remove("active");
}
// close the mobile menu when clicked on a nav link
navLink.forEach((n) => n.addEventListener('click', handleMobileMenu));

// Event Listeners: Handling toggle event
const toggleSwitch = document.querySelector(
'.theme-switch input[type="checkbox"]'
);

function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute("data-theme", "dark");
} else {
document.documentElement.setAttribute("data-theme", "light");
const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');

// toggle background color / theme
const switchTheme = () => {
if (toggleSwitch.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark'); //add this
return;
}
}

toggleSwitch.addEventListener("change", switchTheme, false);
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light'); //add this
};

// Store color theme for future visits
toggleSwitch.addEventListener('change', switchTheme, false);

function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute("data-theme", "dark");
localStorage.setItem("theme", "dark"); //add this
} else {
document.documentElement.setAttribute("data-theme", "light");
localStorage.setItem("theme", "light"); //add this
}
}

// Save user preference on load
// get the currentTheme from local storage or null if there isn't one

const currentTheme = localStorage.getItem("theme")
? localStorage.getItem("theme")
: null;
const currentTheme = localStorage.getItem('theme') || null;

if (currentTheme) {
document.documentElement.setAttribute("data-theme", currentTheme);
const setTheme = function () {
// if there's no current theme in local storage || null, quickly exit this function
if (!currentTheme) return;

if (currentTheme === "dark") {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
}
}
};

setTheme();

//Adding date

let myDate = document.querySelector("#datee");
let myDate = document.querySelector('#datee');

const yes = new Date().getFullYear();
myDate.innerHTML = yes;