Components
Label
A field's label: DM Sans Medium in capitals with 6% tracking, in the soft gray, over the control it names. A muted hint can follow it.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add label<script type="module" src="/spyglass-ds/elements/label.js"></script>Usage
Naming a field or a control when Field's layout does not fit: a label beside a switch, over a slider, over a group of radios.
Never
- A placeholder in place of a label
- Bold, color or a spectrum accent on a label
- Sentence case or long text in a label: say it in two or three words, and put the rest in a hint under the field
Props
The web component takes the same as attributes or properties (<spyglass-label>); title is heading there.
<Label>
| Prop | Type | Default |
|---|---|---|
hint | string A muted note after the label, such as "Optional". | |
children | ReactNode |
Examples
The same example in React and as web components; both render identically.
import { Input } from '@/react/components/ui/Input'
import { Label } from '@/react/components/ui/Label'
// Labels over the fields they name, one with a note that it can stay empty.
export default function Fields() {
return (
<div className="flex w-80 max-w-full flex-col gap-6">
<div className="flex flex-col gap-2">
<Label htmlFor="label-email">Work email</Label>
<Input id="label-email" type="email" placeholder="you@acme.com" />
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="label-team" hint="Optional">Team name</Label>
<Input id="label-team" placeholder="Acme growth" />
</div>
</div>
)
}<!-- Labels over the fields they name, one with a note that it can stay empty. -->
<div class="flex w-80 max-w-full flex-col gap-6">
<div class="flex flex-col gap-2">
<spyglass-label for="label-email">Work email</spyglass-label>
<spyglass-input id="label-email" type="email" placeholder="you@acme.com"></spyglass-input>
</div>
<div class="flex flex-col gap-2">
<spyglass-label for="label-team" hint="Optional">Team name</spyglass-label>
<spyglass-input id="label-team" placeholder="Acme growth"></spyglass-input>
</div>
</div>Source
import type { LabelHTMLAttributes, ReactNode } from 'react'
import { cn } from '@/react/lib/cn'
// A field's label on its own: the brand's label voice, DM Sans Medium in capitals with 6% tracking at 12px, in the soft
// gray (#D4D4D4), over or beside the control it names, never in place of one. `htmlFor` ties it to the control's id;
// `hint` adds a muted note after it, such as "Optional". Every other prop lands on the <label>.
export interface LabelProps extends LabelHTMLAttributes<HTMLLabelElement> {
/** A muted note after the label, such as "Optional". */
hint?: string
children?: ReactNode
}
export function Label({ hint, className, children, ...rest }: LabelProps) {
return <label data-ds="label" {...rest} className={cn('inline-flex items-baseline gap-1.5 type-label text-fg-soft', className)}>{children}{hint && <span className="text-fg-muted">{hint}</span>}</label>
}
export default Label// 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(' ')
}import { html, nothing, type PropertyValues } from 'lit'
import { DsElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'
// Label.tsx as <spyglass-label>: a field's label on its own, DM Sans Medium in capitals with 6% tracking at 12px in the
// soft gray, over or beside the control it names; `hint` adds a muted note after it, such as "Optional". A <label for>
// in a shadow root cannot reach a control outside it, so `for` names the control's id in the same document (or shadow
// root) and the element does what the label would: it gives that control its text as aria-label (an element such as
// <spyglass-input> passes it to its inner field) and a press on it focuses and presses the control.
/**
* @summary A label for one control.
* @slot - The label's text.
*/
export class Label extends DsElement {
static styles = [DsElement.styles, styles]
static properties = { htmlFor: { attribute: 'for' }, hint: {} }
/** The id of the control it names (the `for` attribute). */
htmlFor?: string
/** A muted note after the label, such as "Optional". */
hint?: string
private target() {
if (!this.htmlFor) return null
const root = this.getRootNode() as Document | ShadowRoot
return root.getElementById?.(this.htmlFor) ?? null
}
private name() {
return [this.textContent?.replace(/\s+/g, ' ').trim(), this.hint].filter(Boolean).join(' ')
}
protected updated(changed: PropertyValues) {
super.updated(changed)
this.target()?.setAttribute('aria-label', this.name())
}
connectedCallback() {
super.connectedCallback()
// The control may come after the label in the page: name it once the page around is in place.
queueMicrotask(() => this.requestUpdate())
}
private onClick() {
const t = this.target()
if (!t) return
// A press on a label focuses its control as a native label does: a field you type in shows its focus (browsers
// match :focus-visible on text fields however they are focused); a checkbox, switch or radio gets no ring.
const text = t.matches('spyglass-input, spyglass-textarea, textarea, input:not([type=checkbox], [type=radio], [type=range], [type=button], [type=submit], [type=reset], [type=color], [type=file])')
t.focus({ focusVisible: text } as FocusOptions)
t.click()
}
render() {
return html`<label data-ds="label" class="inline-flex items-baseline gap-1.5 type-label text-fg-soft" @click=${this.onClick}><slot @slotchange=${() => this.requestUpdate()}></slot>${this.hint ? html`<span class="text-fg-muted">${this.hint}</span>` : nothing}</label>`
}
}
define('label', Label)Reference: Derived from the brand guidelines: labels in DM Sans Medium, capitals, 6% letter spacing (type page), the neutral steps for the gray.