fix: move some scripts to central file because of issue with Swup

This commit is contained in:
Stefan Imhoff
2023-06-15 09:01:25 +02:00
parent f2c0769993
commit e42b33fc5b
5 changed files with 38 additions and 53 deletions

View File

@@ -1,20 +0,0 @@
---
---
<script>
function setActiveLink() {
const links = document.querySelectorAll('.navigation a');
const currentPath = window.location.pathname;
console.log(currentPath);
links.forEach((link) => {
if (link.getAttribute('href') === currentPath) {
link.classList.add('is-active');
} else {
link.classList.remove('is-active');
}
});
}
setActiveLink();
</script>

View File

@@ -25,23 +25,3 @@ const { text = 'Email', icon = true, ...props } = Astro.props;
) )
} }
</TextLink> </TextLink>
<script>
function handleClick(event: Event) {
event.preventDefault();
const currentTarget = event.currentTarget;
if (currentTarget instanceof HTMLElement) {
const { name, domain, tld } = currentTarget.dataset;
if (name && domain && tld) {
const mailto = `mailto:${name}@${domain}.${tld}`;
window.open(mailto, '_blank');
}
}
}
const links = document.querySelectorAll('[data-email-link]');
links.forEach((link) => {
link.addEventListener('click', handleClick);
});
</script>

View File

@@ -48,6 +48,8 @@ import '../styles/sal.css';
}); });
setActiveLink(); setActiveLink();
setUpLink();
setEmailLink();
} }
}); });
@@ -64,7 +66,41 @@ import '../styles/sal.css';
}); });
} }
function setUpLink() {
const button = document.getElementById('up-link');
button?.addEventListener('click', (event) => {
event.preventDefault();
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth',
});
});
}
function handleEmailClick(event: Event) {
event.preventDefault();
const currentTarget = event.currentTarget;
if (currentTarget instanceof HTMLElement) {
const { name, domain, tld } = currentTarget.dataset;
if (name && domain && tld) {
const mailto = `mailto:${name}@${domain}.${tld}`;
window.open(mailto, '_blank');
}
}
}
function setEmailLink() {
const links = document.querySelectorAll('[data-email-link]');
links.forEach((link) => {
link.addEventListener('click', handleEmailClick);
});
}
setActiveLink(); setActiveLink();
setUpLink();
setEmailLink();
</script> </script>
<style is:global> <style is:global>

View File

@@ -17,16 +17,4 @@ import { Link } from '.';
<Sprite name="ri:arrow-up-line" class="h-icon-small w-icon-small" /> <Sprite name="ri:arrow-up-line" class="h-icon-small w-icon-small" />
</button> </button>
</Link> </Link>
<script>
const button = document.getElementById('up-link');
button?.addEventListener('click', (event) => {
event.preventDefault();
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth',
});
});
</script>
</div> </div>

3
types/swup.d.ts vendored
View File

@@ -1,5 +1,6 @@
declare module '@swup/a11y-plugin';
declare module '@swup/fade-theme'; declare module '@swup/fade-theme';
declare module '@swup/head-plugin'; declare module '@swup/head-plugin';
declare module '@swup/a11y-plugin';
declare module '@swup/preload-plugin'; declare module '@swup/preload-plugin';
declare module '@swup/scripts-plugin';
declare module '@swup/scroll-plugin'; declare module '@swup/scroll-plugin';