UNPKG

17.2 kBMarkdownView Raw
1<!--docs:
2title: "Chip"
3layout: detail
4section: components
5excerpt: "Chips represent logical groups of interactive actions inside a chip set."
6iconId: chip
7path: /catalog/chips/chip/
8-->
9
10# Chip
11
12Chips represent logical groups of [actions](../action) contained inside a [chip set](../chip-set).
13
14## Basic usage
15
16**Note**: there's work planned to replace the `mdc-evolution-*` prefix of chips with the standard `mdc-chip-*` prefix.
17
18### HTML structure
19
20Chips must contain a [primary action](../action#primary-action) and may contain a [trailing action](../action#trailing-action).
21
22Note: all chips **must** have a unique [`id`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).
23
24#### Layout grid
25
26Both action chips and input chips follow the [layout grid](https://www.w3.org/TR/wai-aria-practices/#layoutGrid) interaction pattern. All navigable actions must be contained by a [`gridcell` role](https://www.w3.org/TR/wai-aria-1.1/#gridcell) and the chip root must have the [`row` role](https://www.w3.org/TR/wai-aria-1.1/#row).
27
28##### Action chips
29
30Action chips have a single mandatory primary action.
31
32```html
33<span class="mdc-evolution-chip" role="row" id="c0">
34 <span class="mdc-evolution-chip__cell mdc-evolution-chip__cell--primary" role="gridcell">
35 <button class="mdc-evolution-chip__action mdc-evolution-chip__action--primary" type="button" tabindex="0">
36 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--primary"></span>
37 <span class="mdc-evolution-chip__text-label">Chip label</span>
38 </button>
39 </span>
40</span>
41```
42
43Action chips with buttons can be disabled with the `mdc-evolution-chip--disabled` class and the [`disabled` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled) on the root. Action chips with links cannot be disabled.
44
45```html
46<span class="mdc-evolution-chip mdc-evolution-chip--disabled" role="row" id="c1">
47 <span class="mdc-evolution-chip__cell mdc-evolution-chip__cell--primary" role="gridcell">
48 <button class="mdc-evolution-chip__action mdc-evolution-chip__action--primary" type="button" tabindex="-1" disabled>
49 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--primary"></span>
50 <span class="mdc-evolution-chip__text-label">Chip label</span>
51 </button>
52 </span>
53</span>
54```
55
56##### Input chips
57
58Input chips have a mandatory primary action and trailing action.
59
60```html
61<span class="mdc-evolution-chip" role="row" id="c2">
62 <span class="mdc-evolution-chip__cell mdc-evolution-chip__cell--primary" role="gridcell">
63 <button class="mdc-evolution-chip__action mdc-evolution-chip__action--primary" type="button" tabindex="0">
64 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--primary"></span>
65 <span class="mdc-evolution-chip__text-label">Chip foo</span>
66 </button>
67 </span>
68 <span class="mdc-evolution-chip__cell mdc-evolution-chip__cell--trailing" role="gridcell">
69 <button class="mdc-evolution-chip__action mdc-evolution-chip__action--trailing" type="button" tabindex="-1" data-mdc-deletable="true" aria-label="Remove chip foo">
70 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--trailing"></span>
71 <span class="mdc-evolution-chip__icon mdc-evolution-chip__icon--trailing">close</span>
72 </button>
73 </span>
74</span>
75```
76
77If it's desirable to have only the primary action be navigable, the trailing action `gridcell` role can be excluded and the trailing action can receive [`aria-hidden="true"`](https://www.w3.org/TR/wai-aria-1.1/#aria-hidden). In this case, it's recommended to include `data-mdc-deletable="true"` on the primary action, thus making it deletable via Backspace/Delete key press on focus, and an `aria-label` indicating the behavior.
78
79```html
80<span class="mdc-evolution-chip" role="presentation" id="c3">
81 <span class="mdc-evolution-chip__cell mdc-evolution-chip__cell--primary" role="gridcell">
82 <button class="mdc-evolution-chip__action mdc-evolution-chip__action--primary" type="button" tabindex="0" data-mdc-deletable="true" aria-label="Chip foo, press backspace or delete to remove">
83 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--primary"></span>
84 <span class="mdc-evolution-chip__text-label">Chip foo</span>
85 </button>
86 </span>
87 <button class="mdc-evolution-chip__action mdc-evolution-chip__action--trailing" type="button" tabindex="-1" data-mdc-deletable="true" aria-hidden="true">
88 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--trailing"></span>
89 <span class="mdc-evolution-chip__icon mdc-evolution-chip__icon--trailing">close</span>
90 </button>
91</span>
92```
93
94Similar to action chips, input chips with buttons can be disabled by setting the [`disabled` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled) on the actions and adding the `mdc-evolution-chip--disabled` class to the root.
95
96```html
97<span class="mdc-evolution-chip mdc-evolution-chip--disabled" role="row" id="c4">
98 <span class="mdc-evolution-chip__cell mdc-evolution-chip__cell--primary" role="gridcell">
99 <button class="mdc-evolution-chip__action mdc-evolution-chip__action--primary" type="button" tabindex="-1" disabled>
100 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--primary"></span>
101 <span class="mdc-evolution-chip__text-label">Chip foo</span>
102 </button>
103 </span>
104 <span class="mdc-evolution-chip__cell mdc-evolution-chip__cell--trailing" role="gridcell">
105 <button class="mdc-evolution-chip__action mdc-evolution-chip__action--trailing" type="button" tabindex="-1" data-mdc-deletable="true" aria-label="Remove chip foo" disabled>
106 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--trailing"></span>
107 <span class="mdc-evolution-chip__icon mdc-evolution-chip__icon--trailing">close</span>
108 </button>
109 </span>
110</span>
111```
112
113#### Listbox
114
115Filter chips follow the [listbox](https://www.w3.org/TR/wai-aria-practices/#Listbox) interaction pattern.
116
117##### Filter chips
118
119Filter chips have a mandatory primary action while the chip root receives a [`presentation` role](https://www.w3.org/TR/wai-aria-1.1/#presentation).
120
121```html
122<span class="mdc-evolution-chip" role="presentation" id="c5">
123 <span class="mdc-evolution-chip__action mdc-evolution-chip__action--primary" role="option" aria-selected="false" tabindex="0">
124 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--primary"></span>
125 <span class="mdc-evolution-chip__graphic">
126 <span class="mdc-evolution-chip__icon mdc-evolution-chip__icon--primary material-icons">favorite</span> <!-- optional -->
127 <span class="mdc-evolution-chip__checkmark">
128 <svg class="mdc-evolution-chip__checkmark-svg" viewBox="-2 -3 30 30">
129 <path class="mdc-evolution-chip__checkmark-path"
130 fill="none" stroke="black" d="M1.73,12.91 8.1,19.28 22.79,4.59" />
131 </svg>
132 </span>
133 </span>
134 <span class="mdc-evolution-chip__text-label">Chip label</span>
135 </span>
136</span>
137```
138
139To disable a filter chip, set [`aria-disabled="true"`](https://www.w3.org/TR/wai-aria-1.1/#aria-disabled) on the primary action and add the `mdc-evolution-chip--disabled` class on the chip root.
140
141```html
142<span class="mdc-evolution-chip mdc-evolution-chip--disabled" role="presentation" id="c6">
143 <span class="mdc-evolution-chip__action mdc-evolution-chip__action--primary" role="option" aria-selected="false" tabindex="0">
144 <span class="mdc-evolution-chip__ripple mdc-evolution-chip__ripple--primary"></span>
145 <span class="mdc-evolution-chip__graphic">
146 <span class="mdc-evolution-chip__icon mdc-evolution-chip__icon--primary material-icons">favorite</span> <!-- optional -->
147 <span class="mdc-evolution-chip__checkmark">
148 <svg class="mdc-evolution-chip__checkmark-svg" viewBox="-2 -3 30 30">
149 <path class="mdc-evolution-chip__checkmark-path"
150 fill="none" stroke="black" d="M1.73,12.91 8.1,19.28 22.79,4.59" />
151 </svg>
152 </span>
153 </span>
154 <span class="mdc-evolution-chip__text-label">Chip label</span>
155 </span>
156</span>
157```
158
159## API
160
161### CSS classes
162
163CSS Class | Description
164--- | ---
165`mdc-evolution-chip` | Mandatory, for the chip root.
166`mdc-evolution-chip--selectable` | Mandatory for selectable (i.e filter) chips.
167`mdc-evolution-chip--selected` | Mandatory for selectable chips that are selected. Used in conjunction with `*--selectable`.
168`mdc-evolution-chip--with-primary-graphic` | Mandatory for chips that have a primary graphic (i.e. the checkmark for filter chips and/or an icon)
169`mdc-evolution-chip--with-primary-icon` | Mandatory for chips that have an icon in the primary graphic slot (used in conjunction with `*--with-primary-graphic`). Not mandatory if the primary graphic only contains the filter chip checkmark.
170`mdc-evolution-chip--with-trailing-action` | Mandatory for chips with a trailing action.
171`mdc-evolution-chip--filter` | Optional for filter chips, making the selected treatment visually distinct.
172`mdc-evolution-chip--with-avatar` | Optinal, for chips with a primary graphic icon that should be receive the avatar treatment.
173`mdc-evolution-chip--disabled` | Optional, visually styles the chip as disabled.
174`mdc-evolution-chip__cell` | Optional, for [layout grid chips](#layout-grid). Applied to the grid cell.
175`mdc-evolution-chip__cell--primary` | Optional, for [layout grid chips](#layout-grid). Applied to the grid cell containing the primary action.
176`mdc-evolution-chip__cell--trailing` | Optional, for [layout grid chips](#layout-grid). Applied to the grid cell containing the trailing action **if** the trailing action is navigable.
177
178### Sass mixins
179
180Access to theme mixins require importing the chips theme style module.
181
182```scss
183@use "@material/chips";
184```
185
186Mixin | Description
187--- | ---
188`ripple-color($color)` | Sets the ripple color of a chip.
189`selected-ripple-color($color)` | Sets the ripple color of a selected chip.
190`trailing-action-ripple-color($color)` | Sets the ripple color of a chip trailing action.
191`trailing-action-ripple-size($size)` | Sets the ripple size of a chip trailing action.
192`density($density)` | Sets the density of a chip.
193`height($height)` | Sets the height of a chip.
194`shape-radius($radius)` | Sets the shape radius of a chip.
195`outline-width($width)` | Sets the outline width of a chip.
196`outline-color($color)` | Sets the outline color of a chip.
197`selected-outline-color($color)` | Sets the outline color of a selected chip.
198`outline-style($style)` | Sets the outline style of a chip.
199`container-color($color)` | Sets the container color of a chip.
200`selected-container-color($color)` | Sets the container color of a selected chip.
201`text-label-color($color)` | Sets the text label color of a chip.
202`selected-text-label-color($color)` | Sets the text label color of a selected chip.
203`text-label-type-scale($type-scale)` | Sets the text label type scale of a chip.
204`graphic-size($size)` | Sets the graphic size of a chip.
205`icon-color($color)` | Sets the icon color of a chip.
206`icon-container-color($color)` | Sets the icon container color of a chip.
207`icon-size($size)` | Sets the icon size of a chip.
208`trailing-action-size($size)` | Sets the trailing action size of a chip.
209`trailing-action-color($color)` | Sets the trailing action color of a chip.
210`checkmark-size($size)` | Sets the checkmark size of a chip.
211`checkmark-color($color)` | Sets the checkmark color of a chip.
212`checkmark-container-color($color)` | Sets the checkmark container color of a chip.
213`horizontal-padding($leading, $trailing)` | Sets the horizontal padding of a chip with no graphic or trailing action.
214`with-graphic-horizontal-padding($graphic-leading, $graphic-trailing, $primary-trailing)` | Sets the horizontal padding of a chip with a primary graphic.
215`with-trailing-action-horizontal-padding($primary-leading, $trailing-action-leading, $trailing-action-trailing)` | Sets the horizontal padding of a chip with a trailing action.
216`with-graphic-and-trailing-action-horizontal-padding($graphic-leading, $graphic-trailing, $trailing-action-leading, $trailing-action-trailing)` | Sets the horizontal padding of a chip with a primary graphic and trailing action.
217
218### `MDCChip` methods
219
220The `MDCChip` is exposed only to be called by the parent [`MDCChipSet`](../chip-set). Users should not interact with the `MDCChip` component nor rely on any exposed APIs or events.
221
222### `MDCChipEvents`
223
224These events are only emitted for consumption by the parent [`MDCChipSet`](../chip-set). Non-wrapping clients **should not** listen to these events.
225
226Event name | Detail | Description
227--- | --- | ---
228`MDCChip:animation` | `MDCChipAnimationEventDetail` | Consumed in the parent chip set `handleChipAnimation` method.
229`MDCChip:interaction` | `MDCChipInteractionEventDetail` | Consumed in the parent chip set `handleChipInteraction` method.
230`MDCChip:navigation` | `MDCChipNavigationEventDetail` | Consumed in the parent chip set `handleChipNavigation` method.
231
232### `MDCChipAdapter`
233
234Method Signature | Description
235--- | ---
236`addClass(className: MDCChipClassNames): void` | Adds the class name to the chip root.
237`emitEvent<D extends object>(eventName: MDCChipEvents, eventDetail: D): void` | Emits the given `eventName` with the given `eventDetail`.
238`getActions(): MDCChipActionType[]` | Returns the actions of the chip in [DOM order](https://developers.google.com/web/fundamentals/accessibility/focus/dom-order-matters).
239`getAttribute(attrName: MDCChipAttributes): string\|null` | Returns the value of the attribute or `null` if it does not exist.
240`getElementID(): string` | Returns the [`id`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) of the root element.
241`getOffsetWidth(): number` | Returns the [`offsetWidth`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth) of the root element.
242`hasClass(className: MDCChipCssClasses): boolean` | Returns `true` if the class exists on the root element, otherwise returns `false`.
243`isActionSelectable(action: MDCChipActionType): boolean` | Returns the seletability of the action with the given type.
244`isActionSelected(action: MDCChipActionType): boolean` | Returns the selected state of the action with the given type.
245`isActionFocusable(action: MDCChipActionType): boolean` | Returns the focusability of the action with the given type.
246`isActionDisabled(action: MDCChipActionType): boolean` | Returns the disabled state of the action with the given type.
247`isRTL(): boolean` | Returns `true` if the chip is rendered in an RTL context, otherwise returns `false`.
248`removeClass(className: MDCChipCssClasses): void` | Remove the given class from the root.
249`setActionDisabled(action: MDCChipActionType, isDisabled: boolean): void` | Sets the disabled state of the action with the given type.
250`setActionFocus(action: MDCChipActionType, behavior: MDCChipActionFocusBehavior): void` | Sets the focus behavior of the action with the given type.
251`setActionSelected(action: MDCChipActionType, isSelected: boolean): void` | Sets the selected state of the action with the given type.
252`setStyleProperty(property: string, value: string): void` | Sets the style property on the root to the given value.
253
254### `MDCChipFoundation`
255
256Method Signature | Description
257--- | ---
258`handleAnimationEnd(event: AnimationEvent): void` | Handles the [`animationend` event](https://developer.mozilla.org/en-US/docs/Web/API/Document/animationend_event).
259`handleTransitionEnd(): void` | Handles the [`transitionend` event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/transitionend_event).
260`handleActionInteraction(event: ActionInteractionEvent): void` | Handles the chip action's interaction event.
261`handleActionNavigation(event: ActionNavigationEvent): void` | Handles the chip action's navigation event.
262`getElementID(): string` | Returns the [`id`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) of the chip.
263`setDisabled(isDisabled: boolean): void` | Sets the disabled state of the chip.
264`isDisabled(): boolean` | Returns the disabled state of the chip.
265`getActions(): ActionType[]` | Returns the actions of the chip.
266`isActionFocusable(action: MDCChipActionType): boolean` | Returns the focusability of the given action.
267`isActionSelectable(action: MDCChipActionType): boolean` | Returns the selectability of the given action.
268`isActionSelected(action: MDCChipActionType): boolean` | Returns the selected state of the given action.
269`setActionFocus(action: MDCChipActionType, focus: MDCChipActionFocusBehavior): void` | Sets the focus behavior of the given action.
270`setActionSelected(action: MDCChipActionType, isSelected: boolean): void` | Sets the selected state of the given action.
271`startAnimation(animation: Animation): void` | Starts the given animation on the chip.
272
273### Usage within frameworks
274
275If you are using a JavaScript framework, such as React or Angular, you can create a chip for your framework. Depending on your needs, you can use the _Simple Approach: Wrapping MDC Web Vanilla Components_, or the _Advanced Approach: Using Foundations and Adapters_. Please follow the instructions [here](../../../docs/integrating-into-frameworks.md).