Skip to content

Instantly share code, notes, and snippets.

@lloydjatkinson
Last active June 30, 2021 10:39
Show Gist options
  • Save lloydjatkinson/20d27dcac96b952653cd0483b28d2d32 to your computer and use it in GitHub Desktop.
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.
<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