Overview
A chip is a small <button> with the nds-chip class, a color class and a label, with an optional icon. Chips sit together in an nds-chips group, as filter choices, categories or picked values. A chip needs no script for its look; your script sets it selected when the user picks it.
Pick another component when:
Markup
Variants
A row on .nds-chip changes every chip. Selected goes on one chip: the builder uses .nds-chip:first-child, but on a page it goes on the chip the user picked.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Single (default) | — | — | One chip |
| Structure | Group | canon #chip-group |
— | Several chips in a row that wraps, with an even gap |
| Color | Primary (default) | .nds-primary |
.nds-chip |
The brand color. Also .nds-green |
| Color | Neutral | .nds-neutral |
.nds-chip |
A quieter gray. Also .nds-gray |
| Size | MD (default) | — | — | 24px high. It needs no class |
| Size | SM | .nds-sm |
.nds-chip |
20px high |
| Size | LG | .nds-lg |
.nds-chip |
32px high, with a larger label |
| Content | Label (default) | — | — | Text only |
| Content | Icon and label | canon #chip-icon |
.nds-chip (start) |
An icon before the label |
| Content | Icon and label | .nds-lead-icon |
.nds-chip |
The same: adds the space after the icon |
| Content | Label and icon | canon #chip-icon |
.nds-chip |
An icon after the label |
| Content | Label and icon | .nds-trail-icon |
.nds-chip |
The same: adds the space before the icon |
| Content | Removable (hint: A value the user can remove) | canon #chip-remove |
.nds-chip |
A cancel icon after the label, for a picked value the user can remove with a click. NDS.buildChip() builds this chip |
| Selected | Selected | [data-state~="selected"] |
.nds-chip:first-child |
The chip the user picked. Your script sets it |
| Selected | Selected | [aria-pressed="true"] |
.nds-chip:first-child |
The same. It tells screen readers the chip is on. Give the other chips of a choice aria-pressed="false" |
| Disabled | Disabled | [disabled] |
.nds-chip |
The user cannot pick these chips now. Put it on one chip to disable one |
| Rounded | Rounded | .nds-rounded |
.nds-chip |
Fully round ends |
| On color | On color (hint: For a deep primary or dark background) | .nds-oncolor |
.nds-chip |
For chips on a deep primary or dark background. Inside an on-color area (data-theme="dark oncolor"), the chips already take this look: skip the class |
| Center | Center (hint: Group only) | .nds-center |
.nds-chips |
Centers the chips in their row |
Built-in Features
-
Pure CSS
No JavaScript needed for the look. Every color, size and state comes from classes and attributes.
-
Interactive States
Hover, pressed, selected, disabled, and a focus ring for keyboard users. On color chips keep a visible ring on a dark background.
-
Label Truncation
A label longer than 160px ends with an ellipsis. A label that holds a formatted number, such as a price range, is never cut.
--truncatesets how many lines it shows first. -
Composable
Combines with icons, the rounded shape, the on color version and chip groups. Tag Input, Multiselect and Filter build their removable chips with
NDS.buildChip().
Best Practices
- Use chips for choices the user clicks: filters, categories and picked values. For a label that only describes something, use a Tag.
- Use one color for every chip of a choice. The selected state shows which one is on, so do not use a second color for it.
- When a chip turns on and off, set
data-state="selected"andaria-pressed="true"together, andaria-pressed="false"when it is off. - Put related chips in one
nds-chipsgroup, so they share the gap and wrap together. - Keep labels to one or two words. A label longer than 160px is cut with an ellipsis.
- Add an icon only when it helps the user recognize the choice.
- Give each chip
type="button", so a chip inside a form does not send the form. - On a deep primary surface, give the surface
data-theme="dark oncolor primary": every chip inside takes the DGA white look. See Color Areas. For one chip, addnds-oncolorinstead. On a neutral or other dark surface, give itdata-theme="dark"for the dark-mode chips.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-green |
.nds-chip |
The same as nds-primary |
nds-gray |
.nds-chip |
The same as nds-neutral |
nds-md |
.nds-chip |
The same as no size class: 24px |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-state~="selected" |
.nds-chip |
The selected look |
data-state~="disabled" |
.nds-chip |
The disabled look, the same as the disabled attribute |
data-state~="hover", data-state~="pressed", data-state~="focused" |
.nds-chip |
Shows that look without a pointer or a keyboard, for a design review |
CSS Custom Properties
Set these on the chip. --chip-size also reaches it from a parent, and it wins over the size classes. The color classes set --chip-bg and --chip-text.
| Property | Default | Controls |
|---|---|---|
--chip-size |
24px |
Height |
--chip-bg |
var(--chip-background-neutral-default) |
Background |
--chip-text |
var(--chip-text-neutral-default) |
Label color |
--chip-icon |
--chip-text |
Icon color |
--truncate |
1 |
Lines the label shows before the ellipsis |
Tokens
Set a token at :root, or on a wrapper to reach every chip inside it. Give a token with a Dark mode value a dark override too: see Tokens.
Source: the chips group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--chip-background-neutral-default | var(--background-neutral-light)Dark mode var(--colors-alpha-white-10) | |
--chip-background-neutral-hovered | var(--colors-neutral-200)Dark mode var(--colors-alpha-white-20) | |
--chip-background-neutral-pressed | var(--colors-neutral-300)Dark mode var(--colors-alpha-white-30) | |
--chip-background-neutral-selected | var(--colors-neutral-700)Dark mode var(--colors-neutral-300) | |
--chip-background-neutral-focused | var(--background-neutral-light)Dark mode var(--colors-alpha-white-10) | |
--chip-background-oncolor-default | var(--colors-base-white) | |
--chip-background-oncolor-hovered | var(--colors-alpha-white-80) | |
--chip-background-oncolor-pressed | var(--colors-alpha-white-60) | |
--chip-background-oncolor-selected | var(--colors-alpha-white-70) | |
--chip-background-oncolor-focused | var(--colors-base-white) | |
--chip-background-oncolor-disabled | var(--colors-alpha-white-20) | |
--chip-background-primary-default | var(--colors-primary-100)Dark mode var(--colors-primary-alpha-10) | |
--chip-background-primary-hovered | var(--colors-primary-200)Dark mode var(--colors-primary-alpha-20) | |
--chip-background-primary-pressed | var(--colors-primary-400)Dark mode var(--colors-primary-alpha-30) | |
--chip-background-primary-selected | var(--colors-primary-600) | |
--chip-background-primary-focused | var(--colors-primary-100)Dark mode var(--colors-primary-alpha-10) | |
--chip-text-neutral-default | var(--colors-base-black)Dark mode var(--colors-base-white) | |
--chip-text-neutral-selected | var(--colors-base-white)Dark mode var(--colors-base-black) | |
--chip-text-primary-default | var(--colors-primary-800)Dark mode var(--colors-primary-200) | |
--chip-text-primary-pressed | var(--colors-primary-900)Dark mode var(--colors-primary-100) | |
--chip-text-primary-selected | var(--colors-base-white) | |
--chip-text-primary-focused | var(--colors-primary-800)Dark mode var(--colors-primary-200) | |
--chip-text-oncolor-default | var(--colors-base-black) | |
--chip-text-disabled | var(--colors-neutral-400) | |
--chip-text-oncolor-disabled | var(--colors-alpha-white-40) |
It also reads these shared tokens. A change to one also changes other components.
Source: the var() reads in _sass/components/_chips.scss.
| Token | Preview | Value |
|---|---|---|
--background-disabledRead by background-color | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) |
JavaScript
A chip has no script of its own. NDS.buildChip() builds a removable chip for your own list of values.
| Method | Effect |
|---|---|
NDS.buildChip(label, options) |
Returns a removable chip: a <button> with the label and a cancel icon. options.chipClass adds classes, options.data adds data attributes, options.onRemove runs on a click anywhere on the chip, options.disabled disables it, and options.signal is an AbortSignal: after it aborts, a click no longer calls onRemove |
NDS.keepChipFocus(chips, render, fallback) |
Runs render, which removes a chip. If a chip had focus, it then focuses the chip in the same place, or fallback when none is left. chips is a function that returns the live chips. Call it from the remove handler of a chip built with NDS.buildChip() |
The full API is in the banner of _js/nds-core.js.