SegmentedControl
A few options side by side in one bar, with exactly one chosen: a view, a period, a mode.
import { SegmentedControl } from '@sypnia/ui/react'
Views
Three ways to see the same rows.
Showing the rows as table.
Code
import { useState } from 'react';
import { SegmentedControl } from '@sypnia/ui/react';
// One is always chosen, and all the options are in sight.
export default function Views() {
const [view, setView] = useState<'table' | 'cards' | 'json'>('table');
return (
<div className="sy-stack">
<SegmentedControl label="View of the rows" value={view} onChange={setView} options={[
{ value: 'table', label: 'Table', icon: 'table' },
{ value: 'cards', label: 'Cards' },
{ value: 'json', label: 'JSON' },
]} />
<p className="sy-text--sm sy-text--soft">Showing the rows as {view === 'json' ? 'JSON' : view}.</p>
</div>
);
} A period
The figure follows the period.
Requests in the period318,904
Code
import { useState } from 'react';
import { SegmentedControl, Stat } from '@sypnia/ui/react';
const requests = { '24h': '48,210', '7d': '318,904', '30d': '1,290,442', '90d': '3,804,117' };
// Four short options. On a phone they sit two by two.
export default function Period() {
const [period, setPeriod] = useState<keyof typeof requests>('7d');
return (
<div className="sy-stack">
<SegmentedControl label="Period" value={period} onChange={setPeriod} options={[
{ value: '24h', label: '24 hours' }, { value: '7d', label: '7 days' }, { value: '30d', label: '30 days' }, { value: '90d', label: '90 days' },
]} />
<Stat label="Requests in the period" value={requests[period]} />
</div>
);
} Two modes
The field under it changes with the mode.
Code
import { useState } from 'react';
import { Field, Input, SegmentedControl, Textarea } from '@sypnia/ui/react';
// Two modes of the same task: what is under it changes at once.
export default function TwoModes() {
const [mode, setMode] = useState<'simple' | 'sql'>('simple');
return (
<div className="sy-stack">
<SegmentedControl label="How to write the rule" value={mode} onChange={setMode} options={[{ value: 'simple', label: 'Simple' }, { value: 'sql', label: 'SQL' }]} />
{mode === 'simple'
? <Field label="A person sees a row when the column"><Input machine defaultValue="customer_id" /></Field>
: <Field label="Condition"><Textarea machine defaultValue="customer_id = auth.user_id()" /></Field>}
</div>
);
} Not available yet
A disabled option stays in sight; its label says why it cannot be chosen.
Code
import { useState } from 'react';
import { SegmentedControl } from '@sypnia/ui/react';
// A mode that is coming stays in sight, but it cannot be chosen yet: its label says why.
export default function NotYet() {
const [provider, setProvider] = useState<'smtp' | 'resend' | 'sypnia_mail'>('smtp');
return (
<SegmentedControl label="Who sends the e-mail" value={provider} onChange={setProvider}
options={[{ value: 'smtp', label: 'SMTP' }, { value: 'resend', label: 'Resend' }, { value: 'sypnia_mail', label: 'Sypnia Mail · Soon', disabled: true }]} />
);
} When to use it
Use it when
- Two to four short options, all worth seeing at once.
- The choice applies at once and changes how the content beside it is shown.
Do not use it when
- Each option shows different content with its own panel: Tabs.
- There are more than four options or their names are long: NativeSelect.
- It is on or off: Switch.
- The choice is saved with a form and needs explaining: Radio, with descriptions.
For everyone
- It is a radio group with a name (
label): one Tab stop, arrow keys move and choose. - Each option is read with its position: «Table, 1 of 3».
- The chosen option is raised and has stronger text; focus shows a ring.
- On phones the options sit two by two and are at least 44 px tall.
Props
| SegmentedControl | Type | Default |
|---|---|---|
labelrequired | string | |
onChangerequired | (v: T) => void | |
optionsrequired`disabled` keeps a mode in sight that cannot be chosen yet («Sypnia Mail · Soon»): say why in its label or next to it. | { value: T; label: ReactNode; icon?: "function" | "error" | "replay" | "performance" | "uptime" | "traces" | "rage-click" | "dead-click" | "alert" | "summary" | "watch" | ... 353 more ... | undefined; disabled?: boolean | undefined; }[] | |
valuerequired | string |