Components
Button
The site's button: a White fill with a Black DM Sans Medium label for the one main action, the quiet secondary (#1A1A1A in a #363636 ring) for the rest, and a ghost for tools in rows; a White glow follows a mouse inside it. Three sizes and a square icon button.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add button<script type="module" src="/spyglass-ds/elements/button.js"></script>Usage
Any action: search, start a plan, explore the product, save. One primary button per view or asset; the rest secondary or ghost. 40px tall by default, 56px large (and 56px on the 1080 canvas).
Never
- Two primary buttons in one view
- A spectrum color as a button fill
- A faded button for "not yet": disabled is a #1A1A1A fill with a #666666 label
- A press that scales the button: the site's buttons hold still
- An icon that moves on hover, or a size change on hover
Props
The web component takes the same as attributes or properties (<spyglass-button>); title is heading there.
<Button>
| Prop | Type | Default |
|---|---|---|
variant | ButtonVariant primary (White), secondary or ghost. | 'primary' |
size | ButtonSize xs (36px, the site header's), sm (32px), md (40px), lg (56px) or form (48px, a short form's buttons, full width). | 'md' |
icon | boolean A square icon button: put its name in aria-label. | |
href | string Renders a link. | |
type | 'button' | 'submit' | 'reset' | 'button' |
Examples
The same example in React and as web components; both render identically.
import { Button } from '@/react/components/ui/Button'
// The three variants: the White primary for the one main action, secondary for the rest, ghost for tools.
export default function Variants() {
return (
<div className="flex flex-wrap items-center gap-3">
<Button>Get 50% off</Button>
<Button variant="secondary">Explore the product</Button>
<Button variant="ghost">Compare plans</Button>
</div>
)
}<!-- The three variants: the White primary for the one main action, secondary for the rest, ghost for tools. -->
<div class="flex flex-wrap items-center gap-3">
<spyglass-button>Get 50% off</spyglass-button>
<spyglass-button variant="secondary">Explore the product</spyglass-button>
<spyglass-button variant="ghost">Compare plans</spyglass-button>
</div>import { Search } from 'lucide-react'
import { Button } from '@/react/components/ui/Button'
// sm, md and lg, and the square icon button (named with aria-label).
export default function Sizes() {
return (
<div className="flex flex-wrap items-center gap-3">
<Button size="sm">Search</Button>
<Button size="md">Search</Button>
<Button size="lg">Search</Button>
<Button icon variant="secondary" aria-label="Search"><Search size={16} strokeWidth={1.5} aria-hidden="true" /></Button>
</div>
)
}<!-- sm, md and lg, and the square icon button (named with aria-label). -->
<div class="flex flex-wrap items-center gap-3">
<spyglass-button size="sm">Search</spyglass-button>
<spyglass-button size="md">Search</spyglass-button>
<spyglass-button size="lg">Search</spyglass-button>
<spyglass-button icon variant="secondary" aria-label="Search"><i data-lucide="search" data-size="16" data-stroke-width="1.5" aria-hidden="true"></i></spyglass-button>
</div>import { ArrowUpRight } from 'lucide-react'
import { Button } from '@/react/components/ui/Button'
// Disabled is a dark fill with a gray label, never faded; a link looks like the button it stands for.
export default function States() {
return (
<div className="flex flex-wrap items-center gap-3">
<Button disabled>Start Creator for 50% off</Button>
<Button variant="secondary" disabled>Explore the product</Button>
<Button href="#pricing" variant="secondary">See pricing <ArrowUpRight size={16} strokeWidth={1.5} aria-hidden="true" /></Button>
</div>
)
}<!-- Disabled is a dark fill with a gray label, never faded; a link looks like the button it stands for. -->
<div class="flex flex-wrap items-center gap-3">
<spyglass-button disabled>Start Creator for 50% off</spyglass-button>
<spyglass-button variant="secondary" disabled>Explore the product</spyglass-button>
<spyglass-button href="#pricing" variant="secondary">See pricing <i data-lucide="arrow-up-right" data-size="16" data-stroke-width="1.5" aria-hidden="true"></i></spyglass-button>
</div>Source
'use client'
import type { AnchorHTMLAttributes, ButtonHTMLAttributes, PointerEvent, Ref } from 'react'
import { cn } from '@/react/lib/cn'
import { followGlow } from '@/lib/glow'
// The site's button (globals.css .btn): a White fill with a Black DM Sans Medium 14/20 label and an 8px corner, 40px
// tall with 16px in (md). One primary per view; secondary is the site's quiet button, a #1A1A1A fill inside a #363636
// hairline ring that turns #262626 under the pointer; ghost has no fill until hovered, for tools in rows. Sizes sm
// (32px), md (40px) and lg (56px, 32px in, a 20/28 label); `icon` makes it square. Under a mouse a White glow follows
// the pointer inside it and the fill changes (160ms on the site's ease-out); like the site, it does not scale on press.
// Disabled is a #1A1A1A fill with a #666666 label, never faded. `href` renders a link that looks the same.
export type ButtonVariant = 'primary' | 'secondary' | 'ghost'
export type ButtonSize = 'xs' | 'sm' | 'md' | 'lg' | 'form'
const VARIANTS: Record<ButtonVariant, string> = {
primary: 'bg-action-primary text-action-primary-fg hover:not-disabled:bg-action-primary-hover',
secondary: 'bg-action-secondary text-action-secondary-fg shadow-[inset_0_0_0_var(--ds-border-hairline)_var(--ds-color-action-secondary-edge)] hover:not-disabled:bg-action-secondary-hover',
ghost: 'bg-transparent text-fg hover:not-disabled:bg-action-ghost-hover',
}
const SIZES: Record<ButtonSize, string> = {
xs: 'h-control-xs gap-1.5 type-ui-strong',
sm: 'h-control-sm gap-1.5 type-ui-strong',
md: 'h-control-md gap-2 type-ui-strong',
lg: 'h-control-lg gap-2 type-body-lg font-medium',
form: 'h-control-form gap-3 type-body font-medium',
}
// The site's glow (globals.css .btn::before): a gradient layer under the label at the pointer, shown on hover only.
const GLOW =
'relative isolate overflow-hidden before:pointer-events-none before:absolute before:inset-0 before:-z-1 before:bg-[radial-gradient(circle_var(--ds-glow-radius)_at_var(--glow-x,50%)_var(--glow-y,50%),var(--ds-color-action-glow),transparent)] before:opacity-0 before:transition-opacity before:duration-240 before:ease-out hover:not-disabled:before:opacity-100'
// One padding class per button, so a square button's 0 never competes with a label's padding in the stylesheet.
const PADDING: Record<ButtonSize, string> = { xs: 'px-4', sm: 'px-3', md: 'px-4', lg: 'px-8', form: 'px-4' }
const SQUARE: Record<ButtonSize, string> = { xs: 'w-control-xs px-0', sm: 'w-control-sm px-0', md: 'w-control-md px-0', lg: 'w-control-lg px-0', form: 'w-control-form px-0' }
export interface ButtonProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'type'> {
/** primary (White), secondary or ghost. */
variant?: ButtonVariant
/** xs (36px, the site header's), sm (32px), md (40px), lg (56px) or form (48px, a short form's buttons, full width). */
size?: ButtonSize
/** A square icon button: put its name in aria-label. */
icon?: boolean
/** Renders a link. */
href?: string
type?: 'button' | 'submit' | 'reset'
ref?: Ref<HTMLButtonElement & HTMLAnchorElement>
}
export function buttonClasses({ variant = 'primary', size = 'md', icon }: Pick<ButtonProps, 'variant' | 'size' | 'icon'> = {}) {
return cn(
'inline-flex shrink-0 cursor-pointer items-center justify-center rounded-control whitespace-nowrap select-none',
'transition-[background-color,color] duration-160 ease-out',
GLOW,
'disabled:cursor-not-allowed disabled:bg-action-disabled disabled:text-action-disabled-fg',
SIZES[size],
icon ? SQUARE[size] : PADDING[size],
VARIANTS[variant],
)
}
export function Button({ variant = 'primary', size = 'md', icon, href, type = 'button', disabled, className, children, ref, onPointerMove, ...rest }: ButtonProps) {
const classes = cn(buttonClasses({ variant, size, icon }), className)
const move = (event: PointerEvent<HTMLButtonElement & HTMLAnchorElement>) => {
followGlow(event.currentTarget, event)
onPointerMove?.(event)
}
if (href) return <a data-ds="button" ref={ref} href={href} className={classes} onPointerMove={move} {...(rest as AnchorHTMLAttributes<HTMLAnchorElement>)}>{children}</a>
return <button data-ds="button" ref={ref} type={type} disabled={disabled} className={classes} onPointerMove={move} {...rest}>{children}</button>
}
export default Button// The site's button glow (ButtonGlow.tsx): under a mouse, a White glow follows the pointer inside a button. The button
// draws it (a radial gradient at --glow-x, --glow-y, faded in on hover over the brand's enter step); this only keeps
// the position current, once a frame, for a fine pointer. Touch and pens never set it.
const frames = new WeakMap<HTMLElement, number>()
export function followGlow(el: HTMLElement | null, event: { clientX: number; clientY: number; pointerType: string }) {
if (!el || event.pointerType !== 'mouse') return
if (frames.has(el)) cancelAnimationFrame(frames.get(el)!)
const { clientX, clientY } = event
frames.set(el, requestAnimationFrame(() => {
frames.delete(el)
const box = el.getBoundingClientRect()
el.style.setProperty('--glow-x', `${clientX - box.left}px`)
el.style.setProperty('--glow-y', `${clientY - box.top}px`)
}))
}// 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 } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { DsFormElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'
import { followGlow } from '@/lib/glow'
// Button.tsx as a web component, <spyglass-button>: a White fill with a Black DM Sans Medium label for the one main
// action, secondary (the site's quiet button: #1A1A1A in a #363636 ring) for the rest, ghost for tools in rows. Sizes
// sm (32px), md (40px) and lg (56px); `icon` makes it square. Under a mouse a White glow follows the pointer and the
// fill changes; it does not scale on press (the site's buttons do not). `href` renders a link. It is form-associated: type="submit" submits its form, type="reset" resets it. aria-label,
// aria-description, aria-expanded, aria-haspopup and aria-controls on it pass to the inner button.
export type ButtonVariant = 'primary' | 'secondary' | 'ghost'
export type ButtonSize = 'xs' | 'sm' | 'md' | 'lg' | 'form'
// The attributes that move from the element to its inner button, by property.
const MOVED = ['aria-expanded', 'aria-haspopup', 'aria-controls']
const FIELD: Record<string, 'expanded' | 'haspopup' | 'controls'> = { 'aria-expanded': 'expanded', 'aria-haspopup': 'haspopup', 'aria-controls': 'controls' }
const VARIANTS: Record<ButtonVariant, string> = {
primary: 'bg-action-primary text-action-primary-fg hover:not-disabled:bg-action-primary-hover',
secondary: 'bg-action-secondary text-action-secondary-fg shadow-[inset_0_0_0_var(--ds-border-hairline)_var(--ds-color-action-secondary-edge)] hover:not-disabled:bg-action-secondary-hover',
ghost: 'bg-transparent text-fg hover:not-disabled:bg-action-ghost-hover',
}
const SIZES: Record<ButtonSize, string> = {
xs: 'h-control-xs gap-1.5 type-ui-strong',
sm: 'h-control-sm gap-1.5 type-ui-strong',
md: 'h-control-md gap-2 type-ui-strong',
lg: 'h-control-lg gap-2 type-body-lg font-medium',
form: 'h-control-form gap-3 type-body font-medium',
}
// The site's glow (globals.css .btn::before): a gradient layer under the label at the pointer, shown on hover only.
const GLOW =
'relative isolate overflow-hidden before:pointer-events-none before:absolute before:inset-0 before:-z-1 before:bg-[radial-gradient(circle_var(--ds-glow-radius)_at_var(--glow-x,50%)_var(--glow-y,50%),var(--ds-color-action-glow),transparent)] before:opacity-0 before:transition-opacity before:duration-240 before:ease-out hover:not-disabled:before:opacity-100'
// One padding class per button, so a square button's 0 never competes with a label's padding in the stylesheet.
const PADDING: Record<ButtonSize, string> = { xs: 'px-4', sm: 'px-3', md: 'px-4', lg: 'px-8', form: 'px-4' }
const SQUARE: Record<ButtonSize, string> = { xs: 'w-control-xs px-0', sm: 'w-control-sm px-0', md: 'w-control-md px-0', lg: 'w-control-lg px-0', form: 'w-control-form px-0' }
/**
* @summary An action: primary (White) by default, secondary or ghost; three sizes; `icon` for a square icon button.
* @slot - The label, with a Lucide icon beside it if any.
* @fires click - When pressed (the native click, from the inner button).
* @csspart control - The inner button or link.
*/
export class Button extends DsFormElement {
static styles = [DsFormElement.styles, styles]
// Layout classes on the element (w-full, ml-auto) land on the box it draws, as on the React root.
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[part~="control"]'))
static properties = {
variant: { reflect: true },
size: { reflect: true },
icon: { type: Boolean },
href: {},
type: {},
disabled: { type: Boolean, reflect: true },
label: { attribute: 'aria-label' },
description: { attribute: 'aria-description' },
expanded: { attribute: false },
haspopup: { attribute: false },
controls: { attribute: false },
rootName: { attribute: false },
}
static get observedAttributes() {
return [...super.observedAttributes, ...MOVED]
}
/** primary (White), secondary or ghost. */
variant: ButtonVariant = 'primary'
/** xs (36px, the site header's), sm (32px), md (40px), lg (56px) or form (48px, a short form's buttons, full width). */
size: ButtonSize = 'md'
/** A square icon button (put the name in aria-label). */
icon = false
/** Renders a link. */
href?: string
type: 'button' | 'submit' | 'reset' = 'button'
disabled = false
/** The name for assistive tech, passed to the inner button (aria-label on the element). */
label?: string
/** A description for assistive tech, passed to the inner button (aria-description on the element). */
description?: string
/** Whether what it opens is open (the aria-expanded attribute). */
expanded?: string
/** What it opens: dialog, menu, listbox (the aria-haspopup attribute). */
haspopup?: string
/** The id of what it opens (the aria-controls attribute). */
controls?: string
/** The data-ds its box says: button, or the name of an item drawn as a button. */
rootName = 'button'
attributeChangedCallback(name: string, old: string | null, value: string | null) {
const field = FIELD[name]
if (!field) return super.attributeChangedCallback(name, old, value)
if (value === null) return
this[field] = value
this.removeAttribute(name)
}
protected updated(changed: Map<PropertyKey, unknown>) {
super.updated(changed)
const inner = this.renderRoot.querySelector('button, a') as (HTMLElement & { ariaControlsElements?: Element[] | null }) | null
if (inner && changed.has('controls') && 'ariaControlsElements' in inner) {
const target = this.controls ? (this.getRootNode() as Document | ShadowRoot).getElementById?.(this.controls) : null
inner.ariaControlsElements = target ? [target] : null
}
}
private onMove(event: PointerEvent) {
followGlow(event.currentTarget as HTMLElement, event)
}
private onClick() {
if (this.inactive || this.href) return
if (this.type === 'submit') this.form?.requestSubmit()
else if (this.type === 'reset') this.form?.reset()
}
render() {
const size = SIZES[this.size] ? this.size : 'md'
const classes = [
'inline-flex shrink-0 cursor-pointer items-center justify-center rounded-control whitespace-nowrap select-none',
'transition-[background-color,color] duration-160 ease-out',
GLOW,
'disabled:cursor-not-allowed disabled:bg-action-disabled disabled:text-action-disabled-fg',
SIZES[size],
this.icon ? SQUARE[size] : PADDING[size],
VARIANTS[this.variant] ?? VARIANTS.primary,
].join(' ')
return this.href
? html`<a data-ds="${this.rootName}" part="control" href=${this.href} class=${classes} aria-label=${ifDefined(this.label)} aria-description=${ifDefined(this.description)} aria-expanded=${ifDefined(this.expanded)} aria-haspopup=${ifDefined(this.haspopup as never)} @pointermove=${this.onMove}><slot></slot></a>`
: html`<button data-ds="${this.rootName}" part="control" type="button" ?disabled=${this.inactive} class=${classes} aria-label=${ifDefined(this.label)} aria-description=${ifDefined(this.description)} aria-expanded=${ifDefined(this.expanded)} aria-haspopup=${ifDefined(this.haspopup as never)} @click=${this.onClick} @pointermove=${this.onMove}><slot></slot></button>`
}
}
define('button', Button)Reference: Site globals.css .btn and controls.css (40px, 8px corner, quiet ring, glow); brand assets (Figma page 8): Button, Get 50% off (258x56, radius 10, White fill, Black DM Sans Medium 20px label); Button, Search (136x56); Button, Start Creator for 50% off (504x56).