fix: JavaScript errors

This commit is contained in:
Stefan Imhoff
2025-01-04 17:25:09 +01:00
parent 6e36071dcd
commit a7312cb106
3 changed files with 52 additions and 40 deletions

View File

@@ -45,29 +45,39 @@ const { nextText, nextUrl, previousText, previousUrl } = Astro.props;
)
}
<script is:inline>
const body = document.body;
const scrollUp = 'scroll-up';
const scrollDown = 'scroll-down';
let lastScroll = 0;
<script>
function initializeScrollListener() {
const body = document.body;
const scrollUp = 'scroll-up';
const scrollDown = 'scroll-down';
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
const scrollHandler = () => {
const currentScroll = window.pageYOffset;
if (currentScroll <= 0) {
body.classList.remove(scrollUp);
return;
}
if (currentScroll <= 0) {
body.classList.remove(scrollUp);
return;
}
if (currentScroll > lastScroll && !body.classList.contains(scrollDown)) {
body.classList.remove(scrollUp);
body.classList.add(scrollDown);
} else if (currentScroll < lastScroll && body.classList.contains(scrollDown)) {
body.classList.remove(scrollDown);
body.classList.add(scrollUp);
}
lastScroll = currentScroll;
});
if (currentScroll > lastScroll && !body.classList.contains(scrollDown)) {
body.classList.remove(scrollUp);
body.classList.add(scrollDown);
} else if (currentScroll < lastScroll && body.classList.contains(scrollDown)) {
body.classList.remove(scrollDown);
body.classList.add(scrollUp);
}
lastScroll = currentScroll;
};
window.addEventListener('scroll', scrollHandler);
}
// Initialize on load
initializeScrollListener();
// Re-initialize after view transitions
document.addEventListener('astro:after-swap', initializeScrollListener);
</script>
<style is:global>

View File

@@ -23,29 +23,32 @@ import { Circle } from './icons';
// Remove no-js class after transitions
document.documentElement.classList.remove('no-js');
class ThemeToggle extends HTMLElement {
constructor() {
super();
const button = this.querySelector('button')!;
const setTheme = (dark: boolean) => {
document.documentElement.classList[dark ? 'add' : 'remove']('dark');
button.setAttribute('aria-pressed', String(dark));
};
// Only define the custom element if it hasn't been defined yet
if (!customElements.get('theme-toggle')) {
class ThemeToggle extends HTMLElement {
constructor() {
super();
const button = this.querySelector('button')!;
const setTheme = (dark: boolean) => {
document.documentElement.classList[dark ? 'add' : 'remove']('dark');
button.setAttribute('aria-pressed', String(dark));
};
button.addEventListener('click', () => {
setTheme(!this.isDark());
button.blur();
});
button.addEventListener('click', () => {
setTheme(!this.isDark());
button.blur();
});
setTheme(this.isDark());
setTheme(this.isDark());
}
isDark() {
return document.documentElement.classList.contains('dark');
}
}
isDark() {
return document.documentElement.classList.contains('dark');
}
customElements.define('theme-toggle', ThemeToggle);
}
customElements.define('theme-toggle', ThemeToggle);
}
// Run on initial load

View File

@@ -155,7 +155,7 @@ const webManifest = isProduction && {
document.documentElement.classList.remove('no-js');
document.documentElement.classList.add('js');
</script>
<ViewTransitions />
<ThemeProvider />
{
isProduction && (
@@ -166,7 +166,6 @@ const webManifest = isProduction && {
/>
)
}
<ViewTransitions />
<Scripts />
</head>
<body