Color Picker
lightning-input
type="color"
For Use In
Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A color picker enables you to specify a color using a color picker or by entering the color into a text field.
lightning-input implements designs in the Salesforce Lightning Design System (SLDS). The input types adapt to SLDS 1 or SLDS 2 styling based on the org's theme or the container app that you use.
| Input Type | SLDS 1 | SLDS 2 |
|---|---|---|
| color | Color Picker | Color Picker |
To ensure a color is selected, use the required attribute. The color value must be in hexadecimal format #RRGGBB. You can also use the pattern attribute to enforce specific color format requirements.
For the default error messages, see the lightning-input documentation.
To validate the color input, use the checkValidity() method and access the value property to verify the color format. If the value isn't valid, the reportValidity() method shows the error message below the input field.
The validity attribute returns an object with read-only boolean properties. For the color type, these attributes apply:
badInput- Indicates that the value is a valid hexadecimal color formatcustomError- Indicates that a custom error has been set usingsetCustomValidity()patternMismatch- Indicates that the value doesn't match the specified pattern attributevalueMissing- Indicates that no value is provided when therequiredattribute is setvalid- True if none of the preceding properties are true
To provide contextual help content, specify help text with the field-level-help attribute. Field-level help adds an info icon next to the input label, with a tooltip displaying your specified help text.
To provide sample input in the field, use the placeholder attribute. For example, in a color type field, show an example hexadecimal value.
Use the change event for immediate updates as the user types or select a color. Alternatively, use the commit event to handle changes only when the user presses Enter or moves focus away from the input field. See Custom Events.
The onchange event provides real-time feedback, while oncommit waits for user confirmation.
Use a combination of variants and utility classes to customize your color picker.
Use the variant attribute with one of these values to position the labels differently relative to the color picker.
standardis the default, which displays the label above the color picker.label-hiddenhides the label but make it available to assistive technology. If you provide a value forfield-level-help, the tooltip icon is still displayed.label-inlinealigns the label and color picker horizontally.label-stackedplaces the label above the color picker.
In most contexts, a stacked label (standard or label-stacked variant) results in better readability and clarity. Use horizontal labels (label-inline variant) when you want to conserve vertical space and have fewer than 10 fields.
To apply additional styling, use the SLDS utility classes with the class attribute.
Component styling hooks provide CSS custom properties that use the --slds-c-* prefix and they change styling for specific elements or properties of a component. Component styling hooks are supported for SLDS 1 only. See the SLDS 1 component blueprints for available component styling hooks.
For more information, see Style Components Using Lightning Design System Styling Hooks in the Lightning Web Components Developer Guide.
When you use the label attribute, the component generates a unique ID for the internal <label> and uses a standard for attribute to link it to the color field.
If you use the label-hidden variant, the component maintains the for attribute to link between the color field and the label.
If you use the field-level-help attribute, the component creates an aria-describedby link between the color field and the help tooltip.
If the color field fails validation, the component adds aria-invalid="true" and links the error message to the input by using aria-describedby.
For additional ARIA attributes that you can use, see the lightning-input Specifications tab.
change
The event fired when a value is changed in the input field.
The change event returns this event.detail parameter.
| Parameter | Type | Description |
|---|---|---|
| value | string | Returns the input value. |
The event properties are as follows.
| Property | Value | Description |
|---|---|---|
| bubbles | true | This event bubbles up through the DOM. |
| cancelable | false | This event has no default behavior that can be canceled. You can't call preventDefault() on this event. |
| composed | true | This event propagates outside of the component in which it was dispatched. |
commit
The event fired when you press Enter after interacting with the input, or move away from the input so it loses focus.
Use event.target.value to access the input value.
The event properties are as follows.
| Property | Value | Description |
|---|---|---|
| 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. |
For this component's attributes and methods, see the lightning-input Specifications tab.