Components

Textarea

Several lines of text, drawn as the input is: a #666666 hairline on Black that turns White on focus, DM Sans, the brand corner. It grows by hand downward.

Install

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

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

The team sees this at the top of the board.

Invalid

Keep the note under 80 characters.

Usage

Text longer than a line: a board brief, a note on a creative, feedback. Label it, and say under it what the text is for.

Never

  • A question to search: use the search bar
  • A red hairline before anything is wrong
  • Under 16px on phones (it switches to 16px there)

Props

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

<Textarea>

PropTypeDefault
invalidboolean

A red hairline (with the message under it).

rowsnumber

Visible lines before it scrolls (4).

4

Examples

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

react/examples/textarea/Brief.tsx
import { Textarea } from '@/react/components/ui/Textarea'
// A brief for a new board: a few lines about what to collect, with a line of help under it.

export default function Brief() {
  return (
    <div className="flex w-96 max-w-full flex-col gap-2">
      <label htmlFor="textarea-brief" className="type-label text-fg-soft">Board brief</label>
      <Textarea id="textarea-brief" name="brief" placeholder="Hooks that open on the product in the first second." />
      <p className="type-caption text-fg-muted">The team sees this at the top of the board.</p>
    </div>
  )
}
elements/examples/textarea/Brief.html
<!-- A brief for a new board: a few lines about what to collect, with a line of help under it. -->
<div class="flex w-96 max-w-full flex-col gap-2">
  <label for="textarea-brief" class="type-label text-fg-soft">Board brief</label>
  <spyglass-textarea id="textarea-brief" name="brief" placeholder="Hooks that open on the product in the first second." aria-label="Board brief"></spyglass-textarea>
  <p class="type-caption text-fg-muted">The team sees this at the top of the board.</p>
</div>
react/examples/textarea/Invalid.tsx
import { Textarea } from '@/react/components/ui/Textarea'
// A note that is too long, with the message that says so.

export default function Invalid() {
  return (
    <div className="flex w-96 max-w-full flex-col gap-2">
      <label htmlFor="textarea-note" className="type-label text-fg-soft">Note</label>
      <Textarea id="textarea-note" name="note" rows={3} invalid aria-describedby="textarea-note-error" defaultValue="Every Acme ad from the last year that opens on a face, a product shot or a number, sorted by spend." />
      <p id="textarea-note-error" className="type-caption text-fg-danger">Keep the note under 80 characters.</p>
    </div>
  )
}
elements/examples/textarea/Invalid.html
<!-- A note that is too long, with the message that says so. -->
<div class="flex w-96 max-w-full flex-col gap-2">
  <label for="textarea-note" class="type-label text-fg-soft">Note</label>
  <spyglass-textarea id="textarea-note" name="note" rows="3" invalid aria-label="Note" aria-description="Keep the note under 80 characters." value="Every Acme ad from the last year that opens on a face, a product shot or a number, sorted by spend."></spyglass-textarea>
  <p id="textarea-note-error" class="type-caption text-fg-danger">Keep the note under 80 characters.</p>
</div>

Source

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

// Several lines of text, drawn as the Input is: a #666666 hairline on Black that turns White on focus, DM Sans at 14px
// (16px on phones, so iOS does not zoom), a #A3A3A3 placeholder, the brand corner, 10px 12px inside. It grows by hand
// downward only. `invalid` turns the hairline red. Put it in a Field for its label and description. Every other prop
// lands on the <textarea> (value and onChange as React's own).
export interface TextareaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
  /** A red hairline (with the message under it). */
  invalid?: boolean
  /** Visible lines before it scrolls (4). */
  rows?: number
  ref?: Ref<HTMLTextAreaElement>
}

export function Textarea({ invalid, rows = 4, className, ...rest }: TextareaProps) {
  return (
    <textarea
      data-ds="textarea"
      {...rest}
      rows={rows}
      aria-invalid={invalid || undefined}
      className={cn('block w-full min-w-0 resize-y rounded-control border border-edge-strong bg-surface-subtle px-3 py-2.5 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', className)}
    />
  )
}

export default Textarea
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/textarea.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'

// Textarea.tsx as <spyglass-textarea>: several lines of text, drawn as the input is: a #666666 hairline on Black that
// turns White on focus, DM Sans at 14px (16px on phones), a #A3A3A3 placeholder and the brand corner. It grows by hand
// downward only; `invalid` turns the hairline red. Form-associated (its `name` and `value` go with its form); the value
// is `value` with the native `input` and `change` events. aria-label and aria-description pass to the inner textarea.

/**
 * @summary Several lines of text.
 * @fires input - On every keystroke (read `value`).
 * @fires change - When the value is committed.
 */
export class Textarea 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>('textarea'))
	static properties = {
		value: {},
		rows: { type: Number },
		name: {},
		placeholder: {},
		maxlength: { type: Number },
		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 = ''
	/** Visible lines before it scrolls. */
	rows = 4
	name?: string
	placeholder?: string
	maxlength?: number
	disabled = false
	readonly = false
	required = false
	/** A red hairline (with the message under it). */
	invalid = false
	/** The name for assistive tech, passed to the inner textarea. */
	label?: string
	/** The description or error, passed to the inner textarea (a field sets it). */
	description?: string

	connectedCallback() {
		super.connectedCallback()
		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)
	}
	// Setting the value as a property puts the caret (and the scroll) at the end; a native textarea starts at the top.
	protected firstUpdated(changed: PropertyValues) {
		super.firstUpdated(changed)
		const area = this.validityControl()
		if (!area) return
		area.setSelectionRange(0, 0)
		area.scrollTop = 0
	}
	protected validityControl() {
		return this.renderRoot.querySelector<HTMLTextAreaElement>('textarea')
	}
	formResetCallback() {
		this.value = this.getAttribute('value') ?? ''
	}
	private onInput(event: Event) {
		this.value = (event.target as HTMLTextAreaElement).value
	}
	// The inner change does not leave the shadow root: the element sends its own.
	private onChange() {
		this.dispatchEvent(new Event('change', { bubbles: true, composed: true }))
	}

	render() {
		const classes = 'block w-full min-w-0 resize-y rounded-control border border-edge-strong bg-surface-subtle px-3 py-2.5 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'
		return html`<textarea data-ds="textarea" .value=${live(this.value)} rows=${this.rows} name=${ifDefined(this.name)} placeholder=${ifDefined(this.placeholder)} maxlength=${ifDefined(this.maxlength)} ?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}></textarea>`
	}
}

define('textarea', Textarea)

Reference: Derived from the brand guidelines (no text area in the brand assets): the input's hairline, fill and corner.