Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Unified Diff: ui/webui/resources/js/cr/ui/node_utils.js

Issue 862403006: extensions: fix focus management for all dialogs (not just options). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@classList-fix
Patch Set: toggle Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/webui/resources/js/cr/ui/node_utils.js
diff --git a/ui/webui/resources/js/cr/ui/node_utils.js b/ui/webui/resources/js/cr/ui/node_utils.js
index a380670e4f31d07968325abddf25797016aa8da7..8f92e9bfe6835bfad0d8cf81ae5f4fde47aac4d0 100644
--- a/ui/webui/resources/js/cr/ui/node_utils.js
+++ b/ui/webui/resources/js/cr/ui/node_utils.js
@@ -31,3 +31,28 @@ cr.ui.reverseButtonStrips = function(opt_root) {
buttonStrip.setAttribute('reversed', '');
}
};
+
+/**
+ * Finds a good place to set initial focus. Generally called when UI is shown.
+ * @param {!Element} root Where to start looking for focusable controls.
+ * @param {string=} opt_extraFocusable A selector for extra focusable items
+ * (e.g. '.my-link-class, [tabindex=0]').
+ */
+cr.ui.setInitialFocus = function(root, opt_extraFocusable) {
+ // Do not change focus if any element in |root| is already focused.
+ if (root.contains(document.activeElement))
+ return;
+
+ var focusableSelector = 'input, list, select, textarea, button';
+ if (opt_extraFocusable)
+ focusableSelector = opt_extraFocusable + ', ' + focusableSelector;
+
+ var elements = root.querySelectorAll(focusableSelector);
+ for (var i = 0; i < elements.length; i++) {
+ var element = elements[i];
+ element.focus();
+ // .focus() isn't guaranteed to work. Continue until it does.
+ if (document.activeElement == element)
+ return;
+ }
+};

Powered by Google App Engine
This is Rietveld 408576698