2022-01-26 21:43:18 +00:00
|
|
|
import { Button, Container } from 'react-bootstrap'
|
|
|
|
import styles from './search.module.css'
|
|
|
|
import SearchIcon from '../svgs/search-fill.svg'
|
|
|
|
import CloseIcon from '../svgs/close-line.svg'
|
2022-01-27 19:18:48 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-01-26 21:43:18 +00:00
|
|
|
import { Form, Input, SubmitButton } from './form'
|
2022-01-27 19:18:48 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2022-01-26 21:43:18 +00:00
|
|
|
|
|
|
|
export default function Search () {
|
2022-01-27 19:18:48 +00:00
|
|
|
const router = useRouter()
|
|
|
|
const [searching, setSearching] = useState(router.query.q)
|
|
|
|
const [q, setQ] = useState(router.query.q)
|
|
|
|
const [atBottom, setAtBottom] = useState()
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-09 19:15:38 +00:00
|
|
|
setAtBottom(Math.ceil(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight)
|
2022-01-27 19:18:48 +00:00
|
|
|
window.onscroll = function (ev) {
|
2022-02-09 19:15:38 +00:00
|
|
|
if (Math.ceil(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
|
2022-01-27 19:18:48 +00:00
|
|
|
setAtBottom(true)
|
|
|
|
} else {
|
|
|
|
setAtBottom(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const showSearch = atBottom || searching || router.query.q
|
2022-01-26 21:43:18 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-01-27 19:18:48 +00:00
|
|
|
<div className={`${styles.searchSection} ${showSearch ? styles.solid : styles.hidden}`}>
|
2022-01-26 21:43:18 +00:00
|
|
|
<Container className={`px-sm-0 ${styles.searchContainer}`}>
|
2022-01-27 19:18:48 +00:00
|
|
|
{showSearch
|
2022-01-26 21:43:18 +00:00
|
|
|
? (
|
|
|
|
<Form
|
|
|
|
initial={{
|
2022-01-27 19:18:48 +00:00
|
|
|
q: router.query.q || ''
|
2022-01-26 21:43:18 +00:00
|
|
|
}}
|
|
|
|
className={`w-auto ${styles.active}`}
|
2022-01-27 19:18:48 +00:00
|
|
|
onSubmit={async ({ q }) => {
|
2022-02-02 22:05:09 +00:00
|
|
|
if (q.trim() !== '') {
|
2022-01-28 23:29:46 +00:00
|
|
|
router.push(`/search?q=${q}`)
|
|
|
|
}
|
2022-01-27 19:18:48 +00:00
|
|
|
}}
|
2022-01-26 21:43:18 +00:00
|
|
|
>
|
|
|
|
<Input
|
|
|
|
name='q'
|
|
|
|
required
|
2022-02-02 19:02:19 +00:00
|
|
|
autoFocus={showSearch && !atBottom}
|
2022-01-26 21:43:18 +00:00
|
|
|
groupClassName='mr-3 mb-0 flex-grow-1'
|
|
|
|
className='w-100'
|
|
|
|
onChange={async (formik, e) => {
|
2022-01-27 19:18:48 +00:00
|
|
|
setSearching(true)
|
2022-01-26 21:43:18 +00:00
|
|
|
setQ(e.target.value?.trim())
|
|
|
|
}}
|
|
|
|
/>
|
2022-01-27 19:18:48 +00:00
|
|
|
{q || atBottom || router.query.q
|
2022-01-26 21:43:18 +00:00
|
|
|
? (
|
|
|
|
<SubmitButton variant='primary' className={styles.search}>
|
|
|
|
<SearchIcon width={22} height={22} />
|
|
|
|
</SubmitButton>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<Button
|
|
|
|
className={styles.search} onClick={() => {
|
|
|
|
setSearching(false)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<CloseIcon width={26} height={26} />
|
|
|
|
</Button>)}
|
|
|
|
|
|
|
|
</Form>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<Button className={`${styles.search} ${styles.active}`} onClick={() => setSearching(true)}>
|
|
|
|
<SearchIcon width={22} height={22} />
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</Container>
|
|
|
|
</div>
|
|
|
|
<div className={styles.searchPadding} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|