Components
Scroll Area
A region that scrolls on its own with thin scrollbars: a #363636 thumb on a clear track. Native scrolling, so wheel, touch and keyboard all work; overscroll stays inside. An edge with more past it fades (fade-y, fade-x, fade-xy) instead of cutting rows off.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add scroll-area<script type="module" src="/spyglass-ds/elements/scroll-area.js"></script>Boards
- Acme hooks
- Acme summer launch
- UGC openers
- Price in the first line
- Product in hand
- Before and after
- Unboxing
- Founder story
- Testimonials
- Bundles
- Free shipping
- Launch week
- Holiday gifting
- Back to school
- New flavor
- Restock
- Behind the scenes
- Comparisons
- Tutorials
- Reviews
Acme ad 1
Acme ad 2
Acme ad 3
Acme ad 4
Acme ad 5
Acme ad 6
Usage
A long list in a short box (boards in a side panel) or a row of creatives that scrolls across. Give it a label so a keyboard can reach it.
Never
- Custom drawn scrollbars that break native scrolling
- A scroll box a keyboard can't reach: give it a label
- Hiding the scrollbar on content people need to find
- A scroll box inside a scroll box going the same way
Props
The web component takes the same as attributes or properties (<spyglass-scroll-area>); title is heading there.
<ScrollArea>
| Prop | Type | Default |
|---|---|---|
label | string Names the region and lets a keyboard reach it. | |
maxHeight | string A cap on its height, such as 288px. | |
axis | 'y' | 'x' | 'both' y (down), x (across) or both. | 'y' |
className | string | |
children | ReactNode |
Examples
The same example in React and as web components; both render identically.
import { ScrollArea } from '@/react/components/ui/ScrollArea'
// A long list of boards in a short box, and a row of creatives that scrolls across.
const BOARDS = [
'Acme hooks',
'Acme summer launch',
'UGC openers',
'Price in the first line',
'Product in hand',
'Before and after',
'Unboxing',
'Founder story',
'Testimonials',
'Bundles',
'Free shipping',
'Launch week',
'Holiday gifting',
'Back to school',
'New flavor',
'Restock',
'Behind the scenes',
'Comparisons',
'Tutorials',
'Reviews',
]
const CREATIVES = ['Acme ad 1', 'Acme ad 2', 'Acme ad 3', 'Acme ad 4', 'Acme ad 5', 'Acme ad 6']
export default function Lists() {
return (
<div className="flex w-full max-w-sm flex-col gap-8">
<ScrollArea label="Boards" className="h-72 rounded-container border border-edge">
<div className="p-4">
<p className="type-label text-fg-soft">Boards</p>
<ul className="mt-3">
{BOARDS.map((b) => <li key={b} className="border-t border-edge py-2 type-ui text-fg first:border-t-0">{b}</li>)}
</ul>
</div>
</ScrollArea>
<ScrollArea label="Creatives" axis="x" className="rounded-container border border-edge">
<ul className="flex w-max gap-4 p-4">
{CREATIVES.map((c) => (
<li key={c} className="flex w-36 shrink-0 flex-col gap-2">
<div className="aspect-square w-full rounded-control bg-surface-raised" />
<p className="type-caption text-fg-muted">{c}</p>
</li>
))}
</ul>
</ScrollArea>
</div>
)
}<!-- A long list of boards in a short box, and a row of creatives that scrolls across. -->
<div class="flex w-full max-w-sm flex-col gap-8">
<spyglass-scroll-area label="Boards" class="h-72 rounded-container border border-edge">
<div class="p-4">
<p class="type-label text-fg-soft">Boards</p>
<ul class="mt-3">
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Acme hooks</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Acme summer launch</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">UGC openers</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Price in the first line</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Product in hand</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Before and after</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Unboxing</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Founder story</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Testimonials</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Bundles</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Free shipping</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Launch week</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Holiday gifting</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Back to school</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">New flavor</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Restock</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Behind the scenes</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Comparisons</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Tutorials</li>
<li class="border-t border-edge py-2 type-ui text-fg first:border-t-0">Reviews</li>
</ul>
</div>
</spyglass-scroll-area>
<spyglass-scroll-area label="Creatives" axis="x" class="rounded-container border border-edge">
<ul class="flex w-max gap-4 p-4">
<li class="flex w-36 shrink-0 flex-col gap-2"><div class="aspect-square w-full rounded-control bg-surface-raised"></div><p class="type-caption text-fg-muted">Acme ad 1</p></li>
<li class="flex w-36 shrink-0 flex-col gap-2"><div class="aspect-square w-full rounded-control bg-surface-raised"></div><p class="type-caption text-fg-muted">Acme ad 2</p></li>
<li class="flex w-36 shrink-0 flex-col gap-2"><div class="aspect-square w-full rounded-control bg-surface-raised"></div><p class="type-caption text-fg-muted">Acme ad 3</p></li>
<li class="flex w-36 shrink-0 flex-col gap-2"><div class="aspect-square w-full rounded-control bg-surface-raised"></div><p class="type-caption text-fg-muted">Acme ad 4</p></li>
<li class="flex w-36 shrink-0 flex-col gap-2"><div class="aspect-square w-full rounded-control bg-surface-raised"></div><p class="type-caption text-fg-muted">Acme ad 5</p></li>
<li class="flex w-36 shrink-0 flex-col gap-2"><div class="aspect-square w-full rounded-control bg-surface-raised"></div><p class="type-caption text-fg-muted">Acme ad 6</p></li>
</ul>
</spyglass-scroll-area>
</div>Source
import type { ReactNode } from 'react'
import { cn } from '@/react/lib/cn'
// A region that scrolls on its own, with the page's thin scrollbars: a #363636 thumb on a clear track. Native
// scrolling, so wheel, touch and keyboard all work and nothing is redrawn by script; overscroll stays inside it. With a
// `label` it is a named region a keyboard can reach. `maxHeight` caps it (or size it with classes); `axis` picks the
// direction.
export interface ScrollAreaProps {
/** Names the region and lets a keyboard reach it. */
label?: string
/** A cap on its height, such as 288px. */
maxHeight?: string
/** y (down), x (across) or both. */
axis?: 'y' | 'x' | 'both'
className?: string
children?: ReactNode
}
// Each edge with more past it fades (the brand's fade-y, fade-x and fade-xy), so nothing is cut off by a hard edge.
const AXIS = { y: 'overflow-x-hidden overflow-y-auto fade-y', x: 'overflow-x-auto overflow-y-hidden fade-x', both: 'overflow-auto fade-xy' } as const
export function ScrollArea({ label, maxHeight, axis = 'y', className, children }: ScrollAreaProps) {
return (
<>
<style href="ds-scroll-area" precedence="ds">{css}</style>
<div
data-ds="scroll-area"
className={cn('ds-scroll relative overscroll-contain', AXIS[axis] ?? AXIS.y, className)}
style={maxHeight ? { maxHeight } : undefined}
role={label ? 'region' : undefined}
aria-label={label}
tabIndex={label ? 0 : undefined}
>
{children}
</div>
</>
)
}
export default ScrollArea
const css = `
.ds-scroll { scrollbar-width: thin; scrollbar-color: var(--ds-color-neutral-5) transparent; }
.ds-scroll:focus-visible { outline-offset: calc(var(--ds-border-focus) * -1); }
`// 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 { DsElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'
// ScrollArea.tsx as <spyglass-scroll-area>: a region that scrolls on its own with thin scrollbars (a #363636 thumb on a
// clear track), native scrolling, overscroll kept inside. The host is the scrolling box, so size classes on the
// element land on it. With a `label` it is a named region a keyboard can reach.
/**
* @summary A region that scrolls on its own, with thin scrollbars.
* @slot - The content.
*/
export class ScrollArea extends DsElement {
static styles = [DsElement.styles, styles]
static properties = { label: {}, maxHeight: { attribute: 'max-height' }, axis: { reflect: true } }
/** Names the region and lets a keyboard reach it. */
label?: string
/** A cap on its height, such as 288px. */
maxHeight?: string
/** y (down), x (across) or both. */
axis: 'y' | 'x' | 'both' = 'y'
protected updated(changed: PropertyValues) {
super.updated(changed)
if (changed.has('maxHeight')) this.style.maxHeight = this.maxHeight ?? ''
// The host is the scroll box, so it takes the scroll fade by attribute (fade-y, fade-x or fade-xy in React).
if (changed.has('axis')) this.dataset.fade = this.axis === 'both' ? 'xy' : this.axis === 'x' ? 'x' : 'y'
if (changed.has('label')) {
if (this.label) {
this.setAttribute('role', 'region')
this.setAttribute('aria-label', this.label)
this.tabIndex = 0
} else {
for (const a of ['role', 'aria-label', 'tabindex']) this.removeAttribute(a)
}
}
}
render() {
return html`<slot></slot>`
}
}
define('scroll-area', ScrollArea)/* The host is the scrolling box (ScrollArea.tsx's root div), so a class on the element lands on it. */
:host { @apply relative block overscroll-contain overflow-x-hidden overflow-y-auto; }
:host([axis="x"]) { @apply overflow-x-auto overflow-y-hidden; }
:host([axis="both"]) { @apply overflow-auto; }
:host { scrollbar-width: thin; scrollbar-color: var(--ds-color-neutral-5) transparent; }
/* !important: the page's focus ring (outline-offset 2px) wins over a plain :host rule. */
:host(:focus-visible) { outline-offset: calc(var(--ds-border-focus) * -1) !important; }Reference: Derived from the brand guidelines (no scrolling region in the brand assets): the base layer's thin scrollbar in the neutral steps.