Last active
June 30, 2021 10:39
-
-
Save lloydjatkinson/20d27dcac96b952653cd0483b28d2d32 to your computer and use it in GitHub Desktop.
A Vue component that conditionally renders slots depending on the combination of "loading" and "failed" props.
This file contains 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> | |
<slot | |
v-if="showLoadingState" | |
name="loading"> | |
<div> | |
Loading... | |
</div> | |
</slot> | |
<slot | |
v-if="showFailedState" | |
name="failed" /> | |
<slot | |
v-if="hasLoaded" | |
name="completed" /> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'Loading', | |
props: { | |
loading: { | |
type: Boolean, | |
required: true, | |
default: false, | |
}, | |
failed: { | |
type: Boolean, | |
required: false, | |
default: false, | |
}, | |
}, | |
computed: { | |
showLoadingState () { | |
// Ensure that the failed state takes precedence in case the parent incorrectly | |
// sets loading and failed to true. | |
if (this.loading === true && !this.failed) { | |
return true; | |
} | |
return false; | |
}, | |
showFailedState () { | |
if (this.failed) { | |
return true; | |
} | |
return false; | |
}, | |
hasLoaded () { | |
if (!this.loading && !this.failed) { | |
return true; | |
} | |
return false; | |
}, | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment