Block
Sidebar
An app's frame: Navigation down the left and the page beside it. On a phone the links move into a Sheet behind a Menu button.
Made of
Navigation, ListItem, Button, Sheet. Nothing else: the block's own CSS is layout, and every value in it is a token.
Use it
import { Sidebar } from 'rost/blocks/sidebar/Sidebar'
<Sidebar
logo={<Logo />}
name="Accounts"
links={[
{ href: '/', label: 'Home', current: true },
{ href: '/invoices', label: 'Invoices', badge: true },
{ href: '/clients', label: 'Clients' },
]}
action={<Button variant="secondary">New invoice</Button>}
>
{page}
</Sidebar> src/blocks/sidebar/Sidebar.tsx
import { useState, type ReactNode } from 'react'
import { Navigation } from '../../components/navigation/Navigation'
import { ListItem } from '../../components/list-item/ListItem'
import { Button } from '../../components/button/Button'
import { Sheet } from '../../components/sheet/Sheet'
import './sidebar.css'
export interface SidebarLink {
readonly href: string
readonly label: string
/** The page you're on. */
readonly current?: boolean
/** Something waiting there, as ListItem's dot. */
readonly badge?: boolean
}
export interface SidebarProps {
/** The app's mark, 40px. */
readonly logo: ReactNode
/** The app's name, for the phone's top bar and the menu's title. */
readonly name: string
readonly links: readonly SidebarLink[]
/** One action at the bottom, usually the thing people make most. */
readonly action?: ReactNode
/** The page. */
readonly children?: ReactNode
}
const b = 'rost-block-sidebar'
/**
* An app's frame: Navigation down the left, the page beside it. On a phone the
* navigation moves into a Sheet behind a Menu button in a top bar.
* A Rost block: only Rost's components, and layout from its tokens.
*/
export function Sidebar({ logo, name, links, action, children }: SidebarProps) {
const [open, setOpen] = useState(false)
const items = links.map((l) => (
<ListItem key={l.href} href={l.href} selected={l.current} aria-current={l.current ? 'page' : undefined} badge={l.badge}>{l.label}</ListItem>
))
return (
<div className={b}>
<div className={`${b}__nav`}>
<Navigation aria-label={name} logo={logo} action={action}>{items}</Navigation>
</div>
<header className={`${b}__bar`}>
<span className={`${b}__mark`}>{logo}</span>
<Button variant="tertiary" aria-expanded={open} onClick={() => setOpen(true)}>Menu</Button>
</header>
<Sheet open={open} onClose={() => setOpen(false)} title={name}>
<nav aria-label={name} className={`${b}__sheet-links`} onClick={(e) => { if ((e.target as HTMLElement).closest('a')) setOpen(false) }}>{items}</nav>
{action && <div className={`${b}__sheet-action`}>{action}</div>}
</Sheet>
<main className={`${b}__page`}>{children}</main>
</div>
)
}
Install
npx github:Runceanu/rost add sidebar It lands in src/rost/sidebar/, with every component it's made of. It's yours from there: change it as much as the app needs. Install has the rest.