Components
Button group
Buttons that belong together, joined into one block: the brand corner on the outer corners only and a 1px Black seam between them. A field with its button, a button with its menu arrow, or a vertical stack.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add button-group<script type="module" src="/spyglass-ds/elements/button-group.js"></script>Usage
Paging, a pair of views, save with a choice of board, an email field with its Invite button.
Never
- Unrelated actions in one group
- Two primary buttons in one group
- A border around the whole group
Props
The web component takes the same as attributes or properties (<spyglass-button-group>); title is heading there.
<ButtonGroup>
| Prop | Type | Default |
|---|---|---|
label | string The group's name for assistive tech. | |
vertical | boolean Stacked. | |
className | string | |
children | ReactNode |
Examples
The same example in React and as web components; both render identically.
import { ChevronDown, ChevronLeft, ChevronRight, LayoutGrid, Rows3 } from 'lucide-react'
import { Button } from '@/react/components/ui/Button'
import { ButtonGroup } from '@/react/components/ui/ButtonGroup'
// A results toolbar: paging, the two views, and a save button with its menu arrow, each set joined into one block.
export default function Toolbar() {
return (
<div className="flex flex-wrap items-center justify-center gap-3">
<ButtonGroup label="Pages">
<Button variant="secondary" icon aria-label="Previous page"><ChevronLeft size={16} strokeWidth={1.5} aria-hidden="true" /></Button>
<Button variant="secondary" icon aria-label="Next page"><ChevronRight size={16} strokeWidth={1.5} aria-hidden="true" /></Button>
</ButtonGroup>
<ButtonGroup label="View">
<Button variant="secondary"><LayoutGrid size={16} strokeWidth={1.5} aria-hidden="true" />Grid</Button>
<Button variant="secondary"><Rows3 size={16} strokeWidth={1.5} aria-hidden="true" />Rows</Button>
</ButtonGroup>
<ButtonGroup label="Save">
<Button variant="secondary">Save to board</Button>
<Button variant="secondary" icon aria-label="Choose a board"><ChevronDown size={16} strokeWidth={1.5} aria-hidden="true" /></Button>
</ButtonGroup>
</div>
)
}<!-- A results toolbar: paging, the two views, and a save button with its menu arrow, each set joined into one block. -->
<div class="flex flex-wrap items-center justify-center gap-3">
<spyglass-button-group label="Pages">
<spyglass-button variant="secondary" icon aria-label="Previous page"><i data-lucide="chevron-left" data-size="16" data-stroke-width="1.5" aria-hidden="true"></i></spyglass-button>
<spyglass-button variant="secondary" icon aria-label="Next page"><i data-lucide="chevron-right" data-size="16" data-stroke-width="1.5" aria-hidden="true"></i></spyglass-button>
</spyglass-button-group>
<spyglass-button-group label="View">
<spyglass-button variant="secondary"><i data-lucide="layout-grid" data-size="16" data-stroke-width="1.5" aria-hidden="true"></i>Grid</spyglass-button>
<spyglass-button variant="secondary"><i data-lucide="rows-3" data-size="16" data-stroke-width="1.5" aria-hidden="true"></i>Rows</spyglass-button>
</spyglass-button-group>
<spyglass-button-group label="Save">
<spyglass-button variant="secondary">Save to board</spyglass-button>
<spyglass-button variant="secondary" icon aria-label="Choose a board"><i data-lucide="chevron-down" data-size="16" data-stroke-width="1.5" aria-hidden="true"></i></spyglass-button>
</spyglass-button-group>
</div>import { Button } from '@/react/components/ui/Button'
import { ButtonGroup } from '@/react/components/ui/ButtonGroup'
import { Input } from '@/react/components/ui/Input'
// A field with its button, and a stack of secondary buttons.
export default function Field() {
return (
<div className="flex w-120 max-w-full flex-col items-center gap-6">
<ButtonGroup label="Invite" className="w-full">
<Input aria-label="Email" placeholder="team@acme.com" />
<Button>Invite</Button>
</ButtonGroup>
<ButtonGroup label="Sort" vertical>
<Button variant="secondary">Newest</Button>
<Button variant="secondary">Longest running</Button>
<Button variant="secondary">Most engagement</Button>
</ButtonGroup>
</div>
)
}<!-- A field with its button, and a stack of secondary buttons. -->
<div class="flex w-120 max-w-full flex-col items-center gap-6">
<spyglass-button-group label="Invite" class="w-full">
<spyglass-input aria-label="Email" placeholder="team@acme.com"></spyglass-input>
<spyglass-button>Invite</spyglass-button>
</spyglass-button-group>
<spyglass-button-group label="Sort" vertical>
<spyglass-button variant="secondary">Newest</spyglass-button>
<spyglass-button variant="secondary">Longest running</spyglass-button>
<spyglass-button variant="secondary">Most engagement</spyglass-button>
</spyglass-button-group>
</div>Source
import type { ReactNode } from 'react'
import { cn } from '@/react/lib/cn'
// shadcn's button group in the brand: buttons that belong together, joined into one block with the brand corner on
// its outer corners only and a 1px Black seam between them (the ground showing through), so #262626 halves read as
// one control. Secondary buttons for a set of tools, a field with its button, a button with its menu arrow.
// `vertical` stacks them. Give it a label for the group.
export interface ButtonGroupProps {
/** The group's name for assistive tech. */
label?: string
/** Stacked. */
vertical?: boolean
className?: string
children?: ReactNode
}
export function ButtonGroup({ label, vertical, className, children }: ButtonGroupProps) {
return (
<div data-ds="button-group" className={cn('ds-btngroup', className)} role="group" aria-label={label} data-vertical={vertical || undefined}>
<style href="ds-button-group" precedence="ds">{css}</style>
{children}
</div>
)
}
export default ButtonGroup
const css = `
.ds-btngroup { display: inline-flex; align-items: stretch; gap: var(--ds-border-hairline); }
.ds-btngroup[data-vertical] { flex-direction: column; }
.ds-btngroup > * { position: relative; }
.ds-btngroup > :is(:hover, :focus-visible, :focus-within) { z-index: 1; }
.ds-btngroup:not([data-vertical]) > :not(:first-child) { border-start-start-radius: 0; border-end-start-radius: 0; }
.ds-btngroup:not([data-vertical]) > :not(:last-child) { border-start-end-radius: 0; border-end-end-radius: 0; }
.ds-btngroup[data-vertical] > :not(:first-child) { border-start-start-radius: 0; border-start-end-radius: 0; }
.ds-btngroup[data-vertical] > :not(:last-child) { border-end-start-radius: 0; border-end-end-radius: 0; }
`// 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 { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'
// ButtonGroup.tsx as <spyglass-button-group>: buttons that belong together, joined into one block with the brand corner
// on its outer corners only and a 1px Black seam between them. `vertical` stacks them. Give it a label for the group.
//
// React's rules reach each child's root (`.ds-btngroup > *`). A library element such as <spyglass-button> draws its
// root in its own shadow root, so the group marks each child with its place (data-group="first middle last") and gives
// that shadow root a small sheet that squares the inner corners there; a child that is its own box (a native field)
// takes the rules in button-group.css.
// For a child's shadow root: the child's root box is the shadow root's element with data-ds.
const CHILD_RULES = `
:host([data-group]) > [data-ds] { position: relative; }
:host([data-group]) > [data-ds]:is(:hover, :focus-visible, :focus-within) { z-index: 1; }
:host([data-group~="after-x"]) > [data-ds] { border-start-start-radius: 0; border-end-start-radius: 0; }
:host([data-group~="before-x"]) > [data-ds] { border-start-end-radius: 0; border-end-end-radius: 0; }
:host([data-group~="after-y"]) > [data-ds] { border-start-start-radius: 0; border-start-end-radius: 0; }
:host([data-group~="before-y"]) > [data-ds] { border-end-start-radius: 0; border-end-end-radius: 0; }
`
const childSheets = new WeakSet<ShadowRoot>()
function adoptChildRules(root: ShadowRoot) {
if (childSheets.has(root)) return
const sheet = new CSSStyleSheet()
sheet.replaceSync(CHILD_RULES)
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet]
childSheets.add(root)
}
/**
* @summary Buttons that belong together, joined into one block.
* @slot - The buttons (or a field with its button).
*/
export class ButtonGroup extends DsElement {
static styles = [DsElement.styles, styles]
// Layout classes on the element (w-full) land on the group's box, as on the React root.
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="button-group"]'))
static properties = { label: {}, vertical: { type: Boolean } }
/** The group's name. */
label?: string
/** Stacked. */
vertical = false
/** The box a child draws: its shadow root's root when its host lays out nothing, else the child itself. */
private box(child: HTMLElement) {
const root = child.shadowRoot
if (root && getComputedStyle(child).display === 'contents') return root.querySelector<HTMLElement>('[data-ds]') ?? child
return child
}
private async mark() {
const kids = [...this.children] as HTMLElement[]
// Library elements draw their roots once defined and rendered.
await Promise.all(kids.filter((k) => k.localName.includes('-')).map(async (k) => {
await customElements.whenDefined(k.localName)
await (k as HTMLElement & { updateComplete?: Promise<unknown> }).updateComplete
}))
const axis = this.vertical ? 'y' : 'x'
kids.forEach((k, i) => {
if (k.shadowRoot && this.box(k) !== k) adoptChildRules(k.shadowRoot)
const marks = [i > 0 ? `after-${axis}` : '', i < kids.length - 1 ? `before-${axis}` : ''].filter(Boolean)
k.setAttribute('data-group', marks.join(' '))
})
}
protected updated() {
void this.mark()
}
render() {
return html`<div data-ds="button-group" class="ds-btngroup" role="group" aria-label=${ifDefined(this.label)} ?data-vertical=${this.vertical}><slot @slotchange=${() => void this.mark()}></slot></div>`
}
}
define('button-group', ButtonGroup)/* ButtonGroup.tsx's styles, for the group's own box. React's rules for its children (`.ds-btngroup > *`) cannot reach
the boxes the library's elements draw in their own shadow roots, so the element marks each child (data-group) and
these rules, with the ones it gives each child's shadow root, draw what React's do. */
.ds-btngroup { display: inline-flex; align-items: stretch; gap: var(--ds-border-hairline); }
.ds-btngroup[data-vertical] { flex-direction: column; }
/* A child that is its own box (a native field). The page's reset outranks ::slotted, so the corners are important. */
::slotted(*) { position: relative; }
::slotted(:is(:hover, :focus-visible, :focus-within)) { z-index: 1; }
::slotted([data-group~="after-x"]) { border-start-start-radius: 0 !important; border-end-start-radius: 0 !important; }
::slotted([data-group~="before-x"]) { border-start-end-radius: 0 !important; border-end-end-radius: 0 !important; }
::slotted([data-group~="after-y"]) { border-start-start-radius: 0 !important; border-start-end-radius: 0 !important; }
::slotted([data-group~="before-y"]) { border-end-start-radius: 0 !important; border-end-end-radius: 0 !important; }Reference: Derived from the guidelines: the brand button (White primary, #262626 secondary, the 10px canvas corner, 8px here) on Black. shadcn's button group for anatomy..