Web Component

Command

Attach modals to keyboard shortcuts, allowing for easy toggling and improved user interaction.

Table of contents

Usage

The Command component is a web component built from scratch to allow developers to bind a keyboard shortcut and toggle a modal.

To get started, simply use the <uk-command> markup in your HTML code with one <select data-fn="data-source" hidden></select> tag as items reference.

Binding a keyboard shortcut

By default, no keyboard shortcuts are bound. To enable a keyboard shortcut, simply add the key attribute to your Command component.

  • Press CTRL + J
PR

Using a toggle

Since we are using the UIkit modal behind the scenes, we can use any element to toggle a Command component. To start, simply add the toggle attribute to the <uk-command> element. Then, you can use an <a> element linked to the toggle ID like this: <a href="#TOGGLE" data-uk-toggle>. If you are using another element, such as a button, just add the data-uk-toggle="target: #TOGGLE" attribute to toggle the Command component.

PR

Programmatic navigation

By default, the command palette component does not perform any action when an item is selected or clicked. This allows for flexibility in handling user input. To respond to user input, you need to manually attach an event listener to the component specifically to the uk-command:click event.

PR

You can try out this example by checking your browser’s developer console. When you select an item from the command palette, the selected value will be logged to the console. From there, you can handle the value as needed to respond to the user’s input.

Customizing searchable keywords

Sometimes, there are elements 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="checkbox,input,radio"

To group related elements, simply use the optgroup tag and items will be automatically sorted and grouped with headers.

Disabling an item

Sometimes, we may want to disable items. To do this, simply use the disabled attribute, and the item will be automatically disabled.

Individual shortcuts

Display keyboard shortcuts next to commands and allow users to execute them while the modal is open.

Use data-key and data-modifier attributes on <options> tag to define shortcuts:

  • Press Alt + O
PR

Available modifiers: ctrl, alt, shift, meta

Internationalization

The Command 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

Available i18n options

KeyDefaultDescription
placeholderSearch commands…Placeholder text for search input
search-labelSearchARIA label for search input
list-labelCommandsARIA label for commands list
close-labelCloseARIA label for close button
modal-labelCommand paletteARIA label for modal dialog
escape-button-labelEscText displayed on escape button

Custom classes and styles

The Command component supports custom CSS classes and inline styles through the cls and stl attributes.

Available cls targets

TargetDescription
modalThe modal wrapper element
dialogThe modal dialog container
headerThe search header section
header-iconThe search icon container
header-inputThe search input wrapper
header-escThe escape button wrapper
listThe commands list (ul element)
itemIndividual command item (li element)
item-headerGroup header for grouped commands
item-linkThe clickable link/button inside each command
item-wrapperWrapper for command content (icon + text + shortcut)
item-iconIcon element for commands with icons
item-textText label for the command
item-keyKeyboard shortcut display
item-subtitleSubtitle/description text for commands
escape-buttonThe Esc button in the header

Attributes

The following attributes are available for this component:

NameTypeDefaultDescription
keyStringKeyboard key used with modifier to toggle the command palette (e.g., “k” for Ctrl+K).
modifierStringctrlModifier key used with key attribute. Options: ctrl, alt, shift, meta.
toggleStringID for the modal element. Used for manual toggling via data-uk-toggle attribute or UIkit modal API.
clsStringCustom CSS classes. Can be simple string or JSON object for targeting specific elements.
stlStringCustom inline styles. Can be simple string or JSON object for targeting specific elements.
i18nStringInternationalization strings as JSON object or via configuration script.
force-prevent-rerenderBooleanfalsePrevents component rerendering (useful for HTMX or SPA scenarios).

Events

The Command component triggers the following events on elements with this component attached:

NameDescription
uk-command:clickFired when a command is selected or clicked. Event detail contains {value: OptionItem}.
uk-command:searchFired when the search term changes. Event detail contains {value: string}.