logo dile-components dile-components

dile-skeleton-loader

Web Component that swaps a skeleton for the real content. While loading is true it shows the skeleton slot and hides the content; when the application sets loading to false, it shows the content.

It does not fetch data: your application decides when the content is ready. Build the skeleton with dile-skeleton blocks, or any markup you like.

Installation

npm i @dile/ui

Usage

Import the components.

import '@dile/ui/components/skeleton/skeleton-loader.js';
import '@dile/ui/components/skeleton/skeleton.js';

Use the component.

<dile-skeleton-loader loading label="Loading user profile">
  <div slot="skeleton">
    <dile-skeleton shape="circle"></dile-skeleton>
    <dile-skeleton lines="2"></dile-skeleton>
  </div>
  <user-profile></user-profile>
</dile-skeleton-loader>

When the data arrives, turn loading off.

loader.loading = false;

Properties

Property Type Default Description
loading Boolean false Shows the skeleton instead of the content and marks the region as busy.
label String "" Message announced to assistive technologies while loading. When empty, "Loading…" in the component language (English and Spanish).
language String "" Language of the default message. When empty, it comes from the closest lang attribute, the default language of @dile/ui or the document.
static Boolean false Disables the shine animation of every dile-skeleton inside.

Slots

CSS Parts

CSS Custom Properties

Custom property Description Default
--dile-skeleton-animation Set it to none to make every skeleton inside static (the static attribute does it for you) dile-skeleton-shine 1.5s ease-in-out infinite

The skeleton blocks are styled with the dile-skeleton custom properties.

Accessibility

dile-skeleton-loader demos

Swapping skeleton and content

<script type="module">
  import { LitElement, html, css } from 'lit';
  import '@dile/ui/components/skeleton/skeleton.js';
  import '@dile/ui/components/skeleton/skeleton-loader.js';
  import '@dile/ui/components/avatar/avatar.js';
  import '@dile/ui/components/button/button.js';

  class SkeletonLoaderDemo extends LitElement {
    static styles = css`
      .row { display: flex; gap: 1rem; align-items: center; margin-top: 1rem; }
      dile-skeleton[shape="text"] { flex: 1; }
    `;
    static properties = { loading: { type: Boolean } };

    constructor() {
      super();
      this.reload();
    }

    reload() {
      this.loading = true;
      setTimeout(() => this.loading = false, 2000);
    }

    render() {
      return html`
        <dile-button @click=${this.reload}>Reload</dile-button>
        <dile-skeleton-loader ?loading=${this.loading} label="Loading user profile">
          <div slot="skeleton" class="row">
            <dile-skeleton shape="circle" width="56px"></dile-skeleton>
            <dile-skeleton lines="2"></dile-skeleton>
          </div>
          <div class="row">
            <dile-avatar initial="J" style="--dile-avatar-size: 56px"></dile-avatar>
            <div><strong>Jane Doe</strong><br>Frontend developer</div>
          </div>
        </dile-skeleton-loader>
      `;
    }
  }
  customElements.define('skeleton-loader-demo', SkeletonLoaderDemo);
</script>
<skeleton-loader-demo></skeleton-loader-demo>

Static skeleton

Content

<dile-skeleton-loader loading static>
  <div slot="skeleton">
    <dile-skeleton shape="rect" height="100px"></dile-skeleton>
    <dile-skeleton lines="2" style="margin-top: 0.8rem;"></dile-skeleton>
  </div>
  <p>Content</p>
</dile-skeleton-loader>