From a6bc4c00b0bf1357cd8dfa14d1c037a90e3e2737 Mon Sep 17 00:00:00 2001 From: Marcos <45741056+Marcos03BR@users.noreply.github.com> Date: Wed, 28 Aug 2024 04:36:22 +0200 Subject: [PATCH] fix: errors when `themeColor.fixed` is set to `true` (#160) Prevent null errors in Navbar.astro by verifying DOM elements before attaching event listeners --- src/components/Navbar.astro | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/components/Navbar.astro b/src/components/Navbar.astro index fd563cc..0b9cc17 100644 --- a/src/components/Navbar.astro +++ b/src/components/Navbar.astro @@ -84,21 +84,31 @@ function switchTheme() { function loadButtonScript() { let switchBtn = document.getElementById("scheme-switch"); - switchBtn!.addEventListener("click", function () { - switchTheme() - }); + if (switchBtn) { + switchBtn.addEventListener("click", function () { + switchTheme() + }); + } let settingBtn = document.getElementById("display-settings-switch"); - settingBtn!.addEventListener("click", function () { - let settingPanel = document.getElementById("display-setting"); - settingPanel!.classList.toggle("float-panel-closed"); - }); + if (settingBtn) { + settingBtn.addEventListener("click", function () { + let settingPanel = document.getElementById("display-setting"); + if (settingPanel) { + settingPanel.classList.toggle("float-panel-closed"); + } + }); + } let menuBtn = document.getElementById("nav-menu-switch"); - menuBtn!.addEventListener("click", function () { - let menuPanel = document.getElementById("nav-menu-panel"); - menuPanel!.classList.toggle("float-panel-closed"); - }); + if (menuBtn) { + menuBtn.addEventListener("click", function () { + let menuPanel = document.getElementById("nav-menu-panel"); + if (menuPanel) { + menuPanel.classList.toggle("float-panel-closed"); + } + }); + } } loadButtonScript(); @@ -119,4 +129,4 @@ async function loadPagefind() { pagefind.search('') // speed up the first search } loadPagefind() -} \ No newline at end of file +}