Tags allow users to categorize content. They can represent keywords or people, and are grouped to describe an item or a search request.
Content
TagGroup follows the Collection Components API, accepting both static and dynamic collections. This example shows a dynamic collection, passing a list of objects to the items prop, and a function to render the children. Items can be removed via the onRemove event.
import {TagGroup, Tag} from '@react-spectrum/s2/TagGroup';
import {useListData} from '@react-spectrum/s2/useListData';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
function PhotoCategories() {
let list = useListData({
initialItems: [
{id: 1, name: 'Landscape'},
{id: 2, name: 'Portrait'},
{id: 3, name: 'Night'},
{id: 4, name: 'Dual'},
{id: 5, name: 'Golden Hour'}
]
});
return (
<TagGroup
label="Photo categories"
styles={style({maxWidth: 320})}
items={list.items}
onRemove={(keys) => list.remove(...keys)}>
{(item) => <Tag>{item.name}</Tag>}
</TagGroup>
);
}
Slots
Tag supports icons, avatars, or images, and text as children.
import {TagGroup, Tag, Text} from '@react-spectrum/s2/TagGroup';
import FileText from '@react-spectrum/s2/icons/FileText';
import Camera from '@react-spectrum/s2/icons/Camera';
import Filmstrip from '@react-spectrum/s2/icons/Filmstrip';
import VectorDraw from '@react-spectrum/s2/icons/VectorDraw';
<TagGroup label="Media types">
<Tag textValue="Text">
<FileText />
<Text>Text</Text>
</Tag>
<Tag textValue="Photos">
<Camera />
<Text>Photos</Text>
</Tag>
<Tag textValue="Videos">
<Filmstrip />
<Text>Videos</Text>
</Tag>
<Tag textValue="Illustration">
<VectorDraw />
<Text>Illustration</Text>
</Tag>
</TagGroup>
Links
Use the href prop on a <Tag> to create a link. See the getting started guide to learn how to integrate with your framework.
import {TagGroup, Tag} from '@react-spectrum/s2/TagGroup';
<TagGroup label="Photo categories">
<Tag href="https://en.wikipedia.org/wiki/Landscape_photography" target="_blank">Landscape</Tag>
<Tag href="https://en.wikipedia.org/wiki/Portrait_photography" target="_blank">Portrait</Tag>
<Tag href="https://en.wikipedia.org/wiki/Macro_photography" target="_blank">Macro</Tag>
<Tag href="https://en.wikipedia.org/wiki/Night_photography" target="_blank">Night</Tag>
<Tag href="https://en.wikipedia.org/wiki/Dualphotography" target="_blank">Dual</Tag>
<Tag href="https://en.wikipedia.org/wiki/Golden_hour_(photography)" target="_blank">Golden Hour</Tag>
</TagGroup>
Empty state
Use renderEmptyState to render placeholder content when the TagGroup is empty.
import {TagGroup} from '@react-spectrum/s2/TagGroup';
import {Link} from '@react-spectrum/s2/Link';
<TagGroup
label="Photo categories"
renderEmptyState={() => (
<>No categories. <Link href="https://react-spectrum.adobe.com/">Click here</Link> to add some.</>
)}>
{[]}
</TagGroup>
Selection and actions
Use the selectionMode prop to enable single or multiple selection. The selected items can be controlled via the selectedKeys prop, matching the id prop of the items. Items can be disabled with the isDisabled prop. See the selection guide for more details.
Current selection:
Use the onAction prop to handle item actions.
import {TagGroup, Tag} from '@react-spectrum/s2/TagGroup';
function Example() {
return (
<TagGroup
label="Music genre"
onAction={key => alert(`Clicked ${key}`)}
>
<Tag id="rock">Rock</Tag>
<Tag id="jazz">Jazz</Tag>
<Tag id="pop">Pop</Tag>
<Tag id="classical">Classical</Tag>
<Tag id="edm">EDM</Tag>
</TagGroup>
);
}
Group actions
Use the groupActionLabel and onGroupAction props to add an action button at the end of the tags.
import {TagGroup, Tag} from '@react-spectrum/s2/TagGroup';
<TagGroup
label="Interests"
groupActionLabel="Clear"
onGroupAction={() => alert('Clear')}>
<Tag>News</Tag>
<Tag>Travel</Tag>
<Tag>Gaming</Tag>
<Tag>Shopping</Tag>
</TagGroup>
API
<TagGroup>
<Tag>
<Icon /> or <Avatar /> or <Image />
<Text />
</Tag>
</TagGroup>
TagGroup
Tags allow users to categorize content. They can represent keywords or people, and are grouped to describe an item or a search request.
| Name | Type | Default |
|---|---|---|
size | 'S'
| 'M'
| 'L' | Default: 'M'
|
The size of the tag group. | ||
isEmphasized | boolean | Default: — |
Whether the tags are displayed in an emphasized style. | ||
maxRows | number | Default: — |
Limit the number of rows initially shown. This will render a button that allows the user to expand to show all tags. | ||
groupActionLabel | string | Default: — |
The label to display on the action button. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
children | ReactNode | | Default: — |
The contents of the collection. | ||
items | Iterable | Default: — |
Item objects in the collection. | ||
renderEmptyState | | Default: — |
Provides content to display when there are no items in the tag group. | ||
selectionMode | SelectionMode | Default: — |
The type of selection that is allowed in the collection. | ||
selectionBehavior | SelectionBehavior | Default: 'toggle'
|
How multiple selection should behave in the collection. | ||
selectedKeys | 'all' | Iterable | Default: — |
The currently selected keys in the collection (controlled). | ||
defaultSelectedKeys | 'all' | Iterable | Default: — |
The initial selected keys in the collection (uncontrolled). | ||
onSelectionChange | | Default: — |
Handler that is called when the selection changes. | ||
disabledKeys | Iterable | Default: — |
The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. | ||
disallowEmptySelection | boolean | Default: — |
Whether the collection allows empty selection. | ||
shouldSelectOnPressUp | boolean | Default: — |
Whether selection should occur on press up instead of press down. | ||
escapeKeyBehavior | 'clearSelection' | 'none' | Default: 'clearSelection'
|
Whether pressing the escape key should clear selection in the TagGroup or not. Most experiences should not modify this option as it eliminates a keyboard user's ability to easily clear selection. Only use if the escape key is being handled externally or should not trigger selection clearing contextually. | ||
label | ReactNode | Default: — |
The content to display as the label. | ||
labelPosition | LabelPosition | Default: 'top'
|
The label's overall position relative to the element it is labeling. | ||
labelAlign | Alignment | Default: 'start'
|
The label's horizontal alignment relative to the element it is labeling. | ||
contextualHelp | ReactNode | Default: — |
A ContextualHelp element to place next to the label. | ||
isInvalid | boolean | Default: — |
Whether the tags are displayed in a error state. | ||
description | ReactNode | Default: — |
A description for the tag group. | ||
errorMessage | ReactNode | Default: — |
An error message for the field. | ||
onGroupAction | | Default: — |
Handler that is called when the action button is pressed. | ||
onRemove | | Default: — |
Handler that is called when a user deletes a tag. | ||
onAction | | Default: — |
Handler that is called when a user performs an action on an item. The exact user event depends
on the collection's | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
Tag
An individual Tag for TagGroups.
| Name | Type | |
|---|---|---|
children | ReactNode | |
The children of the tag. | ||
id | Key | |
A unique id for the tag. | ||
textValue | string | |
A string representation of the tags's contents, used for accessibility. Required if children is not a plain text string. | ||
isDisabled | boolean | |
Whether the tag is disabled. | ||
label | ReactNode | |
The content to display as the label. | ||
onAction | | |
Handler that is called when a user performs an action on the item. The exact user event depends
on the collection's | ||
onHoverStart | | |
Handler that is called when a hover interaction starts. | ||
onHoverEnd | | |
Handler that is called when a hover interaction ends. | ||
onHoverChange | | |
Handler that is called when the hover state changes. | ||
onFocus | | |
Handler that is called when the element receives focus. | ||
onBlur | | |
Handler that is called when the element loses focus. | ||
onFocusChange | | |
Handler that is called when the element's focus status changes. | ||
onPress | | |
Handler that is called when the press is released over the target. | ||
onPressStart | | |
Handler that is called when a press interaction starts. | ||
onPressEnd | | |
Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. | ||
onPressChange | | |
Handler that is called when the press state changes. | ||
onPressUp | | |
Handler that is called when a press is released over the target, regardless of whether it started on the target or not. | ||
href | Href | |
A URL to link to. See MDN. | ||
hrefLang | string | |
Hints at the human language of the linked URL. SeeMDN. | ||
target | HTMLAttributeAnchorTarget | |
The target window for the link. See MDN. | ||
rel | string | |
The relationship between the linked resource and the current page. See MDN. | ||
download | boolean | string | |
Causes the browser to download the linked URL. A string may be provided to suggest a file name. See MDN. | ||
ping | string | |
A space-separated list of URLs to ping when the link is followed. See MDN. | ||
referrerPolicy | HTMLAttributeReferrerPolicy | |
How much of the referrer to send when following the link. See MDN. | ||
routerOptions | RouterOptions | |
Options for the configured client side router. | ||