List and Form
Grouped rows in every iOS list style, with sections, swipe actions, delete, reorder and selection. A Form is an inset grouped List with form row metrics.
import { Form, Label, LabeledContent, List, NavigationLink, Section, SwipeActions, Text } from "@adamexu/glass";List
Each child of a Section is a row; give every row a key. Press and hold a row, swipe one either way, delete with a swipe, or press Edit to delete, reorder and select. Removed rows fade while the rest close up. Inside a NavigationPage the list scrolls with the page under its bar, and a NavigationLink row pushes its value on the stack.
const [listStyle, setListStyle] = useState<ListStyle>("insetGrouped");
const [editing, setEditing] = useState(false);
const [folders, setFolders] = useState(["Projects", "Receipts", "Recipes", "Travel"]);
const [selection, setSelection] = useState<Set<string>>(new Set());
<NavigationPage title="Folders" toolbar={<ToolbarItem title={editing ? "Done" : "Edit"} onAction={() => setEditing(!editing)} />}>
<List listStyle={listStyle} editMode={editing ? "active" : "inactive"} selection={selection} onSelectionChange={setSelection}>
<Section header="Pinned">
<SwipeActions
key="starred"
leading={[{ title: "Flag", tint: "#ff9500", icon: <Flag size={20} /> }]}
trailing={[{ title: "Archive", icon: <Archive size={20} /> }]}
>
<Label title="Starred" icon={<Star size={20} />} />
</SwipeActions>
<Text key="alerts" subtitle="3 new today">Alerts</Text>
</Section>
<Section
header="Folders"
footer="Swipe a folder left to delete it."
onDelete={(index) => setFolders(folders.filter((_, i) => i !== index))}
onMove={(from, to) => setFolders(move(folders, from, to))}
>
{folders.map((name) => (
<NavigationLink key={name} value={name}>
<Label title={name} icon={<Folder size={20} />} />
</NavigationLink>
))}
</Section>
</List>
</NavigationPage>Form
What a row is depends on its element. A Toggle, Slider, Stepper, SegmentedPicker or a Button with a buttonStyle is drawn in the row and keeps its own touches. A Button with no style is the row's press in the accent colour (red with role="destructive"), and a Link opens on release.
<NavigationPage title="Settings">
<Form>
<Section header="Display">
<SegmentedPicker key="mode" aria-label="Mode" defaultSelection="auto"
segments={[{ value: "light", label: "Light" }, { value: "dark", label: "Dark" }, { value: "auto", label: "Auto" }]} />
<Toggle key="bold" defaultOn><Text>Bold text</Text></Toggle>
<Slider key="brightness" aria-label="Brightness" defaultValue={0.5}
minimumValueLabel={<Sun size={20} />} maximumValueLabel={<Moon size={20} />} />
</Section>
<Section header="Booking" footer="Rows hold their own controls.">
<Stepper key="guests" value={guests} onChange={setGuests} in={[1, 8]}><Text>Guests: {guests}</Text></Stepper>
<LabeledContent key="version" title="Version" value="0.1.0" />
<Label key="alerts" title="Alerts" icon={<Bell size={20} />} />
</Section>
<Section>
<Link key="help" destination="/docs"><Text>Help</Text></Link>
<Button key="reset">Reset settings</Button>
<Button key="delete" role="destructive">Delete account</Button>
</Section>
</Form>
</NavigationPage>Props
List
| Prop | Type | Default | Description |
|---|---|---|---|
| listStyle | "automatic" | "insetGrouped" | "grouped" | "inset" | "plain" | "sidebar" | "automatic" | automatic is insetGrouped. |
| editMode | "inactive" | "active" | "inactive" | Minus or selection circles at the leading edge, reorder handles at the trailing. |
| selection | ReadonlySet<string> | - | Rows' keys. In edit mode each row has a circle. |
| onSelectionChange | (next: Set<string>) => void | - | Runs with the new selection. |
| idiom | "phone" | "pad" | "duo" | the page's stack's, else phone | The device whose row metrics to use. |
| headerWeight | 400 | 600 | 600 | 400: headers lose their semibold, as a font set on the whole list does. |
| aria-label | string | - | Names the list. |
| className, style | string, CSSProperties | - | On the list element. |
| children | ReactNode | - | Section elements, or rows (loose rows are a section with no header). |
Form takes the same props except listStyle: it is always insetGrouped, with form row metrics. Outside a NavigationPage a List fills its parent and scrolls itself.
Section
| Prop | Type | Default | Description |
|---|---|---|---|
| header | ReactNode | - | Above the rows. |
| footer | ReactNode | - | Below the rows. |
| onDelete | (index: number) => void | - | Adds swipe to delete and the minus circles. Remove the row from your data. |
| onMove | (from: number, to: number) => void | - | Reorder handles in edit mode; to is the index to insert before, in the list as it was. |
| children | ReactNode | - | The rows. Give each a key. |
Text, Label, LabeledContent, NavigationLink
| Prop | Type | Default | Description |
|---|---|---|---|
| Text children | ReactNode | - | The title. |
| Text subtitle | ReactNode | - | A second, secondary line. |
| Label title | ReactNode | - | The title. |
| Label icon | ReactNode | - | Drawn in the accent colour; a 20 px Lucide icon fits. |
| LabeledContent title | ReactNode | - | The title, at the leading edge. |
| LabeledContent value | ReactNode | - | The value, at the trailing edge; or pass it as children. |
| NavigationLink value | unknown | - | Required. Pushed on the page's stack on release; the row gets a chevron. |
| NavigationLink disabled | boolean | - | Doesn't push. |
| NavigationLink children | ReactNode | - | The row's view: Text, Label or LabeledContent. |
SwipeActions
| Prop | Type | Default | Description |
|---|---|---|---|
| trailing | SwipeAction[] | - | Revealed by a swipe to the left. |
| leading | SwipeAction[] | - | Revealed by a swipe to the right. |
| children | ReactNode | - | The row. |
SwipeAction
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Required. The button's title. |
| role | "destructive" | - | Red. |
| tint | string | gray | The button's colour. |
| icon | ReactNode | - | Drawn with the title. |
| onAction | () => void | - | Runs when it is chosen. |
A component of your own that is a row and keeps its own state (as a navigation-link Picker does) can give the row its press: a static listRowKind(props) returning "link" or "press", and the action set on the ListRowAction context's ref.
Behaviour
The press shows after a short hold while the scroll view waits; a move along the scroll axis ends it with no action, and a release off the row does nothing. A NavigationLink row pushes on release and stays highlighted under the new page. A full swipe acts on the outermost action. Inside a NavigationPage the list scrolls with the page and sets its background; alone it scrolls itself.
Each section's rows are a role="list" of role="listitem" rows.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| List { Section(header:footer:) { } }.listStyle(.plain) | <List listStyle="plain"><Section header footer> |
| Form { } | <Form> |
| ForEach.onDelete / .onMove | <Section onDelete onMove> |
| \.editMode / List(selection:) | editMode="active" / selection onSelectionChange |
| .swipeActions(edge: .leading) { } | <SwipeActions leading={[...]}> |
| Label("Wi-Fi", systemImage:) / LabeledContent("Name", value:) | <Label title icon> / <LabeledContent title value> |
| Text("Title") with a second line | <Text subtitle> |
| NavigationLink(value:) in a List | <NavigationLink value> |
| Toggle("Wi-Fi", isOn:) | <Toggle isOn onChange><Text>Wi-Fi</Text></Toggle> |
| Slider(...) minimumValueLabel: maximumValueLabel: | <Slider minimumValueLabel maximumValueLabel> |
| Button("Delete", role: .destructive) in a List | <Button role="destructive"> (no buttonStyle: the row's press) |
| .font(_:) on the List (headers lose semibold) | headerWeight={400} |