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:
AttachNotifier,DetachNotifier,HasElement,HasSize,HasStyle,HasText,HasTheme,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 Component
implements HasSize, HasText, 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
HasText.WhiteSpace -
Constructor Summary
ConstructorsConstructorDescriptionBadge()Default constructor.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(Signal<Integer> numberSignal) Binds the number property to the given signal.voidBinds aSignal's value to the text content of this component and keeps the text content synchronized with the signal value while the element is in attached state.Gets the component in the default slot of this badge.getIcon()Gets the component that is defined as the icon of this badge.Gets the number displayed in the badge.getText()Gets the text content of this component.protected voidonAttach(AttachEvent attachEvent) Called when the component is attached to a UI.voidsetContent(Component content) Sets the given component as the content of this badge.voidSets the given component as the icon of this badge.voidSets the number to display in 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
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
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
Description copied from class:ComponentCalled when the component is attached to a UI.This method is invoked before the
AttachEventis fired for the component. Make sure to callsuper.onAttachwhen overriding this method. -
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.
-
getText
Gets the text content of this component. -
bindText
Description copied from interface:HasTextBinds aSignal's value to the text content of this component and keeps the text content synchronized with the signal value while the element is in attached state. When the element is in detached state, signal value changes have no effect.nullsignal unbinds the existing binding.While a Signal is bound, any attempt to set the text content manually throws
BindingActiveException. Same happens when trying to bind a new Signal while one is already bound.Example of usage:
ValueSignal<String> signal = new ValueSignal<>(""); Span component = new Span(""); add(component); component.bindText(signal); signal.set("text"); // The component text content is set to "text" -
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
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
Gets the component in the default slot of this badge.- Returns:
- the content component, or
nullif not set
-
setIcon
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
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
-