|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title> |
| 6 | + Restoring ActiveElement Focus After A User-Interaction In JavaScript |
| 7 | + </title> |
| 8 | + <link rel="stylesheet" type="text/css" href="./demo.css" /> |
| 9 | +</head> |
| 10 | +<body> |
| 11 | + |
| 12 | + <h1> |
| 13 | + Restoring ActiveElement Focus After A User-Interaction In JavaScript |
| 14 | + </h1> |
| 15 | + |
| 16 | + <p class="trigger"> |
| 17 | + <button>Open modal (1)</button> , <button>Open modal (2)</button> , |
| 18 | + <button>Open modal (3)</button> , <button>Open modal (4)</button> |
| 19 | + </p> |
| 20 | + <p class="trigger"> |
| 21 | + <button>Open modal (5)</button> , <button>Open modal (6)</button> , |
| 22 | + <button>Open modal (7)</button> , <button>Open modal (8)</button> |
| 23 | + </p> |
| 24 | + |
| 25 | + <!-- |
| 26 | + Our modal window is going to be hidden by default. When triggered, it will take |
| 27 | + over the focus; and, when closed, focus will be returned to the element that |
| 28 | + originally triggered it. |
| 29 | + --> |
| 30 | + <div class="modal"> |
| 31 | + <div role="dialog" aria-labelledby="modal-title" class="modal__panel"> |
| 32 | + <h2 id="modal-title"> |
| 33 | + Hello, Modal |
| 34 | + </h2> |
| 35 | + <p> |
| 36 | + <button>Close</button> |
| 37 | + </p> |
| 38 | + </div> |
| 39 | + </div> |
| 40 | + |
| 41 | + <script type="text/javascript" src="../../vendor/jquery/3.6.0/jquery-3.6.0.min.js"></script> |
| 42 | + <script type="text/javascript"> |
| 43 | + |
| 44 | + var trigger = $( ".trigger" ) |
| 45 | + .on( "click", "button", openModal ) |
| 46 | + ; |
| 47 | + var modal = $( ".modal" ) |
| 48 | + .on( "click", handleModalClick ) |
| 49 | + ; |
| 50 | + var panel = modal |
| 51 | + .find( ".modal__panel" ) |
| 52 | + // We're applying tabindex to the modal panel so that we can programmatically |
| 53 | + // focus the panel after we open the modal window. |
| 54 | + .attr( "tabindex", "-1" ) |
| 55 | + .on( "click", "button", closeModal ) |
| 56 | + ; |
| 57 | + |
| 58 | + // I keep track of element that triggered the modal window. |
| 59 | + var previousElement = null; |
| 60 | + |
| 61 | + // --------------------------------------------------------------------------- // |
| 62 | + // --------------------------------------------------------------------------- // |
| 63 | + |
| 64 | + // I open the modal window and draw focus into the modal container. |
| 65 | + function openModal() { |
| 66 | + |
| 67 | + // We're about to open the modal window and draw focus into the modal panel. |
| 68 | + // But, before we do that, we want to track which element triggered the modal |
| 69 | + // so that we can restore focus to that element when the modal is closed. |
| 70 | + previousElement = ( document.activeElement || document.body ); |
| 71 | + |
| 72 | + modal.addClass( "modal--open" ); |
| 73 | + panel.focus(); |
| 74 | + |
| 75 | + console.group( "Taking focus away from trigger" ); |
| 76 | + console.log( previousElement ); |
| 77 | + console.groupEnd(); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + // I close the modal window and return focus to the previous element. |
| 83 | + function closeModal() { |
| 84 | + |
| 85 | + modal.removeClass( "modal--open" ); |
| 86 | + |
| 87 | + // If we have a reference to the original trigger, let's restore focus to |
| 88 | + // that the trigger so the user can pick-up where they left off. |
| 89 | + if ( previousElement ) { |
| 90 | + |
| 91 | + console.group( "Restoring focus to previously-active element" ); |
| 92 | + console.log( previousElement ); |
| 93 | + console.groupEnd(); |
| 94 | + |
| 95 | + previousElement.focus(); |
| 96 | + previousElement = null; |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + // I handle top-level clicks on the modal. |
| 104 | + function handleModalClick( event ) { |
| 105 | + |
| 106 | + // If the user is clicking directly on the backdrop of the modal, let's |
| 107 | + // consider this a request to close the modal (a common interaction model). |
| 108 | + if ( modal.is( event.target ) ) { |
| 109 | + |
| 110 | + closeModal(); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + </script> |
| 117 | + |
| 118 | +</body> |
| 119 | +</html> |
0 commit comments