💀 Vue Skeletor (Vue 3 Skeleton Loading component)
Vue 3 adaptive skeleton loading component that will match your typography.
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:
<!-- A simple 200px height rect which mimics the post image --> {{ post.title }} {{ post.text }} <!-- Creating 5 skeletons that mimics the text -->
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
;
Option 1 - Register Locally
// SomeComponent.vue ; components: Skeletor
Option 2 - Register Globally
// main.js ; app;
<!-- And use in your <template> or JSX -->
Global Configuration
If you want you can globally turn off the shimmer animation using the Skeletor Plugin usage.
// Import the plugin ; // Register plugin in your vue app app
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 the composable ; { // In your setup function use the composable const skeletor = ; // Set the shimmer config skeletorshimmer = false; }
Width
width
: number | string
<!-- Both are same and evaluate to 100px --> <!-- Any valid css value for width property -->
Width of your skeleton, can be a number or css string value.
Height
height
: number | string
<!-- Both are same and evaluate to 100px --> <!-- Any valid css value for width property -->
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
<!-- Creates a 100x100 square -->
Size sets both width
& height
to simplify creating square/circle shapes
Circle
circle
: boolean (default: false)
<!-- Creates a 50x50 circle -->
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)
Makes rectangular skeletons fully rounded, useful when creating rounded button or chip and e.t.c shapes.
Shimmer
shimmer
: boolean
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')
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.
/* Static background */ /* If you have theme switching in your app for example from light to dark, you can target skeletons under some global theme class or attribute e.g. */ [ /* Text skeleton By default skeletor uses fully rounded style for text type skeletons, you can change that as you like */ /* Shimmer */ /* Default keyframes used in skeletor */ @ }