Skip to main content

LgColorPickerBox

LgColorPickerBox allows you to select a color from a dropdown palette or enter a custom color (CSS name or hexadecimal code). It inherits from LgTextBox: the bound value is a string, normalized to uppercase hexadecimal format (#RRGGBB).

<LgColorPickerBox Label="#Color" @bind-Value="_model.Color" />
  • Namespace: Lagoon.UI.Components
  • Inherits from: LgTextBox → LgInputRenderBase<string> → LgInputBase<string> → LgAriaComponentBase → LgComponentBase
  • Interfaces: ILgDropDownComponent
  • NuGet package: Lagoon.UI

Usage​

Demonstration​

Try this component in the Lagoon showcase.

Selection from the palette​

Without parameters, the proposed palette is the one from the application behavior configuration. The input accepts a CSS name (red), a full hexadecimal code (#FF0000), short (#f00) or without # (f00) — the value is always normalized to #RRGGBB.

TagColorPage.razor
<LgColorPickerBox Label="#TagColor" @bind-Value="_model.Color" />
TagColorPage.razor.cs
public partial class TagColorPage : LgPage
{
private readonly TagVm _model = new();

private sealed class TagVm
{
public string Color { get; set; }
}
}

Field-specific palette​

Palette overrides the application palette for this field.

ThemeColorPage.razor
<LgColorPickerBox Label="#ThemeColor"
Palette="_customPalette"
@bind-Value="_model.Color" />
ThemeColorPage.razor.cs
public partial class ThemeColorPage : LgPage
{
// Brand colors: only this color palette is offered to the user.
private readonly List<string> _customPalette = new() { "#1B6EC2", "#0E7490", "#22B8DD" };

private readonly ThemeVm _model = new();

private sealed class ThemeVm
{
public string Color { get; set; }
}
}

The default palette for the entire application is customized in the client behavior configuration (Main of the client project).

Hexadecimal input and error message​

ShowInput displays or hides the hexadecimal input field (by default, the application configuration applies). ParsingErrorMessage customizes the message displayed when the value is not a valid color.

BadgeColorPage.razor
@* Strict selection from the palette, without free input. *@
<LgColorPickerBox Label="#BadgeColor"
ShowInput="false"
@bind-Value="_model.PaletteColor" />

@* Free input with business error message. *@
<LgColorPickerBox Label="#CustomColor"
ShowInput="true"
ParsingErrorMessage="#InvalidBadgeColor"
@bind-Value="_model.CustomColor" />
BadgeColorPage.razor.cs
public partial class BadgeColorPage : LgPage
{
private readonly BadgeVm _model = new();

private sealed class BadgeVm
{
public string PaletteColor { get; set; }
public string CustomColor { get; set; }
}
}

Reacting to value changes​

OnChangingAsync (inherited from LgTextBox) is called before the assignment and allows canceling the change (args.Cancel); OnChange is called after.

ChartColorPage.razor
<LgColorPickerBox Label="#SeriesColor"
OnChangingAsync="OnColorChangingAsync"
@bind-Value="_model.SeriesColor" />
ChartColorPage.razor.cs
public partial class ChartColorPage : LgPage
{
private readonly ChartVm _model = new();

// Called before the change: refuses colors already used by another series.
private Task OnColorChangingAsync(ChangingEventArgs<string> args)
{
if (args.NewValue == "#FF0000")
{
args.Cancel = true;
ShowWarning("#ColorAlreadyUsed");
}
return Task.CompletedTask;
}

private sealed class ChartVm
{
public string SeriesColor { get; set; }
}
}

Properties​

All [Parameter] properties of the component, including inherited ones.

NameTypeDefaultDeclared inDescription
PaletteList<string>nullLgColorPickerBoxColors offered in the palette. If null, the palette from the application configuration is used.
ParsingErrorMessagestring#lgColorBoxErrFormatLgColorPickerBoxError message displayed when the input is not a valid color.
ShowInputbool?nullLgColorPickerBoxDisplays the hexadecimal input field. If null, the application configuration applies.
AllowResizeboolfalseLgTextBoxAllows resizing when TextMode is TextBoxMode.Multiline.
AutoCompleteboolfalseLgTextBoxEnables browser autocomplete if true; otherwise the autocomplete="off" attribute is rendered.
AutoUnmaskboolfalseLgTextBoxReturns the value without the mask when InputMask is used.
ClearIncompleteboolfalseLgTextBoxClears the value if the input mask is incomplete.
DataListList<string>nullLgTextBoxList of values used to render a datalist associated with the input.
InputMaskstringnullLgTextBoxInput mask applied to the text field.
InputMaskKindInputMaskKindNoneLgTextBoxType of input mask.
MaxLengthint?nullLgTextBoxMaximum number of characters.
MaxRowsint?nullLgTextBoxMaximum number of rows when TextMode is TextBoxMode.Multiline.
MinRowsint?nullLgTextBoxMinimum number of rows when TextMode is TextBoxMode.Multiline.
OnChangeDisabledboolfalseLgTextBoxDisables the native onchange handler of the field.
OnChangingAsyncFunc<ChangingEventArgs<string>, Task>nullLgTextBoxCallback called before the value changes; the change can be canceled there.
OnFocusEventCallback<FocusEventArgs>—LgTextBoxCallback called when the field receives focus.
OnInputEventCallback<ChangeEventArgs>—LgTextBoxCallback called on input (oninput).
OnKeyUpEventCallback<KeyboardEventArgs>—LgTextBoxCallback called when a key is released.
Placeholderstring"red, #FF0000, #f00, f00, ..."LgTextBoxHint text for the hexadecimal input field.
Rowsint?nullLgTextBoxNumber of rows when TextMode is TextBoxMode.Multiline.
TextModeTextBoxMode?nullLgTextBoxText field mode. Leave null for the color picker.
PrefixstringnullLgInputRenderBasePrefix displayed in the field.
PrefixTypeInputLabelTypeTextLgInputRenderBaseNature of the prefix.
SuffixstringnullLgInputRenderBaseSuffix displayed in the field.
SuffixTypeInputLabelTypeTextLgInputRenderBaseNature of the suffix.
AdditionalAttributesIReadOnlyDictionary<string, object>nullLgInputBaseAdditional HTML attributes applied to the rendered element.
AutoFocusboolfalseLgInputBaseGives focus to the field on first render.
ConfirmationMessagestringnullLgInputBaseConfirmation message displayed before applying a value change.
ContainerIInputContainer<string>nullLgInputBaseInternal use: attaches a GridView cell to the field.
CssClassstringnullLgInputBaseAdditional CSS classes.
DisabledboolfalseLgInputBaseDisables the field.
DisplayNamestringnullLgInputBaseField name used in error messages.
HideValidationMessageboolfalseLgInputBaseHides the validation message below the field.
LabelstringnullLgInputBaseField label.
LabelContentRenderFragmentnullLgInputBaseCustom content for the label area.
OnChangeEventCallback<ChangeEventArgs>—LgInputBaseCallback called after the value changes.
OnResetEventCallback<ChangeEventArgs>—LgInputBaseCallback called when the field is reset by the clear button.
PolicyEditstringnullLgInputBasePolicy required to edit the field.
PolicyVisiblestringnullLgInputBasePolicy required to display the field.
ReadOnlyboolfalseLgInputBaseDisplays the color as read-only.
Requiredbool?nullLgInputBaseRequired field. If null, inferred from the [Required] attribute of the bound property. A required field does not show the clear button.
ResetButtonbool?nullLgInputBaseDisplays the clear button. If null, the application behavior configuration applies.
ResetTextstringnullLgInputBaseText of the clear button.
ResetTextAriaLabelstringnullLgInputBaseAccessible label (aria-label) of the clear button.
TooltipstringnullLgInputBaseTooltip.
TooltipIsHtmlboolfalseLgInputBaseIndicates whether the Tooltip content is in HTML format (otherwise plain text).
ValuestringnullLgInputBaseBound color (@bind-Value), normalized to #RRGGBB format.
ValueChangedEventCallback<string>—LgInputBaseCallback for updating the bound value.
ValueExpressionExpression<Func<string>>—LgInputBaseExpression identifying the bound property.
AriaLabelstringnullLgAriaComponentBaseValue of the aria-label attribute.
AriaLabelledBystringnullLgAriaComponentBaseValue of the aria-labelledby attribute.
Cascading parameters

LgColorPickerBox also consumes parameters automatically provided by ancestor components (you don't need to set them): ParentAuthorizeView (LgAuthorizeView) and the edit context from a parent LgEditForm.

Events​

EventTypeDescription
OnChangingAsyncFunc<ChangingEventArgs<string>, Task>Before the color changes; args.Cancel = true cancels the change.
OnChangeEventCallback<ChangeEventArgs>After the color changes.
OnFocusEventCallback<FocusEventArgs>When the field receives focus.
OnResetEventCallback<ChangeEventArgs>When the field is reset by the clear button.

Best practices / Accessibility​

  • Store the normalized #RRGGBB value as is: the component produces it in uppercase, regardless of the input format.
  • ShowInput="false" limits the user to the palette: combine it with Palette to enforce a color palette.
Complete reference

All parameters (including inherited ones): API Reference — LgColorPickerBox.