Skip to content

Commit

Permalink
Adding dark / light mode toggle switch
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWongLife committed May 27, 2024
1 parent 22d7b12 commit 2329a93
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
28 changes: 26 additions & 2 deletions about.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/devicon.min.css">
</head>

<body>
<body data-bs-theme="dark">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="darkModeSwitch" checked>
<label class="form-check-label" for="darkModeSwitch">Dark Mode</label>
</div>
<nav class="navbar navbar-dark navbar-expand-lg bg-dark ">
<div class="container-fluid">
<div class="mx-4">
<a class="navbar-brand" href="#">
<img src="Nicholas_Wong_Favicon.png"
height="60">
height="50">
</a>
</div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
Expand Down Expand Up @@ -109,6 +113,26 @@ <h3 align="center">Senior @ University of Illinois Urbana-Champaign (UIUC) major
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const htmlElement = document.documentElement;
const switchElement = document.getElementById('darkModeSwitch');

const currentTheme = localStorage.getItem('bsTheme') || 'dark';
htmlElement.setAttribute('data-bs-theme', currentTheme);
switchElement.checked = currentTheme === 'dark';

switchElement.addEventListener('change', function () {
if (this.checked) {
htmlElement.setAttribute('data-bs-theme', 'dark');
localStorage.setItem('bsTheme', 'dark');
} else {
htmlElement.setAttribute('data-bs-theme', 'light');
localStorage.setItem('bsTheme', 'light');
}
});
});
</script>
</body>

</html>
28 changes: 26 additions & 2 deletions contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/devicon.min.css">
</head>

<body>
<body data-bs-theme="dark">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="darkModeSwitch" checked>
<label class="form-check-label" for="darkModeSwitch">Dark Mode</label>
</div>
<nav class="navbar navbar-dark navbar-expand-lg bg-dark ">
<div class="container-fluid">
<div class="mx-4">
<a class="navbar-brand" href="#">
<img src="Nicholas_Wong_Favicon.png"
height="60">
height="50">
</a>
</div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
Expand Down Expand Up @@ -109,6 +113,26 @@ <h1 class="text-center mt-5">How to contact me</h1>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const htmlElement = document.documentElement;
const switchElement = document.getElementById('darkModeSwitch');

const currentTheme = localStorage.getItem('bsTheme') || 'dark';
htmlElement.setAttribute('data-bs-theme', currentTheme);
switchElement.checked = currentTheme === 'dark';

switchElement.addEventListener('change', function () {
if (this.checked) {
htmlElement.setAttribute('data-bs-theme', 'dark');
localStorage.setItem('bsTheme', 'dark');
} else {
htmlElement.setAttribute('data-bs-theme', 'light');
localStorage.setItem('bsTheme', 'light');
}
});
});
</script>
</body>

</html>
28 changes: 26 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/devicon.min.css">
</head>

<body>
<body data-bs-theme="dark">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="darkModeSwitch" checked>
<label class="form-check-label" for="darkModeSwitch">Dark Mode</label>
</div>
<nav class="navbar navbar-dark navbar-expand-lg bg-dark ">
<div class="container-fluid">
<div class="mx-4">
<a class="navbar-brand" href="#">
<img src="Nicholas_Wong_Favicon.png"
height="60">
height="50">
</a>
</div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
Expand Down Expand Up @@ -160,6 +164,26 @@ <h4>Visual Studio Code</h4>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const htmlElement = document.documentElement;
const switchElement = document.getElementById('darkModeSwitch');

const currentTheme = localStorage.getItem('bsTheme') || 'dark';
htmlElement.setAttribute('data-bs-theme', currentTheme);
switchElement.checked = currentTheme === 'dark';

switchElement.addEventListener('change', function () {
if (this.checked) {
htmlElement.setAttribute('data-bs-theme', 'dark');
localStorage.setItem('bsTheme', 'dark');
} else {
htmlElement.setAttribute('data-bs-theme', 'light');
localStorage.setItem('bsTheme', 'light');
}
});
});
</script>
</body>

</html>
28 changes: 26 additions & 2 deletions resume.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/devicon.min.css">
</head>

<body>
<body data-bs-theme="dark">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="darkModeSwitch" checked>
<label class="form-check-label" for="darkModeSwitch">Dark Mode</label>
</div>
<nav class="navbar navbar-dark navbar-expand-lg bg-dark ">
<div class="container-fluid">
<div class="mx-4">
<a class="navbar-brand" href="#">
<img src="Nicholas_Wong_Favicon.png"
height="60">
height="50">
</a>
</div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
Expand Down Expand Up @@ -60,6 +64,26 @@
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
const htmlElement = document.documentElement;
const switchElement = document.getElementById('darkModeSwitch');

const currentTheme = localStorage.getItem('bsTheme') || 'dark';
htmlElement.setAttribute('data-bs-theme', currentTheme);
switchElement.checked = currentTheme === 'dark';

switchElement.addEventListener('change', function () {
if (this.checked) {
htmlElement.setAttribute('data-bs-theme', 'dark');
localStorage.setItem('bsTheme', 'dark');
} else {
htmlElement.setAttribute('data-bs-theme', 'light');
localStorage.setItem('bsTheme', 'light');
}
});
});
</script>
</body>

</html>

0 comments on commit 2329a93

Please sign in to comment.