beta

TagGroup

Tags allow users to categorize content. They can represent keywords or people, and are grouped to describe an item or a search request.

installyarn add @react-spectrum/tag
version3.0.0-beta.4
usageimport {Item, TagGroup} from '@react-spectrum/tag'

Example#


<TagGroup aria-label="Static TagGroup items example">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
<TagGroup aria-label="Static TagGroup items example">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
<TagGroup aria-label="Static TagGroup items example">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>

Content#


TagGroup is a component that allows users to categorize content. Basic usage of TagGroup, seen in the example above, shows the use of a static collection where the contents of the TagGroup are hard coded. Dynamic collections, as shown below, can be used when the options come from an external data source such as an API, or update over time.

Each item has a unique key defined by the data. In the example below, the key of each row element is implicitly defined by the id property of the row object. See collections to learn more about keys in dynamic collections.

const items = [
  {id: 1, name: 'News'},
  {id: 2, name: 'Travel'},
  {id: 3, name: 'Gaming'},
  {id: 4, name: 'Shopping'}
];

<TagGroup items={items} aria-label="Dynamic TagGroup items example">
  {item => <Item>{item.name}</Item>}
</TagGroup>
const items = [
  { id: 1, name: 'News' },
  { id: 2, name: 'Travel' },
  { id: 3, name: 'Gaming' },
  { id: 4, name: 'Shopping' }
];

<TagGroup
  items={items}
  aria-label="Dynamic TagGroup items example"
>
  {(item) => <Item>{item.name}</Item>}
</TagGroup>
const items = [
  {
    id: 1,
    name: 'News'
  },
  {
    id: 2,
    name: 'Travel'
  },
  {
    id: 3,
    name: 'Gaming'
  },
  {
    id: 4,
    name: 'Shopping'
  }
];

<TagGroup
  items={items}
  aria-label="Dynamic TagGroup items example"
>
  {(item) => (
    <Item>
      {item.name}
    </Item>
  )}
</TagGroup>

Internationalization#

To internationalize a TagGroup, all text content within the TagGroup should be localized. This includes the aria-label provided to the TagGroup if any. For languages that are read right-to-left (e.g. Hebrew and Arabic), the layout of TagGroup is automatically flipped.

Labeling#


A visual label can be provided with the label prop. If a visual label isn't included, an aria-label must be provided to the TagGroup for accessibility. If the TagGroup is labeled by a separate element, an aria-labelledby prop must be provided using the id of the labeling element instead.

<TagGroup label="Categories">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
<TagGroup label="Categories">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
<TagGroup label="Categories">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>

Events#


onRemove#

Removing tags can be enabled by providing the allowsRemoving prop to the TagGroup. TagGroup will pass the key of the removed item to the onRemove handler as shown below.

function Example() {
  let defaultItems = [
    {id: 1, name: 'News'},
    {id: 2, name: 'Travel'},
    {id: 3, name: 'Gaming'},
    {id: 4, name: 'Shopping'}
  ];

  let [items, setItems] = React.useState(defaultItems);

  let removeItem = (key) => {
    setItems(prevItems => prevItems.filter((item) => key !== item.id));
  };

  return (
    <TagGroup
      items={items}
      allowsRemoving
      onRemove={removeItem}      aria-label="Removable TagGroup example">
      {item => <Item>{item.name}</Item>}
    </TagGroup>
  );
}
function Example() {
  let defaultItems = [
    { id: 1, name: 'News' },
    { id: 2, name: 'Travel' },
    { id: 3, name: 'Gaming' },
    { id: 4, name: 'Shopping' }
  ];

  let [items, setItems] = React.useState(defaultItems);

  let removeItem = (key) => {
    setItems((prevItems) =>
      prevItems.filter((item) => key !== item.id)
    );
  };

  return (
    <TagGroup
      items={items}
      allowsRemoving
      onRemove={removeItem}      aria-label="Removable TagGroup example"
    >
      {(item) => <Item>{item.name}</Item>}
    </TagGroup>
  );
}
function Example() {
  let defaultItems = [
    {
      id: 1,
      name: 'News'
    },
    {
      id: 2,
      name: 'Travel'
    },
    {
      id: 3,
      name: 'Gaming'
    },
    {
      id: 4,
      name: 'Shopping'
    }
  ];

  let [items, setItems] =
    React.useState(
      defaultItems
    );

  let removeItem = (
    key
  ) => {
    setItems(
      (prevItems) =>
        prevItems.filter(
          (item) =>
            key !==
              item.id
        )
    );
  };

  return (
    <TagGroup
      items={items}
      allowsRemoving
      onRemove={removeItem}      aria-label="Removable TagGroup example"
    >
      {(item) => (
        <Item>
          {item.name}
        </Item>
      )}
    </TagGroup>
  );
}

onAction#

TagGroup supports an onAction handler that, when used with the actionLabel prop, will add an action button at the end of the tags that can be used to perform a custom action.

<TagGroup
  actionLabel="Clear"
  onAction={() => alert('Clear action pressed.')}  aria-label="TagGroup with action">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
<TagGroup
  actionLabel="Clear"
  onAction={() => alert('Clear action pressed.')}  aria-label="TagGroup with action">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
<TagGroup
  actionLabel="Clear"
  onAction={() =>
    alert(
      'Clear action pressed.'
    )}  aria-label="TagGroup with action"
>
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>

Props#


NameTypeDefaultDescription
childrenCollectionChildren<object>The contents of the collection.
actionLabelstringThe label to display on the action button.
keyboardDelegateTagKeyboardDelegate<object>

An optional keyboard delegate to handle arrow key navigation, to override the default.

allowsRemovingbooleanWhether the TagGroup allows removal of tags.
maxRowsnumberLimit the number of rows initially shown. This will render a button that allows the user to expand to show all tags.
itemsIterable<object>Item objects in the collection.
validationStateValidationStateWhether the input should display its "valid" or "invalid" visual styling.
isRequiredboolean

Whether user input is required on the input before form submission. Often paired with the necessityIndicator prop to add a visual indicator to the input.

labelPositionLabelPosition'top'The label's overall position relative to the element it is labeling.
labelAlignAlignment'start'The label's horizontal alignment relative to the element it is labeling.
necessityIndicatorNecessityIndicator'icon'Whether the required state should be shown as an icon or text.
contextualHelpReactNodeA ContextualHelp element to place next to the label.
labelReactNodeThe content to display as the label.
isDisabledbooleanWhether the description is displayed with lighter text.
descriptionReactNodeA description for the field. Provides a hint such as specific requirements for what to choose.
errorMessageReactNodeAn error message for the field.
Events
NameTypeDefaultDescription
onAction() => voidHandler that is called when the action button is pressed.
onRemove( (key: Key )) => voidCalled when the user removes a tag.
Layout
NameTypeDefaultDescription
flexResponsive<stringnumberboolean>When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrowResponsive<number>When used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrinkResponsive<number>When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasisResponsive<numberstring>When used in a flex layout, specifies the initial main size of the element. See MDN.
alignSelfResponsive<'auto''normal''start''end''center''flex-start''flex-end''self-start''self-end''stretch'>Overrides the alignItems property of a flex or grid container. See MDN.
justifySelfResponsive<'auto''normal''start''end''flex-start''flex-end''self-start''self-end''center''left''right''stretch'>Specifies how the element is justified inside a flex or grid container. See MDN.
orderResponsive<number>The layout order for the element within a flex or grid container. See MDN.
gridAreaResponsive<string>When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumnResponsive<string>When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRowResponsive<string>When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStartResponsive<string>When used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEndResponsive<string>When used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStartResponsive<string>When used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEndResponsive<string>When used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
marginResponsive<DimensionValue>The margin for all four sides of the element. See MDN.
marginTopResponsive<DimensionValue>The margin for the top side of the element. See MDN.
marginBottomResponsive<DimensionValue>The margin for the bottom side of the element. See MDN.
marginStartResponsive<DimensionValue>The margin for the logical start side of the element, depending on layout direction. See MDN.
marginEndResponsive<DimensionValue>The margin for the logical end side of an element, depending on layout direction. See MDN.
marginXResponsive<DimensionValue>The margin for both the left and right sides of the element. See MDN.
marginYResponsive<DimensionValue>The margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
widthResponsive<DimensionValue>The width of the element. See MDN.
minWidthResponsive<DimensionValue>The minimum width of the element. See MDN.
maxWidthResponsive<DimensionValue>The maximum width of the element. See MDN.
heightResponsive<DimensionValue>The height of the element. See MDN.
minHeightResponsive<DimensionValue>The minimum height of the element. See MDN.
maxHeightResponsive<DimensionValue>The maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
positionResponsive<'static''relative''absolute''fixed''sticky'>Specifies how the element is positioned. See MDN.
topResponsive<DimensionValue>The top position for the element. See MDN.
bottomResponsive<DimensionValue>The bottom position for the element. See MDN.
leftResponsive<DimensionValue>The left position for the element. See MDN. Consider using start instead for RTL support.
rightResponsive<DimensionValue>The right position for the element. See MDN. Consider using start instead for RTL support.
startResponsive<DimensionValue>The logical start position for the element, depending on layout direction. See MDN.
endResponsive<DimensionValue>The logical end position for the element, depending on layout direction. See MDN.
zIndexResponsive<number>The stacking order for the element. See MDN.
isHiddenResponsive<boolean>Hides the element.
Accessibility
NameTypeDefaultDescription
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
Advanced
NameTypeDefaultDescription
UNSAFE_classNamestringSets the CSS className for the element. Only use as a last resort. Use style props instead.
UNSAFE_styleCSSPropertiesSets inline style for the element. Only use as a last resort. Use style props instead.

Visual options#


With icons#

<TagGroup aria-label="TagGroup with icons example">
  <Item>
    <News />
    <Text>News</Text>
  </Item>
  <Item>
    <Airplane />
    <Text>Travel</Text>
  </Item>
  <Item>
    <Game />
    <Text>Gaming</Text>
  </Item>
  <Item>
    <ShoppingCart />
    <Text>Shopping</Text>
  </Item>
</TagGroup>
<TagGroup aria-label="TagGroup with icons example">
  <Item>
    <News />
    <Text>News</Text>
  </Item>
  <Item>
    <Airplane />
    <Text>Travel</Text>
  </Item>
  <Item>
    <Game />
    <Text>Gaming</Text>
  </Item>
  <Item>
    <ShoppingCart />
    <Text>Shopping</Text>
  </Item>
</TagGroup>
<TagGroup aria-label="TagGroup with icons example">
  <Item>
    <News />
    <Text>News</Text>
  </Item>
  <Item>
    <Airplane />
    <Text>Travel</Text>
  </Item>
  <Item>
    <Game />
    <Text>Gaming</Text>
  </Item>
  <Item>
    <ShoppingCart />
    <Text>
      Shopping
    </Text>
  </Item>
</TagGroup>

With avatars#

<TagGroup aria-label="TagGroup with avatars example">
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>Person 1</Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>Person 2</Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>Person 3</Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>Person 4</Text>
  </Item>
</TagGroup>
<TagGroup aria-label="TagGroup with avatars example">
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>Person 1</Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>Person 2</Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>Person 3</Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>Person 4</Text>
  </Item>
</TagGroup>
<TagGroup aria-label="TagGroup with avatars example">
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>
      Person 1
    </Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>
      Person 2
    </Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>
      Person 3
    </Text>
  </Item>
  <Item>
    <Avatar src="https://i.imgur.com/kJOwAdv.png" />
    <Text>
      Person 4
    </Text>
  </Item>
</TagGroup>

Label position and alignment#

By default, the label is positioned above the TagGroup. The labelPosition prop can be used to position the label to the side. The labelAlign prop can be used to align the label as "start" or "end". For left-to-right (LTR) languages, "start" refers to the left most edge of the TagGroup and "end" refers to the right most edge. For right-to-left (RTL) languages, this is flipped.

<TagGroup label="Categories" labelPosition="side" labelAlign="end">
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
<TagGroup
  label="Categories"
  labelPosition="side"
  labelAlign="end"
>
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
<TagGroup
  label="Categories"
  labelPosition="side"
  labelAlign="end"
>
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>

Help text#

View guidelines

Both a description and an error message can be supplied to a TagGroup. The description is always visible unless the validationState is “invalid” and an error message is provided. The error message can be used to help the user fix their input quickly and should be specific to the detected error. All strings should be localized.

function Example() {
  let defaultItems = [
    {id: 1, name: 'News'},
    {id: 2, name: 'Travel'},
    {id: 3, name: 'Gaming'},
    {id: 4, name: 'Shopping'}
  ];

  let [items, setItems] = React.useState(defaultItems);

  let removeItem = (key) => {
    setItems(prevItems => prevItems.filter((item) => key !== item.id));
  };

  let isValid = items.length <= 3;

  return (
    <TagGroup
      label="Categories"
      items={items}
      allowsRemoving
      onRemove={removeItem}
      aria-label="TagGroup help text example"
      validationState={isValid ? 'valid' : 'invalid'}
      description="Please include tags for related categories."
      errorMessage="Must contain no more than 3 tags. Please remove some."
      >
      {item => <Item>{item.name}</Item>}
    </TagGroup>
  );
}
function Example() {
  let defaultItems = [
    { id: 1, name: 'News' },
    { id: 2, name: 'Travel' },
    { id: 3, name: 'Gaming' },
    { id: 4, name: 'Shopping' }
  ];

  let [items, setItems] = React.useState(defaultItems);

  let removeItem = (key) => {
    setItems((prevItems) =>
      prevItems.filter((item) => key !== item.id)
    );
  };

  let isValid = items.length <= 3;

  return (
    <TagGroup
      label="Categories"
      items={items}
      allowsRemoving
      onRemove={removeItem}
      aria-label="TagGroup help text example"
      validationState={isValid ? 'valid' : 'invalid'}
      description="Please include tags for related categories."
      errorMessage="Must contain no more than 3 tags. Please remove some."
    >
      {(item) => <Item>{item.name}</Item>}
    </TagGroup>
  );
}
function Example() {
  let defaultItems = [
    {
      id: 1,
      name: 'News'
    },
    {
      id: 2,
      name: 'Travel'
    },
    {
      id: 3,
      name: 'Gaming'
    },
    {
      id: 4,
      name: 'Shopping'
    }
  ];

  let [items, setItems] =
    React.useState(
      defaultItems
    );

  let removeItem = (
    key
  ) => {
    setItems(
      (prevItems) =>
        prevItems.filter(
          (item) =>
            key !==
              item.id
        )
    );
  };

  let isValid =
    items.length <= 3;

  return (
    <TagGroup
      label="Categories"
      items={items}
      allowsRemoving
      onRemove={removeItem}
      aria-label="TagGroup help text example"
      validationState={isValid
        ? 'valid'
        : 'invalid'}
      description="Please include tags for related categories."
      errorMessage="Must contain no more than 3 tags. Please remove some."
    >
      {(item) => (
        <Item>
          {item.name}
        </Item>
      )}
    </TagGroup>
  );
}

Contextual help#

A ContextualHelp element may be placed next to the label to provide additional information or help about a TagGroup.

import {Content, ContextualHelp, Heading} from '@adobe/react-spectrum';

<TagGroup
  label="Categories"
  contextualHelp={
    <ContextualHelp>
      <Heading>What are tags?</Heading>
      <Content>Tags allow users to categorize content.</Content>
    </ContextualHelp>
  }>
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
import {
  Content,
  ContextualHelp,
  Heading
} from '@adobe/react-spectrum';

<TagGroup
  label="Categories"
  contextualHelp={
    <ContextualHelp>
      <Heading>What are tags?</Heading>
      <Content>
        Tags allow users to categorize content.
      </Content>
    </ContextualHelp>
  }
>
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>
import {
  Content,
  ContextualHelp,
  Heading
} from '@adobe/react-spectrum';

<TagGroup
  label="Categories"
  contextualHelp={
    <ContextualHelp>
      <Heading>
        What are tags?
      </Heading>
      <Content>
        Tags allow
        users to
        categorize
        content.
      </Content>
    </ContextualHelp>
  }
>
  <Item>News</Item>
  <Item>Travel</Item>
  <Item>Gaming</Item>
  <Item>Shopping</Item>
</TagGroup>

Limit rows#

TagGroup supports a maxRows prop that will limit the number of rows initially shown. This will add an action button that can be pressed to show the remaining tags.

<View
  maxWidth="size-3400"
  minHeight="size-2000"
  padding="size-150"
  borderWidth="thin"
  borderColor="dark"
  borderRadius="medium"
>
  <TagGroup
    maxRows={2}    aria-label="Static TagGroup items example with maxRows set"
  >
    <Item>News</Item>
    <Item>Travel</Item>
    <Item>Gaming</Item>
    <Item>Shopping</Item>
    <Item>Business</Item>
    <Item>Entertainment</Item>
    <Item>Food</Item>
    <Item>Technology</Item>
    <Item>Politics</Item>
    <Item>Health</Item>
    <Item>Science</Item>
  </TagGroup>
</View>
<View
  maxWidth="size-3400"
  minHeight="size-2000"
  padding="size-150"
  borderWidth="thin"
  borderColor="dark"
  borderRadius="medium"
>
  <TagGroup
    maxRows={2}    aria-label="Static TagGroup items example with maxRows set"
  >
    <Item>News</Item>
    <Item>Travel</Item>
    <Item>Gaming</Item>
    <Item>Shopping</Item>
    <Item>Business</Item>
    <Item>Entertainment</Item>
    <Item>Food</Item>
    <Item>Technology</Item>
    <Item>Politics</Item>
    <Item>Health</Item>
    <Item>Science</Item>
  </TagGroup>
</View>
<View
  maxWidth="size-3400"
  minHeight="size-2000"
  padding="size-150"
  borderWidth="thin"
  borderColor="dark"
  borderRadius="medium"
>
  <TagGroup
    maxRows={2}    aria-label="Static TagGroup items example with maxRows set"
  >
    <Item>News</Item>
    <Item>Travel</Item>
    <Item>Gaming</Item>
    <Item>
      Shopping
    </Item>
    <Item>
      Business
    </Item>
    <Item>
      Entertainment
    </Item>
    <Item>Food</Item>
    <Item>
      Technology
    </Item>
    <Item>
      Politics
    </Item>
    <Item>Health</Item>
    <Item>
      Science
    </Item>
  </TagGroup>
</View>