-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9764835
commit c716e67
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!DOCTYPE html> | ||
<html data-theme="light"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<link rel="icon" type="image/svg+xml" href="../favicon.svg" /> | ||
|
||
<title>Dark Mode Modal | Vanilla JS Projects</title> | ||
|
||
<link rel="stylesheet" crossorigin href="../assets/pico.css" /> | ||
<link rel="stylesheet" crossorigin href="../assets/darklight.css" /> | ||
</head> | ||
|
||
<body class="container"> | ||
<header> | ||
<h1>Dark mode Modal</h1> | ||
</header> | ||
<button id="open" style="width: 20%">Settings</button> | ||
<main> | ||
<article> | ||
<main id="darklight"></main> | ||
</article> | ||
</main> | ||
|
||
<dialog id="darkModeDialog"> | ||
<div style="align-self: center"> | ||
<label> | ||
<input | ||
type="checkbox" | ||
id="toggle" | ||
name="toggle" | ||
role="switch" | ||
checked | ||
onchange="toggleTheme()" | ||
/> | ||
Use dark mode | ||
</label> | ||
<button id="close">close</button> | ||
</div> | ||
</dialog> | ||
|
||
<script id="theme.js"> | ||
const dialog = document.getElementById("darkModeDialog"); | ||
const settingsBtn = document.getElementById("open"); | ||
|
||
settingsBtn.addEventListener("click", () => { | ||
dialog.showModal(); | ||
}); | ||
|
||
const closeBtn = document.getElementById("close"); | ||
closeBtn.addEventListener("click", () => { | ||
dialog.close(); | ||
}); | ||
|
||
const toggleTheme = () => { | ||
const backgroundTheme = | ||
document.documentElement.getAttribute("data-theme"); | ||
const toggledTheme = backgroundTheme === "dark" ? "light" : "dark"; | ||
document.documentElement.setAttribute("data-theme", toggledTheme); | ||
}; | ||
</script> | ||
</body> | ||
</html> |