# Tag group API

> Explore the API of Cimpress UI's tag group component.

Source: https://ui.cimpress.io/components/tag-group/api/

## Import

Cimpress UI exports two tag-related components:

- `TagGroup`: container component that groups related tags together, providing a label, description, error handling, and selection and removal.
- `Tag`: component to display a tag within a tag group, optionally with an icon.

```tsx
import { TagGroup, Tag } from '@cimpress-ui/react';
```

## Controlled/uncontrolled usage

```tsx
import { Tag, TagGroup, type Selection, Stack } from '@cimpress-ui/react';
import { useState } from 'react';

export default function Demo() {
  const [selectedKeys, setSelectedKeys] = useState<Selection>(() => new Set(['apple']));

  return (
    <Stack gap={32}>
      <TagGroup
        label="Favorite fruits (controlled)"
        selectionMode="multiple"
        selectedKeys={selectedKeys}
        onSelectionChange={setSelectedKeys}
      >
        <Tag id="apple">Apple</Tag>
        <Tag id="banana">Banana</Tag>
        <Tag id="cherry">Cherry</Tag>
      </TagGroup>

      <TagGroup label="Favorite fruits (uncontrolled)" selectionMode="multiple" defaultSelectedKeys={['apple']}>
        <Tag id="apple">Apple</Tag>
        <Tag id="banana">Banana</Tag>
        <Tag id="cherry">Cherry</Tag>
      </TagGroup>
    </Stack>
  );
}
```

## Accessibility notes

`TagGroup` requires a textual label to remain accessible to assistive technologies. See our [accessibility guide](/dev-guides/accessibility#labelling) for more details.

`TagGroup` is a single tab stop within the page's tab order. Use the Tab key to navigate to the group, and the arrow keys to navigate between tags within the group. Use the Backspace or Delete key to remove tags from the group.

## Collections

`TagGroup` is a collection component. See our [collection components guide](/dev-guides/collection-components) to learn how to work with collections.

## API reference

### TagGroup

Displays a group of tags. Allows for selecting or removing tags.

See [tag group usage guidelines](https://ui.cimpress.io/components/tag-group/).

- `ref`: `Ref<HTMLDivElement>`

  The `ref` type for this component.

**Props: `TagGroupProps<T extends CollectionItem = CollectionItem>`**

- `isRequired`: `boolean`

  Whether the tag group is required.

- `error`: `string`

  The error message to display.

- `size`: `'medium' | 'large'`

  The size of the tags.

- `renderEmptyState`: `() => ReactNode`

  Provides content to display when there are no tags in the group.

- `id`: `string`

  The element's unique identifier. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).

- `data-cim-style-root`: `boolean`

  Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

- `UNSAFE_className`: `string`

  Sets the CSS className for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

  Sets the CSS style for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `label`: `string`

  The content to display as the label.

- `aria-label`: `string`

  Defines a string value that labels the current element.

- `aria-labelledby`: `string`

  Identifies the element (or elements) that labels the current element.

- `aria-describedby`: `string`

  Identifies the element (or elements) that describes the object.

- `aria-details`: `string`

  Identifies the element (or elements) that provide a detailed, extended description for the object.

- `description`: `string`

  A description for the field. Provides a hint such as specific requirements for what to choose.

- `items`: `Iterable<T, any, any>`

  Item objects in the collection.

- `disabledKeys`: `Iterable<Key, any, any>`

  The item keys that are disabled. These items cannot be selected, focused, or otherwise
  interacted with.

- `selectionMode`: `SelectionMode`

  The type of selection that is allowed in the collection.

- `selectedKeys`: `'all' | (Iterable<Key, any, any>)`

  The currently selected keys in the collection (controlled).

- `defaultSelectedKeys`: `'all' | (Iterable<Key, any, any>)`

  The initial selected keys in the collection (uncontrolled).

- `onSelectionChange`: `(keys: Selection) => void`

  Handler that is called when the selection changes.

- `onRemove`: `(keys: Set<Key>) => void`

  Handler that is called when a user deletes a tag.

- `children`: `ReactNode | ((item: T) => ReactNode)`

  The contents of the collection.

**Props: `StyleProps`**

- `margin`: `Responsive<Spacing | "auto">`

  The amount of margin applied to all edges of this component.

- `marginX`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left and right edges of this component. Takes priority over `margin`.

- `marginY`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top and bottom edges of this component. Takes priority over `margin`.

- `marginTop`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top edge of this component. Takes priority over `marginY` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the right edge of this component. Takes priority over `marginX` and `margin`.

- `marginBottom`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the bottom edge of this component. Takes priority over `marginY` and `margin`.

- `marginLeft`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left edge of this component. Takes priority over `marginX` and `margin`.

### Tag

Displays a single tag within a tag group.

See [tag group usage guidelines](https://ui.cimpress.io/components/tag-group/).

- `ref`: `Ref<HTMLDivElement>`

  The `ref` type for this component.

**Props: `TagProps`**

- `children` (required): `StringLikeChildren`

  The content to display as the label.

- `iconStart`: `ReactNode`

  The icon to display at the start of the tag.

- `data-cim-style-root`: `boolean`

  Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.

- `UNSAFE_className`: `string`

  Sets the CSS className for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `UNSAFE_style`: `CSSProperties`

  Sets the CSS style for the element. Only use as a **last resort**. Use style props instead.

  See [styling guide](https://ui.cimpress.io/dev-guides/styling/).

- `id`: `Key`

  A unique id for the tag.

- `isDisabled`: `boolean`

  Whether the tag is disabled.

**Props: `StyleProps`**

- `margin`: `Responsive<Spacing | "auto">`

  The amount of margin applied to all edges of this component.

- `marginX`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left and right edges of this component. Takes priority over `margin`.

- `marginY`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top and bottom edges of this component. Takes priority over `margin`.

- `marginTop`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the top edge of this component. Takes priority over `marginY` and `margin`.

- `marginRight`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the right edge of this component. Takes priority over `marginX` and `margin`.

- `marginBottom`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the bottom edge of this component. Takes priority over `marginY` and `margin`.

- `marginLeft`: `Responsive<Spacing | "auto">`

  The amount of margin applied to the left edge of this component. Takes priority over `marginX` and `margin`.
