Skip to content

Instantly share code, notes, and snippets.

@kobitoDevelopment
Last active March 14, 2025 23:40
Show Gist options
  • Save kobitoDevelopment/e6398a55f55c2de58d7e70a8828999d9 to your computer and use it in GitHub Desktop.
Save kobitoDevelopment/e6398a55f55c2de58d7e70a8828999d9 to your computer and use it in GitHub Desktop.
<template>
<div>
<LoadingMyLoading :loaded="isMounted" />
</div>
</template>
<script setup lang="ts">
const isMounted = ref(false);
onMounted(() => {
/* onMountedは「コンポーネントのmount」なため、ページ全体のmountではない。
* そのため、レイアウトやページ全体のコンポーネントが集約しているファイルなどのonMountedの中でflagを管理し、
* ローディング演出用のコンポーネントに渡す
*/
isMounted.value = true;
});
</script>
<style scoped></style>
<template>
<div ref="myLoadingElement" :class="['my-loading', { '-mounted': props.loaded }]" :aria-busy="!props.loaded"></div>
</template>
<script lang="ts" setup>
const props = defineProps<{ loaded: boolean }>();
const myLoadingElement = ref<HTMLElement | null>(null);
const removeElement = (event: AnimationEvent) => {
if (event.animationName.includes("loading-animation")) {
myLoadingElement.value?.remove();
}
};
onMounted(() => {
watch(
() => props.loaded,
(newValue) => {
if (newValue) {
myLoadingElement.value?.addEventListener("animationend", removeElement);
}
}
);
});
onBeforeUnmount(() => {
myLoadingElement.value?.removeEventListener("animationend", removeElement);
});
</script>
<style scoped>
.my-loading {
width: 100%;
height: 100vh;
background-color: red;
}
.my-loading.-mounted {
animation: loading-animation 1s forwards;
}
@keyframes loading-animation {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment