Components

Spinner

Work with no known end: a quarter arc in the color around it, White on Black, turning on a #363636 ring at a constant speed. With reduced motion it breathes instead of turning.

Install

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

npx spyglass-ds add spinner
<script type="module" src="/spyglass-ds/elements/spinner.js"></script>
Where it goes
Searching for Acme hooks.

Usage

A search, a save or a load that takes more than a moment, beside the words that say what is happening or inside the busy button.

Never

  • A spinner where the shape of the content is known: use Skeleton
  • Progress that can be counted: use Progress
  • A colored, glowing or spectrum spinner
  • A blank screen with only a spinner

Props

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

<Spinner>

PropTypeDefault
sizeSpinnerSize

sm (16px) or md (20px).

'sm'
labelstring

What is loading, for assistive tech.

'Loading'

Examples

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

react/examples/spinner/States.tsx
import { Button } from '@/react/components/ui/Button'
import { Spinner } from '@/react/components/ui/Spinner'
// A search running: the spinner beside its line, in a busy button, and at the larger size on its own.

export default function States() {
  return (
    <div className="flex flex-col items-start gap-6">
      <div className="flex items-center gap-3 type-ui text-fg-soft">
        <Spinner label="Searching" />
        <span>Searching for Acme hooks.</span>
      </div>
      <Button variant="secondary" disabled><Spinner label="Saving" />Saving</Button>
      <Spinner size="md" label="Loading boards" />
    </div>
  )
}
elements/examples/spinner/States.html
<!-- A search running: the spinner beside its line, in a busy button, and at the larger size on its own. -->
<div class="flex flex-col items-start gap-6">
  <div class="flex items-center gap-3 type-ui text-fg-soft">
    <spyglass-spinner label="Searching"></spyglass-spinner>
    <span>Searching for Acme hooks.</span>
  </div>
  <spyglass-button variant="secondary" disabled><spyglass-spinner label="Saving"></spyglass-spinner>Saving</spyglass-button>
  <spyglass-spinner size="md" label="Loading boards"></spyglass-spinner>
</div>

Source

react/components/ui/Spinner.tsx
import type { SVGAttributes } from 'react'
import { cn } from '@/react/lib/cn'

// Work in progress with no known end: a quarter arc in the color around it (White on Black) turning on a #363636
// ring, one turn every 750ms at a constant speed (linear, as constant motion is). With reduced motion it stops turning
// and breathes instead. sm is 16px (beside UI text, in a button), md 20px. It announces itself as a status with
// `label`.
export type SpinnerSize = 'sm' | 'md'

const SIZES: Record<SpinnerSize, string> = { sm: 'size-icon-sm', md: 'size-icon-md' }

export interface SpinnerProps extends Omit<SVGAttributes<SVGSVGElement>, 'role'> {
  /** sm (16px) or md (20px). */
  size?: SpinnerSize
  /** What is loading, for assistive tech. */
  label?: string
}

export function Spinner({ size = 'sm', label = 'Loading', className, ...rest }: SpinnerProps) {
  return (
    <>
      <style href="ds-spinner" precedence="ds">{css}</style>
      <svg data-ds="spinner" role="status" aria-label={label} viewBox="0 0 24 24" fill="none" className={cn('ds-spinner shrink-0', SIZES[size] ?? SIZES.sm, className)} {...rest}>
        <circle cx="12" cy="12" r="9" strokeWidth="2.5" className="stroke-edge-strong" />
        <path d="M12 3a9 9 0 0 1 9 9" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" />
      </svg>
    </>
  )
}

export default Spinner

const css = `
.ds-spinner { animation: ds-spinner-turn 750ms linear infinite; }
@keyframes ds-spinner-turn { to { transform: rotate(360deg); } }
@keyframes ds-spinner-breathe { 50% { opacity: 0.5; } }
@media (prefers-reduced-motion: reduce) { .ds-spinner { animation: ds-spinner-breathe 2s var(--ds-easing-in-out) infinite; } }
`
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/spinner.ts
import { html } from 'lit'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'

// Spinner.tsx as <spyglass-spinner>: a quarter arc in the color around it turning on a #363636 ring, one turn every
// 750ms at a constant speed; with reduced motion it breathes instead. sm 16px, md 20px. A status with `label`.

const SIZES = { sm: 'size-icon-sm', md: 'size-icon-md' } as const

/** @summary Work in progress with no known end. */
export class Spinner extends DsElement {
	static styles = [DsElement.styles, styles]
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('svg') as unknown as HTMLElement)
	static properties = { size: { reflect: true }, label: {} }

	/** sm (16px) or md (20px). */
	size: keyof typeof SIZES = 'sm'
	/** What is loading, for assistive tech. */
	label = 'Loading'

	render() {
		return html`<svg data-ds="spinner" role="status" aria-label=${this.label} viewBox="0 0 24 24" fill="none" class=${`ds-spinner shrink-0 ${SIZES[this.size] ?? SIZES.sm}`}><circle cx="12" cy="12" r="9" stroke-width="2.5" class="stroke-edge-strong"></circle><path d="M12 3a9 9 0 0 1 9 9" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"></path></svg>`
	}
}

define('spinner', Spinner)
elements/components/ui/spinner.css
/* Spinner.tsx's styles, as they are. */
.ds-spinner { animation: ds-spinner-turn 750ms linear infinite; }
@keyframes ds-spinner-turn { to { transform: rotate(360deg); } }
@keyframes ds-spinner-breathe { 50% { opacity: 0.5; } }
@media (prefers-reduced-motion: reduce) { .ds-spinner { animation: ds-spinner-breathe 2s var(--ds-easing-in-out) infinite; } }

Reference: Derived from the brand guidelines (no loading state in the brand assets): White on Black, the neutral steps for the ring.