|
| 1 | + |
| 2 | +function tabGroup() { |
| 3 | + |
| 4 | + var host = this.$el; |
| 5 | + |
| 6 | + // Return the public API of the component scope. |
| 7 | + return { |
| 8 | + moveTabIndexToButton: moveTabIndexToButton, |
| 9 | + moveToNextButton: moveToNextButton, |
| 10 | + moveToPrevButton: moveToPrevButton |
| 11 | + }; |
| 12 | + |
| 13 | + // --- |
| 14 | + // PUBLIC METHODS. |
| 15 | + // --- |
| 16 | + |
| 17 | + /** |
| 18 | + * I move the active tabIndex (0) to the target button. This way, when the user clicks |
| 19 | + * a button, this becomes the button that can also be activated via the Tab key. |
| 20 | + */ |
| 21 | + function moveTabIndexToButton( event ) { |
| 22 | + |
| 23 | + var targetButton = event.target.closest( "button" ); |
| 24 | + |
| 25 | + // Since we're using event-delegation on the host, it's possible that the click |
| 26 | + // event isn't targeting a button. In that case, ignore the event. |
| 27 | + if ( ! targetButton ) { |
| 28 | + |
| 29 | + return; |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + for ( var button of getAllButtons() ) { |
| 34 | + |
| 35 | + button.tabIndex = -1; |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + targetButton.tabIndex = 0; |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * I move the focus and active tabIndex (0) to the next button in the set of buttons |
| 45 | + * contained within the host element. |
| 46 | + */ |
| 47 | + function moveToNextButton( event ) { |
| 48 | + |
| 49 | + // Prevent any default browser behaviors (such as scrolling the viewport). |
| 50 | + event.preventDefault(); |
| 51 | + |
| 52 | + // Note: Technically, we're using event-delegation for the arrow keys. However, |
| 53 | + // since no other elements (other than our demo buttons) can be focused within the |
| 54 | + // host element, we can be confident that this was triggered by a button. |
| 55 | + var targetButton = event.target.closest( "button" ); |
| 56 | + var allButtons = getAllButtons(); |
| 57 | + var currentIndex = allButtons.indexOf( targetButton ); |
| 58 | + // Get the NEXT button; or, loop around to the front of the collection. |
| 59 | + var futureButton = ( |
| 60 | + allButtons[ currentIndex + 1 ] || |
| 61 | + allButtons[ 0 ] |
| 62 | + ); |
| 63 | + |
| 64 | + targetButton.tabIndex = -1; |
| 65 | + futureButton.tabIndex = 0; |
| 66 | + futureButton.focus(); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * I move the focus and active tabIndex (0) to the previous button in the set of |
| 72 | + * buttons contained within the host element. |
| 73 | + */ |
| 74 | + function moveToPrevButton( event ) { |
| 75 | + |
| 76 | + // Prevent any default browser behaviors (such as scrolling the viewport). |
| 77 | + event.preventDefault(); |
| 78 | + |
| 79 | + // Note: Technically, we're using event-delegation for the arrow keys. However, |
| 80 | + // since no other elements (other than our demo buttons) can be focused within the |
| 81 | + // host element, we can be confident that this was triggered by a button. |
| 82 | + var targetButton = event.target.closest( "button" ); |
| 83 | + var allButtons = getAllButtons(); |
| 84 | + var currentIndex = allButtons.indexOf( targetButton ); |
| 85 | + // Get the PREVIOUS button; or, loop around to the back of the collection. |
| 86 | + var futureButton = ( |
| 87 | + allButtons[ currentIndex - 1 ] || |
| 88 | + allButtons[ allButtons.length - 1 ] |
| 89 | + ); |
| 90 | + |
| 91 | + targetButton.tabIndex = -1; |
| 92 | + futureButton.tabIndex = 0; |
| 93 | + futureButton.focus(); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + // --- |
| 98 | + // PRIVATE METHODS. |
| 99 | + // --- |
| 100 | + |
| 101 | + /** |
| 102 | + * I get all the buttons in the host element (as a proper array). |
| 103 | + */ |
| 104 | + function getAllButtons() { |
| 105 | + |
| 106 | + return Array.from( host.querySelectorAll( "button" ) ); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments