Components
Table
Rows of one kind of thing on Black: heads as capital labels in muted DM Sans Medium, cells in DM Sans 14px, a #363636 hairline between rows and the ghost fill under the pointer. Framed, it sits in a hairline box with the brand corner. It scrolls sideways with fading edges inside the frame.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add table<script type="module" src="/spyglass-ds/elements/table.js"></script>| Board | Creatives | Brands | Updated |
|---|---|---|---|
| Acme hooks | 48 | 12 | Today |
| Acme summer launch | 112 | 9 | Yesterday |
| UGC openers | 36 | 21 | 3 days ago |
| Price in the first line | 27 | 8 | Last week |
| Total | 223 | 50 |
Usage
Boards, tracked brands, ads with a few columns to compare. Numbers right-aligned in tabular figures; a total in the footer; a caption under it.
Never
- A table for one record: use Item or a card
- Zebra stripes, colored rows or shadows: the hairline separates
- A scroll box a keyboard can't reach: give a framed table a label
- Bold heads: labels are Medium in capitals with 6% tracking
Props
The web component takes the same as attributes or properties (<spyglass-table>); title is heading there.
<Table>
| Prop | Type | Default |
|---|---|---|
framed | boolean In a hairline box with the brand corner. | |
label | string Names the scroll box (a region a keyboard can reach). | |
className | string | |
children | ReactNode The table's caption, thead, tbody and tfoot. |
Examples
The same example in React and as web components; both render identically.
import { Table } from '@/react/components/ui/Table'
// A workspace's boards: what each holds and when it last changed, with a total under them.
const BOARDS = [
{ name: 'Acme hooks', creatives: 48, brands: 12, updated: 'Today' },
{ name: 'Acme summer launch', creatives: 112, brands: 9, updated: 'Yesterday' },
{ name: 'UGC openers', creatives: 36, brands: 21, updated: '3 days ago' },
{ name: 'Price in the first line', creatives: 27, brands: 8, updated: 'Last week' },
]
export default function Boards() {
return (
<Table>
<caption>Boards in the Acme workspace.</caption>
<thead>
<tr>
<th>Board</th>
<th className="text-right">Creatives</th>
<th className="text-right">Brands</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
{BOARDS.map((b) => (
<tr key={b.name} data-state={b.name === 'Acme hooks' ? 'selected' : undefined}>
<td>{b.name}</td>
<td className="text-right">{b.creatives}</td>
<td className="text-right">{b.brands}</td>
<td className="text-fg-muted">{b.updated}</td>
</tr>
))}
</tbody>
<tfoot>
<tr>
<td>Total</td>
<td className="text-right">223</td>
<td className="text-right">50</td>
<td />
</tr>
</tfoot>
</Table>
)
}<!-- A workspace's boards: what each holds and when it last changed, with a total under them. -->
<spyglass-table>
<table>
<caption>Boards in the Acme workspace.</caption>
<thead>
<tr>
<th>Board</th>
<th class="text-right">Creatives</th>
<th class="text-right">Brands</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
<tr data-state="selected"><td>Acme hooks</td><td class="text-right">48</td><td class="text-right">12</td><td class="text-fg-muted">Today</td></tr>
<tr><td>Acme summer launch</td><td class="text-right">112</td><td class="text-right">9</td><td class="text-fg-muted">Yesterday</td></tr>
<tr><td>UGC openers</td><td class="text-right">36</td><td class="text-right">21</td><td class="text-fg-muted">3 days ago</td></tr>
<tr><td>Price in the first line</td><td class="text-right">27</td><td class="text-right">8</td><td class="text-fg-muted">Last week</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td><td class="text-right">223</td><td class="text-right">50</td><td></td></tr>
</tfoot>
</table>
</spyglass-table>import { Button } from '@/react/components/ui/Button'
import { Table } from '@/react/components/ui/Table'
// Tracked brands in a hairline frame, each row with its action at the end; it scrolls sideways on a phone.
const BRANDS = [
{ name: 'Acme', ads: 214, added: 'Jan 12' },
{ name: 'Acme Studio', ads: 86, added: 'Feb 3' },
{ name: 'Acme Goods', ads: 39, added: 'Mar 21' },
]
export default function Tracked() {
return (
<Table framed label="Tracked brands">
<thead>
<tr>
<th>Brand</th>
<th className="text-right">Live ads</th>
<th>Tracked since</th>
<th><span className="sr-only">Actions</span></th>
</tr>
</thead>
<tbody>
{BRANDS.map((b) => (
<tr key={b.name}>
<td>{b.name}</td>
<td className="text-right">{b.ads}</td>
<td className="text-fg-muted">{b.added}</td>
<td className="text-right"><Button variant="secondary" size="sm">View ads</Button></td>
</tr>
))}
</tbody>
</Table>
)
}<!-- Tracked brands in a hairline frame, each row with its action at the end; it scrolls sideways on a phone. -->
<spyglass-table framed label="Tracked brands">
<table>
<thead>
<tr>
<th>Brand</th>
<th class="text-right">Live ads</th>
<th>Tracked since</th>
<th><span class="sr-only">Actions</span></th>
</tr>
</thead>
<tbody>
<tr><td>Acme</td><td class="text-right">214</td><td class="text-fg-muted">Jan 12</td><td class="text-right"><spyglass-button variant="secondary" size="sm">View ads</spyglass-button></td></tr>
<tr><td>Acme Studio</td><td class="text-right">86</td><td class="text-fg-muted">Feb 3</td><td class="text-right"><spyglass-button variant="secondary" size="sm">View ads</spyglass-button></td></tr>
<tr><td>Acme Goods</td><td class="text-right">39</td><td class="text-fg-muted">Mar 21</td><td class="text-right"><spyglass-button variant="secondary" size="sm">View ads</spyglass-button></td></tr>
</tbody>
</table>
</spyglass-table>Source
import type { ReactNode } from 'react'
import { cn } from '@/react/lib/cn'
// Rows of one kind of thing, on Black. Heads are labels: DM Sans Medium 12px in capitals with 6% tracking, muted, over
// a #363636 hairline; cells are DM Sans 14px with a hairline between rows, and a row takes the ghost fill under the
// pointer (160ms). Right-aligned cells (class text-right) take tabular figures. A footer row sits on the card's #262626
// fill; a caption goes under the table in muted 12px. `framed` puts it in a hairline box with the brand corner and a
// 16px inset at both ends. It scrolls sideways on its own, so a phone never scrolls the page, and an edge with more
// past it fades (fade-x) inside the frame; with a `label` the scroll box is a named region a keyboard can reach. Write the <caption>, <thead>, <tbody> and <tfoot> as children;
// mark a selected row with data-state="selected".
export interface TableProps {
/** In a hairline box with the brand corner. */
framed?: boolean
/** Names the scroll box (a region a keyboard can reach). */
label?: string
className?: string
/** The table's caption, thead, tbody and tfoot. */
children?: ReactNode
}
export function Table({ framed, label, className, children }: TableProps) {
return (
<>
<style href="ds-table" precedence="ds">{css}</style>
<div data-ds="table" className={cn('relative w-full', framed && 'overflow-hidden rounded-container border border-edge', className)}>
<div className="w-full overflow-x-auto -outline-offset-2 fade-x" role={label ? 'region' : undefined} aria-label={label} tabIndex={label ? 0 : undefined}>
<table className="ds-table" data-framed={framed || undefined}>{children}</table>
</div>
</div>
</>
)
}
export default Table
const css = `
.ds-table { width: 100%; border-collapse: collapse; caption-side: bottom; color: var(--ds-color-fg); font-family: var(--ds-font-text); font-size: var(--ds-text-ui); line-height: var(--ds-text-ui-leading); }
.ds-table th { height: var(--ds-size-row); padding: 0 var(--ds-space-md); border-bottom: var(--ds-border-hairline) solid var(--ds-color-edge); color: var(--ds-color-fg-muted); font-size: var(--ds-text-label); line-height: var(--ds-text-label-leading); font-weight: var(--ds-font-weight-medium); letter-spacing: var(--ds-tracking-label); text-transform: uppercase; text-align: left; white-space: nowrap; vertical-align: middle; }
.ds-table td { padding: var(--ds-space-md); border-bottom: var(--ds-border-hairline) solid var(--ds-color-edge); vertical-align: middle; }
.ds-table :is(th, td).text-right { text-align: right; font-variant-numeric: tabular-nums; }
.ds-table tbody tr:last-child td { border-bottom: 0; }
.ds-table tbody tr { transition: background-color var(--ds-duration-micro) var(--ds-easing-standard); }
@media (hover: hover) and (pointer: fine) { .ds-table tbody tr:hover { background: var(--ds-color-action-ghost-hover); } }
.ds-table tbody tr[data-state="selected"] { background: var(--ds-color-surface-subtle); }
.ds-table tfoot td { border-top: var(--ds-border-hairline) solid var(--ds-color-edge); border-bottom: 0; background: var(--ds-color-surface-raised); font-weight: var(--ds-font-weight-medium); }
.ds-table caption { margin-top: var(--ds-space-lg); color: var(--ds-color-fg-muted); font-size: var(--ds-text-caption); line-height: var(--ds-text-caption-leading); text-align: left; }
.ds-table[data-framed] :is(th, td):first-child { padding-left: var(--ds-space-lg); }
.ds-table[data-framed] :is(th, td):last-child { padding-right: var(--ds-space-lg); }
.ds-table[data-framed] caption { padding: 0 var(--ds-space-lg) var(--ds-space-lg); }
`// 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, type PropertyValues } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { DsElement, define, HostLayoutController, tagName } from '@/elements/lib/element'
import styles from 'virtual:tw'
// Table.tsx as <spyglass-table>: it wraps the page's own <table> (rows stay in the page, where table markup works),
// styling it with the same rules as React, scoped to tables inside the element. It draws React's root div and the
// scroll box inside it around its slot: it scrolls sideways on its own with its edges fading (fade-x); `framed` gives
// the root the hairline box and the brand corner, and with a `label` the scroll box is a named region a keyboard can
// reach. Layout classes on the element land on the root.
/** Table.tsx's styles, scoped to tables inside the element. */
const RULES = `
.ds-table { width: 100%; border-collapse: collapse; caption-side: bottom; color: var(--ds-color-fg); font-family: var(--ds-font-text); font-size: var(--ds-text-ui); line-height: var(--ds-text-ui-leading); }
.ds-table th { height: var(--ds-size-row); padding: 0 var(--ds-space-md); border-bottom: var(--ds-border-hairline) solid var(--ds-color-edge); color: var(--ds-color-fg-muted); font-size: var(--ds-text-label); line-height: var(--ds-text-label-leading); font-weight: var(--ds-font-weight-medium); letter-spacing: var(--ds-tracking-label); text-transform: uppercase; text-align: left; white-space: nowrap; vertical-align: middle; }
.ds-table td { padding: var(--ds-space-md); border-bottom: var(--ds-border-hairline) solid var(--ds-color-edge); vertical-align: middle; }
.ds-table :is(th, td).text-right { text-align: right; font-variant-numeric: tabular-nums; }
.ds-table tbody tr:last-child td { border-bottom: 0; }
.ds-table tbody tr { transition: background-color var(--ds-duration-micro) var(--ds-easing-standard); }
@media (hover: hover) and (pointer: fine) { .ds-table tbody tr:hover { background: var(--ds-color-action-ghost-hover); } }
.ds-table tbody tr[data-state="selected"] { background: var(--ds-color-surface-subtle); }
.ds-table tfoot td { border-top: var(--ds-border-hairline) solid var(--ds-color-edge); border-bottom: 0; background: var(--ds-color-surface-raised); font-weight: var(--ds-font-weight-medium); }
.ds-table caption { margin-top: var(--ds-space-lg); color: var(--ds-color-fg-muted); font-size: var(--ds-text-caption); line-height: var(--ds-text-caption-leading); text-align: left; }
.ds-table[data-framed] :is(th, td):first-child { padding-left: var(--ds-space-lg); }
.ds-table[data-framed] :is(th, td):last-child { padding-right: var(--ds-space-lg); }
.ds-table[data-framed] caption { padding: 0 var(--ds-space-lg) var(--ds-space-lg); }
`
const sheets = new WeakSet<Document | ShadowRoot>()
function adoptRules(root: Node) {
if (!(root instanceof Document || root instanceof ShadowRoot) || sheets.has(root)) return
const sheet = new CSSStyleSheet()
sheet.replaceSync(`@scope (${tagName('table')}) { ${RULES} }`)
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet]
sheets.add(root)
}
/**
* @summary Rows of one kind of thing: wraps the page's own <table>.
* @slot - A <table> with its caption, thead, tbody and tfoot.
*/
export class Table extends DsElement {
static styles = [DsElement.styles, styles]
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="table"]'))
static properties = { framed: { type: Boolean, reflect: true }, label: {} }
/** In a hairline box with the brand corner. */
framed = false
/** Names the scroll box (a region a keyboard can reach). */
label?: string
connectedCallback() {
super.connectedCallback()
adoptRules(this.getRootNode())
}
private mark() {
const table = this.querySelector(':scope > table')
if (!table) return
table.classList.add('ds-table')
table.toggleAttribute('data-framed', this.framed)
}
protected updated(changed: PropertyValues) {
super.updated(changed)
this.mark()
}
render() {
return html`<div data-ds="table" class=${this.framed ? 'relative w-full overflow-hidden rounded-container border border-edge' : 'relative w-full'}><div class="w-full overflow-x-auto -outline-offset-2 fade-x" role=${ifDefined(this.label ? 'region' : undefined)} aria-label=${ifDefined(this.label)} tabindex=${ifDefined(this.label ? '0' : undefined)}><slot @slotchange=${() => this.mark()}></slot></div></div>`
}
}
define('table', Table)Reference: Derived from the brand guidelines (no table in the brand assets): the label style for heads, DM Sans for cells, the neutral steps for hairlines, the plan card's fill for the footer.