Components

Skeleton

A placeholder in the shape of what is loading: a #1A1A1A block with the brand corner, or a circle, that breathes between full and half opacity. Never a shimmer; still with reduced motion.

Install

React apps get the React files; every other stack gets the web component.

npx spyglass-ds add skeleton
<script type="module" src="/spyglass-ds/elements/skeleton.js"></script>
A creative card

Usage

Holding the place of a creative card, a row or a chart while it loads, sized to the part it stands for so nothing moves when it lands.

Never

  • A shimmer, a gradient sweep or a spectrum color
  • A layout that jumps when the content lands: size each block to its part
  • A skeleton for something that loads in under 300ms

When the picture it holds the place of arrives, pass it as src (with alt, fit and position): the block shows it, still, in the same shape and corner. Without src the block carries data-ds-placeholder, so a page can be checked for pictures still to fill.

Adds with it: creative-card

Props

The web component takes the same as attributes or properties (<spyglass-skeleton>); title is heading there.

<Skeleton>

PropTypeDefault
circleboolean

A circle (an avatar or a logo) in place of the brand corner.

Examples

The same example in React and as web components; both render identically.

react/examples/skeleton/Creative.tsx
import { Skeleton } from '@/react/components/ui/Skeleton'
// A creative card while it loads: the thumbnail, the brand's logo and two lines, in the shapes they will take.

export default function Creative() {
  return (
    <div className="flex w-72 max-w-full flex-col gap-4 rounded-container bg-surface-raised p-4" aria-busy="true" aria-label="Loading creative">
      <Skeleton className="aspect-square w-full" />
      <div className="flex items-center gap-3">
        <Skeleton circle className="size-10 shrink-0" />
        <div className="flex flex-1 flex-col gap-2">
          <Skeleton className="h-4 w-3/4" />
          <Skeleton className="h-3 w-1/2" />
        </div>
      </div>
    </div>
  )
}
elements/examples/skeleton/Creative.html
<!-- A creative card while it loads: the thumbnail, the brand's logo and two lines, in the shapes they will take. -->
<div class="flex w-72 max-w-full flex-col gap-4 rounded-container bg-surface-raised p-4" aria-busy="true" aria-label="Loading creative">
  <spyglass-skeleton class="aspect-square w-full"></spyglass-skeleton>
  <div class="flex items-center gap-3">
    <spyglass-skeleton circle class="size-10 shrink-0"></spyglass-skeleton>
    <div class="flex flex-1 flex-col gap-2">
      <spyglass-skeleton class="h-4 w-3/4"></spyglass-skeleton>
      <spyglass-skeleton class="h-3 w-1/2"></spyglass-skeleton>
    </div>
  </div>
</div>

Source

react/components/ui/Skeleton.tsx
import type { HTMLAttributes } from 'react'
import { cn } from '@/react/lib/cn'
import { imageFit, type ImageSlot } from '@/lib/image'

// A placeholder in the shape of what is loading: a #1A1A1A block with the brand corner (or a circle) that breathes
// between full and half opacity every 2 seconds, never a shimmer. Size it with classes (h-4 w-48) to the part it holds
// the place of, so nothing moves when the content lands. Still with reduced motion. Hidden from assistive tech: say
// "Loading" once on the region (aria-busy) instead. When the picture it holds the place of arrives, pass it (`src`,
// `alt`, `fit`, `position`): the block shows it, still, in the same shape. Without one it carries data-ds-placeholder.
export interface SkeletonProps extends HTMLAttributes<HTMLDivElement>, ImageSlot {
  /** A circle (an avatar or a logo) in place of the brand corner. */
  circle?: boolean
}

export function Skeleton({ circle, src, alt = '', fit, position, className, ...rest }: SkeletonProps) {
  const shape = circle ? 'rounded-full' : 'rounded-control'
  if (src) {
    return (
      <div data-ds="skeleton" className={cn('overflow-hidden bg-surface-subtle', shape, className)} {...rest}>
        <img src={src} alt={alt} decoding="async" className="block size-full object-cover" style={imageFit({ fit, position })} />
      </div>
    )
  }
  return (
    <>
      <style href="ds-skeleton" precedence="ds">{css}</style>
      <div data-ds="skeleton" data-ds-placeholder="" aria-hidden="true" className={cn('ds-skeleton bg-surface-subtle', shape, className)} {...rest} />
    </>
  )
}

export default Skeleton

const css = `
.ds-skeleton { animation: ds-skeleton-breathe 2s var(--ds-easing-in-out) infinite; }
@keyframes ds-skeleton-breathe { 50% { opacity: 0.5; } }
@media (prefers-reduced-motion: reduce) { .ds-skeleton { animation: none; } }
`
react/lib/cn.ts
// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]

export function cn(...values: ClassValue[]): string {
	const out: string[] = []
	for (const v of values) {
		if (!v) continue
		if (Array.isArray(v)) {
			const s = cn(...v)
			if (s) out.push(s)
		}
		else out.push(v)
	}
	return out.join(' ')
}
elements/components/ui/skeleton.ts
import { html, nothing, type PropertyValues } from 'lit'
import { styleMap } from 'lit/directives/style-map.js'
import { imageFit, type ImageFit } from '@/lib/image'
import { DsElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'

// Skeleton.tsx as <spyglass-skeleton>: a placeholder in the shape of what is loading, a #1A1A1A block with the brand
// corner (or a circle) that breathes between full and half opacity, never a shimmer; still with reduced motion. The
// host is the block, so size classes on the element (h-4 w-48) land on it. Hidden from assistive tech. With `src` (and
// `alt`, `fit`, `position`) it shows the picture that arrived, still, in the same shape; without, the host carries
// data-ds-placeholder.

/** @summary A placeholder block in the shape of what is loading. */
export class Skeleton extends DsElement {
	static styles = [DsElement.styles, styles]
	static properties = { circle: { type: Boolean, reflect: true }, src: { reflect: true }, alt: {}, fit: {}, position: {} }

	/** A circle (an avatar or a logo) in place of the brand corner. */
	circle = false
	/** The picture that arrived. */
	src?: string
	/** What it shows. */
	alt = ''
	/** How it fills the block (object-fit); cover by default. */
	fit?: ImageFit
	/** Where it is anchored (object-position). */
	position?: string

	protected updated(changed: PropertyValues) {
		super.updated(changed)
		if (this.src) this.removeAttribute('aria-hidden')
		else this.setAttribute('aria-hidden', 'true')
		this.toggleAttribute('data-ds-placeholder', !this.src)
	}

	render() {
		return this.src ? html`<img src=${this.src} alt=${this.alt} decoding="async" class="block size-full object-cover" style=${styleMap(imageFit(this) ?? {})}>` : nothing
	}
}

define('skeleton', Skeleton)
elements/components/ui/skeleton.css
/* The host is the block (Skeleton.tsx's root div), so size classes on the element land on it. */
:host { @apply block bg-surface-subtle rounded-control; animation: ds-skeleton-breathe 2s var(--ds-easing-in-out) infinite; }
:host([circle]) { @apply rounded-full; }
:host([src]) { overflow: hidden; animation: none; }
@keyframes ds-skeleton-breathe { 50% { opacity: 0.5; } }
@media (prefers-reduced-motion: reduce) { :host { animation: none; } }

Reference: Derived from the brand guidelines (no loading state in the brand assets): the neutral steps for the block, the brand corner.