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.0
usageimport {Item, TagGroup} from '@react-spectrum/tag'

Example#


<TagGroup aria-label="Static TagGroup items example">
  <Item>Adobe Photoshop</Item>
  <Item>Adobe InDesign</Item>
  <Item>Adobe AfterEffects</Item>
  <Item>Adobe Illustrator</Item>
  <Item>Adobe Lightroom</Item>
</TagGroup>
<TagGroup aria-label="Static TagGroup items example">
  <Item>Adobe Photoshop</Item>
  <Item>Adobe InDesign</Item>
  <Item>Adobe AfterEffects</Item>
  <Item>Adobe Illustrator</Item>
  <Item>Adobe Lightroom</Item>
</TagGroup>
<TagGroup aria-label="Static TagGroup items example">
  <Item>
    Adobe Photoshop
  </Item>
  <Item>
    Adobe InDesign
  </Item>
  <Item>
    Adobe AfterEffects
  </Item>
  <Item>
    Adobe Illustrator
  </Item>
  <Item>
    Adobe Lightroom
  </Item>
</TagGroup>
Adobe Photoshop
Adobe InDesign
Adobe AfterEffects
Adobe Illustrator
Adobe Lightroom

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: 'Adobe Photoshop'},
  {id: 2, name: 'Adobe XD'},
  {id: 3, name: 'Adobe InDesign'},
  {id: 4, name: 'Adobe AfterEffects'},
  {id: 5, name: 'Adobe Illustrator'},
  {id: 6, name: 'Adobe Lightroom'},
  {id: 7, name: 'Adobe Premiere Pro'},
  {id: 8, name: 'Adobe Fresco'},
  {id: 9, name: 'Adobe Dreamweaver'}
];

<Flex width={500}>
  <TagGroup items={items} aria-label="Dynamic TagGroup items example">
    {item => <Item>{item.name}</Item>}
  </TagGroup>
</Flex>
const items = [
  { id: 1, name: 'Adobe Photoshop' },
  { id: 2, name: 'Adobe XD' },
  { id: 3, name: 'Adobe InDesign' },
  { id: 4, name: 'Adobe AfterEffects' },
  { id: 5, name: 'Adobe Illustrator' },
  { id: 6, name: 'Adobe Lightroom' },
  { id: 7, name: 'Adobe Premiere Pro' },
  { id: 8, name: 'Adobe Fresco' },
  { id: 9, name: 'Adobe Dreamweaver' }
];

<Flex width={500}>
  <TagGroup
    items={items}
    aria-label="Dynamic TagGroup items example"
  >
    {(item) => <Item>{item.name}</Item>}
  </TagGroup>
</Flex>
const items = [
  {
    id: 1,
    name:
      'Adobe Photoshop'
  },
  {
    id: 2,
    name: 'Adobe XD'
  },
  {
    id: 3,
    name:
      'Adobe InDesign'
  },
  {
    id: 4,
    name:
      'Adobe AfterEffects'
  },
  {
    id: 5,
    name:
      'Adobe Illustrator'
  },
  {
    id: 6,
    name:
      'Adobe Lightroom'
  },
  {
    id: 7,
    name:
      'Adobe Premiere Pro'
  },
  {
    id: 8,
    name: 'Adobe Fresco'
  },
  {
    id: 9,
    name:
      'Adobe Dreamweaver'
  }
];

<Flex width={500}>
  <TagGroup
    items={items}
    aria-label="Dynamic TagGroup items example"
  >
    {(item) => (
      <Item>
        {item.name}
      </Item>
    )}
  </TagGroup>
</Flex>
Adobe Photoshop
Adobe XD
Adobe InDesign
Adobe AfterEffects
Adobe Illustrator
Adobe Lightroom
Adobe Premiere Pro
Adobe Fresco
Adobe Dreamweaver

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#


Accessibility#

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.

Events#


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: 'Adobe Photoshop' },
    { id: 2, name: 'Adobe XD' },
    { id: 3, name: 'Adobe InDesign' },
    { id: 4, name: 'Adobe AfterEffects' },
    { id: 5, name: 'Adobe Illustrator' },
    { id: 6, name: 'Adobe Lightroom' },
    { id: 7, name: 'Adobe Premiere Pro' },
    { id: 8, name: 'Adobe Fresco' },
    { id: 9, name: 'Adobe Dreamweaver' }
  ];

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

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

  return (
    <Flex width={500}>
      <TagGroup
        items={items}
        allowsRemoving
        onRemove={removeItem}
        aria-label="Removable TagGroup example"
      >
        {(item) => <Item>{item.name}</Item>}
      </TagGroup>
    </Flex>
  );
}
function Example() {
  let defaultItems = [
    { id: 1, name: 'Adobe Photoshop' },
    { id: 2, name: 'Adobe XD' },
    { id: 3, name: 'Adobe InDesign' },
    { id: 4, name: 'Adobe AfterEffects' },
    { id: 5, name: 'Adobe Illustrator' },
    { id: 6, name: 'Adobe Lightroom' },
    { id: 7, name: 'Adobe Premiere Pro' },
    { id: 8, name: 'Adobe Fresco' },
    { id: 9, name: 'Adobe Dreamweaver' }
  ];

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

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

  return (
    <Flex width={500}>
      <TagGroup
        items={items}
        allowsRemoving
        onRemove={removeItem}
        aria-label="Removable TagGroup example"
      >
        {(item) => <Item>{item.name}</Item>}
      </TagGroup>
    </Flex>
  );
}
function Example() {
  let defaultItems = [
    {
      id: 1,
      name:
        'Adobe Photoshop'
    },
    {
      id: 2,
      name: 'Adobe XD'
    },
    {
      id: 3,
      name:
        'Adobe InDesign'
    },
    {
      id: 4,
      name:
        'Adobe AfterEffects'
    },
    {
      id: 5,
      name:
        'Adobe Illustrator'
    },
    {
      id: 6,
      name:
        'Adobe Lightroom'
    },
    {
      id: 7,
      name:
        'Adobe Premiere Pro'
    },
    {
      id: 8,
      name:
        'Adobe Fresco'
    },
    {
      id: 9,
      name:
        'Adobe Dreamweaver'
    }
  ];

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

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

  return (
    <Flex width={500}>
      <TagGroup
        items={items}
        allowsRemoving
        onRemove={removeItem}
        aria-label="Removable TagGroup example"
      >
        {(item) => (
          <Item>
            {item.name}
          </Item>
        )}
      </TagGroup>
    </Flex>
  );
}
Adobe Photoshop
Adobe XD
Adobe InDesign
Adobe AfterEffects
Adobe Illustrator
Adobe Lightroom
Adobe Premiere Pro
Adobe Fresco
Adobe Dreamweaver

Props#


NameTypeDefaultDescription
children<object>The contents of the collection.
allowsRemovingbooleanWhether the TagGroup allows removal of tags.
itemsIterable<object>Item objects in the collection.
Events
NameTypeDefaultDescription
onRemove( (key: Key )) => voidCalled when the user removes a tag.
Layout
NameTypeDefaultDescription
flex<stringnumberboolean>When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrow<number>When used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrink<number>When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasis<numberstring>When used in a flex layout, specifies the initial main size of the element. See MDN.
alignSelf<'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.
justifySelf<'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.
order<number>The layout order for the element within a flex or grid container. See MDN.
gridArea<string>When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumn<string>When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRow<string>When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStart<string>When used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEnd<string>When used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStart<string>When used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEnd<string>When used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
margin<>The margin for all four sides of the element. See MDN.
marginTop<>The margin for the top side of the element. See MDN.
marginBottom<>The margin for the bottom side of the element. See MDN.
marginStart<>The margin for the logical start side of the element, depending on layout direction. See MDN.
marginEnd<>The margin for the logical end side of an element, depending on layout direction. See MDN.
marginX<>The margin for both the left and right sides of the element. See MDN.
marginY<>The margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
width<>The width of the element. See MDN.
minWidth<>The minimum width of the element. See MDN.
maxWidth<>The maximum width of the element. See MDN.
height<>The height of the element. See MDN.
minHeight<>The minimum height of the element. See MDN.
maxHeight<>The maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
position<'static''relative''absolute''fixed''sticky'>Specifies how the element is positioned. See MDN.
top<>The top position for the element. See MDN.
bottom<>The bottom position for the element. See MDN.
left<>The left position for the element. See MDN. Consider using start instead for RTL support.
right<>The right position for the element. See MDN. Consider using start instead for RTL support.
start<>The logical start position for the element, depending on layout direction. See MDN.
end<>The logical end position for the element, depending on layout direction. See MDN.
zIndex<number>The stacking order for the element. See MDN.
isHidden<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>
News
Travel
Gaming
Shopping