refactor: replace Swup page transitions with native transitions

This commit is contained in:
Stefan Imhoff
2025-01-03 15:54:26 +01:00
parent e88fff7207
commit e3ffc262a0
6 changed files with 59 additions and 30 deletions

View File

@@ -15,6 +15,7 @@ import customTheme from './shiki-theme.json';
// https://astro.build/config
export default defineConfig({
site: 'https://www.stefanimhoff.de',
viewTransitions: true,
markdown: {
shikiConfig: {
theme: customTheme,

View File

@@ -116,9 +116,16 @@
});
}
function initializeScripts() {
setActiveLink();
setUpLink();
setEmailLink();
setSearchLink();
setSearchModalLink();
setUpLink();
}
// Initialize on first load
initializeScripts();
document.addEventListener('astro:after-swap', initializeScripts);
</script>

View File

@@ -1,8 +1,9 @@
---
---
<script is:inline>
(function () {
function initializeTheme() {
const getThemePreference = () => {
if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
return localStorage.getItem('theme');
@@ -11,7 +12,6 @@
};
const isDark = getThemePreference() === 'dark';
document.documentElement.classList[isDark ? 'add' : 'remove']('dark');
if (typeof localStorage !== 'undefined') {
@@ -24,5 +24,11 @@
attributeFilter: ['class'],
});
}
})();
}
// Run on initial load
initializeTheme();
// Re-run on view transitions
document.addEventListener('astro:after-swap', initializeTheme);
</script>

View File

@@ -19,6 +19,10 @@ import { Circle } from './icons';
</theme-toggle>
<script>
function initializeThemeToggle() {
// Remove no-js class after transitions
document.documentElement.classList.remove('no-js');
class ThemeToggle extends HTMLElement {
constructor() {
super();
@@ -42,9 +46,17 @@ import { Circle } from './icons';
}
customElements.define('theme-toggle', ThemeToggle);
}
// Run on initial load
initializeThemeToggle();
// Re-run on view transitions
document.addEventListener('astro:after-swap', initializeThemeToggle);
</script>
<style>
/* Turn off toggle if JavaScript is disabled */
:global(.no-js) theme-toggle {
display: none;
}

View File

@@ -1,6 +1,7 @@
---
// Cspell:words astro noindex pagefind stylesheet favicons secuela imhoff sketchnotes shibui
import '../styles/global.css';
import { ViewTransitions } from 'astro:transitions';
import { SEO } from 'astro-seo';
import { site } from '../data/site';
import { isProduction } from '../utils';
@@ -165,6 +166,7 @@ const webManifest = isProduction && {
/>
)
}
<ViewTransitions />
<Scripts />
</head>
<body

View File

@@ -18,6 +18,12 @@
color: #ffffff;
}
/* Page Transitions */
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 300ms;
}
/** Critic Markup */
del {
@apply decoration-[0.15em];
@@ -171,11 +177,6 @@
.icon {
@apply fill-shibui-950 dark:fill-shibui-200/[0.87];
}
/** Swup */
main[tabindex='-1'] {
outline: none;
}
}
@layer components {