-
+
diff --git a/src/wp-includes/js/wp-embed-template.js b/src/wp-includes/js/wp-embed-template.js
index b1dc73a8b1..b947465e94 100644
--- a/src/wp-includes/js/wp-embed-template.js
+++ b/src/wp-includes/js/wp-embed-template.js
@@ -38,7 +38,8 @@
function openSharingDialog() {
share_dialog.className = share_dialog.className.replace( 'hidden', '' );
- share_input[ 0 ].select();
+ // Initial focus should go on the currently selected tab in the dialog.
+ document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' ).focus();
}
function closeSharingDialog() {
@@ -47,16 +48,14 @@
}
if ( share_dialog_open ) {
- share_dialog_open.addEventListener( 'click', function ( e ) {
+ share_dialog_open.addEventListener( 'click', function () {
openSharingDialog();
- e.preventDefault();
} );
}
if ( share_dialog_close ) {
- share_dialog_close.addEventListener( 'click', function ( e ) {
+ share_dialog_close.addEventListener( 'click', function () {
closeSharingDialog();
- e.preventDefault();
} );
}
@@ -110,11 +109,26 @@
}
document.addEventListener( 'keydown', function ( e ) {
- if ( e.keyCode === 27 && -1 === share_dialog.className.indexOf( 'hidden' ) ) {
+ if ( 27 === e.keyCode && -1 === share_dialog.className.indexOf( 'hidden' ) ) {
closeSharingDialog();
+ } else if ( 9 === e.keyCode ) {
+ constrainTabbing( e );
}
}, false );
+ function constrainTabbing( e ) {
+ // Need to re-get the selected tab each time.
+ var firstFocusable = document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' );
+
+ if ( share_dialog_close === e.target && ! e.shiftKey ) {
+ firstFocusable.focus();
+ e.preventDefault();
+ } else if ( firstFocusable === e.target && e.shiftKey ) {
+ share_dialog_close.focus();
+ e.preventDefault();
+ }
+ }
+
if ( window.self === window.top ) {
return;
}