Iced · Iced components

This page shows the Iced implementation. Iced components for Rust desktop applications. Each example is live, the source is copy-paste, and the controls are built from the same design tokens as the rest of uni-kit.

Badge

A versatile badge component that can display counts, dots, or icons on elements. Perfect for indicating notifications, status, or other contextual information on avatars, icons, or other UI elements.

Install

#Import

use gpui_kit::component::badge::Badge;
// Installed in ui/components. That directory is on the Slint include path, so import by file name:
import { Badge } from "badge.slint";
use iced::widget::{column, container, row, scrollable, svg, text};
use iced::{Alignment, Color, Element, Length, Task, Theme};
import { signal, View } from "nsh";
import { Image } from "nsh/image";
import { LayoutAnimation } from "nsh/animated";
import { reduceMotion } from "nsh/accessibility";
import { Avatar } from "./avatar";
import { Badge } from "./badge";
import { Button } from "./button";
import { Icon } from "./icon";
import { tokens } from "./theme";

#Usage

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const usage = (
  <Row gap={8}>
    <Badge label="New" variant="success" />
    <Badge count={3} variant="info" />
    <Badge label="Draft" variant="secondary" />
    <Badge dot label="Online" variant="success" />
  </Row>
);

#Warning light badge

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const warning = (
  <Row gap={8}>
    <Badge label="Needs review" variant="warning" />
    <Badge label="Expiring soon" icon="clock" variant="warning" />
    <Badge label="3 warnings" variant="warning" appearance="solid" />
  </Row>
);

#Badge size variations

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const sizes = (
  <Row gap={8} style={{ alignItems: "center" }}>
    <Badge label="Small" size="small" variant="info" />
    <Badge label="Default" size="medium" variant="info" />
    <Badge label="Large" size="large" variant="info" />
    <Badge label="Small solid" size="small" variant="success" appearance="solid" />
    <Badge label="Large solid" size="large" variant="success" appearance="solid" />
  </Row>
);

#Full-radius badges

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const fullRadius = (
  <Row gap={8}>
    <Badge count={8} variant="info" radius="full" />
    <Badge count={128} max={99} variant="success" radius="full" appearance="solid" />
    <Badge label="Inbox" variant="secondary" radius="full" />
    <Badge label="Open" variant="default" radius="full" />
    <Badge label="Outline" variant="outline" radius="full" />
  </Row>
);

#Badge with icon

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const badgeIcon = (
  <Row gap={8}>
    <Badge icon="check" label="Verified" variant="success" />
    <Badge icon="sparkles" label="Pro" variant="info" appearance="solid" />
    <Badge icon="star" label="Featured" variant="warning" />
    <Badge icon="circle-check" label="Complete" variant="outline" />
  </Row>
);

#Badge with dismiss

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const dismiss = (
  <Row gap={8}>
    {tags().map((tag) => (
      <Badge
        label={tag}
        variant="secondary"
        closable
        onRemove={() => removeTag(tag)}
      />
    ))}
    {tags().length === 0 ? (
      <Button label="Restore badges" size="small" onPress={restoreTags} />
    ) : null}
  </Row>
);

#Badge with status dot

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const status = (
  <Row gap={8}>
    <Badge dot label="Online" variant="success" />
    <Badge dot label="Away" variant="warning" />
    <Badge dot label="Busy" variant="danger" />
    <Badge dot label="Offline" variant="secondary" />
    <Badge dot label="Guest" variant="info" />
  </Row>
);
GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const link = (
  <Row gap={8}>
    <Badge
      label="View pull request"
      icon="external-link"
      variant="outline"
      onPress={() => opened.set("Opened pull request #42")}
      accessibilityLabel="Open pull request 42"
    />
    <Badge
      label="Read the docs"
      variant="info"
      onPress={() => opened.set("Opened the documentation")}
    />
    <Badge
      label="Repository"
      icon="github"
      variant="secondary"
      onPress={() => opened.set("Opened the repository")}
      accessibilityLabel="Open the repository"
    />
    <Status>{opened()}</Status>
  </Row>
);

#Badge with avatar

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const people = (
  <Row gap={8}>
    {person() ? (
      <Badge
        leading={<Avatar name="Ada Lovelace" size="xs" />}
        label="Ada Lovelace"
        variant="secondary"
        closable
        onRemove={dismissPerson}
      />
    ) : (
      <Badge label="Ada Lovelace dismissed" variant="outline" />
    )}
    <Badge
      leading={<Avatar name="Grace Hopper" size="xs" />}
      label="Grace Hopper"
      variant="secondary"
    />
    <Badge
      leading={<Avatar name="Alan Turing" size="xs" />}
      label="Alan Turing"
      variant="outline"
    />
  </Row>
);

#Badge with flag/image

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
const image = (
  <Row gap={8}>
    <Badge
      leading={
        <Image
          src={cover}
          resizeMode="cover"
          style={{ width: 16, height: 16, borderRadius: 3 }}
        />
      }
      label="Mountain"
      variant="secondary"
    />
    <Badge
      leading={
        <View
          style={{
            flexDirection: "row",
            width: 18,
            height: 12,
            borderWidth: 1,
            borderColor: tokens.border,
            borderRadius: 2,
            overflow: "hidden",
          }}
        >
          <View style={{ flex: 1, backgroundColor: tokens.info }} />
          <View style={{ flex: 1, backgroundColor: tokens.background }} />
          <View style={{ flex: 1, backgroundColor: tokens.danger }} />
        </View>
      }
      label="Flag"
      variant="outline"
    />
    <Badge
      leading={<Icon name="globe" size={14} color={tokens.mutedForeground} />}
      label="Image slot"
      variant="secondary"
    />
  </Row>
);

#Icon

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
// A count on an icon: 3 reads as it is, 103 rolls over the default maximum
// of 99 and reads `99+`.
let icon = body_box(
    row![
        Badge::new(icon_mark(BELL, 24.0)).count(3),
        Badge::new(icon_mark(INBOX, 24.0)).count(103),
    ]
    .spacing(24.0)
    .align_y(Alignment::Center),
);
NSHNo NSH version of this example yet.
  • Default: Displays a numeric count.
  • Dot: A small dot indicator, typically used for status.
  • Icon: Displays an icon instead of a number.
// Number badge (default)
Badge::new()
    .count(5)
    .child(Avatar::new().src("https://example.com/avatar.jpg"))

// Dot badge
Badge::new()
    .dot()
    .child(Icon::new(IconName::Inbox))

// Icon badge
Badge::new()
    .icon(IconName::Check)
    .child(Avatar::new().src("https://example.com/avatar.jpg"))
GridLayout {
    spacing: 24px;
    Badge {
        horizontal-stretch: 0;
        count: 3;
        Icon {
            name: "bell";
            size: 24px;
            color: Theme.foreground;
        }
    }
    Badge {
        horizontal-stretch: 0;
        count: 103;
        Icon {
            name: "inbox";
            size: 24px;
            color: Theme.foreground;
        }
    }
}
NSHNo NSH version of this example yet.

#Count

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
// The same count on an avatar, which is the shape the component page
// documents this sample with.
let count = body_box(
    row![
        Badge::new(avatar_mark("AL")).count(3),
        Badge::new(avatar_mark("MK")).count(103),
    ]
    .spacing(24.0)
    .align_y(Alignment::Center),
);
NSHNo NSH version of this example yet.

Use count to display a numeric badge, if the count is greater than zero (> 0) the badge will be shown, otherwise it will be hidden.

There is a default maximum count of 99, any count above this will be displayed as 99+. You can customize this maximum using the max method.

Badge::new()
    .count(3)
    .child(Icon::new(IconName::Bell))
GridLayout {
    spacing: 24px;
    Badge {
        horizontal-stretch: 0;
        count: 3;
        Avatar {
            initials: "AL";
        }
    }
    Badge {
        horizontal-stretch: 0;
        count: 103;
        Avatar {
            initials: "MK";
            fill: Theme.muted;
        }
    }
}
NSHNo NSH version of this example yet.

#Badge icon

GPUINo GPUI version of this example yet.
SlintNo Slint version of this example yet.
// A status icon in a round chip instead of a number, in the semantic colours
// the Slint example names: info for the check, warning for the star.
let badge_icon = body_box(
    row![
        Badge::new(avatar_mark("AL"))
            .icon(chip_mark(CHECK, app.tokens.primary_foreground))
            .color(app.tokens.info),
        Badge::new(avatar_mark("JR"))
            .icon(chip_mark(STAR, app.tokens.primary_foreground))
            .color(app.tokens.warning),
    ]
    .spacing(24.0)
    .align_y(Alignment::Center),
);
NSHNo NSH version of this example yet.

The Badge is also implemented with the Sizable trait, allowing you to set small, medium (default), or large sizes.

// Small badge
Badge::new()
    .small()
    .count(1)
    .child(Avatar::new().small())

// Medium badge (default)
Badge::new()
    .count(5)
    .child(Avatar::new())

// Large badge
Badge::new()
    .large()
    .count(10)
    .child(Avatar::new().large())
GridLayout {
    spacing: 24px;
    Badge {
        horizontal-stretch: 0;
        icon: "check";
        color: Theme.info;
        Avatar {
            initials: "AL";
        }
    }
    Badge {
        horizontal-stretch: 0;
        icon: "star";
        color: Theme.warning;
        Avatar {
            initials: "JR";
        }
    }
}
NSHNo NSH version of this example yet.

#Dot

use gpui_kit::component::ActiveTheme;

// Custom colors
Badge::new()
    .count(3)
    .color(cx.theme().blue)
    .child(Avatar::new())

Badge::new()
    .icon(IconName::Star)
    .color(cx.theme().yellow)
    .child(Avatar::new())

Badge::new()
    .dot()
    .color(cx.theme().green)
    .child(Icon::new(IconName::Bell))
HorizontalLayout {
    spacing: 24px;
    alignment: start;
    Badge {
        dot: true;
        count: 1;
        Avatar {
            initials: "AL";
        }
    }
}
// A six-pixel dot in place of the count, for a status rather than a number.
let dot = body_box(
    row![Badge::new(avatar_mark("AL")).dot().count(1)]
        .spacing(24.0)
        .align_y(Alignment::Center),
);
NSHNo NSH version of this example yet.

#Color

use gpui_kit::component::{Icon, IconName};

// Badge with count on icon
Badge::new()
    .count(3)
    .child(Icon::new(IconName::Bell).large())

// Badge with high count (shows max)
Badge::new()
    .count(103)
    .child(Icon::new(IconName::Inbox).large())

// Custom max count
Badge::new()
    .count(150)
    .max(999)
    .child(Icon::new(IconName::Mail))
GridLayout {
    spacing: 24px;
    Badge {
        horizontal-stretch: 0;
        count: 3;
        color: Theme.selection;
        Avatar {
            initials: "AL";
        }
    }
    Badge {
        horizontal-stretch: 0;
        dot: true;
        count: 1;
        color: Theme.success;
        Avatar {
            initials: "AL";
        }
    }
}
// A caller-supplied fill: `selection` for the count, `success` for the dot.
let color = body_box(
    row![
        Badge::new(avatar_mark("AL"))
            .count(3)
            .color(app.tokens.selection),
        Badge::new(avatar_mark("AL"))
            .dot()
            .count(1)
            .color(app.tokens.success),
    ]
    .spacing(24.0)
    .align_y(Alignment::Center),
);
NSHNo NSH version of this example yet.

#Sizes

use gpui_kit::component::avatar::Avatar;

// Basic count badge
Badge::new()
    .count(5)
    .child(Avatar::new().src("https://example.com/avatar.jpg"))

// Status badge with icon
Badge::new()
    .icon(IconName::Check)
    .color(cx.theme().green)
    .child(Avatar::new().src("https://example.com/avatar.jpg"))

// Online indicator with dot
Badge::new()
    .dot()
    .color(cx.theme().green)
    .child(Avatar::new().src("https://example.com/avatar.jpg"))
GridLayout {
    spacing: 24px;
    Badge {
        horizontal-stretch: 0;
        count: 2;
        size: ControlSize.small;
        Icon {
            name: "bell";
            size: 16px;
            color: Theme.foreground;
        }
    }
    Badge {
        horizontal-stretch: 0;
        count: 2;
        Icon {
            name: "bell";
            size: 24px;
            color: Theme.foreground;
        }
    }
    Badge {
        horizontal-stretch: 0;
        count: 2;
        size: ControlSize.large;
        Icon {
            name: "bell";
            size: 32px;
            color: Theme.foreground;
        }
    }
}
// The size ladder, with the icons the Slint example pairs it with: small at
// 16 pixels, the default at 24, large at 32.
let sizes = body_box(
    row![
        Badge::new(icon_mark(BELL, 16.0))
            .count(2)
            .size(ControlSize::Small),
        Badge::new(icon_mark(BELL, 24.0))
            .count(2)
            .size(ControlSize::Medium),
        Badge::new(icon_mark(BELL, 32.0))
            .count(2)
            .size(ControlSize::Large),
    ]
    .spacing(24.0)
    .align_y(Alignment::Center),
);
NSHNo NSH version of this example yet.

#Complex Nested Badges

// Badge on badge for complex status
Badge::new()
    .count(212)
    .large()
    .child(
        Badge::new()
            .icon(IconName::Check)
            .large()
            .color(cx.theme().cyan)
            .child(Avatar::new().large().src("https://example.com/avatar.jpg"))
    )

// Multiple status indicators
Badge::new()
    .count(2)
    .color(cx.theme().green)
    .large()
    .child(
        Badge::new()
            .icon(IconName::Star)
            .large()
            .color(cx.theme().yellow)
            .child(Avatar::new().large().src("https://example.com/avatar.jpg"))
    )
SlintNo Slint version of this example yet.
NSHNo NSH version of this example yet.

#Notification Indicators

// Unread messages
Badge::new()
    .count(12)
    .child(Icon::new(IconName::Mail).large())

// New notifications
Badge::new()
    .count(3)
    .color(cx.theme().red)
    .child(Icon::new(IconName::Bell).large())

// High priority with custom max
Badge::new()
    .count(1234)
    .max(999)
    .color(cx.theme().orange)
    .child(Icon::new(IconName::AlertTriangle))
SlintNo Slint version of this example yet.
const notifications = (
  <Row gap={24}>
    <Badge count={3}><Icon name="bell" size={28} color={tokens.foreground} /></Badge>
    <Badge count={8}><Icon name="message-circle" size={28} color={tokens.foreground} /></Badge>
    <Badge icon="info" color={tokens.info}><Icon name="inbox" size={28} color={tokens.foreground} /></Badge>
    <Badge dot color={tokens.success}>
      <Avatar name="Ada Lovelace" size="lg" />
    </Badge>
  </Row>
);

#Status Indicators

// Online status
Badge::new()
    .dot()
    .color(cx.theme().green)
    .child(Avatar::new().src("https://example.com/user.jpg"))

// Verified status
Badge::new()
    .icon(IconName::CheckCircle)
    .color(cx.theme().blue)
    .child(Avatar::new().src("https://example.com/verified-user.jpg"))

// Warning status
Badge::new()
    .icon(IconName::AlertTriangle)
    .color(cx.theme().yellow)
    .child(Avatar::new().src("https://example.com/user.jpg"))
SlintNo Slint version of this example yet.
NSHNo NSH version of this example yet.

#Different Badge Positions

// The badge automatically positions itself based on variant:
// - Dot: top-right corner (small dot)
// - Number: top-right with dynamic sizing
// - Icon: bottom-right corner with border
SlintNo Slint version of this example yet.
NSHNo NSH version of this example yet.

#Count Formatting

// Numbers 1-99 show as-is
Badge::new().count(5)    // Shows "5"
Badge::new().count(99)   // Shows "99"

// Numbers above max show with "+"
Badge::new().count(100)  // Shows "99+" (default max)
Badge::new().count(1000).max(999) // Shows "999+"

// Zero count hides the badge
Badge::new().count(0)    // Badge not visible
SlintNo Slint version of this example yet.
const formatting = (
  <Row gap={20}>
    <Badge count={0}>
      <Avatar name="Ada Lovelace" size="lg" />
    </Badge>
    <Badge count={9} max={9}>
      <Avatar name="Ada Lovelace" size="lg" />
    </Badge>
    <Badge count={99} max={99}>
      <Avatar name="Ada Lovelace" size="lg" />
    </Badge>
    <Badge count={1000} max={99}>
      <Avatar name="Ada Lovelace" size="lg" />
    </Badge>
  </Row>
);

#API Reference

#Badge

An overlay on an icon or avatar. Mirrors GPUI Component's `Badge`: a count, a dot, or a small status icon anchored to the corner of its child.

NameKindTypeDefault
countinint
maxinint99
iconinstring
colorincolorTheme.danger
dotinbool
sizeinControlSizeControlSize.medium