SegmentedPicker
SwiftUI's segmented Picker. Press the selection and it lifts into a glass droplet you can drag anywhere, even off the control; it takes the nearest segment on release. Press another segment and it is chosen on release.
import { SegmentedPicker } from "@adamexu/glass";Basic
It fills the width it is offered and splits it evenly.
const [range, setRange] = useState("week");
<SegmentedPicker
aria-label="Range"
selection={range}
onChange={setRange}
segments={[
{ value: "day", label: "Day" },
{ value: "week", label: "Week" },
{ value: "month", label: "Month" },
{ value: "year", label: "Year" },
]}
/>Icons, fixed size and control size
A segment can be an icon (give it an aria-label). fixed is .fixedSize(): as wide as the labels need. Control sizes mini to regular are 32 px tall, large and extraLarge 49; the font stays the same.
const icon = (Icon) => <Icon size={17} className="glass-segmented__icon" />;
<SegmentedPicker
aria-label="View"
fixed
defaultSelection="grid"
segments={[
{ value: "grid", icon: icon(LayoutGrid), "aria-label": "Grid" },
{ value: "list", icon: icon(List), "aria-label": "List" },
{ value: "rows", icon: icon(Rows3), "aria-label": "Rows" },
]}
/>
<SegmentedPicker
aria-label="Size"
controlSize="large"
fixed
defaultSelection="m"
segments={[
{ value: "s", label: "Small" },
{ value: "m", label: "Medium" },
{ value: "l", label: "Large", disabled: true },
]}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| segments | Segment<T>[] | - | The options, in order. See below. |
| selection | T | - | SwiftUI selection: controlled. Leave it out and the picker keeps its own. |
| defaultSelection | T | - | The initial selection when uncontrolled. |
| onChange | (value: T) => void | - | After the lift that changes it, never during a drag. |
| controlSize | "mini" | "small" | "regular" | "large" | "extraLarge" | "regular" | mini to regular are 32 px tall, large and extraLarge 49. |
| fixed | boolean | false | SwiftUI .fixedSize(): as wide as the labels need instead of the width offered. |
| disabled | boolean | false | SwiftUI .disabled(_:): the whole control at half opacity. |
| tint | string | - | Accepted and ignored, as on iOS 27.2, where .tint leaves the segmented picker as it is. |
| id, aria-label | string | - | On the role="radiogroup" element. |
| className, style | string, CSSProperties | - | On the same element. |
Segment<T>
| Prop | Type | Default | Description |
|---|---|---|---|
| value | T | - | SwiftUI's .tag. |
| label | string | - | The title. With an icon too, the title is drawn alone, as iOS draws a Label in a segment. |
| icon | ReactNode | - | An icon, 17 px. |
| disabled | boolean | - | This segment can't be chosen (UIKit's setEnabled(_:forSegmentAt:)). |
| aria-label | string | - | Its accessible name, for an icon. |
Behaviour
The picker is a role="radiogroup": the arrow keys move the selection, Home and End jump to the ends. Pressing an unselected segment dims its label, and sliding off cancels. It is drawn for Chromium, where the lifted droplet refracts the labels and whatever is behind it.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| Picker(selection:).pickerStyle(.segmented) | <SegmentedPicker segments selection onChange> |
| Text("Day").tag("day") | { value: "day", label: "Day" } |
| .controlSize(.large) | controlSize="large" |
| .fixedSize() | fixed |
| .disabled(true) | disabled |