Awesomplete
Awesomplete is an ultra-lightweight, customizable, and dependency-free autocomplete widget designed for modern browsers. It provides an accessible solution for adding suggestion lists to input fields, aiming to fill the gap where native `<datalist>` elements might fall short in customization or functionality. The current stable version is 1.1.7, which was last published to npm in 2018. While effective for its intended purpose, the project appears to be unmaintained, with no significant updates or active development in recent years. Its key differentiators include its minimal footprint (2KB minified & gzipped), zero dependencies, and the ability to be easily integrated via HTML attributes or a simple JavaScript API, offering flexibility for various use cases.
Common errors
-
ReferenceError: Awesomplete is not defined
cause The `awesomplete.js` script was not loaded or executed before attempting to use the `Awesomplete` global object, or it was incorrectly imported in a module environment.fixEnsure `awesomplete.js` is included via a `<script>` tag in your HTML *before* any inline scripts that use it. If using `npm`/`yarn`, ensure your bundler correctly processes and exposes it globally, or explicitly `require('awesomplete')` in a CommonJS context. -
Autocomplete suggestions do not appear when typing.
cause The `minChars` option is set too high (default is 2), or the `list` property (either `data-list` HTML attribute or JavaScript `list` option) is empty, incorrectly formatted, or points to a non-existent element/selector.fixCheck the `minChars` option and ensure it's set to an appropriate number (e.g., `1`). Verify that the `data-list` attribute or the `list` JavaScript option contains valid suggestions (e.g., a comma-separated string, array of strings, or a valid CSS selector pointing to a list element). -
Awesomplete interferes with other scripts or styles.
cause The library creates and manages DOM elements and attaches event listeners, which can sometimes conflict with other client-side libraries, especially given its unmaintained status.fixEnsure that Awesomplete's CSS and JS are loaded in a way that minimizes global impact (e.g., specific selectors, scoped styles). If conflicts occur, investigate the `Awesomplete.destroy()` method to clean up instances, or consider isolating its usage to specific parts of your application.
Warnings
- breaking A breaking change was introduced in `v1.1.3` or `v1.1.4` (exact version unclear from provided data), but was subsequently reverted in `v1.1.4`.
- gotcha Awesomplete is largely unmaintained. The last significant update was in 2018 (version 1.1.7). This means it may not receive updates for new browser features, security vulnerabilities, or bug fixes. Community support is also limited.
- gotcha The library primarily exposes `Awesomplete` as a global variable. In environments using module bundlers (Webpack, Rollup, Parcel), attempting to `import Awesomplete from 'awesomplete'` will not work directly as the package lacks native ES module exports. It only defines a `main` entry for CommonJS.
- gotcha Awesomplete does not officially ship with TypeScript definitions. While `@types/awesomplete` is available on DefinitelyTyped, it is also somewhat outdated (last updated ~2 years ago) and may not perfectly reflect the runtime API, especially for edge cases or specific event types.
- gotcha When dynamically manipulating the suggestion list while the Awesomplete popup is open, changes may not immediately reflect. The `evaluate()` method must be called to refresh the displayed suggestions.
Install
-
npm install awesomplete -
yarn add awesomplete -
pnpm add awesomplete
Imports
- Awesomplete
<!-- In HTML, after awesomplete.js is loaded --> <script> const input = document.querySelector('.awesomplete-input'); const awesomplete = new Awesomplete(input, { list: ['Apple', 'Banana', 'Cherry'] }); </script> - Awesomplete
import Awesomplete from 'awesomplete';
const Awesomplete = require('awesomplete'); // Then use `new Awesomplete(...)` - CSS and JS
import 'awesomplete/awesomplete.css'; // May not work depending on bundler config import 'awesomplete/awesomplete.js';
<link rel="stylesheet" href="path/to/awesomplete.css" /> <script src="path/to/awesomplete.js" async></script>
Quickstart
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Awesomplete Quickstart</title>
<link rel="stylesheet" href="https://unpkg.com/awesomplete@1.1.7/awesomplete.css" />
</head>
<body>
<h1>Awesome Autocomplete</h1>
<label for="my-input">Select a Programming Language:</label>
<input id="my-input" class="awesomplete" data-list="Ada, Java, JavaScript, Brainfuck, LOLCODE, Node.js, Ruby on Rails" />
<h2>Programmatic Example</h2>
<label for="custom-input">Choose a Fruit:</label>
<input id="custom-input" />
<script src="https://unpkg.com/awesomplete@1.1.7/awesomplete.js" async></script>
<script>
window.addEventListener('load', () => {
// Example 1: Declarative setup using HTML attributes (data-list)
// The input with class="awesomplete" and data-list is automatically processed.
// Example 2: Programmatic setup for more control
const inputCustom = document.getElementById('custom-input');
new Awesomplete(inputCustom, {
list: [
'Apple', 'Apricot', 'Avocado', 'Banana', 'Blackberry', 'Blueberry',
'Cherry', 'Coconut', 'Cranberry', 'Date', 'Dragonfruit', 'Durian',
'Elderberry', 'Fig', 'Grape', 'Guava', 'Kiwi', 'Lemon', 'Lime',
'Mango', 'Melon', 'Nectarine', 'Orange', 'Papaya', 'Passion Fruit',
'Peach', 'Pear', 'Pineapple', 'Plum', 'Pomegranate', 'Raspberry',
'Strawberry', 'Watermelon'
],
minChars: 1, // Show suggestions after 1 character
autoFirst: true // Highlight the first item automatically
});
// Event listener example (optional)
inputCustom.addEventListener('awesomplete-selectcomplete', (e) => {
console.log('Selected:', e.text.value);
});
});
</script>
</body>
</html>