11import React , { useState , useEffect } from "react" ;
2+ import { createPortal } from "react-dom" ;
23import classNames from "classnames" ;
34import FocusTrap from "focus-trap-react" ;
45import { useRouter , withRouter } from "next/router" ;
@@ -27,14 +28,7 @@ const Search = () => {
2728 if ( e && e . currentTarget !== e . target ) {
2829 return ;
2930 }
30-
3131 setIsModalOpen ( ! isModalOpen ) ;
32-
33- if ( document . body . style . overflow == "hidden" ) {
34- document . body . style . overflow = "unset" ;
35- } else {
36- document . body . style . overflow = "hidden" ;
37- }
3832 } ;
3933
4034 const focus = ( ) => {
@@ -46,20 +40,17 @@ const Search = () => {
4640
4741 const searchClicked = ( ) => {
4842 setIsModalOpen ( true ) ;
49- document . body . style . overflow = "hidden" ;
5043 focus ( ) ;
5144 } ;
5245
5346 const handleKey = ( e ) => {
5447 if ( e . key === "k" && ( e . ctrlKey || e . metaKey ) ) {
5548 e . preventDefault ( ) ; // prevent "Save Page" from getting triggered.
5649 setIsModalOpen ( true ) ;
57- document . body . style . overflow = "hidden" ;
5850 focus ( ) ;
5951 }
6052 if ( e . key === "Escape" ) {
6153 setIsModalOpen ( false ) ;
62- document . body . style . overflow = "unset" ;
6354 }
6455 if ( isModalOpen === true ) {
6556 const resultCount = document . querySelectorAll ( ".ais-Hits-item" ) . length ;
@@ -264,7 +255,10 @@ const Search = () => {
264255 < p className = { styles . SearchText } > Search</ p >
265256 { windowWidth > 1024 && < p className = { styles . HotKey } > { hotkey } </ p > }
266257 </ section >
267- { modal }
258+ { /* Render modal via portal to escape header's stacking context */ }
259+ { typeof document !== "undefined" && modal
260+ ? createPortal ( modal , document . body )
261+ : null }
268262 { /* <SearchBox /> */ }
269263 </ section >
270264 ) ;
0 commit comments