Color picker
A color from a saturation and brightness area, hue and alpha tracks, typed in hex, rgb, hsl or oklch, or picked from the screen.
Previews are drawn in the theme you made.
Edit itInline #
Accent
Hue
Alpha
import { Bundles, ColorPicker, ColorPickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
// The hue and alpha tracks are Sliders, painted from the color.
const Channel = Recipes.Slider.style({ track: 'paint', label: 'hidden' })
// The colors one press away: the one equal to the color is checked.
const accents: ReadonlyArray<ColorPicker.ColorPickerPreset> = [
{ label: 'Blue', value: ColorPicker.color('#3b82f6') },
{ label: 'Teal', value: ColorPicker.color('#14b8a6') },
{ label: 'Amber', value: ColorPicker.color('#f59e0b') },
{ label: 'Rose', value: ColorPicker.color('#f43f5e') },
{ label: 'Glass', value: ColorPicker.color('#3b82f680') },
]
// The color, its format and the text being typed are the Bundle's Model. It
// holds the color, so a parent that wants it reads `model.<field>.color` or
// answers `Changed` in `onOut`. `eyedropper` is false unless the application
// asks, so a server and a browser draw the same picker.
export default example({
title: 'Inline',
slots: ColorPickerSlots,
recipe: Recipes.ColorPicker,
state: Bundles.ColorPicker.with({ id: 'accent', color: ColorPicker.color('#3b82f6') }),
view: ({ style, model, onMessage, h }) =>
ColorPicker.view(
{ model, onMessage, label: 'Accent', presets: accents, style, slider: Channel },
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.
In a popover #
import { Bundle } from 'foldkit-bundle'
import { Bundles, ColorPicker, ColorPickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Channel = Recipes.Slider.style({ track: 'paint', label: 'hidden' })
const Panel = Recipes.Popover.style({})
const accents: ReadonlyArray<ColorPicker.ColorPickerPreset> = [
{ label: 'Blue', value: ColorPicker.color('#3b82f6') },
{ label: 'Rose', value: ColorPicker.color('#f43f5e') },
{ label: 'Amber', value: ColorPicker.color('#f59e0b') },
]
// The picker behind a swatch of its color, in a Popover that is a Bundle of
// its own. `ColorPicker.popover` is a value of its own file, so an inline
// picker never loads the Popover; the Popover is made with `contentFocus`,
// since the picker owns its panel's focus and a press or drag inside never
// closes it.
const App = Bundle.compose({}).pipe(
Bundle.withChild('picker', Bundles.ColorPicker, {
args: { id: 'brand', color: ColorPicker.color('#6366f1') },
onOut: Bundle.ignore,
}),
Bundle.withChild('popover', Bundles.Popover, {
args: { id: 'brand-popover', contentFocus: true, isAnimated: true },
onOut: Bundle.ignore,
}),
)
export default example({
title: 'In a popover',
slots: ColorPickerSlots,
recipe: Recipes.ColorPicker,
state: App.placements.runtime({ Model: App.Model, initial: {} }),
view: ({ style, model, onMessage, h }) =>
ColorPicker.popover(
{
model: model.picker,
onMessage: message => onMessage(App.Message.GotPickerMessage({ message })),
popover: model.popover,
onPopover: message => onMessage(App.Message.GotPopoverMessage({ message })),
label: 'Brand color',
presets: accents,
style,
slider: Channel,
popoverMixins: [Panel.mixin],
},
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 #
Ink
Tint
import { Bundles, ColorPicker, ColorPickerSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
const Channel = Recipes.Slider.style({ track: 'paint', label: 'hidden' })
// What every picker of an application starts from: its swatches, its sliders'
// look and its words. A preset is a named set over it: the opaque one drops
// the alpha slider.
const AppPicker = ColorPicker.configure({
base: {
slider: Channel,
presets: [
{ label: 'Ink', value: ColorPicker.color('#111827') },
{ label: 'Sky', value: ColorPicker.color('#0ea5e9') },
{ label: 'Lime', value: ColorPicker.color('#84cc16') },
],
words: { hue: 'Tint', presets: 'Palette' },
},
presets: { opaque: { alpha: false } },
})
export default example({
title: 'Configure once',
slots: ColorPickerSlots,
recipe: Recipes.ColorPicker,
state: Bundles.ColorPicker.with({
id: 'ink',
color: ColorPicker.color('#0ea5e9'),
format: 'rgb',
}),
view: ({ style, model, onMessage, h }) =>
AppPicker.view({ model, onMessage, label: 'Ink', preset: 'opaque', 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.
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.