brumate/ui

TagGroup

A group of tags, which the user can select and remove.

News
Travel
Gaming
Shopping
Music

Selected: Travel.

<script>
  import { $bind, $signal } from 'brumate/signals';
  import Button from '@brumate/ui/Button.brm';
  import Label from '@brumate/ui/Label.brm';
  import Tag from '@brumate/ui/Tag.brm';
  import TagGroup from '@brumate/ui/TagGroup.brm';
  import TagList from '@brumate/ui/TagList.brm';
  import type { Key } from '@brumate/ui/collection.ts';

  let tags = $signal(['News', 'Travel', 'Gaming', 'Shopping', 'Music']);
  let picked = $signal<Key[]>(['Travel']);
</script>

<TagGroup
  class="ui-field"
  selectionMode="multiple"
  selectedKeys=$bind(picked)
  onRemove=((keys) => (tags = tags.filter((tag) => !keys.includes(tag))))
>
  <Label>Categories</Label>
  <TagList class="ui-taglist">
    #for (tag of tags by tag) {
      <Tag id=tag class="ui-tag">{tag}<Button slot="remove" class="ui-tag-remove">×</Button></Tag>
    }
  </TagList>
</TagGroup>
<p>Selected: {picked.join(', ') || 'none'}.</p>
<TagGroup selectionMode="multiple" onRemove=((keys) => remove(keys))>
  <Label>Categories</Label>
  <TagList>
    #for (tag of tags by tag) {
      <Tag id=tag>{tag}<Button slot="remove">×</Button></Tag>
    }
  </TagList>
</TagGroup>

Removing

With onRemove, the group can remove tags. It gets the keys, and the caller removes them from its list.

Keyboard

Tags

The TagList is a grid, and each Tag is a row with one cell, as in React Aria, so a screen reader reaches the Button inside a tag. A Tag shows data-selected, data-disabled, data-allows-removing, data-pressed, data-hovered, data-focused and data-focus-visible.