Angle slider
An angle in degrees from a dial, dragged round or stepped by the keys, with marks.
Previews are drawn in the theme you made.
Edit itBasic #
Heading
export const AngleSliderStyle = forSlots(AngleSliderSlots)(Recipes.AngleSlider({}))
/** What `update` and the view agree on: whole degrees, the compass points marked. */
export const HEADING_LAYOUT: AngleSlider.AngleSliderLayout = {
step: 1,
marks: [
{ value: 0, label: 'N' },
{ value: 90, label: 'E' },
{ value: 180, label: 'S' },
{ value: 270, label: 'W' },
],
}
/** The angle and the drag are the dial's Model, folded with `AngleSlider.update(HEADING_LAYOUT)`. */
export const angleSlider = <Message>(
style: NamedStyle<typeof AngleSliderSlots>,
model: AngleSlider.Model,
onMessage: (message: AngleSlider.Message) => Message,
h: HtmlBuilder<Message>,
): Html =>
AngleSlider.view(
{ ...HEADING_LAYOUT, model, onMessage, label: 'Heading', name: 'heading', style },
h,
)Kept to marks #
Rotation
/** A quarter turn at a time: a drag lands on the nearest mark, and the keys go mark to mark. */
export const ROTATION_LAYOUT: AngleSlider.AngleSliderLayout = {
step: 90,
marks: [{ value: 0 }, { value: 90 }, { value: 180 }, { value: 270 }],
restrictToMarks: true,
}
export const angleSliderMarks = <Message>(
style: NamedStyle<typeof AngleSliderSlots>,
model: AngleSlider.Model,
onMessage: (message: AngleSlider.Message) => Message,
h: HtmlBuilder<Message>,
): Html =>
AngleSlider.view(
{
...ROTATION_LAYOUT,
model,
onMessage,
label: 'Rotation',
format: value => `${value / 90} of 4`,
valueText: value => `${value} degrees`,
style,
},
h,
)A bundle with its layout #
Bearing
import { AngleSlider, AngleSliderSlots, Bundles, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// The steps and marks are data, stated once: they are the Bundle's args (so
// `update` folds with them) and the view's options (so it draws them). The
// angle is the dial's own value, read from the placed Model.
const Layout = {
step: 15,
marks: [{ value: 0, label: 'N' }, { value: 90, label: 'E' }, { value: 180 }, { value: 270 }],
}
export default example({
title: 'A bundle with its layout',
slots: AngleSliderSlots,
recipe: Recipes.AngleSlider,
state: Bundles.AngleSlider.with({ id: 'bearing', value: 90, ...Layout }),
view: ({ style, model, onMessage, h }) =>
AngleSlider.view({ model, onMessage, label: 'Bearing', ...Layout, 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 #
Wind direction
import { AngleSlider, AngleSliderSlots, Bundles, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// Every angle slider of the application steps by fifteen and reads its angle
// aloud the same way, said once; a use gives its Model and its label.
const AppDial = AngleSlider.configure({
base: { step: 15, words: { degrees: '{value} degrees clockwise from north' } },
})
export default example({
title: 'Configure once',
slots: AngleSliderSlots,
recipe: Recipes.AngleSlider,
state: Bundles.AngleSlider.with({ id: 'wind', value: 45, step: 15 }),
view: ({ model, onMessage, h }) => AppDial.view({ model, onMessage, label: 'Wind direction' }, 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.