Skip to main contentSkip to docs navigation

Chassis

Chip Input

View on GitHub

A multi-value text field that wraps typed entries into removable chips. Use it for tags, categories, email recipients, or any free-form list of values.

@layer components
Requires JavaScript
Depends onForm Input, Chip
Component tokens

Introduction

A chip input is a text field that holds several values: each typed entry becomes a removable chip. .chip-input is the unstyled container of the chips and of a .ghost-input; add .form-input next to it for the appearance of a text field.

Basic structure

Add data-cx-chips to the container to enable the plugin. Typing a value and pressing Enter or , creates a chip; the close button of a chip, or Backspace on a focused chip, removes it.

The value of data-cx-chips sets the classes of every chip the input creates — data-cx-chips="primary smooth" produces chips with the .primary and .smooth classes. Omit the value or use default for the default chip appearance.

JavaScript TypeScript
HTML
<div class="form-input chip-input" data-cx-chips>
  <span class="chip" data-cx-chip-value="JavaScript">JavaScript</span>
  <span class="chip" data-cx-chip-value="TypeScript">TypeScript</span>
  <input type="text" class="ghost-input" placeholder="Add skill…" />
</div>

Chip styles

Pass any combination of chip modifier classes as the data-cx-chips value to set the appearance of the chips the input creates. Give the chips that are already in the markup the same classes: the plugin applies the value only to the chips it creates.

Approved Verified
Bug Critical
Draft Review
HTML
<div class="form-input chip-input" data-cx-chips="primary">
  <span class="chip" data-cx-chip-value="Approved">Approved</span>
  <span class="chip" data-cx-chip-value="Verified">Verified</span>
  <input type="text" class="ghost-input" placeholder="Add status…" />
</div>
<div class="form-input chip-input" data-cx-chips="danger smooth">
  <span class="chip" data-cx-chip-value="Bug">Bug</span>
  <span class="chip" data-cx-chip-value="Critical">Critical</span>
  <input type="text" class="ghost-input" placeholder="Add issue…" />
</div>
<div class="form-input chip-input" data-cx-chips="default outline">
  <span class="chip" data-cx-chip-value="Draft">Draft</span>
  <span class="chip" data-cx-chip-value="Review">Review</span>
  <input type="text" class="ghost-input" placeholder="Add label…" />
</div>

Mixed chip styles

Chips in the markup can carry classes that differ from the data-cx-chips value, and keep them. Only the chips added at runtime receive the classes of data-cx-chips.

Low Medium High
HTML
<div class="form-input chip-input" data-cx-chips="primary smooth">
  <span class="chip default" data-cx-chip-value="Low">Low</span>
  <span class="chip warning smooth" data-cx-chip-value="Medium">Medium</span>
  <span class="chip danger smooth" data-cx-chip-value="High">High</span>
  <input type="text" class="ghost-input" placeholder="Add priority…" />
</div>

Empty state

A chip input can start with the .ghost-input alone, without any .chip element: the plugin creates the chips as the user types.

HTML
<div class="form-input chip-input" data-cx-chips="primary smooth">
  <input type="text" class="ghost-input" placeholder="Type and press Enter…" />
</div>

With form field

Wrap the chip input in a form field to pair it with a label, help text, and validation feedback.

React CSS
Press Enter or , to add a skill.
HTML
<div class="form-field">
  <label class="form-label" for="skillsInput">Skills</label>
  <div class="form-input chip-input" data-cx-chips="primary smooth">
    <span class="chip" data-cx-chip-value="React">React</span>
    <span class="chip" data-cx-chip-value="CSS">CSS</span>
    <input type="text" class="ghost-input" placeholder="Add skill…" id="skillsInput" />
  </div>
  <div class="form-help">Press <kbd>Enter</kbd> or <kbd>,</kbd> to add a skill.</div>
</div>

Point the label's for attribute at the ghost input's id, not the container's id. The container does not need its own id.

Disabled

Apply the disabled attribute to the ghost input to prevent new chips from being added. The existing chips are dimmed and ignore pointer events.

React CSS
HTML
<div class="form-input chip-input" data-cx-chips="primary smooth">
  <span class="chip" data-cx-chip-value="React">React</span>
  <span class="chip" data-cx-chip-value="CSS">CSS</span>
  <input type="text" class="ghost-input" placeholder="Disabled…" disabled />
</div>

Floating labels

A chip input works inside a floating label container like a text input.

A Chip Another Chip
HTML
<div class="form-floating mb-md">
  <label for="floatingInput">Add chips</label>
  <div class="chip-input form-input" data-cx-chips="primary smooth">
    <span class="chip" data-cx-chip-value="A Chip">A Chip</span>
    <span class="chip" data-cx-chip-value="Another Chip">Another Chip</span>
    <input type="text" class="ghost-input" id="floatingInput" />
  </div>
</div>

Accessibility

The plugin makes every chip focusable with tabindex="0" and keeps aria-selected on it in sync with the selection. The close button it adds to a chip has aria-label="Remove" and stays out of the tab order, since the keyboard removes a chip from the chip itself. The container gets no role or name from the plugin: label the .ghost-input, as in With form field.

Keys in the input

While the .ghost-input has the focus, the keys create chips and move the focus to the existing ones.

KeyAction
EnterCreate a chip from the current input value.
, (or separator)Create a chip from the current input value.
Backspace / DeleteWhen the input is empty, select and focus the last chip.
←When the cursor is at the start, select and focus the last chip.
Shift+←When the cursor is at the start, add the last chip to the selection and focus it.
EscapeClear selection, clear the input value, and blur.

Keys on a chip

While a chip has the focus, the keys move between the chips, extend the selection and remove chips.

KeyAction
Backspace / DeleteRemove all selected chips, or the focused chip when none is selected, and focus the next closest chip or the input.
←Move to the previous chip.
→Move to the next chip, or to the input if already at the last chip.
Shift+← / Shift+→Extend the selection in that direction.
HomeMove to the first chip.
EndMove focus to the input.
Cmd/Ctrl+ASelect all chips.
EscapeClear selection and return focus to the input.

Mouse and pointer

A click selects one chip, and the modifier keys extend the selection to several.

ActionEffect
Click chipSelect that chip (clears any previous selection).
Cmd/Ctrl+clickToggle the chip in or out of the current selection.
Shift+clickRange-select from the anchor chip to the clicked chip.
Click close buttonRemove the chip immediately.
Click container backgroundClear selection and focus the input.

JavaScript API

The ChipInput plugin creates and removes chips, tracks their selection, and fires an event for each change. Chassis JS ships as an ES module — import the ChipInput class:

JavaScript
import { ChipInput } from '@chassis-ui/css'

Triggers

A container with data-cx-chips is initialized when the page loads. The value of the attribute sets the classes of the created chips.

HTML
<div class="form-input chip-input" data-cx-chips="primary smooth">
  <input type="text" class="ghost-input" placeholder="Add tags…" />
</div>

Further data-cx-* attributes on the container set the options of the plugin:

AttributeDescription
data-cx-chipsInitializes chip input. The value sets the chip style classes ("primary smooth", "default", etc.).
data-cx-separatorCharacter that creates a new chip when typed. Default is ",". Set to null to disable.
data-cx-allow-duplicatesSet to "true" to allow the same value to be added more than once.
data-cx-max-chipsMaximum number of chips allowed. Omit for no limit.
data-cx-create-on-blurSet to "false" to prevent chip creation when the input loses focus.

Typed or pasted text that contains the separator is split at it: every part before the last separator becomes a chip, and the rest stays in the input.

Initialization

Create an instance with the ChipInput class for a container added after the page has loaded, or to pass the options as an object:

JavaScript
const el = document.querySelector('.form-input.chip-input')
const chipInput = new ChipInput(el, {
  separator: ',',
  maxChips: 5,
  chipClass: 'primary smooth'
})

Methods

The plugin exposes instance methods to add, remove and select chips and to read their values:

MethodDescription
add(value)Creates a chip with the given value. Returns the chip element, or null if the value was rejected (empty, duplicate, or max reached).
remove(chipOrValue)Removes a chip by element reference or value string. Returns true on success.
removeSelected()Removes all currently selected chips and returns focus to the input.
getValues()Returns an array of all current chip values in order.
getSelectedValues()Returns an array of selected chip values.
clear()Removes all chips.
clearSelection()Deselects all chips without removing them.
selectChip(chip, options)Selects a chip element. Accepts { addToSelection, rangeSelect } options.
focus()Focuses the ghost input.
dispose()Destroys the component and removes all event listeners.
getInstance(element)Static — returns the instance bound to a container, or null.
getOrCreateInstance(element)Static — returns the instance bound to a container, or creates one.
JavaScript
const el = document.querySelector('.form-input.chip-input')
const chipInput = ChipInput.getOrCreateInstance(el)

chipInput.add('JavaScript')
chipInput.add('TypeScript')

console.log(chipInput.getValues()) // ['JavaScript', 'TypeScript']

chipInput.remove('JavaScript')
chipInput.clear()

Options

Options are set via data-cx-* attributes or passed to the constructor as an object. Attribute names use the kebab-case form of the option name — data-cx-custom-class, not data-cx-customClass. Attribute values are parsed to their native types:"true" → true, "0" → 0, and valid JSON strings to objects.

Use data-cx-config to pass multiple options as a JSON string:data-cx-config='{"delay":200}'. Individual data-cx-* attributes take precedence over data-cx-config. Individual attributes accept JSON values too, such as data-cx-delay='{"show":100,"hide":200}'.

When initializing components, Chassis merges configurations from multiple sources in this priority order: default settings, data-cx-config values, individual data-cx-*attributes, and finally any JavaScript object options. Values defined later in this sequence override earlier ones.

NameTypeDefaultDescription
separatorstring | null','Character that triggers chip creation when typed. Set to null to disable.
allowDuplicatesbooleanfalseAllow duplicate chip values.
maxChipsnumber | nullnullMaximum number of chips allowed. null for no limit.
placeholderstring''Placeholder of the .ghost-input that the plugin creates when the markup has none.
dismissiblebooleantrueAppend a close button to each chip.
dismissTextstring'×'Text of the close button.
createOnBlurbooleantrueCreate a chip from the current input value when focus leaves the field.
chipClassstring'default'Space-separated chip modifier classes applied to every created chip. Overridden by the data-cx-chips attribute value.

Events

All events fire on the container. add.cx.chip-input and remove.cx.chip-input fire before the change and are cancelable.

EventDescription
add.cx.chip-inputFired before a chip is added. The event carries a value property. Call event.preventDefault() to cancel.
remove.cx.chip-inputFired before a chip is removed. The event carries value and chip properties. Call event.preventDefault() to cancel.
change.cx.chip-inputFired after any chip is added or removed. The event carries a values array with all current chip values.
select.cx.chip-inputFired when chip selection changes. The event carries a selected array of selected values.
JavaScript
const el = document.querySelector('.form-input.chip-input')

el.addEventListener('add.cx.chip-input', event => {
  if (event.value.length < 2) {
    event.preventDefault() // reject values shorter than 2 characters
  }
})

el.addEventListener('change.cx.chip-input', event => {
  console.log('Current values:', event.values)
})

CSS

The Chip input component can be customized at both runtime (via custom properties) and compile time (via Sass variables).

Custom properties

The Chip input component exposes CSS custom properties to control its appearance at runtime.

--padding-y: var(--chip-input-padding-y, #{$form-chip-input-padding-y});
--padding-x: var(--chip-input-padding-x, #{$form-chip-input-padding-x});
--gap: var(--chip-input-gap, #{$form-chip-input-gap});
--min-width: var(--chip-input-min-width, #{$form-chip-input-min-width});
--min-height: var(--chip-input-min-height, #{$form-chip-input-min-height});

Sass variables

The Chip input component uses Sass variables in scss/config/_defaults.scss to define its defaults; they are also exposed as CSS custom properties using the --cx- prefix for runtime override — $variable-name becomes --cx-variable-name. See the component anatomy page.

$form-chip-input-padding-y: $form-md-padding-y;
$form-chip-input-padding-x: $form-md-padding-x;
$form-chip-input-gap: .375rem;
$form-chip-input-min-width: 5rem;
$form-chip-input-min-height: 2rem;

Design tokens

The Chip component consumes design tokens from Chassis Tokens with the $cx- prefix. — See the design tokens page.

$chip-medium-font:                      $cx-font-chip-medium;
$chip-large-font:                       $cx-font-chip-large;
$chip-small-font:                       $cx-font-chip-small;

$chip-medium-min-size:                 $cx-size-chip-medium-main;
$chip-medium-padding-y:                 $cx-space-chip-medium-padding-y;
$chip-medium-padding-x:                 $cx-space-chip-medium-padding-x;
$chip-medium-gap:                       $cx-space-chip-medium-gap;
$chip-medium-nudge:                      $cx-space-chip-medium-nudge;

$chip-large-min-size:                  $cx-size-chip-large-main;
$chip-large-padding-y:                  $cx-space-chip-large-padding-y;
$chip-large-padding-x:                  $cx-space-chip-large-padding-x;
$chip-large-gap:                        $cx-space-chip-large-gap;
$chip-large-nudge:                       $cx-space-chip-large-nudge;

$chip-small-min-size:                  $cx-size-chip-small-main;
$chip-small-padding-y:                  $cx-space-chip-small-padding-y;
$chip-small-padding-x:                  $cx-space-chip-small-padding-x;
$chip-small-gap:                        $cx-space-chip-small-gap;
$chip-small-nudge:                       $cx-space-chip-small-nudge;

$chip-medium-icon-size:                 $cx-size-chip-medium-icon;
$chip-large-icon-size:                  $cx-size-chip-large-icon;
$chip-small-icon-size:                  $cx-size-chip-small-icon;

$chip-medium-avatar-size:               $cx-size-chip-medium-avatar;
$chip-large-avatar-size:                $cx-size-chip-large-avatar;
$chip-small-avatar-size:                $cx-size-chip-small-avatar;

$chip-border-width:                     $cx-border-width-chip-main;
$chip-medium-border-radius:             $cx-border-radius-chip-medium;
$chip-large-border-radius:              $cx-border-radius-chip-large;
$chip-small-border-radius:              $cx-border-radius-chip-small;

$chip-remove-icon:                      $cx-icon-chip-remove;