Skip to content

Vue and Nuxt

Copy page

To show a skeleton loader in Vue, wrap the part of the page that loads in Dotquilt’s <LoadingRegion>, put a loader such as <SkeletonText :lines="3" /> in its #placeholder slot, and set its state from your data: pending, refreshing or ready.

@dotquilt/vue wraps the same native components in typed Vue components. They register themselves, render their placeholder on the server, and take their state from your props. No Vue or Nuxt configuration is needed.

Terminal window
npm install @dotquilt/vue dotquilt
import { LoadingRegion, SkeletonText } from '@dotquilt/vue';

Each component also has its own entry point, such as @dotquilt/vue/region, if you prefer to import only what a page uses.

A region needs only two things from you: whether the data is pending, refreshing or ready, and a cycle naming the current request. Keep the request in your own state:

  • Start a new cycle per request, such as the id you are loading. A stale answer for an older cycle can’t settle a newer one.
  • Use refreshing when you already show data and are fetching a newer copy: the content stays visible and usable.
  • On an error, show your message with state="ready" and :timing="{ duration: 0 }", so it replaces the placeholder at once.

Its Reload button starts the list over, so it goes back to pending. A refresh that keeps the list on screen would pass refreshing instead.

components/TeamList.vue
<script setup lang="ts">
import { LoadingRegion, SkeletonCircle, SkeletonLine } from '@dotquilt/vue';
import { onMounted, onUnmounted, ref } from 'vue';
interface Member {
name: string;
role: string;
}
const status = ref<'loading' | 'done' | 'failed'>('loading');
const members = ref<Member[]>([]);
const cycle = ref(0);
const handovers = ref(0);
let latest: AbortController | undefined;
function load() {
// Only the latest request may update the list.
latest?.abort();
const request = new AbortController();
latest = request;
status.value = 'loading';
$fetch<Member[]>('/api/team', { signal: request.signal })
.then((list) => {
members.value = list;
status.value = 'done';
})
.catch(() => {
if (!request.signal.aborted) status.value = 'failed';
});
}
function reload() {
cycle.value++;
load();
}
onMounted(load);
onUnmounted(() => latest?.abort());
</script>
<template>
<section>
<h2>Members</h2>
<button type="button" @click="reload">Reload</button>
<!-- An error replaces the placeholder at once, without the handoff. -->
<LoadingRegion
aria-label="Team members"
:state="status === 'loading' ? 'pending' : 'ready'"
:cycle="String(cycle)"
:timing="status === 'failed' ? { duration: 0 } : undefined"
@handoff-end="handovers++"
>
<template #placeholder>
<div v-for="row in 3" :key="row" class="member-skeleton">
<SkeletonCircle />
<SkeletonLine style="width: 45%" />
<SkeletonLine style="width: 30%" />
</div>
</template>
<template v-if="status === 'done'">
<div v-for="member in members" :key="member.name" class="member">
<span class="avatar" />
<h3>{{ member.name }}</h3>
<p>{{ member.role }}</p>
</div>
</template>
<p v-else-if="status === 'failed'" role="alert">
We couldn't load the team. Try again later.
</p>
</LoadingRegion>
<p class="note">Handovers: {{ handovers }}</p>
</section>
</template>

The components render on the server too: the placeholder is in the first HTML, before any JavaScript runs. Hydration leaves it alone, because the elements register only after the component mounts. Load your data in the component, as above, or with useFetch. Its status is 'idle' on the server and 'pending' in the first browser render, so map from the data instead:

const state = computed(() =>
data.value
? status.value === 'pending' ? 'refreshing' : 'ready'
: status.value === 'error' ? 'ready' : 'pending',
);

Data the server already fetched renders ready. While pending, render no content in the region (v-if on the data), so the placeholder holds its own size.

Functions such as target-provider and custom animations stay in the browser, so a chart’s marks can glide onto the real line once it’s there:

components/RevenueChart.vue
<script setup lang="ts">
import { ChartLoader, LoadingRegion } from '@dotquilt/vue';
import type { TargetProvider } from 'dotquilt/chart';
import { computed, onMounted, onUnmounted, ref } from 'vue';
/** Where each value sits in a box of `width` × `height`. */
function linePoints(values: readonly number[], width: number, height: number) {
const top = Math.max(...values) * 1.15;
return values.map((value, i) => ({
x: (i / Math.max(1, values.length - 1)) * width,
y: height - (value / top) * height,
}));
}
const series = ref<number[] | null>(null);
const request = new AbortController();
onMounted(() => {
$fetch<{ series: number[] }>('/api/revenue', { signal: request.signal })
.then((body) => {
series.value = body.series;
})
.catch(() => {});
});
onUnmounted(() => request.abort());
// Where the real line will be, in the loader's own size: the marks glide
// onto it before the chart draws in.
const target: TargetProvider = ({ width, height }) =>
series.value
? {
kind: 'line',
points: linePoints(series.value, width, height),
baseline: height,
}
: null;
const path = computed(() =>
series.value
? linePoints(series.value, 320, 160)
.map(({ x, y }, i) => `${i ? 'L' : 'M'}${x.toFixed(1)} ${y.toFixed(1)}`)
.join(' ')
: '',
);
</script>
<template>
<LoadingRegion
aria-label="Revenue by month"
:state="series ? 'ready' : 'pending'"
>
<template #placeholder>
<ChartLoader variant="line" :target-provider="target" style="height: 160px" />
</template>
<svg
v-if="series"
viewBox="0 0 320 160"
preserveAspectRatio="none"
style="display: block; width: 100%; height: 160px"
role="img"
aria-label="Revenue by month"
>
<path
:d="path"
fill="none"
stroke="currentColor"
stroke-width="2"
vector-effect="non-scaling-stroke"
/>
</svg>
</LoadingRegion>
</template>

Client-side navigation works the same: a loader on the next page starts fresh, and its listeners go when the page does.

Each component emits @handoff-start, @handoff-end, @handoff-cancel and @loader-error, with the native CustomEvent: event.detail.reason says how a handoff ended. A template ref on a component exposes the native element as element, if you need its methods. A component that leaves through a <Transition> is unmounted before its element leaves the page, so it doesn’t report the handoff-cancel that removal causes.

By default each component adds its styles inline, inside its element. If your site has a Content Security Policy, pass a style-policy to every component: a nonce, or an external stylesheet from @dotquilt/vue/styles/. See Content Security Policy.

You can also use the native elements directly, without @dotquilt/vue. Tell Vue they are custom elements, register them once, and pass objects such as a table’s columns as properties:

vite.config.ts
vue({
template: {
compilerOptions: { isCustomElement: (tag) => tag.startsWith('dotquilt-') },
},
});

In Nuxt, set the same option as vue.compilerOptions.isCustomElement in nuxt.config.ts. The native elements have no placeholder in the server HTML: it appears once they register in the browser.