diff --git a/vaadin-notification-flow-parent/vaadin-notification-flow/src/main/java/com/vaadin/flow/component/notification/Notification.java b/vaadin-notification-flow-parent/vaadin-notification-flow/src/main/java/com/vaadin/flow/component/notification/Notification.java index 8d380b2bad3..92e1408c2c1 100644 --- a/vaadin-notification-flow-parent/vaadin-notification-flow/src/main/java/com/vaadin/flow/component/notification/Notification.java +++ b/vaadin-notification-flow-parent/vaadin-notification-flow/src/main/java/com/vaadin/flow/component/notification/Notification.java @@ -198,6 +198,31 @@ public Notification(String text, int duration, Position position) { setPosition(position); } + /** + * Creates a Notification with given text String, duration, position and + * assertive state. + *

+ * Set to {@code 0} or a negative number to disable the notification + * auto-closing. + * + * @param text + * the text of the notification + * @param duration + * the duration in milliseconds to show the notification + * @param position + * the position of the notification. Valid enumerate values are + * TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE, + * BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH + * @param assertive + * whether the notification should have {@code aria-live} + * attribute set to {@code assertive} or {@code polite} + */ + public Notification(String text, int duration, Position position, + boolean assertive) { + this(text, duration, position); + setAssertive(assertive); + } + /** * Creates a notification with given components inside. *

@@ -232,6 +257,31 @@ private void removeAutoAdded() { } } + /** + * Shows a notification in the current page with given text, duration, + * position and assertive state. + * + * @param text + * the text of the Notification + * @param duration + * the duration in milliseconds to show the notification + * @param position + * the position of the notification. Valid enumerate values are + * TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE, + * BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH + * @param assertive + * whether the notification should have {@code aria-live} + * attribute set to {@code assertive} or {@code polite} + * @return the notification + */ + public static Notification show(String text, int duration, + Position position, boolean assertive) { + Notification notification = new Notification(text, duration, position, + assertive); + notification.open(); + return notification; + } + /** * Shows a notification in the current page with given text, duration and * position. @@ -248,9 +298,7 @@ private void removeAutoAdded() { */ public static Notification show(String text, int duration, Position position) { - Notification notification = new Notification(text, duration, position); - notification.open(); - return notification; + return show(text, duration, position, false); } /** @@ -501,6 +549,29 @@ public int getDuration() { return getElement().getProperty("duration", 0); } + /** + * When true, the notification card has {@code aria-live} attribute set to + * {@code assertive} instead of {@code polite}. This makes screen readers + * announce the notification content immediately when it appears. + * + * @param assertive + * the value to set + */ + public void setAssertive(boolean assertive) { + getElement().setProperty("assertive", assertive); + } + + /** + * When true, the notification card has {@code aria-live} attribute set to + * {@code assertive} instead of {@code polite}. This makes screen readers + * announce the notification content immediately when it appears. + * + * @return the {@code assertive} property from the webcomponent + */ + public boolean isAssertive() { + return getElement().getProperty("assertive", false); + } + /** * {@inheritDoc} *

diff --git a/vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/NotificationTest.java b/vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/NotificationTest.java index ae1a9224d51..a9a73d590d9 100644 --- a/vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/NotificationTest.java +++ b/vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/NotificationTest.java @@ -367,4 +367,18 @@ public void setText_notificationHasUnmodifiedText() { Assert.assertEquals("foo > bar", notification.getElement().getProperty("text")); } + + @Test + public void setAssertive_isAssertive() { + Notification notification = new Notification(); + notification.setAssertive(true); + Assert.assertEquals(notification.isAssertive(), true); + Assert.assertTrue( + notification.getElement().getProperty("assertive", false)); + + notification.setAssertive(false); + Assert.assertEquals(notification.isAssertive(), false); + Assert.assertFalse( + notification.getElement().getProperty("assertive", false)); + } } diff --git a/vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/tests/NotificationTest.java b/vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/tests/NotificationTest.java index 9c758addeaa..f7f3bc85a28 100644 --- a/vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/tests/NotificationTest.java +++ b/vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/tests/NotificationTest.java @@ -59,6 +59,15 @@ public void stringDurAndPositionCtor() { notification.getPosition().getClientName()); } + @Test + public void stringDurationPositionAndAssertiveCtor() { + notification = new Notification("fooo", 10000, Position.TOP_END, true); + Assert.assertEquals(10000, notification.getDuration(), 0); + Assert.assertEquals("top-end", + notification.getPosition().getClientName()); + Assert.assertTrue(notification.isAssertive()); + } + @Test public void componentCtor() { notification = new Notification(new Label(), new NativeButton()); @@ -70,10 +79,11 @@ public void componentCtor() { @Test public void staticCtor() { - notification = Notification.show("fooooo", 4000, - Position.BOTTOM_CENTER); + notification = Notification.show("fooooo", 4000, Position.BOTTOM_CENTER, + true); Assert.assertEquals("bottom-center", notification.getPosition().getClientName()); + Assert.assertTrue(notification.isAssertive()); } @Test