Pickers
Picker in its segmented, menu, navigation-link and wheel styles, DatePicker and ColorPicker: the SwiftUI pickers with their pop-ups, calendar, wheels and pushed option list.
import { ColorPicker, DatePicker, Picker, pickerDestination } from "@adamexu/glass";Where they live
Pickers are rows of a Form or List inside a NavigationStack, as in SwiftUI. Two things set them up:
- A navigation-link Picker pushes a value; give the stack a
destinationthat triespickerDestination(value)first. - Pop-ups, sheets and popovers go through
PresentationRoot. Itsidiompicks a phone's sheet or an iPad's popover, andwindow(the screen element's client rect, kept current on scroll and resize) androot(that element) keep them inside an embedded screen like the ones here. Without one they cover the browser window, and its width picks the idiom.
Picker
segmented is a SegmentedPicker; menu (and automatic) is the title and a menu button that opens the glass menu; navigationLink is a row with the selection and a chevron that pushes an inline-titled page of the options, a check on the selection, where picking another selects it and goes back; wheel is one column on a cylinder.
const destination = (value: unknown) => pickerDestination(value) ?? <NavigationPage title="Detail" />;
<PresentationRoot idiom="phone" window={screenRect} root={screen}>
<NavigationStack
destination={destination}
root={
<NavigationPage title="Pickers">
<Form>
<Section header="Segmented">
<Picker title="Flavor" options={FLAVORS} selection={segment} onChange={setSegment} pickerStyle="segmented" />
</Section>
<Section header="Menu & Navigation">
<Picker title="Menu Picker" options={FLAVORS} selection={menu} onChange={setMenu} pickerStyle="menu" />
<Picker title="Navigation Picker" options={FLAVORS} selection={navigation} onChange={setNavigation} pickerStyle="navigationLink" />
</Section>
<Section header="Wheel">
<Picker title="Flavor" options={FLAVORS} defaultSelection="Chocolate" pickerStyle="wheel" />
</Section>
</Form>
</NavigationPage>
}
/>
</PresentationRoot>DatePicker
compact (the default) is the label and one or two pills; a pill pops up a glass panel with the calendar or a time wheel, growing from the pill on a spring. graphical is the month in place: swipe it or step it with the arrows, and the title opens month and year wheels; with dateAndTime a Time row and pill sit under it. wheel is the columns in place.
const [date, setDate] = useState(() => new Date());
const destination = (value: unknown) => pickerDestination(value) ?? <NavigationPage title="Detail" />;
<PresentationRoot idiom="phone" window={screenRect} root={screen}>
<NavigationStack
destination={destination}
root={
<NavigationPage title="Pickers">
<Form>
<Section header="Date Picker">
<DatePicker selection={date} onChange={setDate}>
<Text>Date & Time</Text>
</DatePicker>
<DatePicker selection={date} onChange={setDate} displayedComponents="date" datePickerStyle="graphical">
<Text>Calendar</Text>
</DatePicker>
</Section>
<Section header="Wheels">
<DatePicker selection={date} onChange={setDate} displayedComponents="hourAndMinute" datePickerStyle="wheel">
<Text>Time</Text>
</DatePicker>
</Section>
</Form>
</NavigationPage>
}
/>
</PresentationRoot>"Today" is read from the clock; pass now to pin it. Names, column order, the first weekday and 12 or 24 hours come from locale, the browser's by default.
A wheel drags one to one with a finger or mouse past a 10 px gate and flings on lift; a tap on a row turns it there, and past an end it rubber-bands. A notched mouse wheel turns one row per notch; a trackpad follows and rests on the nearest row 120 ms after it stops.
ColorPicker
The label and a well (a swatch in a rainbow ring). The well opens a sheet on a phone and a popover beside it on an iPad, with Grid, Spectrum and Sliders tabs, opacity, saved colours and Chrome's eyedropper. onChange gives #RRGGBB (#RRGGBBAA when not opaque), or color(display-p3 ...) past sRGB.
const [color, setColor] = useState("#0088FF");
const destination = (value: unknown) => pickerDestination(value) ?? <NavigationPage title="Detail" />;
<PresentationRoot idiom="phone" window={screenRect} root={screen}>
<NavigationStack
destination={destination}
root={
<NavigationPage title="Pickers">
<Form>
<Section header="Color Picker">
<ColorPicker selection={color} onChange={setColor}>
<Text>Color</Text>
</ColorPicker>
<ColorPicker defaultSelection="#FF2D55" supportsOpacity={false}>
<Text>No opacity</Text>
</ColorPicker>
</Section>
</Form>
</NavigationPage>
}
/>
</PresentationRoot>Props
Picker
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Required. The row's label, the pushed page's title, the accessible name. |
| options | string[] | - | Required. The choices. |
| selection | string | - | Controlled selection. |
| defaultSelection | string | the first option | Initial selection when uncontrolled. |
| onChange | (value: string) => void | - | Runs with the picked option. |
| pickerStyle | "automatic" | "segmented" | "menu" | "navigationLink" | "wheel" | "automatic" | automatic is menu. |
| disabled | boolean | false | Stops responding. |
| tint | string | - | The menu style's button. |
DatePicker
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | - | The row's label (or pass label). |
| label | ReactNode | - | The row's label, if not children. |
| selection | Date | - | Controlled date. |
| defaultSelection | Date | now | Initial date when uncontrolled. |
| onChange | (date: Date) => void | - | A day picked, a wheel come to rest. |
| displayedComponents | "date" | "hourAndMinute" | "dateAndTime" | "dateAndTime" | What it picks. |
| datePickerStyle | "automatic" | "compact" | "graphical" | "wheel" | "automatic" | automatic is compact. graphical with hourAndMinute draws compact. |
| in | { min?: Date; max?: Date } | - | The range; days and rows outside it read gray. |
| disabled | boolean | false | Stops responding. |
| labelsHidden | boolean | false | The label isn't drawn. |
| locale | string | the browser's | BCP 47: names, column order, first weekday, 12 or 24 hours. |
| now | Date | today | The calendar's highlighted day. |
| tint | string | - | The accent: today, the open pill, the calendar's chevrons. |
| firstLayout | boolean | false | Only for comparing with an iOS capture: the graphical row as iOS first lays it out. |
| className, style | string, CSSProperties | - | On the row. |
ColorPicker
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | - | The row's label; a string names the well. |
| selection | string | - | "#RRGGBB", "#RRGGBBAA" or "color(display-p3 r g b / a)". |
| defaultSelection | string | "#0088FF" | Initial colour when uncontrolled. |
| onChange | (color: string) => void | - | "#RRGGBB" ("#RRGGBBAA" when not opaque), or color(display-p3 ...) past sRGB. |
| supportsOpacity | boolean | true | Shows the opacity track. |
| disabled | boolean | false | Stops responding. |
| labelsHidden | boolean | false | The label isn't drawn. |
| className, style | string, CSSProperties | - | On the row. |
Building blocks
The pickers are made of three exported pieces, for a screen of your own: Calendar (the month grid with its paging, title wheels and keyboard), TimeWheel (hours, minutes and AM/PM in the locale's clock) and Wheel (any columns on a cylinder). Each is controlled.
Calendar
| Prop | Type | Default | Description |
|---|---|---|---|
| selection | Date | - | Required. The selected day. |
| onChange | (date: Date) => void | - | Required. A day picked. |
| today | Date | - | Required. The highlighted day. |
| locale | string | - | Required. BCP 47. |
| layout | "inline" | "popup" | "regular" | "duo" | "inline" | A phone's row, the compact pop-up, an iPad's row, the Duo's outer panel. |
| min, max | Date | - | The range. |
| tint | string | - | The accent. |
| wheelBox | { x: number; y: number; width: number } | - | Where the month and year wheels sit. |
| footer | ReactNode | - | Under the grid. |
| style | CSSProperties | - | On the root. |
TimeWheel
| Prop | Type | Default | Description |
|---|---|---|---|
| value | Date | - | Required. |
| onChange | (date: Date) => void | - | Required. A column came to rest. |
| locale | string | - | Required. 12 or 24 hours, column order. |
| width | number | - | Required. |
| height, center | number | 216, 107.833 | The wheel's height and where its axis is. |
| disabled | boolean | false | Stops responding. |
Wheel
| Prop | Type | Default | Description |
|---|---|---|---|
| columns | WheelColumn[] | - | Required. Each column's id, position, width, alignment, row range, text and spoken value. |
| values | Record<string, number> | - | Required. Each column's row. |
| onChange | (id: string, index: number, from: number) => void | - | Required. A column came to rest on a row. |
| onCommit | () => void | - | While moving, what's under the band changed. |
| width | number | - | Required. |
| height, center | number | 216, 107.833 | The wheel's height and where its axis is. |
| row | number | 32 | Row pitch: 32 in a date picker, 30 in a Picker. |
| bandInset | number | 9 | The band's inset from the sides: none under a calendar's title. |
| fontSize | number | - | One size for every row; a date picker's are 21 and 23.5 by default. |
| disabled | boolean | false | Stops responding. |
| aria-label, style | string, CSSProperties | - | On the role="group" root. |
Keyboard and accessibility
- A wheel column is a
spinbutton: ArrowUp and ArrowDown step, PageUp and PageDown step five, Home and End go to the ends. - The calendar's days are a
gridofgridcellbuttons: arrows move a day or a week, Home and End go to the week's ends, PageUp and PageDown a month, Enter and Space pick. - A compact pill and the colour well have
aria-haspopup="dialog"andaria-expanded; a tap outside or Escape closes the pop-up and focus goes back.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| Picker(title, selection:) { } | <Picker title options selection onChange> |
| .pickerStyle(.segmented / .menu / .navigationLink / .wheel) | pickerStyle="segmented" / "menu" / "navigationLink" / "wheel" |
| .navigationDestination for a navigation-link Picker | destination={(v) => pickerDestination(v) ?? ...} |
| DatePicker(title, selection:, in:, displayedComponents:) | <DatePicker selection onChange in displayedComponents>title</DatePicker> |
| .datePickerStyle(.compact / .graphical / .wheel) | datePickerStyle="compact" / "graphical" / "wheel" |
| displayedComponents: [.date, .hourAndMinute] | displayedComponents="dateAndTime" (or "date", "hourAndMinute") |
| .environment(\.locale, Locale(identifier:)) | locale |
| ColorPicker(title, selection:, supportsOpacity:) | <ColorPicker selection onChange supportsOpacity>title</ColorPicker> |
| .labelsHidden() | labelsHidden |