feat(Layout): add loading spinner component and to layout

Reusable spinner component

Apply spinner to layout to show a "loading state" between transitions, the spinner only shows if the transition is taking more than .2 secconds
This commit is contained in:
Alexander Navarro 2024-03-07 17:20:45 -03:00
parent d770f51948
commit a1528a2ad8
3 changed files with 109 additions and 1 deletions

View file

@ -4,9 +4,11 @@ export interface Props {
title: string;
}
const { title } = Astro.props;
import '../assets/style/global.css';
import Navbar from '../components/Navbar.astro';
import Spinner from '../components/Spinner.astro';
const { title } = Astro.props;
---
<!DOCTYPE html>
@ -28,12 +30,51 @@ import Navbar from '../components/Navbar.astro';
<Navbar />
</header>
<main transition:animate="fade">
<div id="layout-loading-spinner" class="d-none">
<Spinner />
</div>
<slot />
</main>
<style>
header > :global(*) {
margin-left: auto;
}
body > main {
position: relative;
}
/* Position spinner in the center of the screen instead of the center of it's parent */
/* This is because the height of the main div can change */
/* but we still want the background to hide the content */
#layout-loading-spinner :global(.spinner) :global(svg) {
position: fixed;
width: 200px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<script is:inline>
document.addEventListener('astro:before-preparation', (ev) => {
const originalLoader = ev.loader;
ev.loader = async function () {
const spinner = document.querySelector('#layout-loading-spinner');
// Only show the animation if page load is > than timeout seconds
const timeoutId = setTimeout(
() => spinner.classList.remove('d-none'),
200,
);
await originalLoader();
// cancel timeout if is not run yet
clearTimeout(timeoutId);
// spinner.classList.add('d-none');
};
});
</script>
</body>
</html>