coss.com Svelte

Autocomplete

An input that suggests options as you type.

API Reference
Loading p-autocomplete-1 preview…

Installation

pnpm dlx shadcn-svelte@latest add https://coss-sv.vercel.app/r/autocomplete.json

Usage

<script lang="ts">
  import * as Autocomplete from "$lib/components/ui/autocomplete/index.js";

  const items = [
    { value: "apple", label: "Apple" },
    { value: "banana", label: "Banana" },
    { value: "orange", label: "Orange" },
    { value: "grape", label: "Grape" },
  ];
</script>

<Autocomplete.Root {items}>
  <Autocomplete.Input placeholder="Search..." />
  <Autocomplete.Popup>
    <Autocomplete.Empty>No results found.</Autocomplete.Empty>
    <Autocomplete.List>
      <Autocomplete.Collection>
        {#snippet children(item)}
          <Autocomplete.Item value={item}>{item.label}</Autocomplete.Item>
        {/snippet}
      </Autocomplete.Collection>
    </Autocomplete.List>
  </Autocomplete.Popup>
</Autocomplete.Root>

API Reference

Autocomplete.Root

The root autocomplete component. Manages the autocomplete state and provides context to child components.

PropTypeDescription
itemsreadonly unknown[]The array of items to display in the autocomplete
openbooleanControls whether the popup is open
childrenSnippetAutocomplete content
...propsComponentProps<typeof Autocomplete.Root>All Shards UI Autocomplete props are supported

Autocomplete.Input

The input field component with extended features for size variants and addon support.

PropTypeDefaultDescription
size"sm" \| "default" \| "lg""default"The size variant of the input field
startAddonSnippet-Content to display at the start of the input, such as an icon
showTriggerbooleanfalseWhether to display a trigger button on the right side of the input
showClearbooleanfalseWhether to display a clear button on the right side of the input when there is a value
triggerPropsAutocompleteTriggerProps-Props forwarded to the internal trigger button, useful for overriding aria-label
clearPropsAutocompleteClearProps-Props forwarded to the internal clear button, useful for overriding aria-label
classstring-Additional CSS classes to apply to the component
...propsAutocompleteInputProps-All standard autocomplete input attributes are supported

Autocomplete.Popup

The popup container that displays the autocomplete suggestions.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
portalPropsAutocompletePopupProps["portalProps"]Props forwarded to the internal portal
...propsAutocompletePopupPropsAll standard autocomplete popup attributes are supported

Autocomplete.List

A scrollable container for autocomplete items.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
childrenSnippetList content
...propsAutocompleteListPropsAll standard autocomplete list attributes are supported

Autocomplete.Item

An individual selectable autocomplete item.

PropTypeDescription
valueunknownThe value of the item
classstringAdditional CSS classes to apply to the component
childrenSnippetItem content
...propsAutocompleteItemPropsAll standard autocomplete item attributes are supported

Autocomplete.Empty

Displays a message when no results are found.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
childrenSnippetEmpty-state content
...propsAutocompleteEmptyPropsAll standard autocomplete empty attributes are supported

Autocomplete.Group

Groups related autocomplete items together.

PropTypeDescription
itemsreadonly unknown[]The array of items in this group
classstringAdditional CSS classes to apply to the component
childrenSnippetGroup content
...propsAutocompleteGroupPropsAll standard autocomplete group attributes are supported

Autocomplete.GroupLabel

Displays a label for an autocomplete group.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
childrenSnippetLabel content
...propsAutocompleteGroupLabelPropsAll standard autocomplete group label attributes are supported

Autocomplete.Collection

Used to wrap items within a group for rendering.

PropTypeDescription
childrenSnippet<[unknown]>Renders each filtered item
...propsComponentProps<typeof Autocomplete.Collection>All standard autocomplete collection attributes are supported

Examples

Disabled

Loading p-autocomplete-2 preview…

Small Size

Loading p-autocomplete-3 preview…

Large Size

Loading p-autocomplete-4 preview…

With Label

Loading p-autocomplete-5 preview…

Inline Autocomplete

Autofill the input with the highlighted item while navigating with arrow keys.

Loading p-autocomplete-6 preview…

Auto Highlight

Automatically highlight the first matching option.

Loading p-autocomplete-7 preview…

With Clear Button

Loading p-autocomplete-8 preview…

With Trigger and Clear Buttons

Loading p-autocomplete-9 preview…

With Start Addon

Display an icon or other element at the start of the input using the startAddon snippet.

Loading p-autocomplete-14 preview…

With Groups

Loading p-autocomplete-10 preview…

With Limit Results

Loading p-autocomplete-11 preview…
Loading p-autocomplete-12 preview…

Form Integration

Loading p-autocomplete-13 preview…