Components

Input

A text field on Black: a #666666 hairline that turns White on focus, DM Sans, a #A3A3A3 placeholder and the brand corner. Sizes match the button's.

Install

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

npx spyglass-ds add input
<script type="module" src="/spyglass-ds/elements/input.js"></script>
With a label

Add any brand or creator in under a minute.

Sizes
Invalid

Enter an email like team@acme.com.

Usage

Free text in forms and filters: an email, a brand or creator to track, a board name. Label every field (a visible label, or aria-label).

Never

  • A placeholder as the only label
  • A colored or glowing focus ring: the hairline turns White
  • A filled or gray field on Black

Props

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

<Input>

PropTypeDefault
sizeInputSize

sm (32px), md (40px) or lg (48px).

'md'
invalidboolean

A red hairline (with the message beside it).

Examples

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

react/examples/input/Field.tsx
import { Input } from '@/react/components/ui/Input'
// A labeled field with a line of help under it.

export default function Field() {
  return (
    <div className="flex w-80 max-w-full flex-col gap-2">
      <label htmlFor="track-brand" className="type-label text-fg-soft">Brand or creator</label>
      <Input id="track-brand" name="brand" placeholder="Acme" />
      <p className="type-caption text-fg-muted">Add any brand or creator in under a minute.</p>
    </div>
  )
}
elements/examples/input/Field.html
<!-- A labeled field with a line of help under it. -->
<div class="flex w-80 max-w-full flex-col gap-2">
  <label for="track-brand" class="type-label text-fg-soft">Brand or creator</label>
  <spyglass-input id="track-brand" name="brand" placeholder="Acme" aria-label="Brand or creator"></spyglass-input>
  <p class="type-caption text-fg-muted">Add any brand or creator in under a minute.</p>
</div>
react/examples/input/Sizes.tsx
import { Input } from '@/react/components/ui/Input'
// sm, md and lg, matching the buttons beside them.

export default function Sizes() {
  return (
    <div className="flex w-80 max-w-full flex-col gap-3">
      <Input size="sm" aria-label="Board name, small" placeholder="Acme launch board" />
      <Input size="md" aria-label="Board name" placeholder="Acme launch board" />
      <Input size="lg" aria-label="Board name, large" placeholder="Acme launch board" />
    </div>
  )
}
elements/examples/input/Sizes.html
<!-- sm, md and lg, matching the buttons beside them. -->
<div class="flex w-80 max-w-full flex-col gap-3">
  <spyglass-input size="sm" aria-label="Board name, small" placeholder="Acme launch board"></spyglass-input>
  <spyglass-input size="md" aria-label="Board name" placeholder="Acme launch board"></spyglass-input>
  <spyglass-input size="lg" aria-label="Board name, large" placeholder="Acme launch board"></spyglass-input>
</div>
react/examples/input/Invalid.tsx
import { Input } from '@/react/components/ui/Input'
// An invalid field: the hairline turns red and the message says what to do.

export default function Invalid() {
  return (
    <div className="flex w-80 max-w-full flex-col gap-2">
      <label htmlFor="work-email" className="type-label text-fg-soft">Work email</label>
      <Input id="work-email" type="email" defaultValue="team@acme" invalid aria-describedby="work-email-error" />
      <p id="work-email-error" className="type-caption text-fg-danger">Enter an email like team@acme.com.</p>
    </div>
  )
}
elements/examples/input/Invalid.html
<!-- An invalid field: the hairline turns red and the message says what to do. -->
<div class="flex w-80 max-w-full flex-col gap-2">
  <label for="work-email" class="type-label text-fg-soft">Work email</label>
  <spyglass-input id="work-email" type="email" value="team@acme" invalid aria-label="Work email" aria-description="Enter an email like team@acme.com."></spyglass-input>
  <p id="work-email-error" class="type-caption text-fg-danger">Enter an email like team@acme.com.</p>
</div>

Source

react/components/ui/Input.tsx
import type { InputHTMLAttributes, Ref } from 'react'
import { cn } from '@/react/lib/cn'

// A text field on Black: a #666666 hairline that turns White on focus, DM Sans at 14px (16px on phones, so iOS does
// not zoom), a #A3A3A3 placeholder and the brand corner. Sizes follow the button's: sm 32px, md 40px, lg 48px.
// `invalid` turns the hairline red. Every other prop lands on the <input> (value and onChange as React's own).
export type InputSize = 'sm' | 'md' | 'lg'

const SIZES: Record<InputSize, string> = { sm: 'h-control-sm px-2.5', md: 'h-control-md px-3', lg: 'h-control-lg px-4' }

export interface InputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'size'> {
  /** sm (32px), md (40px) or lg (48px). */
  size?: InputSize
  /** A red hairline (with the message beside it). */
  invalid?: boolean
  ref?: Ref<HTMLInputElement>
}

export function Input({ size = 'md', invalid, className, ...rest }: InputProps) {
  return (
    <input
      data-ds="input"
      {...rest}
      aria-invalid={invalid || undefined}
      className={cn('w-full min-w-0 rounded-control border border-edge-strong bg-surface-subtle type-ui text-fg outline-none transition-[border-color] duration-160 ease-standard placeholder:text-fg-muted hover:not-disabled:not-focus-visible:border-action-secondary-edge-hover focus-visible:border-edge-focus aria-invalid:border-action-danger disabled:cursor-not-allowed disabled:text-fg-faint max-md:text-input pointer-coarse:text-input', SIZES[size], className)}
    />
  )
}

export default Input
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/input.ts
import { html, LitElement, type PropertyValues } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { live } from 'lit/directives/live.js'
import { DsFormElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'

// Input.tsx as <spyglass-input>: a text field on Black, a #666666 hairline that turns White on focus, DM Sans at 14px
// (16px on phones), a #A3A3A3 placeholder and the brand corner. Sizes sm, md and lg; `invalid` turns the hairline red.
// It is form-associated (its `name` and `value` go with its form) and its value is `value` with the native `input`
// and `change` events. aria-label, aria-description and aria-describedby's text pass to the inner <input>.

const SIZES = { sm: 'h-control-sm px-2.5', md: 'h-control-md px-3', lg: 'h-control-lg px-4' } as const
export type InputSize = keyof typeof SIZES

/**
 * @summary A text field.
 * @fires input - On every keystroke (read `value`).
 * @fires change - When the value is committed.
 */
export class Input extends DsFormElement {
	static styles = [DsFormElement.styles, styles]
	static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true }
	static dsField = true
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('input'))
	static properties = {
		value: {},
		type: {},
		name: {},
		placeholder: {},
		autocomplete: {},
		size: { reflect: true },
		disabled: { type: Boolean, reflect: true },
		readonly: { type: Boolean },
		required: { type: Boolean },
		invalid: { type: Boolean, reflect: true },
		label: { attribute: 'aria-label' },
		description: { attribute: 'aria-description' },
	}

	value = ''
	type = 'text'
	name?: string
	placeholder?: string
	autocomplete?: string
	/** sm (32px), md (40px) or lg (48px). */
	size: InputSize = 'md'
	disabled = false
	readonly = false
	required = false
	/** A red hairline (with the message beside it). */
	invalid = false
	/** The name for assistive tech, passed to the inner input. */
	label?: string
	/** The hint or error, passed to the inner input. */
	description?: string

	connectedCallback() {
		super.connectedCallback()
		// The initial value from the attribute, as an <input value> gives it.
		if (!this.value && this.getAttribute('value')) this.value = this.getAttribute('value')!
	}
	protected updated(changed: PropertyValues) {
		super.updated(changed)
		if (changed.has('value')) this.internals.setFormValue(this.value)
	}
	protected validityControl() {
		return this.renderRoot.querySelector<HTMLInputElement>('input')
	}
	formResetCallback() {
		this.value = this.getAttribute('value') ?? ''
	}
	private onInput(event: Event) {
		this.value = (event.target as HTMLInputElement).value
	}
	// Enter submits its form, as Enter in a native field does.
	private onKeyDown(event: KeyboardEvent) {
		if (event.key === 'Enter' && !event.isComposing && this.form) {
			event.preventDefault()
			this.form.requestSubmit()
		}
	}
	private onChange() {
		this.dispatchEvent(new Event('change', { bubbles: true, composed: true }))
	}

	render() {
		const size = SIZES[this.size] ? this.size : 'md'
		const classes = ['w-full min-w-0 rounded-control border border-edge-strong bg-surface-subtle type-ui text-fg outline-none transition-[border-color] duration-160 ease-standard placeholder:text-fg-muted hover:not-disabled:not-focus-visible:border-action-secondary-edge-hover focus-visible:border-edge-focus aria-invalid:border-action-danger disabled:cursor-not-allowed disabled:text-fg-faint max-md:text-input pointer-coarse:text-input', SIZES[size]].join(' ')
		return html`<input data-ds="input" .value=${live(this.value)} type=${this.type} name=${ifDefined(this.name)} placeholder=${ifDefined(this.placeholder)} autocomplete=${ifDefined(this.autocomplete as never)} ?disabled=${this.inactive} ?readonly=${this.readonly} ?required=${this.required} aria-invalid=${ifDefined(this.invalid ? 'true' : undefined)} aria-label=${ifDefined(this.label)} aria-description=${ifDefined(this.description)} class=${classes} @input=${this.onInput} @change=${this.onChange} @keydown=${this.onKeyDown}>`
	}
}

define('input', Input)

Reference: Derived from the brand guidelines (no field in the brand assets): the search bar's Black field and corner, the neutral steps for the hairline and placeholder.