Skip to content

Commit

Permalink
Refactor Search Bar
Browse files Browse the repository at this point in the history
Remove onFocus and onClick events

Signed-off-by: Randy <randymoralesg@gmail.com>
  • Loading branch information
randymorales committed May 24, 2021
1 parent 378f2a8 commit 5e1798c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 22 deletions.
4 changes: 0 additions & 4 deletions components/Navbar.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Image from 'next/image'
import Link from 'next/link'

import { useState, useEffect } from 'react'

import { CSSTransition } from 'react-transition-group'

import useTranslation from '@/i18n/useTranslation'
import Translator from '@/components/Translator'
import ThemeSelector from '@/components/ThemeSelector'
Expand Down
20 changes: 3 additions & 17 deletions pages/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default function BlogIndex({ allLocalePostsData }) {

const searchRef = useRef(null)
const [query, setQuery] = useState('')
const [active, setActive] = useState(false)
const [results, setResults] = useState([])

const searchEndpoint = query => `/api/search?q=${query}`
Expand All @@ -50,27 +49,14 @@ export default function BlogIndex({ allLocalePostsData }) {
}
}, [])

const onFocus = useCallback(() => {
setActive(true)
window.addEventListener('click', onClick)
}, [])

const onClick = useCallback(event => {
if (searchRef.current && !searchRef.current.contains(event.target)) {
setActive(false)
window.removeEventListener('click', onClick)
}
}, [])

return (
<Layout pageTitle={t('blog')}>
<div ref={searchRef}>
<div className={styles.container}>
<div>
<div ref={searchRef} className={styles.container}>
{/* Search bar */}
<input
className={styles.search}
onChange={onChange}
onFocus={onFocus}
placeholder={t('search-posts')}
type='text'
value={query}
Expand All @@ -88,7 +74,7 @@ export default function BlogIndex({ allLocalePostsData }) {

{/* Show blog posts list */}
<section className={styles.cardsContainer}>
{results.length == 0 && query != '' && active ? (
{results.length == 0 && query != '' ? (
<> </>
) : (
<>
Expand Down
2 changes: 1 addition & 1 deletion styles/blog.module.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* ******** Search Bar ******** */

.container {
display: flex;
margin: 0 auto;
Expand All @@ -7,7 +8,6 @@

.search {
border: 1px solid #666;
box-sizing: border-box;
cursor: 'pointer';
font-size: 1.3rem;
margin-bottom: 1rem;
Expand Down

1 comment on commit 5e1798c

@vercel
Copy link

@vercel vercel bot commented on 5e1798c May 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.