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: account, changes: 0.

Asked: none. Density: none.

Sent: nothing

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

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

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%; }
TokenDefault
--sf-color-border#e4e4e7
--sf-color-focus#2563eb
--sf-segmented-bgvar(--sf-color-hover)
--sf-segmented-current-bgvar(--sf-color-surface)
--sf-segmented-current-bordervar(--sf-field-border-color)
--sf-segmented-gap3px
--sf-segmented-paddingvar(--sf-space-3)
--sf-segmented-fade2rem
--sf-radius-segmentedvar(--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.