> Markdown version of [Badge](https://vaadin.com/docs/latest/it/components/badge). Section index: [llms.txt](https://vaadin.com/docs/latest/it/components/llms.txt)

# Badge (since V25.1)

I badge sono elementi di testo colorati che contengono piccole informazioni. Sono utilizzati per etichettare contenuti, visualizzare metadati e/o evidenziare informazioni.

**Lit** — `badge-basic.ts`

```html
<vaadin-badge>Pending</vaadin-badge>
<vaadin-badge theme="success">Confirmed</vaadin-badge>
<vaadin-badge theme="warning">Warning</vaadin-badge>
<vaadin-badge theme="error">Denied</vaadin-badge>
```

**Flow** — `BadgeBasic.java`

```java
Badge pending = new Badge("Pending");

Badge confirmed = new Badge("Confirmed");
confirmed.addThemeVariants(BadgeVariant.SUCCESS);

Badge warning = new Badge("Warning");
warning.addThemeVariants(BadgeVariant.WARNING);

Badge denied = new Badge("Denied");
denied.addThemeVariants(BadgeVariant.ERROR);
```

**React** — `badge-basic.tsx`

```tsx
<Badge>Pending</Badge>
<Badge theme="success">Confirmed</Badge>
<Badge theme="warning">Warning</Badge>
<Badge theme="error">Denied</Badge>
```

## <a id="etichetta"></a>Etichetta

I badge dovrebbero contenere un’etichetta. Le etichette dovrebbero essere chiare, concise e scritte usando la maiuscola a inizio frase. Punti a 1 o 2 parole.

## <a id="icone"></a>Icone

I badge possono contenere icone oltre al testo. Le icone vengono posizionate prima del testo usando lo slot `icon`.

**Lit** — `badge-icons.ts`

```html
<vaadin-horizontal-layout theme="spacing">
  <vaadin-badge>
    <vaadin-icon icon="vaadin:clock" slot="icon"></vaadin-icon>
    Pending
  </vaadin-badge>
  <vaadin-badge theme="success">
    <vaadin-icon icon="vaadin:check" slot="icon"></vaadin-icon>
    Confirmed
  </vaadin-badge>
  <vaadin-badge theme="warning">
    <vaadin-icon icon="vaadin:warning" slot="icon"></vaadin-icon>
    Warning
  </vaadin-badge>
  <vaadin-badge theme="error">
    <vaadin-icon icon="vaadin:exclamation-circle-o" slot="icon"></vaadin-icon>
    Denied
  </vaadin-badge>
</vaadin-horizontal-layout>
```

**Flow** — `BadgeIcons.java`

```java
Badge pending = new Badge("Pending", VaadinIcon.CLOCK.create());

Badge confirmed = new Badge("Confirmed", VaadinIcon.CHECK.create());
confirmed.addThemeVariants(BadgeVariant.SUCCESS);

Badge warning = new Badge("Warning", VaadinIcon.WARNING.create());
warning.addThemeVariants(BadgeVariant.WARNING);

Badge denied = new Badge("Denied",
        VaadinIcon.EXCLAMATION_CIRCLE_O.create());
denied.addThemeVariants(BadgeVariant.ERROR);
```

**React** — `badge-icons.tsx`

```tsx
<HorizontalLayout theme="spacing">
  <Badge>
    <Icon icon="vaadin:clock" slot="icon" />
    Pending
  </Badge>
  <Badge theme="success">
    <Icon icon="vaadin:check" slot="icon" />
    Confirmed
  </Badge>
  <Badge theme="warning">
    <Icon icon="vaadin:warning" slot="icon" />
    Warning
  </Badge>
  <Badge theme="error">
    <Icon icon="vaadin:exclamation-circle-o" slot="icon" />
    Denied
  </Badge>
</HorizontalLayout>
```

> **Note: Usi le icone con parsimonia**
>
> Il vantaggio dell’uso delle icone dovrebbe essere valutato rispetto al rumore visivo che aggiungono.

### <a id="solo-icona"></a>Solo icona

I badge possono anche essere usati con icone senza un’etichetta. Per l’accessibilità, il testo dovrebbe comunque essere fornito quando si usa la variante di stile `icon-only`. È nascosto solo visivamente, ma disponibile per gli screen reader, per garantire che tutti gli utenti possano identificare il significato del badge.

**Lit** — `badge-icons-only.ts`

```html
<vaadin-badge theme="success icon-only">
  <vaadin-icon icon="vaadin:check" slot="icon"></vaadin-icon>
  Confirmed
</vaadin-badge>
<vaadin-badge theme="error icon-only">
  <vaadin-icon icon="vaadin:close-small" slot="icon"></vaadin-icon>
  Cancelled
</vaadin-badge>
```

**Flow** — `BadgeIconsOnly.java`

```java
Badge confirmed = new Badge("Confirmed", VaadinIcon.CHECK.create());
confirmed.addThemeVariants(BadgeVariant.SUCCESS,
        BadgeVariant.ICON_ONLY);

Badge cancelled = new Badge("Cancelled",
        VaadinIcon.CLOSE_SMALL.create());
cancelled.addThemeVariants(BadgeVariant.ERROR, BadgeVariant.ICON_ONLY);
```

**React** — `badge-icons-only.tsx`

```tsx
<Badge theme="success icon-only">
  <Icon icon="vaadin:check" slot="icon" />
  Confirmed
</Badge>
<Badge theme="error icon-only">
  <Icon icon="vaadin:close-small" slot="icon" />
  Cancelled
</Badge>
```

I badge solo icona dovrebbero essere usati principalmente per contenuti comuni e ricorrenti con icone altamente standardizzate e universalmente comprese (come un segno di spunta per "*yes*"), e per contenuti ripetuti, ad esempio in elenchi e tabelle.

**Lit** — `badge-icons-only-table.ts`

```typescript
const renderBoolean: GridColumnBodyLitRenderer<UserPermissions> = (item, _model, column) => {
  let icon: string;
  let title: string;
  let theme: string;

  if (item[column.id as keyof UserPermissions]) {
    icon = 'vaadin:check';
    title = 'Yes';
    theme = 'success';
  } else {
    icon = 'vaadin:close-small';
    title = 'No';
    theme = 'error';
  }

  return html`
    <vaadin-badge theme="${theme} icon-only">
      <vaadin-icon icon="${icon}" slot="icon"></vaadin-icon>
      ${title}
    </vaadin-badge>
  `;
};

return html`
  <vaadin-grid .items="${this.items}">
    <vaadin-grid-column path="name" header="Name"></vaadin-grid-column>
    <vaadin-grid-column
      id="view"
      header="View"
      ${columnBodyRenderer(renderBoolean, [])}
    ></vaadin-grid-column>
    <vaadin-grid-column
      id="comment"
      header="Comment"
      ${columnBodyRenderer(renderBoolean, [])}
    ></vaadin-grid-column>
    <vaadin-grid-column
      id="edit"
      header="Edit"
      ${columnBodyRenderer(renderBoolean, [])}
    ></vaadin-grid-column>
  </vaadin-grid>
`;
```

**Flow** — `BadgeIconsOnlyTable.java`

```java
grid.addColumn(UserPermissions::getName).setHeader("Name");
grid.addComponentColumn(userPermissions -> createPermissionBadge(
        userPermissions.getView())).setHeader("View");
grid.addComponentColumn(userPermissions -> createPermissionBadge(
        userPermissions.getComment())).setHeader("Comment");
grid.addComponentColumn(userPermissions -> createPermissionBadge(
        userPermissions.getEdit())).setHeader("Edit");

...

private Badge createPermissionBadge(boolean hasPermission) {
    Badge badge;
    if (hasPermission) {
        badge = new Badge("Yes", VaadinIcon.CHECK.create());
        badge.addThemeVariants(BadgeVariant.SUCCESS,
                BadgeVariant.ICON_ONLY);
    } else {
        badge = new Badge("No", VaadinIcon.CLOSE_SMALL.create());
        badge.addThemeVariants(BadgeVariant.ERROR, BadgeVariant.ICON_ONLY);
    }
    return badge;
}
```

**React** — `badge-icons-only-table.tsx`

```tsx
const renderBadge = ({
  item,
  original: column,
}: {
  item: any;
  original: GridColumnElement<UserPermissions>;
}) => {
  let icon;
  let title;
  let theme;

  if (item[column.id]) {
    icon = 'vaadin:check';
    title = 'Yes';
    theme = 'success';
  } else {
    icon = 'vaadin:close-small';
    title = 'No';
    theme = 'error';
  }

  return (
    <Badge theme={`${theme} icon-only`}>
      <Icon icon={icon} slot="icon" />
      {title}
    </Badge>
  );
};

return (
  <Grid items={items.value}>
    <GridColumn path="name" header="Name" />
    <GridColumn id="view" header="View" renderer={renderBadge} />
    <GridColumn id="comment" header="Comment" renderer={renderBadge} />
    <GridColumn id="edit" header="Edit" renderer={renderBadge} />
  </Grid>
);
```

## <a id="numero"></a>Numero

I badge possono visualizzare un valore numerico usando la proprietà `number`. Questo è utile per mostrare conteggi, come messaggi non letti o notifiche. Quando sono impostati sia il contenuto testuale sia un numero, vengono visualizzati entrambi.

**Lit** — `badge-number.ts`

```html
<vaadin-badge number="12">New messages</vaadin-badge>
<vaadin-badge number="3" theme="success">Completed</vaadin-badge>
<vaadin-badge number="1" theme="error">Failed</vaadin-badge>
```

**Flow** — `BadgeNumber.java`

```java
Badge inbox = new Badge("New messages", 12);

Badge completed = new Badge("Completed", 3);
completed.addThemeVariants(BadgeVariant.SUCCESS);

Badge failed = new Badge("Failed", 1);
failed.addThemeVariants(BadgeVariant.ERROR);
```

**React** — `badge-number.tsx`

```tsx
<Badge number={12}>New messages</Badge>
<Badge number={3} theme="success">
  Completed
</Badge>
<Badge number={1} theme="error">
  Failed
</Badge>
```

### <a id="solo-numero"></a>Solo numero

I badge possono visualizzare un numero senza un’etichetta visibile usando la variante di stile `number-only`. Per l’accessibilità, l’etichetta di testo dovrebbe comunque essere fornita — è nascosta solo visivamente, ma disponibile per gli screen reader, per garantire che tutti gli utenti possano identificare il significato del badge.

**Lit** — `badge-number-only.ts`

```html
<vaadin-badge number="12" theme="number-only">New messages</vaadin-badge>
<vaadin-badge number="3" theme="error number-only">Alerts</vaadin-badge>
```

**Flow** — `BadgeNumberOnly.java`

```java
Badge inbox = new Badge("New messages", 12);
inbox.addThemeVariants(BadgeVariant.NUMBER_ONLY);

Badge alerts = new Badge("Alerts", 3);
alerts.addThemeVariants(BadgeVariant.ERROR, BadgeVariant.NUMBER_ONLY);
```

**React** — `badge-number-only.tsx`

```tsx
<Badge number={12} theme="number-only">
  New messages
</Badge>
<Badge number={3} theme="error number-only">
  Alerts
</Badge>
```

## <a id="casi-duso"></a>Casi d’uso

### <a id="evidenziare-e-distinguere-le-informazioni"></a>Evidenziare e distinguere le informazioni

Un caso d’uso tipico per i badge è evidenziare lo stato di un elemento, ad esempio in un Grid.

**Lit** — `badge-highlight.ts`

```typescript
return html`
  <vaadin-grid .items="${this.items}">
    <vaadin-grid-column path="report" header="Report"></vaadin-grid-column>
    <vaadin-grid-column
      header="Due date"
      ${columnBodyRenderer<Report>(
        ({ due }) => html`${dateFormatter.format(new Date(due))}`,
        []
      )}
    ></vaadin-grid-column>
    <vaadin-grid-column path="assignee" header="Assignee"></vaadin-grid-column>
    <vaadin-grid-column
      header="Status"
      ${columnBodyRenderer<Report>(({ status }) => {
        let title: string;
        let theme: string;

        switch (status) {
          case ReportStatus.COMPLETED:
            title = 'Completed';
            theme = 'success';
            break;
          case ReportStatus.IN_PROGRESS:
            title = 'In progress';
            theme = '';
            break;
          case ReportStatus.CANCELLED:
            title = 'Cancelled';
            theme = 'error';
            break;
          case ReportStatus.ON_HOLD:
            title = 'On hold';
            theme = 'warning';
            break;
        }

        return html`<vaadin-badge theme="${theme} filled">${title}</vaadin-badge>`;
      }, [])}
    ></vaadin-grid-column>
  </vaadin-grid>
`;
```

**Flow** — `BadgeHighlight.java`

```java
grid.addComponentColumn(report -> createStatusBadge(report.getStatus()))
        .setHeader("Status");

...

private Badge createStatusBadge(String status) {
    Badge badge = new Badge(status);
    badge.addThemeVariants(BadgeVariant.FILLED);
    switch (status) {
    case "Completed":
        badge.addThemeVariants(BadgeVariant.SUCCESS);
        break;
    case "Cancelled":
        badge.addThemeVariants(BadgeVariant.ERROR);
        break;
    case "On hold":
        badge.addThemeVariants(BadgeVariant.WARNING);
        break;
    default:
        break;
    }
    return badge;
}
```

**React** — `badge-highlight.tsx`

```tsx
function renderDueDate({ item: report }: { item: Report }) {
  return <span>{dateFormatter.format(new Date(report.due))}</span>;
}

function renderStatus({ item: report }: { item: Report }) {
  let title: string;
  let theme: string;

  switch (report.status) {
    case ReportStatus.COMPLETED:
      title = 'Completed';
      theme = 'success';
      break;
    case ReportStatus.IN_PROGRESS:
      title = 'In progress';
      theme = '';
      break;
    case ReportStatus.CANCELLED:
      title = 'Cancelled';
      theme = 'error';
      break;
    case ReportStatus.ON_HOLD:
      title = 'On hold';
      theme = 'warning';
      break;
  }

  return <Badge theme={`${theme} filled`}>{title}</Badge>;
}

function Example() {
  const items = useSignal<Report[]>([]);
  useEffect(() => {
    getReports().then((reports) => {
      items.value = reports as Report[];
    });
  }, []);

  return (
    <Grid items={items.value}>
      <GridColumn path="report" header="Report" />
      <GridColumn header="Due date" renderer={renderDueDate} />
      <GridColumn path="assignee" header="Assignee" />
      <GridColumn header="Status" renderer={renderStatus} />
    </Grid>
  );
}
```

Sono anche spesso usati per visualizzare tag di metadati.

### <a id="contenuto-interattivo"></a>Contenuto interattivo

I badge possono contenere contenuti interattivi come Anchor e Button. Ad esempio, i badge che evidenziano filtri attivi potrebbero contenere un Button "Clear" che rimuove il filtro associato.

**Lit** — `badge-interactive.ts`

```typescript
return html`
  <vaadin-vertical-layout theme="spacing">
    <vaadin-combo-box
      label="Profession"
      .items="${this.items}"
      @change="${this.onChange}"
    ></vaadin-combo-box>
    <vaadin-horizontal-layout theme="spacing wrap">
      ${repeat(
        this.selectedProfessions,
        (profession) => profession,
        (profession) => html`
          <vaadin-badge style="padding-right: 0">
            <span>${profession}</span>
            <vaadin-button
              aria-label="Clear filter: ${profession}"
              data-profession="${profession}"
              theme="tertiary"
              title="Clear filter: ${profession}"
              style="height: 1.5rem; min-width: 1.5rem; margin: 0; padding: 0"
              @click="${this.onClick}"
            >
              <vaadin-icon icon="vaadin:close-small"></vaadin-icon>
            </vaadin-button>
          </vaadin-badge>
        `
      )}
    </vaadin-horizontal-layout>
  </vaadin-vertical-layout>
`;
```

**Flow** — `BadgeInteractive.java`

```java
HorizontalLayout badges = new HorizontalLayout();
badges.addThemeNames("wrap");

ComboBox<String> comboBox = new ComboBox<>("Profession");
comboBox.setItems(DataService.getProfessions());
comboBox.addValueChangeListener(e -> {
    Badge filterBadge = createFilterBadge(e.getValue());
    badges.add(filterBadge);
});

...

private Badge createFilterBadge(String profession) {
    Button clearButton = new Button(VaadinIcon.CLOSE_SMALL.create());
    clearButton.addThemeVariants(ButtonVariant.TERTIARY);
    clearButton.getStyle().set("height", "1.5rem")
            .set("min-width", "1.5rem").set("margin", "0")
            .set("padding", "0");
    // Accessible button name
    clearButton.getElement().setAttribute("aria-label",
            "Clear filter: " + profession);
    // Tooltip
    clearButton.getElement().setAttribute("title",
            "Clear filter: " + profession);

    Badge badge = new Badge(profession);
    badge.getStyle().set("padding-right", "0");
    badge.getElement().appendChild(clearButton.getElement());

    // Add handler for removing the badge
    clearButton.addClickListener(event -> {
        badge.getElement().removeFromParent();
    });

    return badge;
}
```

**React** — `badge-interactive.tsx`

```tsx
return (
  <VerticalLayout theme="spacing">
    <ComboBox label="Profession" items={items.value} onChange={onChange} />

    <HorizontalLayout theme="spacing wrap">
      {selectedProfessions.value.map((profession) => (
        <Badge key={profession} style={{ paddingRight: '0' }}>
          <span>{profession}</span>
          <Button
            aria-label={`Clear filter: ${profession}`}
            data-profession={profession}
            theme="tertiary"
            title={`Clear filter: ${profession}`}
            style={{ height: '1.5rem', minWidth: '1.5rem', margin: '0', padding: '0' }}
            onClick={onClick}
          >
            <Icon icon="vaadin:close-small" />
          </Button>
        </Badge>
      ))}
    </HorizontalLayout>
  </VerticalLayout>
);
```

### <a id="contatore"></a>Contatore

I badge possono essere usati come contatori, ad esempio per mostrare il numero di messaggi non letti/nuovi, il conteggio delle selezioni, ecc.

**Lit** — `badge-counter.ts`

```html
<vaadin-tabs>
  <vaadin-tab>
    <span>Inbox</span>
    <vaadin-badge number="12" theme="filled number-only">unread messages</vaadin-badge>
  </vaadin-tab>
  <vaadin-tab>
    <span>Important</span>
    <vaadin-badge number="3" theme="filled number-only">unread messages</vaadin-badge>
  </vaadin-tab>
  <vaadin-tab>
    <span>Spam</span>
    <vaadin-badge number="45" theme="filled number-only">unread messages</vaadin-badge>
  </vaadin-tab>
  <vaadin-tab>
    <span>Archive</span>
    <vaadin-badge number="23" theme="filled number-only">unread messages</vaadin-badge>
  </vaadin-tab>
</vaadin-tabs>
```

**Flow** — `BadgeCounter.java`

```java
Tabs tabs = new Tabs(createTab("Inbox", 12), createTab("Important", 3),
        createTab("Spam", 45), createTab("Archive", 23));

...

private static Tab createTab(String labelText, int messageCount) {
    Span label = new Span(labelText);
    Badge counter = new Badge("unread messages", messageCount);
    counter.addThemeVariants(BadgeVariant.FILLED, BadgeVariant.NUMBER_ONLY);
    counter.getStyle().set("margin-inline-start", "var(--vaadin-gap-s)");

    return new Tab(label, counter);
}
```

**React** — `badge-counter.tsx`

```tsx
<Tabs>
  <Tab>
    <span>Inbox</span>
    <Badge number={12} theme="filled number-only" style={badgeStyle}>
      unread messages
    </Badge>
  </Tab>
  <Tab>
    <span>Important</span>
    <Badge number={3} theme="filled number-only" style={badgeStyle}>
      unread messages
    </Badge>
  </Tab>
  <Tab>
    <span>Spam</span>
    <Badge number={45} theme="filled number-only" style={badgeStyle}>
      unread messages
    </Badge>
  </Tab>
  <Tab>
    <span>Archive</span>
    <Badge number={23} theme="filled number-only" style={badgeStyle}>
      unread messages
    </Badge>
  </Tab>
</Tabs>
```

Le tecnologie assistive, come gli screen reader, interpretano i badge esclusivamente in base al loro contenuto. Senza un contesto adeguato, possono finire per confondere l’utente. Per fornire contesto alle persone che usano gli screen reader, imposti l’attributo `aria-label` del badge.

## <a id="best-practice"></a>Best practice

### <a id="badge-vs-button"></a>Badge vs Button

Badge e Button sono simili nell’aspetto. Questo potrebbe indurre gli utenti a pensare che i badge siano interattivi.

Posizionamento, linguaggio e colore possono contribuire a mitigare qualsiasi confusione. Per prima cosa, i badge non dovrebbero essere etichettati con verbi d’azione. Non sono azioni, ma piuttosto testo/contenuto statico. In secondo luogo, eviti di posizionare i badge direttamente accanto ai Button, in particolare se stanno usando temi simili.

`69AF8882-CA67-4AB8-BF9F-69A1EA6CE84C`
