💀 Vue Skeletor (Vue 3 Skeleton Loading component)
Vue 3 adaptive skeleton loading component that will match your typography.
Codesandbox Card Example
Installation
npm install vue-skeletor -S
Why adaptive skeletons?
Skeletons are used to mimic how the real content would look, so in order to create nice skeleton you would have to manually create squares, circles and position/size them to match your real component and keep it updated whenever you change it.
Aww sounds awful isn't it?
Wouldn't be nice if you had a skeleton that automatically adjusts to your existing components layout? I think it would =) so the Vue Skeletor comes to rescue.
Instead of creating separate skeleton components you can inject skeletons directly into your existing components.
Example:
<template>
<div class="post">
<div class="post__image">
<img
v-if="post"
:src="post.img"
height="200"
/>
<Skeletor v-else-if="isPostLoading" height="200"/>
</div>
<div class="post__title">
<template v-if="post">
{{ post.title }}
</template>
<Skeletor v-else-if="isPostLoading">
</div>
<div class="post__text">
<template v-if="post">
{{ post.text }}
</template>
<template v-else-if="isPostLoading">
<Skeletor v-for="i in 5">
</template>
</div>
</div>
</template>
And that's it, the text skeletons will automatically catch up with the styles you defined for the title and text elements.
Basic Usage
First import the Skeletor styles
import 'vue-skeletor/dist/vue-skeletor.css';
Option 1 - Register Locally
import { Skeletor } from 'vue-skeletor';
export default {
components: { Skeletor }
}
Option 2 - Register Globally
import { Skeletor } from 'vue-skeletor';
app.component(Skeletor.name, Skeletor);
<Skeletor />
Global Configuration
If you want you can globally turn off the shimmer animation using the Skeletor Plugin
usage.
import VueSkeletor from 'vue-skeletor';
app.use(VueSkeletor, {
shimmer: false,
})
useSkeletor
When you install skeletor as Plugin it provides
global config to your app and you get access to 'useSkeletor' composable which will inject the skeletor
config object through which you can set any global config at runtime.
import { useSkeletor } from 'vue-skeletor';
export default defineComponent({
setup() {
const skeletor = useSkeletor();
skeletor.shimmer = false;
}
})
Width
width
: number | string
<Skeletor width="100"/>
<Skeletor :width="100"/>
<Skeletor width="50%">
Width of your skeleton, can be a number or css string value.
Height
height
: number | string
<Skeletor height="100"/>
<Skeletor :height="100"/>
<Skeletor height="50%">
Height of your skeleton, can be a number or css string value.
⚠️ Notice
When you set height, your skeleton automatically becomes a rect with display: block
meaning it will no longer adapt to your typography, which is useful for
creating non text block level skeletons like image placeholders, buttons, and e.t.c.
Size
size
: number | string
<Skeletor size="100"/>
Size sets both width
& height
to simplify creating square/circle shapes
Circle
circle
: boolean (default: false)
<Skeletor circle size="50"/>
As the name suggest it just turns the element into a circle, use only when width
& height
or size is set.
Pill
pill
: boolean (default: false)
<Skeletor width="200" height="50" pill/>
Makes rectangular skeletons fully rounded, useful when creating rounded button or chip
and e.t.c shapes.
Shimmer
shimmer
: boolean
<Skeletor :shimmer="false"/>
Optionally you can turn off/on specific skeleton's shimmer animation, it is based of
your global config, if you disable shimmer globally, then this prop should be set
accordingly.
As
as
: string (default: 'span')
<Skeletor :size="100" as="div"/>
By default skeletons are rendered as span
tags, but you can change it
using this prop.
Customizing the style and animation
Skeletor uses bem classes, that you can use to override your skeletons color and shimmer animation and you have the full control over how your skeletons look, there is no need for any javascript api for this in my opinion.
.vue-skeletor {
background-color: #ccc;
}
[data-theme="dark"] .vue-skeletor {
background: #363636;
}
.vue-skeletor--text {
border-radius: 0;
}
.vue-skeletor:not(.vue-skeletor--shimmerless):after {
background: linear-gradient(
90deg,
rgba(255,255,255,0) 0%,
rgba(255,255,255,1) 50%,
rgba(255,255,255,0) 100%
);
animation-duration: 2s;
animation-timing-function: ease-in-out;
}
@keyframes shimmer {
100% {
transform: translateX(100%);
}
}