Interface HasAriaLabel

All Superinterfaces:
HasElement, Serializable
All Known Implementing Classes:
AbstractNumberField, Anchor, BigDecimalField, Button, Checkbox, CheckboxGroup, ComboBox, ComboBoxBase, DatePicker, DrawerToggle, EmailField, GridMenuItem, Image, Input, IntegerField, ListBox, ListBoxBase, Main, MenuItem, MenuItemBase, MultiSelectComboBox, MultiSelectListBox, NativeButton, Nav, NumberField, PasswordField, RadioButtonGroup, RangeInput, Select, Tab, TextArea, TextField, TextFieldBase, TimePicker

public interface HasAriaLabel extends HasElement
A generic interface for components and other user interface objects that may have an aria-label and an aria-labelledby DOM attributes to set the accessible name of the component.

The default implementation set the aria-label and aria-labelledby of the component to the given HasElement.getElement(). Override all methods in this interface if the aria-label and aria-labelledby should be added to some other element.

The purpose of aria-label is to provide the user with a recognizable name of the component. If the label text is visible on screen, aria-labelledby should be used and aria-label should not be used. There may be instances where the name of an element cannot be determined programmatically from the content of the element, and there are cases where providing a visible label is not the desired user experience. In the cases where a visible label or visible tooltip is undesirable, aria-label may be used to set the accessible name of the component.

Don't include both. If both are present on the same element, aria-labelledby will take precedence over aria-label.

See: https://www.w3.org/TR/wai-aria/#aria-label

See: https://www.w3.org/TR/wai-aria/#aria-labelledby

Note: The aria-label and aria-labelledby attributes are not valid on every component, see https://www.w3.org/TR/using-aria/#label-support for more details.

Since:
Author:
Vaadin Ltd
  • Method Details

    • setAriaLabel

      default void setAriaLabel(String ariaLabel)
      Set the aria-label of the component to the given text.

      This method should not be used if setAriaLabelledBy(String) is also used. If both attributes are present, aria-labelledby will take precedence over aria-label.

      Parameters:
      ariaLabel - the aria-label text to set or null to clear
    • getAriaLabel

      default Optional<String> getAriaLabel()
      Gets the aria-label of the component.
      Returns:
      an optional aria-label of the component if no aria-label has been set
    • setAriaLabelledBy

      default void setAriaLabelledBy(String ariaLabelledBy)
      Set the aria-labelledby of the component. The value must be a valid id attribute of another element that labels the component. The label element must be in the same DOM scope of the component, otherwise screen readers may fail to announce the label content properly.

      This method should not be used if setAriaLabel(String) is also used. If both attributes are present, aria-labelledby will take precedence over aria-label.

      Parameters:
      ariaLabelledBy - the string with the id of the element that will be used as label or null to clear
    • getAriaLabelledBy

      default Optional<String> getAriaLabelledBy()
      Gets the aria-labelledby of the component
      Returns:
      an optional aria-labelledby of the component if no aria-labelledby has been set