Color Gradient
avonni-color-gradient
Lets users pick a color from a gradient surface, with hue and opacity controls.
Overview
Color Gradient is a Lightning Web Component that provides an interactive gradient picker, letting users select a color by saturation, brightness, hue, and optional opacity.
Use it in your own Lightning Web Components to let users choose a precise color— for theming, tagging, or styling settings. You control the current value, whether the opacity slider is shown, read-only and disabled states, and the error message for invalid input—all through the component's attributes.
Use Cases
Theme builders: Let users pick brand or accent colors for a custom theme.
Styling settings: Capture a color value for a record or configuration.
Color tagging: Assign a color to categories, labels, or events.
Design tools: Provide a fine-grained picker with opacity control.
Read-only previews: Display a previously chosen color without editing.
Use Case Examples
Example 1: Color picker with opacity
Scenario: Let users choose a color with an opacity slider and capture the resulting value in multiple formats.
Result: A gradient picker with an opacity slider; each adjustment fires change with the color in hex, hexa, rgb, and rgba formats plus the alpha value.
Example 2: Read-only color preview
Scenario: Display a previously selected color without allowing edits.
Result: A non-editable gradient showing the stored color.
Specifications
Attributes
disabled
If present, the input field is disabled and users cannot interact with it.
Boolean
—
message-when-bad-input
Error message to be displayed when a bad input is detected.
String
"Please ensure value is correct"
read-only
If present, the palette is read-only and cannot be edited by users.
Boolean
false
show-opacity
If present, the opacity slider will be displayed.
Boolean
false
value
Specifies the value of an input element.
String
—
Methods
focus
Set the focus on the color gradient.
renderValue
Display the given color in the color gradient.
color
String
Color to display.
Custom Events
blur
The event fired when the color gradient loses focus.
The blur event doesn't return any parameters.
bubbles
false
This event does not bubble.
cancelable
false
This event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composed
false
This event does not propagate outside of the component in which it was dispatched.
change
The event fired when the color value changed.
The change event returns the following parameters.
hex
string
Color in hexadecimal format.
hexa
string
Color in hexadecimal format with alpha.
rgb
string
Color in rgb format.
rgba
string
Color in rgba format.
alpha
string
Alpha value of the color.
The event properties are as follows.
bubbles
true
This event bubbles up through the DOM.
cancelable
true
This event can be canceled. You can call preventDefault() on this event.
composed
false
This event does not propagate outside of the component in which it was dispatched.
focus
The event fired when the focus is set on the color gradient.
The focus event doesn't return any parameters.
bubbles
false
This event does not bubble.
cancelable
false
This event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composed
false
This event does not propagate outside of the component in which it was dispatched.
blur
The event fired when the color gradient loses focus.
The blur event doesn't return any parameters.
bubbles
false
This event does not bubble.
cancelable
false
This event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composed
false
This event does not propagate outside of the component in which it was dispatched.
change
The event fired when the color value changed.
The change event returns the following parameters.
hex
string
Color in hexadecimal format.
hexa
string
Color in hexadecimal format with alpha.
rgb
string
Color in rgb format.
rgba
string
Color in rgba format.
alpha
string
Alpha value of the color.
The event properties are as follows.
bubbles
true
This event bubbles up through the DOM.
cancelable
true
This event can be canceled. You can call preventDefault() on this event.
composed
false
This event does not propagate outside of the component in which it was dispatched.
focus
The event fired when the focus is set on the color gradient.
The focus event doesn't return any parameters.
bubbles
false
This event does not bubble.
cancelable
false
This event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composed
false
This event does not propagate outside of the component in which it was dispatched.
Styling Hooks
--avonni-color-gradient-max-width
dimension
20rem
Key Considerations
Output formats:
changeprovideshex,hexa,rgb,rgba, andalpha—pick the one your data model expects.Opacity: Use
hexaorrgbafrom the event detail whenshow-opacityis enabled to preserve the alpha channel.Read-only vs. disabled:
read-onlyshows the value but blocks editing;disabledgreys out and deactivates the control.Validation:
message-when-bad-inputsurfaces when an invalid value is supplied.Best Practice: Enable
show-opacityonly when your downstream usage supports an alpha channel; otherwise the chosen opacity may be discarded.
Troubleshooting Common Issues
Opacity slider missing: Confirm
show-opacityis set on the component.Alpha not captured: Read
hexaorrgba(nothex/rgb) fromevent.detailto keep the opacity value.Value not updating: Ensure your
changehandler writes the detail back to the boundvalueproperty and thatread-only/disabledare not set.If issues persist: Contact our support team at [email protected] for assistance.
Last updated
Was this helpful?
