I want to add a dark mode button to my statically generated site with eleventy
. The code for dark mode is take from this site But I don't know where to put the js
file/function. The button should be at the header, shared by all pages. So I put reference to js
file in base layer template base-layout.njk
, but it is not working. The button is there but when I click the page does not change.
This is my base-layout.njk
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Eleventy Blog</title>
<link rel="stylesheet" href="/css/site.css">
</head>
<body>
<header>
<a href="/" class="link--home">My Blog</a>
<a href="/About">About</a>
<button id="dark-button" onclick="darkToggle()">Dark Mode</button>
</header>
<main>
{{ content | safe }}
</main>
<footer>© My Blog</footer>
</body>
<script type="text/javascript" src="../global.js"></script>
</html>
Here is my directory structure
.
├── css
│ └── site.css
├── global.js
├── _includes
│ ├── base-layout.njk
│ └── post-layout.njk
├── index.njk
├── package.json
├── package-lock.json
├── posts
│ └── 2021-0520.md
├── README.md
└── _site
├── css
│ └── site.css
├── index.html
├── posts
│ └── 2021-0520
│ └── index.html
└── README
└── index.html
8 directories, 13 files