Segmented
A few options in a tray, one pressed, with a pill that slides to a new press.
Previews are drawn in the theme you made.
Edit itBasic #
export const SegmentedStyle = forSlots(SegmentedSlots)(Recipes.Segmented({}))
export const Range = Schema.Literals(['day', 'week', 'month', 'year'])
export type Range = typeof Range.Type
export const segmented = <Message>(
style: NamedStyle<typeof SegmentedSlots>,
range: Range,
onRange: (range: Range) => Message,
direction: 'ltr' | 'rtl',
h: HtmlBuilder<Message>,
): Html =>
Segmented.view(
{
id: 'range',
label: 'Range',
// A single choice is a radio group: its arrows choose, and swap when right to left.
direction,
options: Range.literals.map(value => ({
value,
label: value[0]!.toUpperCase() + value.slice(1),
})),
pressed: [range],
onPress: onRange,
style,
// The pill glides to a new press; one is pressed at a time.
mixins: Segmented.fluid,
},
h,
)Icons #
export const ViewMode = Schema.Literals(['list', 'board', 'calendar'])
export type ViewMode = typeof ViewMode.Type
const modes: Record<ViewMode, { readonly label: string; readonly icon: string }> = {
list: { label: 'List', icon: 'M2 4h12M2 8h12M2 12h12' },
board: { label: 'Board', icon: 'M2 2h5v12H2zM9 2h5v7H9z' },
calendar: { label: 'Calendar', icon: 'M2 4h12v10H2zM2 7h12M5 2v3M11 2v3' },
}
// An icon before each option's words, or alone (`iconOnly`), named by its label.
export const segmentedIcons = <Message>(
style: NamedStyle<typeof SegmentedSlots>,
mode: ViewMode,
onMode: (mode: ViewMode) => Message,
iconOnly: boolean,
direction: 'ltr' | 'rtl',
h: HtmlBuilder<Message>,
): Html =>
Segmented.view(
{
id: 'view',
label: 'View',
direction,
options: ViewMode.literals.map(value => ({
value,
label: modes[value].label,
icon: outline(h, modes[value].icon),
iconOnly,
})),
pressed: [mode],
onPress: onMode,
style,
mixins: Segmented.fluid,
},
h,
)Icons alone #
// The icons alone, each option named by its label (`aria-label` and `title`):
// `segmentedIcons(style, mode, onMode, true, direction, h)`.Sizes #
export const SegmentedSizes = (['sm', 'md', 'lg'] as const).map(size =>
forSlots(SegmentedSlots)(Recipes.Segmented({ size })),
)
// Three steps of the size ladder, each a control of its own.
export const segmentedSizes = <Message>(
range: Range,
onRange: (range: Range) => Message,
direction: 'ltr' | 'rtl',
h: HtmlBuilder<Message>,
): Html =>
h.div(
[h.Style({ display: 'grid', gap: '0.75rem', justifyItems: 'start' })],
SegmentedSizes.map(style => segmented(style, range, onRange, direction, h)),
)Several at once #
export const Weekday = Schema.Literals(['mon', 'tue', 'wed', 'thu', 'fri'])
export type Weekday = typeof Weekday.Type
// With `multiple` it is a group of toggle buttons (`aria-pressed`), any number
// on, and `name` submits each chosen value. `Segmented.toggled(pressed, value,
// { multiple: true })` is the update. Draw it without `fluid`: the pill marks one.
export const segmentedMultiple = <Message>(
style: NamedStyle<typeof SegmentedSlots>,
days: ReadonlyArray<Weekday>,
onDay: (day: Weekday) => Message,
h: HtmlBuilder<Message>,
): Html =>
Segmented.view(
{
multiple: true,
label: 'Repeat on',
name: 'repeat',
options: Weekday.literals.map(value => ({
value,
label: value[0]!.toUpperCase() + value.slice(1),
})),
pressed: days,
onPress: onDay,
style,
},
h,
)Full width #
export const SegmentedFullStyle = forSlots(SegmentedSlots)(Recipes.Segmented({ width: 'fill' }))Underline #
export const SegmentedUnderlineStyle = forSlots(SegmentedSlots)(
Recipes.Segmented({ tray: 'underline' }),
)Vertical toggle group #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Segmented, SegmentedSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Model = Schema.Struct({ pressed: Schema.Array(Schema.String) })
const Message = defineMessageUnion({ Pressed: { value: Schema.String } })
// `multiple` is a toggle group: any number on, each a button with
// `aria-pressed`. `orientation: 'vertical'` stacks the options; it is a look
// over the same markup, read from `data-orientation`.
export default example({
title: 'Vertical toggle group',
slots: SegmentedSlots,
recipe: Recipes.Segmented,
state: {
Model,
Message,
init: () => ({ model: { pressed: ['Bold'] } }),
update: (model, { value }) => ({
model: { pressed: Segmented.toggled(model.pressed, value, { multiple: true }) },
}),
},
view: ({ style, model, onMessage, h }) =>
Segmented.view(
{
multiple: true,
orientation: 'vertical',
label: 'Text style',
options: ['Bold', 'Italic', 'Underline'].map(value => ({ value, label: value })),
pressed: model.pressed,
onPress: value => onMessage(Message.Pressed({ value })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Configure once #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Recipes, Segmented, SegmentedSlots } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Small = Recipes.Segmented.style({ size: 'sm' })
// Every segmented control of the application is small, and glides its pill
// (`Segmented.fluid`, passed here and nowhere else); a use gives only its
// options and its choice.
const AppSegmented = Segmented.configure({ base: { style: Small, mixins: Segmented.fluid } })
const Model = Schema.Struct({ range: Schema.String })
const Message = defineMessageUnion({ Chose: { range: Schema.String } })
export default example({
title: 'Configure once',
slots: SegmentedSlots,
recipe: Recipes.Segmented,
state: {
Model,
Message,
init: () => ({ model: { range: 'week' } }),
update: (model, { range }) => ({ model: model.range === range ? model : { range } }),
},
view: ({ model, onMessage, h }) =>
AppSegmented.view(
{
id: 'range',
label: 'Range',
options: ['day', 'week', 'month'].map(value => ({ value, label: value })),
pressed: [model.range],
onPress: range => onMessage(Message.Chose({ range })),
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Usage
Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.
Related
- What a form submits Give the widget a name and a form reads it as it reads a native field.
- Right to left The direction switch above turns the preview over; this is what each widget reads.