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,13 +45,14 @@ const { nextText, nextUrl, previousText, previousUrl } = Astro.props;
)
}
<script is:inline>
<script>
function initializeScrollListener() {
const body = document.body;
const scrollUp = 'scroll-up';
const scrollDown = 'scroll-down';
let lastScroll = 0;
window.addEventListener('scroll', () => {
const scrollHandler = () => {
const currentScroll = window.pageYOffset;
if (currentScroll <= 0) {
@@ -67,7 +68,16 @@ const { nextText, nextUrl, previousText, previousUrl } = Astro.props;
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,6 +23,8 @@ import { Circle } from './icons';
// Remove no-js class after transitions
document.documentElement.classList.remove('no-js');
// Only define the custom element if it hasn't been defined yet
if (!customElements.get('theme-toggle')) {
class ThemeToggle extends HTMLElement {
constructor() {
super();
@@ -47,6 +49,7 @@ import { Circle } from './icons';
customElements.define('theme-toggle', ThemeToggle);
}
}
// Run on initial load
initializeThemeToggle();

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