summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorPéter Török <31210544+torok-peter@users.noreply.github.com>2017-11-07 11:39:11 +0200
committerGitHub <noreply@github.com>2017-11-07 11:39:11 +0200
commitb5b57375a3b785d758e09a4f27d3023b87e6f339 (patch)
treedf9652d6e979534a5018ca63f011a0519e73ac44 /client
parentf7abbea6bef3eb46fa4f3580a7da2a2440463266 (diff)
downloadvaadin-framework-b5b57375a3b785d758e09a4f27d3023b87e6f339.tar.gz
vaadin-framework-b5b57375a3b785d758e09a4f27d3023b87e6f339.zip
Make focus circulate in modal dialog to improve accessibility (#10260)
Make focus circulate in modal dialog to improve accessibility
Diffstat (limited to 'client')
-rw-r--r--client/src/main/java/com/vaadin/client/ui/FocusUtil.java23
-rw-r--r--client/src/main/java/com/vaadin/client/ui/VWindow.java10
2 files changed, 32 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/FocusUtil.java b/client/src/main/java/com/vaadin/client/ui/FocusUtil.java
index 16b3f68456..9a0a4ecea4 100644
--- a/client/src/main/java/com/vaadin/client/ui/FocusUtil.java
+++ b/client/src/main/java/com/vaadin/client/ui/FocusUtil.java
@@ -15,6 +15,7 @@
*/
package com.vaadin.client.ui;
+import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.Widget;
@@ -94,4 +95,26 @@ public class FocusUtil {
return focusable.getElement().getTabIndex();
}
+
+ public static native Element[] getFocusableChildren(Element parent)
+ /*-{
+ var focusableChildren = parent.querySelectorAll('[type][tabindex]:not([tabindex="-1"]), [role=button][tabindex]:not([tabindex="-1"])');
+ return focusableChildren;
+ }-*/;
+
+ public static void focusOnFirstFocusableElement(Element parent)
+ {
+ Element[] focusableChildren = getFocusableChildren(parent);
+ if (focusableChildren.length > 0) {
+ focusableChildren[0].focus();
+ }
+ }
+
+ public static void focusOnLastFocusableElement(Element parent)
+ {
+ Element[] focusableChildren = getFocusableChildren(parent);
+ if (focusableChildren.length > 0) {
+ focusableChildren[focusableChildren.length - 1].focus();
+ }
+ }
}
diff --git a/client/src/main/java/com/vaadin/client/ui/VWindow.java b/client/src/main/java/com/vaadin/client/ui/VWindow.java
index e7618ed59b..e53997ec17 100644
--- a/client/src/main/java/com/vaadin/client/ui/VWindow.java
+++ b/client/src/main/java/com/vaadin/client/ui/VWindow.java
@@ -429,14 +429,18 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner,
Roles.getDialogRole().setAriaLabelledbyProperty(getElement(),
Id.of(headerText));
- // Handlers to Prevent tab to leave the window
+ // Handlers to Prevent tab to leave the window (by circulating focus)
// and backspace to cause browser navigation
topEventBlocker = event -> {
+ if (!getElement().isOrHasChild(WidgetUtil.getFocusedElement())) {
+ return;
+ }
NativeEvent nativeEvent = event.getNativeEvent();
if (nativeEvent.getEventTarget().cast() == topTabStop
&& nativeEvent.getKeyCode() == KeyCodes.KEY_TAB
&& nativeEvent.getShiftKey()) {
nativeEvent.preventDefault();
+ FocusUtil.focusOnLastFocusableElement(this.getElement());
}
if (nativeEvent.getEventTarget().cast() == topTabStop
&& nativeEvent.getKeyCode() == KeyCodes.KEY_BACKSPACE) {
@@ -445,11 +449,15 @@ public class VWindow extends VOverlay implements ShortcutActionHandlerOwner,
};
bottomEventBlocker = event -> {
+ if (!getElement().isOrHasChild(WidgetUtil.getFocusedElement())) {
+ return;
+ }
NativeEvent nativeEvent = event.getNativeEvent();
if (nativeEvent.getEventTarget().cast() == bottomTabStop
&& nativeEvent.getKeyCode() == KeyCodes.KEY_TAB
&& !nativeEvent.getShiftKey()) {
nativeEvent.preventDefault();
+ FocusUtil.focusOnFirstFocusableElement(this.getElement());
}
if (nativeEvent.getEventTarget().cast() == bottomTabStop
&& nativeEvent.getKeyCode() == KeyCodes.KEY_BACKSPACE) {