Component · in flow
Segmented
A short row of choices: radio buttons that change the page in place, or links that change the address.
React
Value: , changes: .
Asked: . Density: .
Sent:
Usage
Astro:
---
import Segmented from '@subfloor/astro/Segmented.astro'
import SegmentedLinks from '@subfloor/astro/SegmentedLinks.astro'
---
<Segmented label="Period" name="period" value="30" options={[
{ value: '7', label: '7 days' },
{ value: '30', label: '30 days' },
]} />
<SegmentedLinks label="Analytics" links={[
{ href: '/sites/', label: 'Websites', current: true },
{ href: '/social/', label: 'Social' },
]} />React:
import { Segmented, SegmentedLinks } from '@subfloor/react'
<Segmented
label="Group by"
value={groupBy}
onValueChange={setGroupBy}
options={[
{ value: 'account', label: 'By account' },
{ value: 'service', label: 'By service' },
]}
/>
// Views of the same page. renderLink needs a client component ('use client').
<SegmentedLinks
label="Order"
kind="view"
links={[
{ href: '?sort=service', label: 'By service', current: true },
{ href: '?sort=attention', label: 'By attention' },
]}
renderLink={(props) => <Link {...props} scroll={false} />}
/>Which one
- Segmented: a filter, a view or a form value that changes in place. It is a group of native radio buttons.
- SegmentedLinks: choices with their own address, so a reload or a shared link opens the same one. Links between pages are in a
<nav>; views of the same page (kind="view") are in a group. - Keep it to two to five short labels. For more, use a Select.
Markup
<div class="sf-segmented" role="radiogroup" aria-label="Period" data-sf-segmented>
<div class="sf-segmented__row" tabindex="-1">
<label class="sf-segmented__item">
<input class="sf-segmented__input" type="radio" name="period" value="7">
<span>7 days</span>
</label>
…
</div>
</div>
<nav class="sf-segmented" aria-label="Analytics" data-sf-segmented>
<div class="sf-segmented__row" tabindex="-1">
<a class="sf-segmented__item" href="/sites/" aria-current="page">Websites</a>
…
</div>
</nav>
<script type="module">
import { initSegmented } from '@subfloor/core'
// Shows the current choice of a row too long for its place; keeps the focus ring in WebKit.
initSegmented()
</script>Behaviour
- Radio buttons choose as soon as an arrow key reaches them. If each choice loads data, each arrow press does too.
- In React, Segmented is part of a form only if it has a
name. In Astro,nameis required. - In React, a choice made before React hydrates is kept, and
onValueChangeis called once when React takes over. - A row that does not fit scrolls sideways, starting at its current choice, and fades on the side that has more. It still shows that choice when a web font arrives late, unless it was scrolled. It follows a current choice that changes later. In plain script, call
enhanceSegmentedagain after you change it. The fade needs scroll-driven animations; without them, the row still scrolls. - The current choice has an edge, not only a colour, and choosing another moves nothing.
Without JavaScript
Choosing, links and forms work. A long row starts at its current choice only where the browser supports scroll-initial-target; elsewhere it starts at the beginning. WebKit also loses the focus ring after an arrow key.
Customisation
:root {
--sf-radius-segmented: 999px;
--sf-segmented-current-bg: var(--brand);
}
/* The current choice, and one row only. */
.sf-segmented__item:has(:checked) { color: white; }
.toolbar .sf-segmented { width: 100%; }| Token | Default |
|---|---|
--sf-color-border | #e4e4e7 |
--sf-color-focus | #2563eb |
--sf-segmented-bg | var(--sf-color-hover) |
--sf-segmented-current-bg | var(--sf-color-surface) |
--sf-segmented-current-border | var(--sf-field-border-color) |
--sf-segmented-gap | 3px |
--sf-segmented-padding | var(--sf-space-3) |
--sf-segmented-fade | 2rem |
--sf-radius-segmented | var(--sf-radius-control) |
Background
Content 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.