Components

Resizable

Two panes and the #363636 hairline between them. The hairline is the handle: it turns White under the pointer, while dragged and when focused, and can carry a small grip. Arrow keys move it too. A pane that overflows scrolls with fading edges.

Install

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

npx spyglass-ds add resizable
<script type="module" src="/spyglass-ds/elements/resizable.js"></script>
Side by side, with a grip

Boards

Acme hooks

Acme summer launch

UGC openers

Creative

Acme ad, live for 42 days.

One over the other

Results

36 creatives match Acme hooks.

Notes

Open on the product, in use.

Usage

A list beside the thing that is open (boards and a creative), or results over notes, where people want to choose how much room each gets.

Never

  • A thick or colored divider: the hairline is the handle
  • Panes that can close to nothing: set a minimum
  • Easing on the drag: the split follows the hand

Props

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

<Resizable>

PropTypeDefault
direction'horizontal' | 'vertical'

horizontal (side by side) or vertical (one over the other).

'horizontal'
minnumber

The first pane's smallest share, in percent.

20
maxnumber

The first pane's largest share, in percent.

80
labelstring

The handle's name for assistive tech.

'Resize panes'
valuenumber

The first pane's share, in percent.

defaultValuenumber50
onValueChange(size: number) => void
withHandleboolean

A small grip on the hairline.

firstReactNode

The first pane (left, or top when vertical).

secondReactNode

The second pane.

classNamestring

Examples

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

react/examples/resizable/Panes.tsx
import { Resizable } from '@/react/components/ui/Resizable'
// Boards beside the creative that is open, with a grip on the line between them.

export default function Panes() {
  return (
    <Resizable
      className="h-64 w-full max-w-xl rounded-container border border-edge"
      label="Resize boards and creative"
      defaultValue={40}
      withHandle
      first={
        <div className="flex h-full flex-col gap-2 p-4">
          <p className="type-label text-fg-soft">Boards</p>
          <p className="type-ui text-fg">Acme hooks</p>
          <p className="type-ui text-fg-muted">Acme summer launch</p>
          <p className="type-ui text-fg-muted">UGC openers</p>
        </div>
      }
      second={
        <div className="flex h-full flex-col gap-3 p-4">
          <p className="type-label text-fg-soft">Creative</p>
          <div className="flex-1 rounded-control bg-surface-raised" />
          <p className="type-caption text-fg-muted">Acme ad, live for 42 days.</p>
        </div>
      }
    />
  )
}
elements/examples/resizable/Panes.html
<!-- Boards beside the creative that is open, with a grip on the line between them. -->
<spyglass-resizable class="h-64 w-full max-w-xl rounded-container border border-edge" label="Resize boards and creative" value="40" with-handle>
  <div slot="first" class="flex h-full flex-col gap-2 p-4">
    <p class="type-label text-fg-soft">Boards</p>
    <p class="type-ui text-fg">Acme hooks</p>
    <p class="type-ui text-fg-muted">Acme summer launch</p>
    <p class="type-ui text-fg-muted">UGC openers</p>
  </div>
  <div slot="second" class="flex h-full flex-col gap-3 p-4">
    <p class="type-label text-fg-soft">Creative</p>
    <div class="flex-1 rounded-control bg-surface-raised"></div>
    <p class="type-caption text-fg-muted">Acme ad, live for 42 days.</p>
  </div>
</spyglass-resizable>
react/examples/resizable/Vertical.tsx
import { Resizable } from '@/react/components/ui/Resizable'
// A search over its notes, one above the other; the bare hairline is the handle.

export default function Vertical() {
  return (
    <Resizable
      direction="vertical"
      className="h-72 w-full max-w-md rounded-container border border-edge"
      label="Resize results and notes"
      defaultValue={60}
      first={<div className="p-4"><p className="type-label text-fg-soft">Results</p><p className="mt-2 type-ui text-fg">36 creatives match Acme hooks.</p></div>}
      second={<div className="p-4"><p className="type-label text-fg-soft">Notes</p><p className="mt-2 type-ui text-fg-muted">Open on the product, in use.</p></div>}
    />
  )
}
elements/examples/resizable/Vertical.html
<!-- A search over its notes, one above the other; the bare hairline is the handle. -->
<spyglass-resizable direction="vertical" class="h-72 w-full max-w-md rounded-container border border-edge" label="Resize results and notes" value="60">
  <div slot="first" class="p-4"><p class="type-label text-fg-soft">Results</p><p class="mt-2 type-ui text-fg">36 creatives match Acme hooks.</p></div>
  <div slot="second" class="p-4"><p class="type-label text-fg-soft">Notes</p><p class="mt-2 type-ui text-fg-muted">Open on the product, in use.</p></div>
</spyglass-resizable>

Source

react/components/ui/Resizable.tsx
'use client'
import { useId, useRef, useState, type KeyboardEvent, type PointerEvent, type ReactNode } from 'react'
import { useControllable } from '@/react/hooks/useControllable'
import { cn } from '@/react/lib/cn'

// Two panes and the #363636 hairline between them. The hairline is the handle, with a 12px reach either side (20px
// on touch); it turns White under the pointer, while dragged and when focused (160ms). `withHandle` adds a small grip
// on it in the card's fill. Drag it, or focus it and use the arrow keys (Shift for bigger steps, Home and End for the
// limits). The split follows the hand with no easing. The value is the first pane's share in percent, held between
// `min` and `max` (`value` with `onValueChange`, or `defaultValue` alone). The panes go in `first` and `second`.
export interface ResizableProps {
  /** horizontal (side by side) or vertical (one over the other). */
  direction?: 'horizontal' | 'vertical'
  /** The first pane's smallest share, in percent. */
  min?: number
  /** The first pane's largest share, in percent. */
  max?: number
  /** The handle's name for assistive tech. */
  label?: string
  /** The first pane's share, in percent. */
  value?: number
  defaultValue?: number
  onValueChange?: (size: number) => void
  /** A small grip on the hairline. */
  withHandle?: boolean
  /** The first pane (left, or top when vertical). */
  first?: ReactNode
  /** The second pane. */
  second?: ReactNode
  className?: string
}

export function Resizable({ direction = 'horizontal', min = 20, max = 80, label = 'Resize panes', value, defaultValue = 50, onValueChange, withHandle, first, second, className }: ResizableProps) {
  const [size, setSize] = useControllable(value, defaultValue, onValueChange)
  const root = useRef<HTMLDivElement | null>(null)
  const [dragging, setDragging] = useState(false)
  const firstId = `pane-${useId().replace(/:/g, '')}`
  const across = direction !== 'vertical'
  const clamp = (n: number) => Math.min(max, Math.max(min, Math.round(n * 10) / 10))

  function onDown(e: PointerEvent<HTMLDivElement>) {
    if (e.button !== 0) return
    e.currentTarget.setPointerCapture(e.pointerId)
    setDragging(true)
  }
  function onMove(e: PointerEvent<HTMLDivElement>) {
    if (!dragging || !root.current) return
    const box = root.current.getBoundingClientRect()
    setSize(clamp(across ? ((e.clientX - box.left) / box.width) * 100 : ((e.clientY - box.top) / box.height) * 100))
  }
  function onUp(e: PointerEvent<HTMLDivElement>) {
    setDragging(false)
    if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId)
  }
  function onKey(e: KeyboardEvent<HTMLDivElement>) {
    const step = e.shiftKey ? 10 : 2
    const back = across ? 'ArrowLeft' : 'ArrowUp'
    const fwd = across ? 'ArrowRight' : 'ArrowDown'
    if (e.key === back) setSize(clamp(size - step))
    else if (e.key === fwd) setSize(clamp(size + step))
    else if (e.key === 'Home') setSize(min)
    else if (e.key === 'End') setSize(max)
    else return
    e.preventDefault()
  }

  return (
    <>
      <style href="ds-resizable" precedence="ds">{css}</style>
      <div data-ds="resizable" ref={root} className={cn('flex min-h-0 min-w-0', across ? 'flex-row' : 'flex-col', dragging && 'select-none', className)}>
        <div id={firstId} className="min-h-0 min-w-0 overflow-auto fade-xy" style={{ flexBasis: `${size}%` }}>{first}</div>
        <div
          className="ds-resize-handle"
          data-direction={across ? 'horizontal' : 'vertical'}
          data-dragging={dragging || undefined}
          role="separator"
          tabIndex={0}
          aria-label={label}
          aria-orientation={across ? 'vertical' : 'horizontal'}
          aria-controls={firstId}
          aria-valuenow={size}
          aria-valuemin={min}
          aria-valuemax={max}
          onPointerDown={onDown}
          onPointerMove={onMove}
          onPointerUp={onUp}
          onPointerCancel={onUp}
          onKeyDown={onKey}
        >
          {withHandle && <span className="ds-resize-grip" aria-hidden="true" />}
        </div>
        <div className="min-h-0 min-w-0 flex-1 overflow-auto fade-xy">{second}</div>
      </div>
    </>
  )
}

export default Resizable

const css = `
.ds-resize-handle { position: relative; display: flex; flex-shrink: 0; align-items: center; justify-content: center; background: var(--ds-color-edge); touch-action: none; transition: background-color var(--ds-duration-micro) var(--ds-easing-standard); }
.ds-resize-handle[data-direction="horizontal"] { width: var(--ds-border-hairline); cursor: col-resize; }
.ds-resize-handle[data-direction="vertical"] { height: var(--ds-border-hairline); cursor: row-resize; }
/* The reach: 12px either side of the hairline, 20px on touch. */
.ds-resize-handle::before { position: absolute; content: ""; }
.ds-resize-handle[data-direction="horizontal"]::before { inset: 0 calc(var(--ds-space-md) * -1); }
.ds-resize-handle[data-direction="vertical"]::before { inset: calc(var(--ds-space-md) * -1) 0; }
@media (pointer: coarse) {
  .ds-resize-handle[data-direction="horizontal"]::before { inset: 0 -20px; }
  .ds-resize-handle[data-direction="vertical"]::before { inset: -20px 0; }
}
.ds-resize-handle[data-dragging], .ds-resize-handle:focus-visible { background: var(--ds-color-edge-focus); }
.ds-resize-handle:focus-visible { outline: none; }
@media (hover: hover) and (pointer: fine) { .ds-resize-handle:hover { background: var(--ds-color-edge-focus); } }
/* The grip: a small box on the hairline, the card's fill in a #666666 hairline that turns White with it. */
.ds-resize-grip { position: relative; z-index: 1; flex-shrink: 0; border: var(--ds-border-hairline) solid var(--ds-color-edge-strong); border-radius: var(--ds-radius-sm); background: var(--ds-color-surface-raised); transition: border-color var(--ds-duration-micro) var(--ds-easing-standard); }
.ds-resize-handle[data-direction="horizontal"] .ds-resize-grip { width: var(--ds-space-md); height: var(--ds-space-xl); }
.ds-resize-handle[data-direction="vertical"] .ds-resize-grip { width: var(--ds-space-xl); height: var(--ds-space-md); }
.ds-resize-handle[data-dragging] .ds-resize-grip, .ds-resize-handle:focus-visible .ds-resize-grip { border-color: var(--ds-color-edge-focus); }
@media (hover: hover) and (pointer: fine) { .ds-resize-handle:hover .ds-resize-grip { border-color: var(--ds-color-edge-focus); } }
`
react/hooks/useControllable.ts
'use client'
import { useCallback, useRef, useState } from 'react'

// Vue's defineModel in React: a value that is controlled when the caller passes it (value + onValueChange, as Radix
// does) and kept inside otherwise (defaultValue). setValue calls onChange either way.
export function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (value: T) => void) {
	const [inner, setInner] = useState<T>(defaultValue)
	const controlled = value !== undefined
	const current = controlled ? value : inner
	const ref = useRef(onChange)
	ref.current = onChange
	const set = useCallback((next: T) => {
		if (!controlled) setInner(next)
		ref.current?.(next)
	}, [controlled])
	return [current, set] as const
}
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/resizable.ts
import { html, nothing, type PropertyValues } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { styleMap } from 'lit/directives/style-map.js'
import { DsElement, define, uid } from '@/elements/lib/element'
import styles from 'virtual:tw'

// Resizable.tsx as <spyglass-resizable>: two panes and the hairline between them. The hairline is the handle (a 12px
// reach either side), White under the pointer, while dragged and when focused; `with-handle` adds a small grip.
// Drag it, or use the arrow keys (Shift for bigger steps, Home and End). The host is the split, so a class on the
// element lands on it. `value` (the first pane's share in percent) with the native `input` and `change` events.

/**
 * @summary Two panes and the hairline between them.
 * @slot first - The first pane (left, or top when vertical).
 * @slot second - The second pane.
 * @fires input - The split moved (read `value`).
 * @fires change - The split moved (read `value`).
 */
export class Resizable extends DsElement {
	static styles = [DsElement.styles, styles]
	static properties = { value: { type: Number }, direction: { reflect: true }, min: { type: Number }, max: { type: Number }, label: {}, withHandle: { type: Boolean, attribute: 'with-handle' }, dragging: { state: true } }

	/** The first pane's share, in percent. */
	value = 50
	/** horizontal (side by side) or vertical. */
	direction: 'horizontal' | 'vertical' = 'horizontal'
	/** The first pane's smallest share, in percent. */
	min = 20
	/** The first pane's largest share, in percent. */
	max = 80
	/** The handle's name for assistive tech. */
	label = 'Resize panes'
	/** A small grip on the hairline. */
	withHandle = false
	private dragging = false
	private firstId = uid('pane')

	private get across() {
		return this.direction !== 'vertical'
	}
	private clamp(n: number) {
		return Math.min(this.max, Math.max(this.min, Math.round(n * 10) / 10))
	}
	protected updated(changed: PropertyValues) {
		super.updated(changed)
		if (changed.has('dragging')) this.toggleAttribute('data-dragging', this.dragging)
	}

	private onDown(e: PointerEvent) {
		if (e.button !== 0) return
		;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
		this.dragging = true
	}
	private onMove(e: PointerEvent) {
		if (!this.dragging) return
		const box = this.getBoundingClientRect()
		this.commit(this.clamp(this.across ? ((e.clientX - box.left) / box.width) * 100 : ((e.clientY - box.top) / box.height) * 100))
	}
	private onUp(e: PointerEvent) {
		this.dragging = false
		const el = e.currentTarget as HTMLElement
		if (el.hasPointerCapture(e.pointerId)) el.releasePointerCapture(e.pointerId)
	}
	private onKey(e: KeyboardEvent) {
		const step = e.shiftKey ? 10 : 2
		const back = this.across ? 'ArrowLeft' : 'ArrowUp'
		const fwd = this.across ? 'ArrowRight' : 'ArrowDown'
		if (e.key === back) this.commit(this.clamp(this.value - step))
		else if (e.key === fwd) this.commit(this.clamp(this.value + step))
		else if (e.key === 'Home') this.commit(this.min)
		else if (e.key === 'End') this.commit(this.max)
		else return
		e.preventDefault()
	}

	render() {
		return html`<div id=${this.firstId} class="min-h-0 min-w-0 overflow-auto fade-xy" style=${styleMap({ flexBasis: `${this.value}%` })}><slot name="first"></slot></div>
			<div
				class="ds-resize-handle"
				data-direction=${this.across ? 'horizontal' : 'vertical'}
				data-dragging=${ifDefined(this.dragging ? 'true' : undefined)}
				role="separator"
				tabindex="0"
				aria-label=${this.label}
				aria-orientation=${this.across ? 'vertical' : 'horizontal'}
				aria-controls=${this.firstId}
				aria-valuenow=${this.value}
				aria-valuemin=${this.min}
				aria-valuemax=${this.max}
				@pointerdown=${this.onDown}
				@pointermove=${this.onMove}
				@pointerup=${this.onUp}
				@pointercancel=${this.onUp}
				@keydown=${this.onKey}
			>${this.withHandle ? html`<span class="ds-resize-grip" aria-hidden="true"></span>` : nothing}</div>
			<div class="min-h-0 min-w-0 flex-1 overflow-auto fade-xy"><slot name="second"></slot></div>`
	}
}

define('resizable', Resizable)
elements/components/ui/resizable.css
/* The host is the split (Resizable.tsx's root div and its classes), so a class on the element lands on it. */
:host { @apply flex min-h-0 min-w-0 flex-row; }
:host([direction="vertical"]) { @apply flex-col; }
:host([data-dragging]) { @apply select-none; }
/* Resizable.tsx's styles, as they are. */
.ds-resize-handle { position: relative; display: flex; flex-shrink: 0; align-items: center; justify-content: center; background: var(--ds-color-edge); touch-action: none; transition: background-color var(--ds-duration-micro) var(--ds-easing-standard); }
.ds-resize-handle[data-direction="horizontal"] { width: var(--ds-border-hairline); cursor: col-resize; }
.ds-resize-handle[data-direction="vertical"] { height: var(--ds-border-hairline); cursor: row-resize; }
/* The reach: 12px either side of the hairline, 20px on touch. */
.ds-resize-handle::before { position: absolute; content: ""; }
.ds-resize-handle[data-direction="horizontal"]::before { inset: 0 calc(var(--ds-space-md) * -1); }
.ds-resize-handle[data-direction="vertical"]::before { inset: calc(var(--ds-space-md) * -1) 0; }
@media (pointer: coarse) {
  .ds-resize-handle[data-direction="horizontal"]::before { inset: 0 -20px; }
  .ds-resize-handle[data-direction="vertical"]::before { inset: -20px 0; }
}
.ds-resize-handle[data-dragging], .ds-resize-handle:focus-visible { background: var(--ds-color-edge-focus); }
.ds-resize-handle:focus-visible { outline: none; }
@media (hover: hover) and (pointer: fine) { .ds-resize-handle:hover { background: var(--ds-color-edge-focus); } }
/* The grip: a small box on the hairline, the card's fill in a #666666 hairline that turns White with it. */
.ds-resize-grip { position: relative; z-index: 1; flex-shrink: 0; border: var(--ds-border-hairline) solid var(--ds-color-edge-strong); border-radius: var(--ds-radius-sm); background: var(--ds-color-surface-raised); transition: border-color var(--ds-duration-micro) var(--ds-easing-standard); }
.ds-resize-handle[data-direction="horizontal"] .ds-resize-grip { width: var(--ds-space-md); height: var(--ds-space-xl); }
.ds-resize-handle[data-direction="vertical"] .ds-resize-grip { width: var(--ds-space-xl); height: var(--ds-space-md); }
.ds-resize-handle[data-dragging] .ds-resize-grip, .ds-resize-handle:focus-visible .ds-resize-grip { border-color: var(--ds-color-edge-focus); }
@media (hover: hover) and (pointer: fine) { .ds-resize-handle:hover .ds-resize-grip { border-color: var(--ds-color-edge-focus); } }

Reference: Derived from the brand guidelines (no split view in the brand assets): the neutral steps for the hairline, White for the active state, the plan card's fill for the grip.