Skip to content

Commit

Permalink
fix: search input overlay behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
andyesp committed Oct 13, 2023
1 parent 04f4aba commit 8088d82
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/components/Search/SearchInputMobile.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
justify-content: center;
background: var(--white-900);
width: 40px;
height: 35px;
height: 33px;
gap: 8px;
font-size: 16px;
}

.SearchInputMobile--open {
width: 100%;
height: 34px;
height: 32px;
padding-left: 10px;
padding-right: 16px;
top: 0;
Expand Down
16 changes: 15 additions & 1 deletion src/components/Search/SearchInputMobile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from 'react'
import { useLayoutEffect, useRef, useState } from 'react'

import { useLocation } from '@reach/router'
import classNames from 'classnames'
Expand Down Expand Up @@ -62,6 +62,19 @@ export default function SearchInputMobile() {
}
}

useLayoutEffect(() => {
if (typeof window !== 'undefined') {
const onScroll = function () {
searchInput.current?.blur()
setOpen(false)
}

window.addEventListener('scroll', onScroll)

return () => window.removeEventListener('scroll', onScroll)
}
}, [])

return (
<div className={classNames('SearchInputMobile', open && 'SearchInputMobile--open')}>
<div className="SearchInputMobile__Container">
Expand All @@ -81,6 +94,7 @@ export default function SearchInputMobile() {
)}
</div>
<div
onClick={() => setOpen(false)}
className={classNames(
'SearchInputMobile__Overlay',
open && 'SearchInputMobile__Overlay--open',
Expand Down

0 comments on commit 8088d82

Please sign in to comment.