ColorPanel
Since 0.1.0-alpha.22A color, chosen inline: swatches, a picker (a saturation and brightness area and a hue slider), an opacity slider, and hex and RGB fields, each optional. Bound to a hex string, "#2563eb", or "#2563ebcc" with ShowAlpha.
Swatches and a picker
A ColorPanel shows swatches for the usual choices and a picker for any other color. The value is a hex string such as #22c55e.
Static
Preview
Code
@* A calendar's category color: a swatch for the usual choices, the picker for the rest. *@
<ColorPanel @bind-Value="_color" Label="Category color" />
{
private string? _color = "#22c55e";
}Static: IzzyBlazor's script runs the picker and fields, and keeps one value that posts with the form.
Add this example to a page with the izzy CLI:
izzy add component ColorPanel --at <file>:<line>The picker has a saturation and brightness area, a hue slider, and hex and RGB fields. Beside the sliders, a swatch shows the current color.
To set your own swatches, set Swatches. To hide the picker or the fields, set ShowPicker or ShowFields to false.
Swatches only
For a fixed set of colors, set ShowPicker and ShowFields to false and give Swatches. Name each with SwatchText; otherwise screen readers hear the color's name, such as "light green". Clearable adds a swatch for no color.
Static
Preview
Code
@* A label's color from the team's own set, named so screen readers say "Ocean", not a hex value. *@
<ColorPanel @bind-Value="_color" Label="Label color" ShowPicker="false" ShowFields="false" Clearable
Swatches="(["#0ea5e9", "#14b8a6", "#f59e0b", "#e11d48", "#8b5cf6"])" SwatchText="Name" />
{
private string? _color = "#14b8a6";
private static string? Name(string hex) => hex switch
{
"#0ea5e9" => "Ocean",
"#14b8a6" => "Lagoon",
"#f59e0b" => "Sunset",
"#e11d48" => "Berry",
"#8b5cf6" => "Dusk",
_ => null,
};
}Any color, see-through
For the picker alone, set Swatches to an empty list. ShowAlpha adds an opacity slider and field, and the value becomes #rrggbbaa when the color isn't opaque. ShowEyedropper adds a button that picks a color from the screen, in browsers that have one.
Static
Preview
Code
@* A hero banner's overlay: any color, see-through, so the photo shows under the headline. *@
<ColorPanel @bind-Value="_overlay" Label="Overlay" Swatches="([])" ShowAlpha ShowEyedropper />
{
private string? _overlay = "#0f172a99";
}Details
Changes apply at once. With no script, the swatches are radios and post as the field; with none, the hex field does. With script, all parts write one value, kept in a hidden field that posts.
Mode support
| Feature | Static | Interactive |
|---|---|---|
| Swatches that post with the form | Yes | Yes |
| Picker, opacity slider and number fields | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Without JavaScript
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, the swatches (or, with none, the hex field) choose the color and post; the picker, sliders and number fields need script.
Accessibility
- It's a group named by the label. The swatches are a radio group, each named by
SwatchTextor by its color's name ("light green"); the chosen one has a ring around it, set apart from the swatch by a gap, so it shows on any color, and the focus outline sits outside that. - The area is one slider with the role "2D slider": its value says both numbers and the color's name ("Saturation 45%, brightness 80%, dark blue"). Left and Right change the saturation, Up and Down the brightness.
- Hue and opacity are sliders; a click anywhere on the area or a slider jumps there, and the fields take typed values, so nothing needs dragging.
Keyboard navigation
On the area
| Press this key | To do this |
|---|---|
| Left or Right | Lower or raise the saturation by 1 |
| Down or Up | Lower or raise the brightness by 1 |
| Shift with an arrow | Move by 10 |
| Page Down or Page Up | Lower or raise the brightness by 10 |
| Home or End | Go to the lowest or highest saturation |
On the sliders
| Press this key | To do this |
|---|---|
| Left or Right or Up or Down | Move by 1 |
| Shift with an arrow or Page Up or Page Down | Move by 10 |
| Home or End | Go to the start or end |
On the swatches
| Press this key | To do this |
|---|---|
| Left or Right or Up or Down | Choose the swatch before or after |
Parameters
| Prop | Type | Default |
|---|---|---|
SwatchesThe swatches, as hex strings. | IEnumerable<string>? | computed |
SwatchTextA name for each swatch, such as "Ocean", given its hex string; screen readers hear it and it's the tooltip. | Func<string, string?>? | computed |
ShowPickerThe picker: a saturation and brightness area and a hue slider. | bool | true |
ShowAlphaAn opacity slider and field; a color that isn't opaque is #rrggbbaa. | bool | false |
ShowFieldsHex, red, green and blue fields (and opacity with ShowAlpha). | bool | true |
ClearableA "no color" swatch first, which makes the value null. | bool | false |
ShowEyedropperAn eyedropper button beside the fields, shown only in browsers that have one (Chromium). | bool | false |
ReadOnlyMakes the field read-only: nothing changes it, and it still posts. | bool | false |
@bind-ValueThe value, bound with @bind-Value. | string? | |
DisplayNameThe field's name in validation messages from the input itself, such as a value that can't be parsed. | string? |
Plus 28 field props.
Full APISee also
- ColorPicker: a swatch and a hex box; the swatch opens a color panel.
- RadioGroup: lets the user pick one choice from a list of radio buttons.