Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make Popover implement HasAriaLabel, add overlayRole #6425

Merged
merged 2 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import elemental.json.Json;
import elemental.json.JsonArray;

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasAriaLabel;
import com.vaadin.flow.component.HasComponents;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.Text;
Expand All @@ -53,7 +55,7 @@
@NpmPackage(value = "@vaadin/popover", version = "24.5.0-alpha4")
@JsModule("@vaadin/polymer-legacy-adapter/style-modules.js")
@JsModule("@vaadin/popover/src/vaadin-popover.js")
public class Popover extends Component implements HasComponents {
public class Popover extends Component implements HasAriaLabel, HasComponents {

private Component target;
private Registration targetAttachRegistration;
Expand All @@ -69,6 +71,50 @@ public Popover() {
getElement().getNode().addAttachListener(this::attachComponentRenderer);

updateTrigger();
setOverlayRole("dialog");
}

@Override
public void setAriaLabel(String ariaLabel) {
getElement().setProperty("accessibleName", ariaLabel);
}

@Override
public Optional<String> getAriaLabel() {
return Optional.ofNullable(getElement().getProperty("accessibleName"));
}

@Override
public void setAriaLabelledBy(String labelledBy) {
getElement().setProperty("accessibleNameRef", labelledBy);
}

@Override
public Optional<String> getAriaLabelledBy() {
return Optional
.ofNullable(getElement().getProperty("accessibleNameRef"));
}

/**
* Sets the ARIA role for the overlay element, used by screen readers.
*
* @param role
* the role to set
*/
public void setOverlayRole(String role) {
Objects.requireNonNull(role, "Role cannot be null");

getElement().setProperty("overlayRole", role);
}

/**
* Gets the ARIA role for the overlay element, used by screen readers.
* Defaults to {@code dialog}.
*
* @return the role
*/
public String getOverlayRole() {
return getElement().getProperty("overlayRole");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,46 @@ public void setOpenOnHover_triggerPropertyUpdated() {
Assert.assertEquals("click", jsonArray.get(0).asString());
Assert.assertEquals("hover", jsonArray.get(1).asString());
}

@Test
public void setAriaLabel_getAriaLabel() {
popover.setAriaLabel("aria-label");
Assert.assertTrue(popover.getAriaLabel().isPresent());
Assert.assertEquals("aria-label", popover.getAriaLabel().get());

popover.setAriaLabel(null);
Assert.assertTrue(popover.getAriaLabel().isEmpty());
}

@Test
public void setAriaLabelledBy_getAriaLabelledBy() {
popover.setAriaLabelledBy("aria-labelledby");
Assert.assertTrue(popover.getAriaLabelledBy().isPresent());
Assert.assertEquals("aria-labelledby",
popover.getAriaLabelledBy().get());

popover.setAriaLabelledBy(null);
Assert.assertTrue(popover.getAriaLabelledBy().isEmpty());
}

@Test
public void getOverlayRole_defaultDialog() {
Assert.assertEquals("dialog", popover.getOverlayRole());
Assert.assertEquals("dialog",
popover.getElement().getProperty("overlayRole"));
}

@Test
public void setOverlayRole_getOverlayRole() {
popover.setOverlayRole("alertdialog");

Assert.assertEquals("alertdialog", popover.getOverlayRole());
Assert.assertEquals("alertdialog",
popover.getElement().getProperty("overlayRole"));
}

@Test(expected = NullPointerException.class)
public void setOverlayRole_null_throws() {
popover.setOverlayRole(null);
}
}