Tailwind CSS Combobox Plugin API
Headless, unstyled combobox plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Combobox plugin via npm, else you can skip this step if you are already using Preline UI as a package.
-
Install the plugin
Install
@preline/comboboxvia npmTerminalnpm i @preline/combobox -
Add the plugin CSS
Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.
main.css@import "tailwindcss"; /* @preline/combobox */ /* [!code highlight:3] */ @source "../node_modules/@preline/combobox/*.js"; @import "./node_modules/@preline/combobox/variants.css"; @import "./node_modules/@preline/combobox/theme.css"; -
Add the plugin JavaScript
Include the JavaScript
<script>that powers the interactive elements near the end of your</body>tag:HTML
Additional Initialization Options
Use the
non-autoentry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.HTML (non-auto)
Via bundler
When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.
JavaScript (auto)import "@preline/combobox";JavaScript (non-auto)import HSCombobox from "@preline/combobox/non-auto"; HSCombobox.autoInit(); // Or initialize a specific element manually const el = document.querySelector("#combobox"); if (el) new HSCombobox(el);
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<div class="inline-flex relative" data-hs-combo-box>
<div class="relative">
<input type="text" role="combobox" aria-expanded="false" value="" data-hs-combo-box-input />
</div>
<div class="absolute z-50" style="display: none;" data-hs-combo-box-output>
<div class="cursor-pointer" tabindex="0" data-hs-combo-box-output-item>
<span data-hs-combo-box-search-text="1" data-hs-combo-box-value>1</span>
</div>
<div class="cursor-pointer" tabindex="1" data-hs-combo-box-output-item>
<span data-hs-combo-box-search-text="2" data-hs-combo-box-value>2</span>
</div>
</div>
</div>
Methods
The HSCombobox object is contained within the global window object
Open item (public method).
const comboBox = new HSComboBox(document.querySelector('#combo-box'));
const openBtn = document.querySelector('#open-btn');
openBtn.addEventListener('click', () => {
comboBox.open();
});
Open item (mixed).
const { element } = HSComboBox.getInstance('#combo-box', true);
const openBtn = document.querySelector('#open-btn');
openBtn.addEventListener('click', () => {
element.open();
});
Ready to use Components
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
ComboBox (Autocomplete)
Explore ready-to-use Tailwind CSS examples built with Preline UI.
View ComboBox (Autocomplete) examples