Last active
March 14, 2025 23:40
-
-
Save kobitoDevelopment/e6398a55f55c2de58d7e70a8828999d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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