Package com.vaadin.flow.component.badge
Class Badge
java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.badge.Badge
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasSize,com.vaadin.flow.component.HasStyle,com.vaadin.flow.component.HasText,com.vaadin.flow.component.HasTheme,com.vaadin.flow.component.shared.HasThemeVariant<BadgeVariant>,Serializable
@Tag("vaadin-badge")
@NpmPackage(value="@vaadin/badge",
version="25.1.0-alpha9")
@JsModule("@vaadin/badge/src/vaadin-badge.js")
public class Badge
extends com.vaadin.flow.component.Component
implements com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasText, com.vaadin.flow.component.shared.HasThemeVariant<BadgeVariant>
Badge is a component for displaying small pieces of information, such as
statuses, counts, or labels. It can display plain text, a number, an icon, or
a custom component, as well as combinations of these. Use theme variants to
customize the badge's appearance.
Badge badge = new Badge("Completed", LumoIcon.CHECKMARK.create());
badge.addThemeVariants(BadgeVariant.SUCCESS);
The dot variant renders the badge as a small dot
indicator, visually hiding all content:
Badge badge = new Badge(); badge.addThemeVariants(BadgeVariant.DOT);
Accessibility
When a Badge displays only an icon or a number, it may not provide enough context for screen reader users. To address this, you can add descriptive text viasetText(String) and use the icon-only or number-only theme variant to
hide the text visually while keeping it available to screen readers:
Badge badge = new Badge("new messages", 5); // announced as "5 new messages"
badge.addThemeVariants(BadgeVariant.NUMBER_ONLY);
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasText
com.vaadin.flow.component.HasText.WhiteSpace -
Constructor Summary
ConstructorsConstructorDescriptionBadge()Default constructor.Badge(com.vaadin.flow.component.Component icon) Creates a badge with an icon inside.Creates a badge with a text signal bound to it.Creates a badge with a text signal and an icon inside.Creates a badge with a text inside.Creates a badge with a text and a number inside.Creates a badge with a text, a number, and an icon inside.Creates a badge with a text and an icon inside. -
Method Summary
Modifier and TypeMethodDescriptionvoidbindNumber(com.vaadin.flow.signals.Signal<Integer> numberSignal) Binds the number property to the given signal.voidcom.vaadin.flow.component.ComponentGets the component in the default slot of this badge.com.vaadin.flow.component.ComponentgetIcon()Gets the component that is defined as the icon of this badge.Gets the number displayed in the badge.getRole()Gets the ARIA role attribute of the badge.getText()Gets the text content of this component.protected voidonAttach(com.vaadin.flow.component.AttachEvent attachEvent) voidsetContent(com.vaadin.flow.component.Component content) Sets the given component as the content of this badge.voidsetIcon(com.vaadin.flow.component.Component icon) Sets the given component as the icon of this badge.voidSets the number to display in the badge.voidSets the ARIA role attribute on the badge.voidSets the given string as the text content of this component.Methods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasSize
bindHeight, bindWidth, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasText
getWhiteSpace, setWhiteSpaceMethods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, bindThemeName, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeNameMethods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant
addThemeVariants, bindThemeVariant, removeThemeVariants, setThemeVariant, setThemeVariants, setThemeVariants
-
Constructor Details
-
Badge
public Badge()Default constructor. Creates an empty badge. -
Badge
Creates a badge with a text inside.- Parameters:
text- the text inside the badge- See Also:
-
Badge
public Badge(com.vaadin.flow.component.Component icon) Creates a badge with an icon inside.- Parameters:
icon- the icon inside the badge- See Also:
-
Badge
Creates a badge with a text and an icon inside.- Parameters:
text- the text inside the badgeicon- the icon inside the badge- See Also:
-
Badge
Creates a badge with a text and a number inside.- Parameters:
text- the text inside the badgenumber- the number to display in the badge- See Also:
-
Badge
Creates a badge with a text, a number, and an icon inside.- Parameters:
text- the text inside the badgenumber- the number to display in the badgeicon- the icon inside the badge- See Also:
-
Badge
Creates a badge with a text signal bound to it.- Parameters:
textSignal- the signal providing the text content- See Also:
-
Badge
public Badge(com.vaadin.flow.signals.Signal<String> textSignal, com.vaadin.flow.component.Component icon) Creates a badge with a text signal and an icon inside.- Parameters:
textSignal- the signal providing the text contenticon- the icon inside the badge- See Also:
-
-
Method Details
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttachin classcom.vaadin.flow.component.Component
-
setText
Sets the given string as the text content of this component.This method removes any existing text-content and replaces it with the given text. Other slotted children (such as icons) are preserved.
- Specified by:
setTextin interfacecom.vaadin.flow.component.HasText- Parameters:
text- the text content to set, ornullto remove existing text
-
getText
Gets the text content of this component.- Specified by:
getTextin interfacecom.vaadin.flow.component.HasText- Returns:
- the text content, or an empty string if not set
-
bindText
- Specified by:
bindTextin interfacecom.vaadin.flow.component.HasText
-
setNumber
Sets the number to display in the badge.- Parameters:
number- the number to display, ornullto clear it
-
bindNumber
Binds the number property to the given signal.- Parameters:
numberSignal- the signal providing the number value
-
getNumber
Gets the number displayed in the badge.- Returns:
- the number, or
nullif not set
-
setContent
public void setContent(com.vaadin.flow.component.Component content) Sets the given component as the content of this badge.The content is placed in the default slot of the badge.
- Parameters:
content- the content component, ornullto remove it
-
getContent
public com.vaadin.flow.component.Component getContent()Gets the component in the default slot of this badge.- Returns:
- the content component, or
nullif not set
-
setIcon
public void setIcon(com.vaadin.flow.component.Component icon) Sets the given component as the icon of this badge.The icon is placed in the
iconslot of the badge.- Parameters:
icon- component to be used as an icon, ornullto remove it
-
getIcon
public com.vaadin.flow.component.Component getIcon()Gets the component that is defined as the icon of this badge.- Returns:
- the icon of this badge, or
nullif the icon is not set
-
setRole
Sets the ARIA role attribute on the badge.- Parameters:
role- the ARIA role, ornullto clear
-
getRole
Gets the ARIA role attribute of the badge.- Returns:
- the ARIA role, or
nullif not set
-