Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

ColorPanel

Since 0.1.0-alpha.22
Lets the user choose a color inline: swatches, a picker, or both.

A 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

Usage.razor
@* A calendar's category color: a swatch for the usual choices, the picker for the rest. *@
<ColorPanel @bind-Value="_color" Label="Category color" />

@code {
    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:

bash
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

SwatchesOnly.razor
@* 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" />

@code {
    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

Opacity.razor
@* 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 />

@code {
    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

What works in each mode, ColorPanel
FeatureStaticInteractive
Swatches that post with the formYesYes
Picker, opacity slider and number fieldsYesYes

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 SwatchText or 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

ColorPanel keys, On the area
Press this keyTo do this
Left or RightLower or raise the saturation by 1
Down or UpLower or raise the brightness by 1
Shift with an arrowMove by 10
Page Down or Page UpLower or raise the brightness by 10
Home or EndGo to the lowest or highest saturation

On the sliders

ColorPanel keys, On the sliders
Press this keyTo do this
Left or Right or Up or DownMove by 1
Shift with an arrow or Page Up or Page DownMove by 10
Home or EndGo to the start or end

On the swatches

ColorPanel keys, On the swatches
Press this keyTo do this
Left or Right or Up or DownChoose the swatch before or after

Parameters

ColorPanel parameters
PropTypeDefault
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.booltrue
ShowAlphaAn opacity slider and field; a color that isn't opaque is #rrggbbaa.boolfalse
ShowFieldsHex, red, green and blue fields (and opacity with ShowAlpha).booltrue
ClearableA "no color" swatch first, which makes the value null.boolfalse
ShowEyedropperAn eyedropper button beside the fields, shown only in browsers that have one (Chromium).boolfalse
ReadOnlyMakes the field read-only: nothing changes it, and it still posts.boolfalse
@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?

See 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.

Connection lost

Rejoining the server…