Web Component

Select

Displays a list of options for the user to pick from—triggered by a button.

Table of contents

Usage

The custom select component is a web component built from scratch to enhance the native <select> tag. To get started, simply use the <uk-select> markup in your HTML code with one <select data-fn="data-source" hidden></select> tag as options reference.

PR

Capturing values

There are several ways to capture values from the <uk-select> component. The simplest approach is to add a name attribute to the component. When you do this, a hidden input field with the specified name will be automatically generated, allowing you to easily capture the selected value on your server.

PR

Note If you don’t specify a value attribute for an <option> tag, it will default to using its text content as the value instead.

Position

Because the <uk-select> component uses the Drop component under the hood, we can leverage its positioning capabilities and position our dropdown anywhere we want. To position it, just use the drop attribute with your drop options.

PR

Size modifiers

There are several size modifiers available. Just add one of the following classes to make the fake input smaller or larger.

ClassDescription
.uk-form-xsmallApplies extra small size.
.uk-form-smallApplies small size.
.uk-form-mediumApplies medium size.
.uk-form-largeApplies large size.
PR

Adding header

To add a header to your custom select component, use the <optgroup> tag, which will be automatically converted into a dropdown header, providing a seamless developer experience.

PR

Selecting multiple values

To enable the selection of multiple values, simply add the multiple attribute to the <uk-select> element. This will allow users to choose multiple options from the dropdown list.

PR

Note When utilizing the multiple attribute, note that it will automatically append [] to the name of your input. This is particularly useful when your server expects multiple values, even if the user selects only one option. This ensures that your server-side logic can properly handle the input, regardless of the number of values selected.

Prepopulating values

There are two ways to prepopulate values from the <uk-select> component. The simplest approach is to add a selected attribute to one or more of your <option> tags.

PR

In multiple mode:

  • Clicking an option toggles selection (doesn’t close dropdown)
  • Selected options show checkmarks
  • Button text shows ”{n} options selected” when 2+ options are selected
  • Hidden inputs are created with name[] for form submission
  • Options have aria-multiselectable="true"

Manually setting value

Alternatively, if setting selected on individual <option> elements is not feasible, you can use the value attribute on the <uk-select> tag to prepopulate it with default values. This attribute accepts a comma-separated list of values, allowing you to set multiple defaults, such as value="Mercedes-Benz,Bentley,Porsche,Lamborghini".

PR

Note When both value and selected are used, the value attribute takes precedence. Therefore, it’s recommended to use one method or the other to avoid conflicts.

Disabling options

Similar to the native <select> tag, you can disable specific options in the custom select component by adding the disabled attribute to one or more of your <option> or <optgroup> tags. This allows you to prevent users from selecting certain options.

  • Disabled options are library and not a framework.

PR

Reactivity

By default, the component is not reactive. This is a deliberate design choice, as using MutationObserver can be computationally expensive. As a result, changes to the referenced <select data-fn="data-source" hidden> element will not trigger an update.

To enable reactivity, simply add the data-reactive attribute to the <select data-fn="data-source" hidden> tag. This will use MutationObserver under the hood to monitor the element for changes.

This feature is particularly useful when using libraries like HTMX, where you may need to dynamically update the options in the <select data-fn="data-source" hidden> element based on user interactions, such as searching for data or fetching new options from the server.

Searchable

To add a search box, simply add the searchable attribute. This will add an input field within the dropdown menu, enabling users to search for specific options. This feature is particularly useful when dealing with extensive lists, such as selecting a country from a long list of options.

PR

Search features:

  • Case-insensitive filtering
  • Real-time results as you type
  • Searches through option keywords (automatically generated from text)
  • Emits uk-select:search event on search term change
  • Supports keyboard navigation through filtered results

Custom keywords

Sometimes, there are items that have related keywords that may be slightly off or awkward when included as anchor tags. For these use cases, we can leverage the data-keywords attribute.

For example, if we have a “Form” link but also want it to appear when users search for terms like “checkbox,” “input,” “toggle switch,” etc., we can simply add a comma-separated list of terms like this: data-keywords="apple, mango, lemon".

PR

Disable input

To prevent user input, add the disabled attribute to the <uk-select> element. This will disable the custom select, making it impossible for users to enter or modify its value.

PR

Icon

This component allows you to customize icons through the template system:

PR

Options icon

To add icon to options, just add data-icon attribute with the name of the icon to your <option> tags.

PR

Internationalization

The Select component supports internationalization through multiple methods with the following priority order (highest to lowest):

  • Component-level i18n (via i18n attribute or script tag)
  • Global component-specific namespace (via <script id="uk-i18n">)
  • Default values

Using the i18n attribute

PR

Using a configuration script

PR

Using global i18n

Place this in your document <head> or before any select components:

Available i18n options

KeyDefaultDescription
search-placeholderSearchPlaceholder text for search input field
selection-count{n} options selectedText shown when multiple options are selected (supports {n} placeholder)
insertInsertButton text for inserting custom options
list-labelOptionsARIA label for the listbox
button-labelSelect an optionARIA label and default text for the select button

Custom classes

The Select component supports custom CSS classes through the cls attribute. This allows you to style specific elements within the select without modifying the component’s internal structure.

Using simple string format

Apply a class to the default element (button):

Using JSON object format

Target specific elements within the select:

Using configuration script

Available cls targets

TargetDescription
host-innerThe main wrapper container for the entire component
buttonThe clickable button that opens the dropdown
button-textThe text span inside the button showing selected option(s)
iconThe icon element inside the button (when icon attribute is set)
dropdownThe dropdown container holding the list and search
listThe listbox (ul) containing all options
itemIndividual list item (li) wrapper
item-activeThe active list item (li)
item-disabledClass for disabled list item (li)
item-headerGroup header for grouped options
item-linkThe clickable button/link inside each item
item-wrapperWrapper for item content (icon + text)
item-iconIcon element for options with data-icon
item-textText label for the option
item-checkCheckmark indicator for selected options
item-subtitleDescription/subtitle text for options with data-description
searchSearch input wrapper (when searchable is enabled)
search-inputThe actual search input field
search-iconIcon displayed in search input
dividerClass for the divider element

Custom inline styles

The Select component supports custom inline styles through the stl attribute. This allows you to apply specific CSS styles to individual elements within the select.

Using simple string format

Apply styles to the default element (button):

Using JSON object format

Target specific elements with custom styles:

Using configuration script

Available stl targets

The stl attribute supports the same targets as the cls attribute. See the Available cls targets table above for a complete list of targetable elements.

Error state

To indicate an error state in the form, simply add the .uk-form-danger class to the cls attribute. This will apply a “danger” state to the component, providing visual feedback to the user.

  • This field is required.

PR

Preventing layout shift

When loading Web Components, a brief delay may cause a flash of unstyled content. To mitigate this issue, consider setting a predefined height on the parent element to prevent layout shift and ensure a smooth user experience.

Attributes

The following attributes are available for this component:

NameTypeDefaultDescription
dropStringmode: click; animation: uk-animation-slide-top-small;Dropdown positioning options (UIkit Drop configuration)
searchableBooleanfalseEnables search functionality to filter options
insertableBooleanfalseAllows inserting custom options (auto-enables searchable)
send-headersStringJSON object of HTTP headers for server validation requests
send-urlStringServer endpoint URL for validating inserted options
send-methodStringPOSTHTTP method for server requests
multipleBooleanfalseEnables multi-select mode
iconStringCustom icon/text displayed in the button
nameStringName attribute for hidden input(s), enabling form submission
disabledBooleanfalseDisables the entire component, preventing user interaction
requiredBooleanfalseMarks the select as required for form validation
placeholderStringCustom placeholder text when no option is selected
valueStringPre-selected value(s). Use comma-separated for multiple selections
clsStringCustom CSS classes. Can be simple string or JSON object for targeting elements
stlStringCustom inline styles. Can be simple string or JSON object for targeting elements
i18nStringInternationalization strings as JSON object or via configuration script
force-prevent-rerenderBooleanfalsePrevents component rerendering (useful for HTMX or SPA scenarios)

Events

The Select component triggers the following events:

NameDescription
uk-select:inputFired when the selected value(s) change. Event detail contains {value: string | string[]}.
uk-select:searchFired when the search term changes (when searchable is enabled). Event detail contains {value: string}.