| 1 | const |
| 2 | HEADER_CONTAINER_CLASS = 'vector-header-container', |
| 3 | SEARCH_BOX_SELECTOR = '.vector-search-box', |
| 4 | SEARCH_VISIBLE_CLASS = 'vector-header-search-toggled'; |
| 5 | |
| 6 | /** |
| 7 | * Binds event handlers necessary for the searchBox to disappear when the user |
| 8 | * clicks outside the searchBox. |
| 9 | * |
| 10 | * @param {HTMLElement} searchBox |
| 11 | * @param {HTMLElement} header |
| 12 | */ |
| 13 | function bindSearchBoxHandler( searchBox, header ) { |
| 14 | /** |
| 15 | * @param {Event} ev |
| 16 | * @ignore |
| 17 | */ |
| 18 | const clickHandler = ( ev ) => { |
| 19 | if ( |
| 20 | ev.target instanceof HTMLElement && |
| 21 | // Check if the click target was a suggestion link. Codex clears the |
| 22 | // suggestion elements from the DOM when a suggestion is clicked so we |
| 23 | // can't test if the suggestion is a child of the searchBox. |
| 24 | // |
| 25 | // Note: The .closest API is feature detected in `initSearchToggle`. |
| 26 | !ev.target.closest( '.cdx-typeahead-search .cdx-menu-item__content' ) && |
| 27 | !searchBox.contains( ev.target ) |
| 28 | ) { |
| 29 | header.classList.remove( SEARCH_VISIBLE_CLASS ); |
| 30 | |
| 31 | document.removeEventListener( 'click', clickHandler ); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | document.addEventListener( 'click', clickHandler ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Binds event handlers necessary for the searchBox to show when the toggle is |
| 40 | * clicked. |
| 41 | * |
| 42 | * @param {HTMLElement} searchBox |
| 43 | * @param {HTMLElement} header |
| 44 | * @param {Element} searchToggle |
| 45 | */ |
| 46 | function bindToggleClickHandler( searchBox, header, searchToggle ) { |
| 47 | /** |
| 48 | * @param {Event} ev |
| 49 | * @ignore |
| 50 | */ |
| 51 | const handler = ( ev ) => { |
| 52 | // The toggle is an anchor element. Prevent the browser from navigating away |
| 53 | // from the page when clicked. |
| 54 | ev.preventDefault(); |
| 55 | |
| 56 | header.classList.add( SEARCH_VISIBLE_CLASS ); |
| 57 | |
| 58 | // Defer binding the search box handler until after the event bubbles to the |
| 59 | // top of the document so that the handler isn't called when the user clicks |
| 60 | // the search toggle. Event bubbled callbacks execute within the same task |
| 61 | // in the event loop. |
| 62 | // |
| 63 | // Also, defer focusing the input to another task in the event loop. At the time |
| 64 | // of this writing, Safari 14.0.3 has trouble changing the visibility of the |
| 65 | // element and focusing the input within the same task. |
| 66 | setTimeout( () => { |
| 67 | bindSearchBoxHandler( searchBox, header ); |
| 68 | |
| 69 | const searchInput = /** @type {HTMLInputElement|null} */ ( searchBox.querySelector( 'input[type="search"]' ) ); |
| 70 | |
| 71 | if ( searchInput ) { |
| 72 | const beforeScrollX = window.scrollX; |
| 73 | const beforeScrollY = window.scrollY; |
| 74 | searchInput.focus(); |
| 75 | // For some reason, Safari 14,15 tends to undesirably change the scroll |
| 76 | // position of `input` elements inside fixed position elements. |
| 77 | // While an Internet search suggests similar problems with mobile Safari |
| 78 | // it didn't yield any results for desktop Safari. |
| 79 | // This line resets any unexpected scrolling that occurred while the |
| 80 | // input received focus. |
| 81 | // If you are in the future with a modern version of Safari, where 14 and 15 |
| 82 | // receive a low amount of page views, please reference T297636 and test |
| 83 | // to see whether this line of code can be removed. |
| 84 | // Additionally, these lines might become unnecessary when/if Safari |
| 85 | // supports the `preventScroll` focus option [1] in the future: |
| 86 | // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus#parameters |
| 87 | if ( beforeScrollX !== undefined && beforeScrollY !== undefined ) { |
| 88 | window.scroll( beforeScrollX, beforeScrollY ); |
| 89 | } |
| 90 | } |
| 91 | } ); |
| 92 | }; |
| 93 | |
| 94 | searchToggle.addEventListener( 'click', handler ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Enables search toggling behavior in a header given a toggle element (e.g. |
| 99 | * search icon). When the toggle element is clicked, a class, |
| 100 | * `SEARCH_VISIBLE_CLASS`, will be applied to a header matching the selector |
| 101 | * `HEADER_SELECTOR` and the input inside the element, SEARCH_BOX_SELECTOR, will |
| 102 | * be focused. This class can be used in CSS to show/hide the necessary |
| 103 | * elements. When the user clicks outside of SEARCH_BOX_SELECTOR, the class will |
| 104 | * be removed. |
| 105 | * |
| 106 | * @param {HTMLElement|Element} searchToggle |
| 107 | */ |
| 108 | module.exports = function initSearchToggle( searchToggle ) { |
| 109 | const headerContainer = |
| 110 | /** @type {HTMLElement|null} */ ( searchToggle.closest( `.${ HEADER_CONTAINER_CLASS }` ) ); |
| 111 | const header = |
| 112 | /** @type {HTMLElement|null} */ ( headerContainer && headerContainer.firstElementChild ); |
| 113 | |
| 114 | if ( !header ) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | const searchBox = |
| 119 | /** @type {HTMLElement|null} */ ( header.querySelector( SEARCH_BOX_SELECTOR ) ); |
| 120 | |
| 121 | if ( !searchBox ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | bindToggleClickHandler( searchBox, header, searchToggle ); |
| 126 | }; |