Skip to main content Design tokens Components Style utilities
Skip to table of contents

    Select

    Selects allow you to choose items from a menu of predefined options.

    <pc-select>
      <pc-option value="option-1">Option 1</pc-option>
      <pc-option value="option-2">Option 2</pc-option>
      <pc-option value="option-3">Option 3</pc-option>
      <pc-option value="option-4">Option 4</pc-option>
      <pc-option value="option-5">Option 5</pc-option>
      <pc-option value="option-6">Option 6</pc-option>
    </pc-select>
    Code

    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

    Labels

    Use the label attribute to give the select an accessible label. For labels that contain HTML, use the label slot instead.

    Hints

    Add a descriptive hint to a select with the hint attribute. For hints that contain HTML, use the hint slot instead.

    Placeholders

    Use the placeholder attribute to add a placeholder.

    Clearable

    Use the clearable attribute to make the select clearable. The clear button only appears when an option is selected.

    Filled

    Add the filled attribute to draw a filled select.

    Pill

    Use the pill attribute to give selects a pill‐shaped look.

    Disabled

    Use the disabled attribute to disable the select.

    Multiple options

    To allow multiple options to be selected, use the multiple attribute. It’s a good practise to use the clearable attribute as well when this option is enabled. To set multiple values at once, set value to a space‐delimited list of values.

    Multi‐select options may wrap, causing the select to expand vertically. You can use the max-options-visible attribute to control the maximum number of selected options to show at once.

    Setting initial values

    Use the value attribute to set the initial selection.

    When using the multiple attribute, the value attribute uses space‐delimited values to select more than one option. Because of this, <pc-option> values cannot contain spaces. If you’re accessing the value property through JavaScript, it will be an array.

    Grouping options

    Use the Divider component to group listbox items visually. You can also use the <small> tag to provide labels, but they won’t be announced by most assistive devices.

    Sizes

    Use the size attribute to change the select’s combobox size.

    Placement

    The preferred placement of the select’s listbox can be set with the placement attribute. The actual position may vary to ensure the panel remains in the viewport.

    Prefixes and suffixes

    Use the prefix and suffix slots to add presentational icons and text. Avoid slotting in interactive elements, such as buttons and links.

    Custom tags

    When multiple options can be selected, you can provide custom tags by passing a function to the getTag property. Your function can return a string of HTML, a Lit template or an HTMLElement. The getTag() function will be called for each option. The first argument is a <pc-option> element and the second argument is the tag’s index (its position in the tag list).

    Remember that custom tags are rendered in a shadow root. To style them, you can use the style attribute in your template or you can add your own parts and target them with the ::part() selector.

    Lazy loading options

    Lazy loading options is very hard to get right. <pc-select> largely follows how a native <select> works.

    Here are the following conditions:

    • If a <pc-select> is created without any options, but is given a value attribute, its value will be "", and then when options are added, if any of the options have a value equal to the <pc-select> value, the value of the <pc-select> will be equal that of the option.

    For example, <pc-select value="foo"> will have a value of "" until <pc-option value="foo">Foo</pc-option> connects, at which point its value will become "foo" when submitting.

    • If a <pc-select multiple> with an initial value has multiple values, but only some of the options are present, it will only respect the options that are present, and if a selected option is loaded in later, and the value of the select has not changed via user interaction or direct property assignment, it will add the selected option ot the form value and to the .value of the select.

    This can be hard to conceptualise, so here’s a fairly large example showing how lazy‐loaded options work with <pc-select> and <pc-select multiple> when given initial value attributes. Feel free to play around with it in this code demo.

    Be sure you trust the content you are outputting! Passing unsanitised user input to getTag() can result in XSS vulnerabilities.

    Properties

    NameDescriptionReflectsDefault
    nameThe name of the select, submitted as a name/value pair with form data. string""
    defaultValue
    value
    The default value of the select. Primarily used for resetting the select. string | string[]""
    sizeThe select’s size. "small" | "medium" | "large""medium"
    placeholderPlaceholder text to show as a hint when the select is empty. string""
    multipleAllows more than one option to be selected. booleanfalse
    maxOptionsVisible
    max-options-visible
    The maximum number of selected options to show when the multiple attribute is true. After the maximum limit, “+number” will be shown to indicate the number of additional items that are selected. Set the value to 0 to remove the limit. number3
    disabledDisables the select. booleanfalse
    clearableAdds a clear button when the select is not empty. booleanfalse
    openIndicates whether or not the select is open. You can toggle this attribute to show and hide the menu, or you can use the show() and hide() methods and this attribute will reflect the select’s open state. booleanfalse
    hoistEnable this option to prevent the listbox from being clipped when the component is placed inside a container with overflow: auto|scroll. Hoisting uses a fixed positioning strategy that works in many, but not all scenarios. booleanfalse
    filledDraws a filled select control. booleanfalse
    pillDraws a pill‐style select. booleanfalse
    labelThe select’s label. If you need to display HTML, use the label slot instead. string""
    placementThe preferred placement of the select’s listbox. Note that the actual placement may vary to keep the listbox inside of the viewport. "top" | "bottom""bottom"
    hintThe select’s hint. If you need to display HTML, use the hint slot instead. 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. string""
    requiredIndicates if the select must be filled in or not. booleanfalse
    getTagA function that customises the tags to be rendered when the multiple attribute is true. The first parameter is the option, the second parameter is the current tag’s index. The function should either return either a Lit TemplateResult or a string containing trusted HTML of the symbol to render at the specified value. ( option: PcOption, index: number, ) => TemplateResult | string | HTMLElement-
    valueThe current value of the select, submitted as a name/value pair with form data. When the multiple attribute is true, the value attribute will be a space‐delimited list of values based on the options selected, and the value property will be an array. For this reason, values must not contain spaces. -
    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
    (default)The listbox options. Only <pc-option> and <pc-divider> elements can be slotted here. You can use <pc-divider> to group items visually.
    labelThe input’s label. Alternatively, you can use the label attribute.
    prefixUsed to prepend a presentational icon or similar element to the combobox.
    suffixUsed to append a presentational icon or similar element to the combobox.
    clear-iconAn icon to use in place of the default clear icon.
    expand-iconThe icon to show when the select is expanded and collapsed. Rotates on open and close.
    hintText that describes how to use the input. Alternatively, you can use the hint attribute.

    Learn more about using slots.

    Methods

    NameDescriptionArguments
    handleDefaultSlotChange()-
    handleDisabledChange()-
    handleValueChange()-
    handleOpenChange()-
    show()Shows the listbox.-
    hide()Hides the listbox.-
    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
    focus()Focuses the control.options: FocusOptions
    blur()Unfocuses the select (i.e. blurs it).-

    Learn more about methods.

    Events

    NameDescriptionEvent detail
    pc-changeEmitted when the select’s value changes.-
    pc-clearEmitted when the select’s value is cleared.-
    pc-inputEmitted when the select receives input.-
    pc-focusEmitted when the select gains focus.-
    pc-blurEmitted when the select loses focus.-
    pc-showEmitted when the select’s menu opens.-
    pc-after-showEmitted after the select’s menu opens and all animations are complete.-
    pc-hideEmitted when the select’s menu closes.-
    pc-after-hideEmitted after the select’s menu closes and all animations are complete.-
    pc-invalidEmitted when the select has been checked for validity and its constraints aren’t satisfied.-

    Learn more about events.

    Parts

    NameDescription
    form-controlThe form control that wraps the label, input and hint text.
    labelThe label’s wrapper.
    inputThe select’s wrapper.
    hintThe hint’s wrapper.
    comboboxThe container the wraps the prefix, suffix, combobox, clear icon and expand button.
    prefixThe container that wraps the prefix slot.
    suffixThe container that wraps the suffix slot.
    display-inputThe element that displays the selected option’s label, an <input> element.
    listboxThe listbox container where options are slotted.
    tagsThe container that houses option tags when multiselect is used.
    tagThe individual tags that represent each multiselect option.
    tag__baseThe tag’s base part.
    tag__contentThe tag’s content part.
    tag__remove-buttonThe tag’s remove button.
    tag__remove-button__baseThe tag’s remove button base part.
    clear-buttonThe clear button.
    expand-iconThe container that wraps the expand icon.

    Learn more about customising CSS parts.

    Importing

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

    ScriptImportBundler

    To import this component from the CDN with a script tag, copy this snippet and paste it in your HTML.

    <script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/select/select.js"></script>

    To import this component from the CDN using a JavaScript import, copy this snippet and paste it in your JavaScript:

    import "https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/select/select.js";

    To import this component with a bundler using a JavaScript import, copy this snippet and paste it in your JavaScript:

    import "placer-toolkit/dist/components/select/select.js";

    Dependencies

    This component automatically imports these components: