Components

Native Select

The browser's own select, drawn as the input: a #666666 hairline on Black that turns White on focus, the brand corner and a muted chevron. Phones open the system picker.

Install

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

npx spyglass-ds add native-select
<script type="module" src="/spyglass-ds/elements/native-select.js"></script>
Filters
Sizes

Usage

A pick of one from a short, plain list in a form or a filter row: a platform, a date range, a sort order.

Never

  • A list that needs search or custom rows: use Select
  • Options that do something at once: use Dropdown Menu
  • A filled or gray field on Black

Adds with it: packages lucide-react

Props

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

<NativeSelect>

PropTypeDefault
sizeNativeSelectSize

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/native-select/Filters.tsx
import { NativeSelect } from '@/react/components/ui/NativeSelect'
// Two filters over a search: where the ads ran and how long ago.

export default function Filters() {
  return (
    <div className="grid w-full max-w-120 gap-4 sm:grid-cols-2">
      <div className="flex flex-col gap-2">
        <label htmlFor="native-select-platform" className="type-label text-fg-soft">Platform</label>
        <NativeSelect id="native-select-platform" name="platform" defaultValue="meta">
          <option value="all">All platforms</option>
          <option value="meta">Meta</option>
          <option value="tiktok">TikTok</option>
          <option value="youtube">YouTube</option>
        </NativeSelect>
      </div>
      <div className="flex flex-col gap-2">
        <label htmlFor="native-select-range" className="type-label text-fg-soft">Running for</label>
        <NativeSelect id="native-select-range" name="range" defaultValue="30">
          <option value="7">Over 7 days</option>
          <option value="30">Over 30 days</option>
          <option value="90">Over 90 days</option>
        </NativeSelect>
      </div>
    </div>
  )
}
elements/examples/native-select/Filters.html
<!-- Two filters over a search: where the ads ran and how long ago. -->
<div class="grid w-full max-w-120 gap-4 sm:grid-cols-2">
  <div class="flex flex-col gap-2">
    <label for="native-select-platform" class="type-label text-fg-soft">Platform</label>
    <spyglass-native-select id="native-select-platform" name="platform" value="meta" aria-label="Platform">
      <option value="all">All platforms</option>
      <option value="meta">Meta</option>
      <option value="tiktok">TikTok</option>
      <option value="youtube">YouTube</option>
    </spyglass-native-select>
  </div>
  <div class="flex flex-col gap-2">
    <label for="native-select-range" class="type-label text-fg-soft">Running for</label>
    <spyglass-native-select id="native-select-range" name="range" value="30" aria-label="Running for">
      <option value="7">Over 7 days</option>
      <option value="30">Over 30 days</option>
      <option value="90">Over 90 days</option>
    </spyglass-native-select>
  </div>
</div>
react/examples/native-select/Sizes.tsx
import { NativeSelect } from '@/react/components/ui/NativeSelect'
// The three sizes, matching the input's and the button's.

export default function Sizes() {
  return (
    <div className="flex w-64 max-w-full flex-col gap-3">
      <NativeSelect size="sm" aria-label="Sort, small" defaultValue="spend"><option value="spend">Sort by spend</option><option value="new">Newest first</option></NativeSelect>
      <NativeSelect size="md" aria-label="Sort, medium" defaultValue="spend"><option value="spend">Sort by spend</option><option value="new">Newest first</option></NativeSelect>
      <NativeSelect size="lg" aria-label="Sort, large" defaultValue="spend"><option value="spend">Sort by spend</option><option value="new">Newest first</option></NativeSelect>
    </div>
  )
}
elements/examples/native-select/Sizes.html
<!-- The three sizes, matching the input's and the button's. -->
<div class="flex w-64 max-w-full flex-col gap-3">
  <spyglass-native-select size="sm" aria-label="Sort, small" value="spend"><option value="spend">Sort by spend</option><option value="new">Newest first</option></spyglass-native-select>
  <spyglass-native-select size="md" aria-label="Sort, medium" value="spend"><option value="spend">Sort by spend</option><option value="new">Newest first</option></spyglass-native-select>
  <spyglass-native-select size="lg" aria-label="Sort, large" value="spend"><option value="spend">Sort by spend</option><option value="new">Newest first</option></spyglass-native-select>
</div>

Source

react/components/ui/NativeSelect.tsx
import type { Ref, SelectHTMLAttributes } from 'react'
import { ChevronDown } from 'lucide-react'
import { cn } from '@/react/lib/cn'

// The browser's own select, drawn as the Input is: a #666666 hairline on Black that turns White on focus, DM Sans at
// 14px (16px on phones, where the system picker opens), the brand corner, and its own 16px chevron in the muted gray,
// the same in every browser. Sizes follow the input's: sm 32px, md 40px, lg 48px. Options go in as <option> children.
// For a list with search or custom rows, use Select. `className` lands on the wrapper, every other prop on the
// <select> (value and onChange as React's own).
export type NativeSelectSize = 'sm' | 'md' | 'lg'

const SIZES: Record<NativeSelectSize, string> = { sm: 'h-control-sm pr-8 pl-2.5', md: 'h-control-md pr-9 pl-3', lg: 'h-control-lg pr-10 pl-4' }
const CHEVRON: Record<NativeSelectSize, string> = { sm: 'right-2.5', md: 'right-3', lg: 'right-4' }

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

export function NativeSelect({ size = 'md', invalid, className, children, ...rest }: NativeSelectProps) {
  return (
    <span data-ds="native-select" className={cn('relative inline-flex w-full items-center', className)}>
      <select {...rest} aria-invalid={invalid || undefined} className={cn('w-full min-w-0 cursor-pointer appearance-none rounded-control border border-edge-strong bg-surface-subtle type-ui text-fg outline-none transition-[border-color] duration-160 ease-standard hover:not-disabled: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])}>{children}</select>
      <ChevronDown size={16} strokeWidth={1.5} aria-hidden="true" className={cn('pointer-events-none absolute text-fg-muted', CHEVRON[size])} />
    </span>
  )
}

export default NativeSelect
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/native-select.ts
import { html, LitElement, type PropertyValues } from 'lit'
import { ChevronDown } from 'lucide'
import { ifDefined } from 'lit/directives/if-defined.js'
import { DsFormElement, define } from '@/elements/lib/element'
import { icon } from '@/elements/lib/icon'
import styles from 'virtual:tw'

// NativeSelect.tsx as <spyglass-native-select>: the browser's own select, drawn as the input is: a #666666 hairline on
// Black that turns White on focus, DM Sans at 14px (16px on phones), the brand corner and its own 16px chevron in the
// muted gray. Sizes sm, md and lg. Options go inside as <option>s: a <select> takes only its own children, so the
// element draws a copy of them in its shadow root and follows changes to them. The host is the box (React's wrapper
// span), so a class on the element lands on it. 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 select.

const SIZES = { sm: 'h-control-sm pr-8 pl-2.5', md: 'h-control-md pr-9 pl-3', lg: 'h-control-lg pr-10 pl-4' } as const
const CHEVRON = { sm: 'right-2.5', md: 'right-3', lg: 'right-4' } as const
export type NativeSelectSize = keyof typeof SIZES

/**
 * @summary The browser's own select, drawn as the input.
 * @slot - The <option>s (copied into the select).
 * @fires input - The pick changed (read `value`).
 * @fires change - The pick changed (read `value`).
 */
export class NativeSelect extends DsFormElement {
	static styles = [DsFormElement.styles, styles]
	static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true }
	static dsField = true
	static properties = {
		value: {},
		name: {},
		size: { reflect: true },
		disabled: { type: Boolean, reflect: true },
		required: { type: Boolean },
		invalid: { type: Boolean, reflect: true },
		label: { attribute: 'aria-label' },
		description: { attribute: 'aria-description' },
	}

	/** The picked option's value (the first option's when unset, as a select's). */
	value?: string
	name?: string
	/** sm (32px), md (40px) or lg (48px). */
	size: NativeSelectSize = 'md'
	disabled = false
	required = false
	/** A red hairline (with the message beside it). */
	invalid = false
	/** The name for assistive tech, passed to the inner select. */
	label?: string
	/** The description or error, passed to the inner select (a field sets it). */
	description?: string
	private observer = new MutationObserver(() => this.requestUpdate())

	connectedCallback() {
		super.connectedCallback()
		this.observer.observe(this, { childList: true, subtree: true, characterData: true, attributes: true })
	}
	disconnectedCallback() {
		super.disconnectedCallback()
		this.observer.disconnect()
	}

	private get select() {
		return this.renderRoot.querySelector('select')
	}
	protected updated(changed: PropertyValues) {
		super.updated(changed)
		const select = this.select
		if (!select) return
		// The options render after the select's own bindings, so the value goes on once they are there.
		if (this.value === undefined) this.value = select.value
		else if (select.value !== this.value) select.value = this.value
		this.internals.setFormValue(this.value ?? null)
		this.syncValidity()
	}
	protected validityControl() {
		return this.renderRoot.querySelector<HTMLSelectElement>('select')
	}
	formResetCallback() {
		this.value = this.getAttribute('value') ?? undefined
	}
	private onInput(event: Event) {
		this.value = (event.target as HTMLSelectElement).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 size = SIZES[this.size] ? this.size : 'md'
		const options = [...this.querySelectorAll('option')]
		const classes = ['w-full min-w-0 cursor-pointer appearance-none rounded-control border border-edge-strong bg-surface-subtle type-ui text-fg outline-none transition-[border-color] duration-160 ease-standard hover:not-disabled: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`<select name=${ifDefined(this.name)} ?disabled=${this.inactive} ?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}>${options.map((o) => html`<option value=${o.value} ?disabled=${o.disabled}>${o.text}</option>`)}</select>${icon(ChevronDown, 'chevron-down', { size: 16, strokeWidth: 1.5, class: `pointer-events-none absolute text-fg-muted ${CHEVRON[size]}`, attrs: { 'aria-hidden': 'true' } })}`
	}
}

define('native-select', NativeSelect)
elements/components/ui/native-select.css
/* The host is the box (NativeSelect.tsx's wrapper span), so a class on the element (a width) lands on it. */
:host { @apply relative inline-flex w-full items-center; }

Reference: Derived from the brand guidelines (no select in the brand assets): the input's hairline, fill and corner, the neutral steps for the chevron.