Components
Field
A label over one control, with a description or an error under it. The label speaks in capitals; the help is muted; an error takes the help's place in red.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add field<script type="module" src="/spyglass-ds/elements/field.js"></script>Add any brand or creator in under a minute.
Use your work email, such as you@acme.com.
Usage
Every labeled control in a form: an email, a board name, a note, a pick from a list. One Field per control.
Never
- A placeholder in place of the label
- A red hairline before anything is wrong: errors show once a field is left or the form is sent
- Fields under 16px on phones (the input switches to 16px there)
Props
The web component takes the same as attributes or properties (<spyglass-field>); title is heading there.
<Field>
| Prop | Type | Default |
|---|---|---|
label | string The label over the control. | |
description | string A line under the control: what to type, or what happens next. | |
error | string What is wrong, in the description's place (the control turns red). | |
id | string The control's id (generated when not given). | |
children | ReactNode | ((slot: FieldSlot) => ReactNode) |
Examples
The same example in React and as web components; both render identically.
import { Field } from '@/react/components/ui/Field'
import { Input } from '@/react/components/ui/Input'
// A field with a line of help under it, and one that says what is wrong.
export default function Fields() {
return (
<div className="flex w-80 max-w-full flex-col gap-6">
<Field label="Brand or creator" description="Add any brand or creator in under a minute.">
{({ id, describedBy, invalid }) => <Input id={id} name="brand" placeholder="Acme" aria-describedby={describedBy} invalid={invalid} />}
</Field>
<Field label="Work email" error="Use your work email, such as you@acme.com.">
{({ id, describedBy, invalid }) => <Input id={id} name="email" type="email" defaultValue="acme" aria-describedby={describedBy} invalid={invalid} />}
</Field>
</div>
)
}<!-- A field with a line of help under it, and one that says what is wrong. -->
<div class="flex w-80 max-w-full flex-col gap-6">
<spyglass-field label="Brand or creator" description="Add any brand or creator in under a minute.">
<spyglass-input name="brand" placeholder="Acme"></spyglass-input>
</spyglass-field>
<spyglass-field label="Work email" error="Use your work email, such as you@acme.com.">
<spyglass-input name="email" type="email" value="acme"></spyglass-input>
</spyglass-field>
</div>Source
import { useId, type HTMLAttributes, type ReactNode } from 'react'
import { cn } from '@/react/lib/cn'
// A label over one control, with a description or an error under it: the label in the brand's label voice (DM Sans
// Medium, capitals, 6% tracking) in the soft gray, the description in the muted gray at caption size, the error in the
// danger red in its place, 8px apart. children may be a function that receives the ids to wire:
// <Field label="Email">{({ id, describedBy, invalid }) => <Input id={id} aria-describedby={describedBy} invalid={invalid} />}</Field>;
// plain children work too (give the control the field's `id`).
export interface FieldSlot { id: string, describedBy?: string, invalid: boolean }
export interface FieldProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
/** The label over the control. */
label: string
/** A line under the control: what to type, or what happens next. */
description?: string
/** What is wrong, in the description's place (the control turns red). */
error?: string
/** The control's id (generated when not given). */
id?: string
children?: ReactNode | ((slot: FieldSlot) => ReactNode)
}
export function Field({ label, description, error, id: given, className, children, ...rest }: FieldProps) {
const auto = useId()
const id = given ?? `field-${auto}`
const describedBy = description || error ? `${id}-description` : undefined
return (
<div data-ds="field" className={cn('flex flex-col gap-2', className)} {...rest}>
<label htmlFor={id} className="type-label text-fg-soft">{label}</label>
{typeof children === 'function' ? children({ id, describedBy, invalid: !!error }) : children}
{(error || description) && <p id={describedBy} className={cn('type-caption', error ? 'text-fg-danger' : 'text-fg-muted')} role={error ? 'alert' : undefined}>{error || description}</p>}
</div>
)
}
export default Field// 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 } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { DsElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'
// Field.tsx as <spyglass-field>: a label over one control, with a description or an error under it: the label in the
// brand's label voice in the soft gray, the description in the muted gray at caption size, the error in the danger red
// in its place. React's render function hands the control its ids; here the field wires the control it holds: the
// label names it (aria-label) and a press on the label focuses it, the description or error describes it
// (aria-description), and an error sets its `invalid`.
/**
* @summary A label over one control, with a description or an error under it.
* @slot - The control: <spyglass-input>, <spyglass-textarea>, <spyglass-native-select>, or any input.
*/
export class Field extends DsElement {
static styles = [DsElement.styles, styles]
static properties = { label: {}, description: {}, error: {} }
/** The label over the control. */
label = ''
/** A line under the control: what to type, or what happens next. */
description?: string
/** What is wrong, in the description's place (the control turns red). */
error?: string
private control() {
return [...this.children].find((c) => !c.hasAttribute('slot')) as (HTMLElement & { invalid?: boolean }) | undefined
}
protected updated() {
const c = this.control()
if (!c) return
c.setAttribute('aria-label', this.label)
const text = this.error || this.description
if (text) c.setAttribute('aria-description', text)
else c.removeAttribute('aria-description')
if ('invalid' in c) c.invalid = !!this.error
}
render() {
const text = this.error || this.description
return html`<label class="type-label text-fg-soft" @click=${() => this.control()?.focus()}>${this.label}</label>
<slot @slotchange=${() => this.updated()}></slot>
${text ? html`<p class=${`type-caption ${this.error ? 'text-fg-danger' : 'text-fg-muted'}`} role=${ifDefined(this.error ? 'alert' : undefined)}>${text}</p>` : nothing}`
}
}
define('field', Field)/* The host is the field's box (Field.tsx's root div), so a class on <spyglass-field> (a width, a margin) lands on it as
a class on <Field> does. */
:host { @apply flex flex-col gap-2; }Reference: Derived from the brand guidelines (no form in the brand assets): the label voice from the type page, the neutral steps for help text, the red tints for errors.