A powerful, lightweight vanilla JavaScript autocomplete plugin with advanced features like fuzzy search, async data loading, and custom rendering.
- 🚀 Zero dependencies
- 📦 Lightweight (minified + gzipped < 5KB)
- 🔍 Fuzzy search support
- ⚡ Async data loading
- 🎨 Custom item rendering
- ⌨️ Keyboard navigation
- 🔄 Debounced search
- 📱 Mobile-friendly
- 🎯 TypeScript support
- 🎨 Customizable styling
npm install smart-autocompleteOr include it directly in your HTML:
<script src="https://unpkg.com/smart-autocomplete/dist/smart-autocomplete.js"></script>import SmartAutocomplete from 'smart-autocomplete';
const autocomplete = new SmartAutocomplete({
input: document.getElementById('my-input'),
data: [
{ label: 'Option 1', value: 1 },
{ label: 'Option 2', value: 2 },
// ... more options
]
});const autocomplete = new SmartAutocomplete({
input: document.getElementById('my-input'),
data: async (query) => {
const response = await fetch(`/api/search?q=${query}`);
const data = await response.json();
return data.map(item => ({
label: item.name,
value: item.id
}));
},
debounceTime: 300
});const autocomplete = new SmartAutocomplete({
input: document.getElementById('my-input'),
data: items,
customRender: (item) => `
<div class="custom-item">
<img src="${item.avatar}" alt="${item.label}">
<span>${item.label}</span>
</div>
`
});const autocomplete = new SmartAutocomplete({
input: document.getElementById('my-input'),
data: items,
fuzzySearch: true
});| Option | Type | Default | Description |
|---|---|---|---|
input |
HTMLInputElement |
- | The input element to attach the autocomplete to |
data |
Array<{label: string, value: any}> | ((query: string) => Promise<Array<{label: string, value: any}>>) |
- | Static data array or async function that returns data |
minChars |
number |
2 | Minimum characters required to trigger search |
debounceTime |
number |
300 | Debounce time in milliseconds for search |
maxResults |
number |
10 | Maximum number of results to show |
onSelect |
(item: {label: string, value: any}) => void |
- | Callback function when an item is selected |
onSearch |
(query: string) => void |
- | Callback function when search is performed |
placeholder |
string |
'Search...' | Placeholder text for the input |
noResultsText |
string |
'No results found' | Text to show when no results are found |
loadingText |
string |
'Loading...' | Text to show while loading results |
highlightMatches |
boolean |
true | Whether to highlight matching text |
caseSensitive |
boolean |
false | Whether search should be case sensitive |
fuzzySearch |
boolean |
false | Whether to use fuzzy search |
customRender |
(item: {label: string, value: any}) => string |
- | Custom render function for items |
position |
'bottom' | 'top' |
'bottom' | Position of the dropdown |
width |
string |
'100%' | Width of the autocomplete container |
zIndex |
number |
1000 | z-index of the dropdown |
| Method | Description |
|---|---|
destroy() |
Removes the autocomplete instance and cleans up event listeners |
The plugin emits the following events on the input element:
smart-autocomplete:select- Fired when an item is selectedsmart-autocomplete:search- Fired when a search is performedsmart-autocomplete:open- Fired when the dropdown openssmart-autocomplete:close- Fired when the dropdown closes
↑/↓- Navigate through resultsEnter- Select the current resultEscape- Close the dropdown
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
- IE11 (with polyfills)
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see the LICENSE file for details.