Skip to main content Sidebar
Skip to table of contents

    Color Picker

    <pc-color-picker> 1.0.0-alpha.2 experimental

    Colour pickers allow the user to pick a colour.

    Edit

    This component works well with standard <form> elements. Please refer to the form controls page to learn more about form submission and client‐side validation.

    Demos#

    Initial value#

    Use the value attribute to set an initial value for the colour picker.

    Edit

    Opacity#

    Use the opacity attribute to enable the opacity slider.

    Edit

    Formats#

    Use the format attribute to set the colour picker’s format. Valid options include hex, rgb, hsl and hsv. Note that the colour picker’s input will accept any parsable format (including CSS colour names) regardless of this option.

    To prevent users from toggling the format themselves, add the no-format-toggle attribute.

    Edit

    Swatches#

    Use the swatches attribute to add convenient presets to the colour picker. Any format the colour picker can parse is acceptable (including CSS colour names), but each value must be separated by a semicolon (;). Alternatively, you can pass an array of colour values to this property using JavaScript.

    Edit

    Size#

    Use the size attribute to change the colour picker’s size.

    Edit

    Disabled#

    Use the disabled attribute to disable the colour picker.

    Edit

    Inline#

    The colour picker can be rendered inline instead of in a popup using the inline attribute.

    Edit

    Properties#

    NameDescriptionReflectsDefault
    valueThe current value of the colour picker. The value’s format will vary based on the format attribute. To get the value in a specific format, use the getFormattedValue() method. The value is submitted as a name/value pair with form data.
    Type: string
    ""
    defaultValueThe default value of the colour picker. Primarily used for resetting the colour picker.
    Type: string
    ""
    labelThe colour picker’s label. If you need to display HTML, use the label slot instead.
    Type: string
    ""
    hintThe colour picker’s hint. If you need to display HTML, use the hint slot instead.
    Type: string
    ""
    formatThe format to use. The colour picker will accept user input in any format (including CSS colour names) and convert it to the desired format.
    Type: "hex" | "rgb" | "hsl" | "hsv"
    "hex"
    inlineRenders the color picker inline rather than in a dropdown.
    Type: boolean
    false
    sizeDetermines the size of the colour picker’s trigger. This has no effect on inline colour pickers.
    Type: "small" | "medium" | "large"
    "medium"
    noFormatToggle
    no-format-toggle
    Removes the button that lets users toggle between formats.
    Type: boolean
    false
    nameThe name of the colour picker, submitted as a name/value pair with form data.
    Type: string
    ""
    disabledDisables the colour picker.
    Type: boolean
    false
    openIndicates whether or not the popup is open. You can toggle this attribute to show and hide the popup, or you can use the show() and hide() methods and this attribute will reflect the popup’s open state.
    Type: boolean
    false
    opacityShows the opacity slider.
    Type: boolean
    false
    uppercaseBy default, values are lowercase. With this attribute, values will be uppercase instead.
    Type: boolean
    false
    swatchesOne or more predefined colour swatches to display as presets in the colour picker. Can include any format the colour picker can parse, including hex, rgb, hsl, hsv and CSS colour names. Each colour must be separated by a semicolon (;). Alternatively, you can pass an array of colour values to this property using JavaScript.
    Type: string | string[]
    ""
    formBy default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work.
    Type: string
    ""
    requiredIndicates if the colour picker must be filled in or not.
    Type: boolean
    false
    validityGets the validity state object.
    validationMessageGets the validation message.
    updateComplete A read‐only promise that resolves when the component has finished updating.

    Learn more about attributes and properties.

    Slots#

    NameDescription
    labelThe colour picker’s label. Alternatively, you can use the label attribute.
    hintThe colour picker’s hint. Alternatively, you can use the hint attribute.

    Learn more about using slots.

    Methods#

    NameDescriptionArguments
    focus()Focuses the colour picker.options: FocusOptions
    blur()Unfocuses the colour picker (i.e., blurs it).
    getFormattedValue()Returns the current value as a string in the specified format.format: "hex" | "hexa" | "rgb" | "rgba" | "hsl" | "hsla" | "hsv" | "hsva"
    show()Shows the colour picker popup.
    hide()Hides the colour picker popup.
    checkValidity()Checks for validity but does not show a validation message. Returns true when valid and false when invalid.
    getForm()Gets the associated form, if one exists.
    reportValidity()Checks for validity and shows the browser’s validation message if the control is invalid.
    setCustomValidity()Sets a custom validation message. Pass an empty string to restore validity.message: string

    Learn more about methods.

    Events#

    NameDescriptionEvent detail
    pc-changeEmitted when the colour picker’s value changes.
    pc-inputEmitted when the colour picker receives input.
    pc-focusEmitted when the colour picker receives focus.
    pc-blurEmitted when the colour picker loses focus (i.e., is blurred).
    pc-invalidEmitted when the form control has been checked for validity and its constraints aren’t satisfied.

    Learn more about events.

    Custom properties#

    NameDescriptionDefault
    --grid-widthThe width of the colour grid.17em
    --grid-heightThe height of the colour grid.12em
    --grid-handle-sizeThe size of the colour grid’s handle.1.25em
    --slider-heightThe height of the hue and alpha sliders.1em
    --slider-thumb-sizeThe size of both slider thumbs.calc(var(--slider-height) + 0.25em)
    --swatch-sizeThe size of each predefined colour swatch.1.5em

    Learn more about customising custom properties.

    Parts#

    NameDescription
    baseThe component’s base wrapper.
    popupThe colour picker’s popup popup.
    triggerThe colour picker’s popup trigger.
    swatchesThe container that holds the swatches.
    swatchThe individual swatches in the colour picker.
    gridThe colour grid.
    grid-handleThe colour grid’s handle.
    sliderThe hue and opacity sliders.
    slider-thumbThe hue and opacity slider thumbs.
    hue-sliderThe hue slider.
    hue-slider-thumbThe hue slider’s thumb.
    opacity-sliderThe opacity slider.
    opacity-slider-thumbThe opacity slider’s thumb.
    previewThe preview colour.
    inputThe text input.
    copy-buttonThe text input’s copy button.
    copy-button-buttonThe button part of the text input’s copy button.
    eyedropper-buttonThe eyedropper button.
    eyedropper-button-baseThe eyedropper button’s button part.
    eyedropper-button-prefixThe eyedropper button’s prefix part.
    eyedropper-button-labelThe eyedropper button’s label part.
    eyedropper-button-suffixThe eyedropper button’s suffix part.
    format-buttonThe format switch button.
    format-button-baseThe format switch button’s button part.
    format-button-prefixThe format switch button’s prefix part.
    format-button-labelThe format switch button’s label part.
    format-button-suffixThe format switch button’s suffix part.

    Learn more about customising CSS parts.

    Animations#

    NameDescription
    colorPicker.showThe animation to use when showing the colour picker popup.
    colorPicker.hideThe animation to use when hiding the colour picker popup.

    Learn more about customising animations.

    Importing#

    If you’re using the autoloader or the standard loader, you can skip this section. But if you’re cherry picking, you can use any of the following snippets to import this component.

    CDN (script tag) CDN (import) npm (import)

    To manually import this component from the CDN, copy this code snippet and paste it in your HTML.

    <script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/color-picker/color-picker.js"></script>

    To manually import this component from the CDN, copy this code snippet and paste it in your JavaScript file.

    import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.3/cdn/components/color-picker/color-picker.js";

    To manually import this component from npm, copy this code snippet and paste it in your JavaScript file.

    import "placer-toolkit/dist/components/color-picker/color-picker.js";

    Dependencies#

    This component automatically imports these components:

    We’d love to hear from you. Please reach out to us with any questions or enquiries you may have.

    You can contact us via e‐mail at placer.coc.reports+contact@gmail.com.

    We look forward to hearing from you!

    Got it!
    Dangerous lands

    Whoa! You’ve wandered into the dangerous lands of Placer Toolkit. Version 0 is out of date and doesn’t meet EU privacy standards, including GDPR.

    Want the latest powers, security and compliance? Stick with the current version of Placer Toolkit!

    Yikes! Power up!