Skip to content

Commit b2fe10c

Browse files
authored
feat: add API for setting assertive to Notification (#6862)
1 parent 5109bed commit b2fe10c

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

vaadin-notification-flow-parent/vaadin-notification-flow/src/main/java/com/vaadin/flow/component/notification/Notification.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,31 @@ public Notification(String text, int duration, Position position) {
198198
setPosition(position);
199199
}
200200

201+
/**
202+
* Creates a Notification with given text String, duration, position and
203+
* assertive state.
204+
* <P>
205+
* Set to {@code 0} or a negative number to disable the notification
206+
* auto-closing.
207+
*
208+
* @param text
209+
* the text of the notification
210+
* @param duration
211+
* the duration in milliseconds to show the notification
212+
* @param position
213+
* the position of the notification. Valid enumerate values are
214+
* TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE,
215+
* BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH
216+
* @param assertive
217+
* whether the notification should have {@code aria-live}
218+
* attribute set to {@code assertive} or {@code polite}
219+
*/
220+
public Notification(String text, int duration, Position position,
221+
boolean assertive) {
222+
this(text, duration, position);
223+
setAssertive(assertive);
224+
}
225+
201226
/**
202227
* Creates a notification with given components inside.
203228
* <p>
@@ -232,6 +257,31 @@ private void removeAutoAdded() {
232257
}
233258
}
234259

260+
/**
261+
* Shows a notification in the current page with given text, duration,
262+
* position and assertive state.
263+
*
264+
* @param text
265+
* the text of the Notification
266+
* @param duration
267+
* the duration in milliseconds to show the notification
268+
* @param position
269+
* the position of the notification. Valid enumerate values are
270+
* TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE,
271+
* BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH
272+
* @param assertive
273+
* whether the notification should have {@code aria-live}
274+
* attribute set to {@code assertive} or {@code polite}
275+
* @return the notification
276+
*/
277+
public static Notification show(String text, int duration,
278+
Position position, boolean assertive) {
279+
Notification notification = new Notification(text, duration, position,
280+
assertive);
281+
notification.open();
282+
return notification;
283+
}
284+
235285
/**
236286
* Shows a notification in the current page with given text, duration and
237287
* position.
@@ -248,9 +298,7 @@ private void removeAutoAdded() {
248298
*/
249299
public static Notification show(String text, int duration,
250300
Position position) {
251-
Notification notification = new Notification(text, duration, position);
252-
notification.open();
253-
return notification;
301+
return show(text, duration, position, false);
254302
}
255303

256304
/**
@@ -501,6 +549,29 @@ public int getDuration() {
501549
return getElement().getProperty("duration", 0);
502550
}
503551

552+
/**
553+
* When true, the notification card has {@code aria-live} attribute set to
554+
* {@code assertive} instead of {@code polite}. This makes screen readers
555+
* announce the notification content immediately when it appears.
556+
*
557+
* @param assertive
558+
* the value to set
559+
*/
560+
public void setAssertive(boolean assertive) {
561+
getElement().setProperty("assertive", assertive);
562+
}
563+
564+
/**
565+
* When true, the notification card has {@code aria-live} attribute set to
566+
* {@code assertive} instead of {@code polite}. This makes screen readers
567+
* announce the notification content immediately when it appears.
568+
*
569+
* @return the {@code assertive} property from the webcomponent
570+
*/
571+
public boolean isAssertive() {
572+
return getElement().getProperty("assertive", false);
573+
}
574+
504575
/**
505576
* {@inheritDoc}
506577
* <p>

vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/NotificationTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,18 @@ public void setText_notificationHasUnmodifiedText() {
367367
Assert.assertEquals("foo > bar",
368368
notification.getElement().getProperty("text"));
369369
}
370+
371+
@Test
372+
public void setAssertive_isAssertive() {
373+
Notification notification = new Notification();
374+
notification.setAssertive(true);
375+
Assert.assertEquals(notification.isAssertive(), true);
376+
Assert.assertTrue(
377+
notification.getElement().getProperty("assertive", false));
378+
379+
notification.setAssertive(false);
380+
Assert.assertEquals(notification.isAssertive(), false);
381+
Assert.assertFalse(
382+
notification.getElement().getProperty("assertive", false));
383+
}
370384
}

vaadin-notification-flow-parent/vaadin-notification-flow/src/test/java/com/vaadin/flow/component/notification/tests/NotificationTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public void stringDurAndPositionCtor() {
5959
notification.getPosition().getClientName());
6060
}
6161

62+
@Test
63+
public void stringDurationPositionAndAssertiveCtor() {
64+
notification = new Notification("fooo", 10000, Position.TOP_END, true);
65+
Assert.assertEquals(10000, notification.getDuration(), 0);
66+
Assert.assertEquals("top-end",
67+
notification.getPosition().getClientName());
68+
Assert.assertTrue(notification.isAssertive());
69+
}
70+
6271
@Test
6372
public void componentCtor() {
6473
notification = new Notification(new Label(), new NativeButton());
@@ -70,10 +79,11 @@ public void componentCtor() {
7079

7180
@Test
7281
public void staticCtor() {
73-
notification = Notification.show("fooooo", 4000,
74-
Position.BOTTOM_CENTER);
82+
notification = Notification.show("fooooo", 4000, Position.BOTTOM_CENTER,
83+
true);
7584
Assert.assertEquals("bottom-center",
7685
notification.getPosition().getClientName());
86+
Assert.assertTrue(notification.isAssertive());
7787
}
7888

7989
@Test

0 commit comments

Comments
 (0)