Components

Progress

How far a task has got: a White bar on a 6px #262626 track with round ends, sliding to each new value on transform. Without a value, a short bar sweeps across at a steady pace.

Install

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

npx spyglass-ds add progress
<script type="module" src="/spyglass-ds/elements/progress.js"></script>
Counted and open-ended

Importing Acme ads

40%

Finding new creatives

Usage

An import, an upload or a scan that can be counted, with the number in text beside it; a long search with no known end.

Never

  • Percentages with no label or number beside the bar
  • A striped, glowing or spectrum-colored bar
  • Work that ends in under a second: show nothing

Props

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

<Progress>

PropTypeDefault
valuenumber | null

How far, from 0 to max; leave it out for work with no known end.

maxnumber

The value that means done.

100
labelstring

What is in progress, for assistive tech.

classNamestring

Examples

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

react/examples/progress/Import.tsx
'use client'
import { useState } from 'react'
import { Button } from '@/react/components/ui/Button'
import { Progress } from '@/react/components/ui/Progress'
// An import with a count, a step that moves it on, and a search with no known end.

export default function Import() {
  const [value, setValue] = useState(40)
  return (
    <div className="flex w-80 max-w-full flex-col gap-8">
      <div className="flex flex-col gap-3">
        <div className="flex items-baseline justify-between gap-4">
          <p className="type-ui text-fg">Importing Acme ads</p>
          <p className="type-caption text-fg-muted tabular-nums">{value}%</p>
        </div>
        <Progress value={value} label="Importing Acme ads" />
        <Button variant="secondary" size="sm" className="self-start" onClick={() => setValue((v) => Math.min(100, v + 20))}>Import more</Button>
      </div>
      <div className="flex flex-col gap-3">
        <p className="type-ui text-fg">Finding new creatives</p>
        <Progress label="Finding new creatives" />
      </div>
    </div>
  )
}
elements/examples/progress/Import.html
<!-- An import with a count, a step that moves it on, and a search with no known end. -->
<div class="flex w-80 max-w-full flex-col gap-8">
  <div class="flex flex-col gap-3">
    <div class="flex items-baseline justify-between gap-4">
      <p class="type-ui text-fg">Importing Acme ads</p>
      <p id="import-count" class="type-caption text-fg-muted tabular-nums">40%</p>
    </div>
    <spyglass-progress id="import-progress" value="40" label="Importing Acme ads"></spyglass-progress>
    <spyglass-button id="import-more" variant="secondary" size="sm" class="self-start">Import more</spyglass-button>
  </div>
  <div class="flex flex-col gap-3">
    <p class="type-ui text-fg">Finding new creatives</p>
    <spyglass-progress label="Finding new creatives"></spyglass-progress>
  </div>
</div>
<script type="module">
  const bar = document.getElementById('import-progress')
  document.getElementById('import-more').addEventListener('click', () => {
    bar.value = Math.min(100, bar.value + 20)
    document.getElementById('import-count').textContent = `${bar.value}%`
  })
</script>

Source

react/components/ui/Progress.tsx
import { cn } from '@/react/lib/cn'

// How far a task has got: a White bar on a 6px #262626 track with round ends. The fill slides to a new value over
// 320ms on the strong ease-out, on transform only. Leave out `value` when the end is not known: a short White bar
// sweeps across the track at a steady pace, and with reduced motion it rests and breathes instead. `label` names it
// for assistive tech (a progressbar); say the number in text beside it too.
export interface ProgressProps {
  /** How far, from 0 to max; leave it out for work with no known end. */
  value?: number | null
  /** The value that means done. */
  max?: number
  /** What is in progress, for assistive tech. */
  label?: string
  className?: string
}

export function Progress({ value, max = 100, label, className }: ProgressProps) {
  const known = typeof value === 'number'
  const share = known ? Math.max(0, Math.min(1, value / (max || 1))) : 0
  return (
    <>
      <style href="ds-progress" precedence="ds">{css}</style>
      <div
        data-ds="progress"
        role="progressbar"
        aria-label={label}
        aria-valuemin={0}
        aria-valuemax={max}
        aria-valuenow={known ? value : undefined}
        className={cn('relative h-1.5 w-full overflow-hidden rounded-full bg-action-secondary', className)}
      >
        {known
          ? <div className="h-full w-full rounded-full bg-action-primary transition-transform duration-320 ease-out motion-reduce:transition-none" style={{ transform: `translateX(-${(1 - share) * 100}%)` }} />
          : <div className="ds-progress-sweep h-full w-2/5 rounded-full bg-action-primary" />}
      </div>
    </>
  )
}

export default Progress

const css = `
.ds-progress-sweep { animation: ds-progress-sweep 1.5s var(--ds-easing-in-out) infinite; }
@keyframes ds-progress-sweep { from { transform: translateX(-100%); } to { transform: translateX(250%); } }
@keyframes ds-progress-breathe { 50% { opacity: 0.5; } }
@media (prefers-reduced-motion: reduce) { .ds-progress-sweep { width: 100%; animation: ds-progress-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/progress.ts
import { html } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { styleMap } from 'lit/directives/style-map.js'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'

// Progress.tsx as <spyglass-progress>: a White bar on a 6px #262626 track with round ends, sliding to a new value over
// 320ms on transform only. Without `value` a short White bar sweeps across at a steady pace (breathing in place with
// reduced motion). `label` names it for assistive tech.

/** @summary How far a task has got, as a bar. */
export class Progress extends DsElement {
	static styles = [DsElement.styles, styles]
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="progress"]'))
	static properties = { value: { type: Number }, max: { type: Number }, label: {} }

	/** How far, from 0 to max; leave it out for work with no known end. */
	value?: number | null
	/** The value that means done. */
	max = 100
	/** What is in progress, for assistive tech. */
	label?: string

	render() {
		const known = typeof this.value === 'number' && !Number.isNaN(this.value)
		const share = known ? Math.max(0, Math.min(1, (this.value as number) / (this.max || 1))) : 0
		return html`<div data-ds="progress" role="progressbar" aria-label=${ifDefined(this.label)} aria-valuemin="0" aria-valuemax=${this.max} aria-valuenow=${ifDefined(known ? String(this.value) : undefined)} class="relative h-1.5 w-full overflow-hidden rounded-full bg-action-secondary">${known
			? html`<div class="h-full w-full rounded-full bg-action-primary transition-transform duration-320 ease-out motion-reduce:transition-none" style=${styleMap({ transform: `translateX(-${(1 - share) * 100}%)` })}></div>`
			: html`<div class="ds-progress-sweep h-full w-2/5 rounded-full bg-action-primary"></div>`}</div>`
	}
}

define('progress', Progress)
elements/components/ui/progress.css
/* Progress.tsx's styles, as they are. */
.ds-progress-sweep { animation: ds-progress-sweep 1.5s var(--ds-easing-in-out) infinite; }
@keyframes ds-progress-sweep { from { transform: translateX(-100%); } to { transform: translateX(250%); } }
@keyframes ds-progress-breathe { 50% { opacity: 0.5; } }
@media (prefers-reduced-motion: reduce) { .ds-progress-sweep { width: 100%; animation: ds-progress-breathe 2s var(--ds-easing-in-out) infinite; } }

Reference: Derived from the brand guidelines (no progress bar in the brand assets): White on Black, the neutral steps for the track.