From 6b1c784ef23af649d09977f1019d717885798228 Mon Sep 17 00:00:00 2001 From: Vladimir Siljkovic Date: Tue, 11 Dec 2018 19:53:59 +0100 Subject: [PATCH] Fix: `focuselement` to work with `keep focus:false` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Perhaps a user wants to set the focus on the tooltip but don’t lock a focus inside of it. --- jquery.popupoverlay.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/jquery.popupoverlay.js b/jquery.popupoverlay.js index bec4751..762b435 100644 --- a/jquery.popupoverlay.js +++ b/jquery.popupoverlay.js @@ -366,25 +366,21 @@ // Remember which element had focus before opening a popup $el.data('focusedelementbeforepopup', document.activeElement); - // Handler: Keep focus inside dialog box - if (options.keepfocus) { - // Make holder div focusable - $el.attr('tabindex', -1); - - // Focus popup or user specified element. - // Initial timeout of 50ms is set to give some time to popup to show after clicking on - // `open` element, and after animation is complete to prevent background scrolling. - setTimeout(function() { - if (options.focuselement === 'closebutton') { - $('#' + el.id + ' .' + el.id + closesuffix + ':first').focus(); - } else if (options.focuselement) { - $(options.focuselement).focus(); - } else { - $el.focus(); - } - }, options.focusdelay); + // Make holder div programatically focusable with tabindex:-1 (tabindex:0 is keyboard focusable) + $el.attr('tabindex', -1); - } + // Focus the popup or user specified element. + // Initial timeout of 50ms is set to give some time to popup to show after clicking on + // `open` element, and after animation is complete to prevent background scrolling. + setTimeout(function() { + if (options.focuselement === 'closebutton') { // e.g. focuselement:'closebutton' + $('#' + el.id + ' .' + el.id + closesuffix + ':first').focus(); + } else if (options.focuselement) { // e.g. focuselement:'#my-close-button' + $(options.focuselement).focus(); + } else if (options.focuselement === true || options.keepfocus) { // e.g. focuselement:true OR keepfocus:true + $el.focus(); + } + }, options.focusdelay); // Hide main content from screen readers $(options.pagecontainer).attr('aria-hidden', true);