Home

Awesome

English | 中文

Vue-Default-Page

NPM Version Open in StackBlitz

A Vue 3.0 custom directives plugin package, built-in with v-loading, v-skeleton, v-skeleton-avatar, v-skeleton-list, v-error, and v-empty default page directives, focusing on solving scenarios such as waiting, feedback, and error during network requests.

Table of Contents

Installation

npm i vue-default-page

Quickstart

Full Import

// main.js

// Import the directives
import vueDefaultPage from 'vue-default-page';

import { createApp } from 'vue';

const app = createApp();

// Register the directives
app.use(vueDefaultPage);
<!-- demo.vue -->

<div v-loading="true"></div>

Global Configuration

// main.js

app.use(vueDefaultPage, {
  background: '#000',
  loading: {
    iconColor: '#fff',
    miniIconColor: '#fff',
    textColor: '#fff',
  },
});
NameDescriptionTypeDefault
zIndexThe stack level of the directivenumber / string100
backgroundBackground color of the maskstring#fff
loadingv-loading configurationsboolean / VdpLoadingtrue
skeletonv-skeleton configurationsboolean / VdpSkeletontrue
skeletonAvatarv-skeleton-avatar configurationsboolean / VdpSkeletonAvatarfalse
skeletonListv-skeleton-list configurationsboolean / VdpSkeletonListfalse
errorv-error configurationsboolean / VdpErrortrue
emptyv-empty configurationsboolean / VdpEmptytrue

On-demand Import

// main.js

// Import the directive
import { vdpLoading } from 'vue-default-page';

import { createApp } from 'vue';

const app = createApp();

// Register the directive
app.use(vdpLoading);

On-demand Import Global Configuration

// main.js

app.use(vdpLoading, {
  background: '#000',
  iconColor: '#fff',
  miniIconColor: '#fff',
  textColor: '#fff',
});
NameDescriptionConfigurations Type
vdpLoadingv-loadingVdpLoading
vdpSkeletonv-skeletonVdpSkeleton
vdpSkeletonAvatarv-skeleton-avatarVdpSkeletonAvatar
vdpSkeletonListv-skeleton-listVdpSkeletonList
vdpErrorv-errorVdpError
vdpEmptyv-emptyVdpEmpty

Partial Import

<!-- demo.vue -->

<script setup lang="js">
  // Import the directives creation function
  import { createVueDefaultPage } from 'vue-default-page';
  // Create the directive
  const vLoading = createVueDefaultPage('loading');
</script>

<template>
  <div v-loading="true"></div>
</template>

Partial Import Configuration

Method 1: Configure in the directives creation function, the detailed configurations is consistent with On-demand Import Global Configuration.

<!-- demo.vue -->

<script setup lang="js">
  // Create the directive
  const vLoading = createVueDefaultPage('loading', {
    background: '#000',
    iconColor: '#fff',
    miniIconColor: '#fff',
    textColor: '#fff',
  });
</script>

<template>
  <div v-loading="true"></div>
</template>

Method 2: Add attribute configurations to the element.

<!-- demo.vue -->

<template>
  <div
    v-loading="true"
    vdp-background="#000"
    vdp-loading-icon-color="#fff"
    vdp-loading-mini-icon-color="#fff"
    vdp-loading-text-color="#fff"
  ></div>
</template>

Common attribute configurations, for specific directive attribute configurations, please refer to the detailed sections below.

NameDescriptionTypeDefault
vdp-z-indexThe stack level of the directivestring100
vdp-backgroundBackground color of the maskstring#fff

Advanced

<!-- demo.vue -->

<script setup lang="js">
  import { ref } from 'vue';
  // npm i vue-hooks-plus
  import { useRequest } from 'vue-hooks-plus';

  const data = ref([]);
  // Simulate the first request failure, and then refresh to request successfully.
  let times = 0;
  const api = () =>
    new Promise((resolve, reject) => {
      setTimeout(() => {
        data.value = Array.from({ length: 10 }, (v, k) => k);
        times ? resolve('Successful') : reject('Failed');
        times++;
      }, 2000);
    });

  // https://inhiblabcore.github.io/docs/hooks/useRequest
  const { loading, error, refresh } = useRequest(api);
</script>

<template>
  <div v-loading="loading" v-error="[!!error, refresh]" v-empty="!data.length">
    <div v-for="i in data" :key="i">{{ i }}</div>
  </div>
</template>

Show Priority

When all directives are true, it will be shown according to the following priority.

v-loading > v-skeleton = v-skeleton-avatar = v-skeleton-list > v-error > v-empty

v-loading

Custom icon, same configuration as v-error and v-empty.

<!-- demo.vue -->

<script setup lang="js">
  import { createVueDefaultPage } from 'vue-default-page';
  const icon = `<svg class="circle" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160">
    <path
      d="M151.5,80A71.5,71.5,0,1,1,80,8.5"
      fill="none"
      stroke="#bbb"
      stroke-miterlimit="10"
      stroke-width="16"
    />
  </svg>`;
  // Create the directive
  const vLoading = createVueDefaultPage('loading', {
    icon,
    miniIcon: icon,
  });
</script>

<template>
  <div v-loading="true"></div>
</template>

<style>
  .circle {
    display: block;
    animation: spin 1.5s ease-in-out infinite;
  }
  @keyframes spin {
    100% {
      transform: rotate(360deg);
    }
  }
</style>

VdpLoading

NameDescriptionTypeDefault
enableEnable the directive (Enable when Global Configuration)booleantrue
textTextstringLoading…
textColorText colorstring#999
iconColorIcon color (Disable when custom icon)string#bbb
miniIconColorMini icon color (Disable when custom mini icon)string#bbb
iconCustom iconstring
miniIconCustom mini iconboolean / stringtrue
iconMaxSizeMaximum size of iconnumber / string24
iconShowTextWhether to show text when using large iconbooleantrue
zIndexThe stack level of the directivenumber / string100
backgroundBackground color of the maskstring#fff

Attribute Configurations

NameDescriptionTypeDefault
vdp-loading-textTextstringLoading…
vdp-loading-text-colorText colorstring#999
vdp-loading-icon-colorIcon color (Disable when custom icon)string#bbb
vdp-loading-mini-icon-colorMini icon color (Disable when custom mini ico)string#bbb
vdp-loading-iconCustom iconstring
vdp-loading-mini-iconCustom mini iconstring
vdp-loading-icon-max-sizeMaximum size of iconstring24

v-skeleton

Default show avatar and list skeleton, can also be used independently.

<!-- demo.vue -->

<template>
  <div v-skeleton="true"></div>
  <div v-skeleton.avatar="true"></div>
  <div v-skeleton.list="true"></div>
</template>

VdpSkeleton

NameDescriptionTypeDefault
enableEnable the directive (Enable when Global Configuration)booleantrue
animationAnimationboolean / ('avatar' | 'list')[] / Animationtrue
avatarMaxSizeMaximum size of avatarnumber / string54
zIndexThe stack level of the directivenumber / string100
backgroundBackground color of the maskstring#fff

Attribute Configurations

NameDescriptionTypeDefault
vdp-skeleton-avatar-max-sizeMaximum size of avatarstring54

Animation

NameDescriptionTypeDefault
avatarEnable avatar animationbooleantrue
listEnable list animationbooleantrue

Avatar or list skeleton can also be used individually by registering directives, but unlike other directives, these two are false by default and need to be manually enabled.

// main.js

app.use(vueDefaultPage, {
  skeletonAvatar: true,
  skeletonList: true,
});
<!-- demo.vue -->

<template>
  <div v-skeleton-avatar="true"></div>
  <div v-skeleton-list="true"></div>
</template>

VdpSkeletonAvatar

NameDescriptionTypeDefault
enableEnable the directive (Enable when Global Configuration)booleanfalse
animationAnimationbooleantrue
avatarMaxSizeMaximum size of avatarnumber / string54
zIndexThe stack level of the directivenumber / string100
backgroundBackground color of the maskstring#fff

VdpSkeletonList

NameDescriptionTypeDefault
enableEnable the directive (Enable when Global Configuration)booleanfalse
animationAnimationbooleantrue
zIndexThe stack level of the directivenumber / string100
backgroundBackground color of the maskstring#fff

v-error

Like other directives, it can be controlled to show or hide using a Boolean.

<!-- demo.vue -->

<template>
  <div v-error="true"></div>
</template>

You can also pass a refresh function in an Array. For detailed usage, refer to Advanced.

<!-- demo.vue -->

<template>
  <div v-error="[error, () => {}]"></div>
</template>

VdpError

NameDescriptionTypeDefault
enableEnable the directive (Enable when Global Configuration)booleantrue
textTextstringNetwork Error
refreshTextRefresh text (Enable when refresh function is passed)boolean / string, Click to Refresh
textColorText colorstring#999
iconCustom iconstring
miniIconCustom mini iconboolean / stringtrue
iconMaxSizeMaximum size of iconnumber / string180
iconShowTextWhether to show text when using large iconbooleantrue
zIndexThe stack level of the directivenumber / string100
backgroundBackground color of the maskstring#fff

Attribute Configurations

NameDescriptionTypeDefault
vdp-error-textTextstringNetwork Error
vdp-error-refresh-textRefresh text (Enable when refresh function is passed)string, Click to Refresh
vdp-error-text-colorText colorstring#999
vdp-error-iconCustom iconstring
vdp-error-mini-iconCustom mini iconstring
vdp-error-icon-max-sizeMaximum size of iconstring180

v-empty

All directives will automatically adjust their height according to the container's size, and they have different display states.

<!-- demo.vue -->

<template>
  <div v-empty="true" style="height: 500px;"></div>
  <div v-empty="true"></div>
</template>

The maximum icon size can be adjusted using the iconMaxSize or the vdp-empty-icon-max-size (v-skeleton series directives allow only the avatarMaxSize to change the maximum avatar size).

<!-- demo.vue -->

<template>
  <div
    v-empty="true"
    style="height: 500px;"
    vdp-empty-icon-max-size="400"
  ></div>
</template>

VdpEmpty

NameDescriptionTypeDefault
enableEnable the directive (Enable when Global Configuration)booleantrue
textTextstringNo Data
textColorText colorstring#999
iconCustom iconstring
miniIconCustom mini iconboolean / stringfalse
iconMaxSizeMaximum size of iconnumber / string180
iconShowTextWhether to show text when using largebooleantrue
zIndexThe stack level of the directivenumber / string100
backgroundBackground color of the maskstring#fff

Attribute Configurations

NameDescriptionTypeDefault
vdp-empty-textTextstringNo Data
vdp-empty-text-colorText colorstring#999
vdp-empty-iconCustom iconstring
vdp-empty-mini-iconCustom mini iconstring
vdp-empty-icon-max-sizeMaximum size of iconstring180

Adapt to Mobile

Vue-Default-Page uses px by default and design width is 375. You can use plugins such as postcss-px-to-viewport to convert to viewport units.

// vite.config.js

import pxToViewport from 'postcss-px-to-viewport';

export default defineConfig(() => {
  return {
    css: {
      postcss: {
        plugins: [pxToViewport({ viewportWidth: 375 })],
      },
    },
  };
});

Thanks

Thank element-plus for providing inspiration.

LICENSE

MIT.