-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
267 additions
and
5 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
'use client'; | ||
import React, { FunctionComponent, useEffect, useRef, useState } from 'react'; | ||
import Image from 'next/image'; | ||
import Link from 'next/link'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useDebounce } from '@/app/blog/(search-bar)/useDebounce'; | ||
import type { PostsFetchResponse } from '@/components/blog-post-item/types'; | ||
import { db } from '@/scripts/fetch'; | ||
import styles from './styles.module.scss'; | ||
|
||
const SearchBar: FunctionComponent = () => { | ||
const router = useRouter(); | ||
|
||
const [isActive, setFormActive] = useState(false); | ||
const [searchQuery, setSearchQuery] = useState(''); | ||
const [searchResults, setSearchResults] = useState<PostsFetchResponse['data'] | null>(null); | ||
const [focusedIndex, setFocusedIndex] = useState(-1); | ||
|
||
const searchInputRef = useRef<HTMLInputElement>(null); | ||
const debouncedSearchQuery = useDebounce(searchQuery, 500); | ||
|
||
const fetchPosts = async () => { | ||
const posts = await db.getPostsBySearchTerm(debouncedSearchQuery); | ||
setSearchResults(posts.data); | ||
}; | ||
|
||
const handleBlur = (e: React.FocusEvent<HTMLDivElement>) => { | ||
const { currentTarget, relatedTarget } = e; | ||
|
||
if (currentTarget.contains(relatedTarget)) { | ||
return; | ||
} | ||
|
||
setFormActive(false); | ||
}; | ||
|
||
const handleEscKey = (e: KeyboardEvent) => { | ||
if (e.key === 'Escape') { | ||
searchInputRef.current?.blur(); | ||
setSearchQuery(''); | ||
} | ||
}; | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setSearchQuery(e.target.value); | ||
setFormActive(true); | ||
}; | ||
|
||
// Keyboard navigation | ||
const handleKeyDown = (e: React.KeyboardEvent) => { | ||
if (e.key === 'ArrowDown') { | ||
e.preventDefault(); | ||
|
||
if (!searchResults) { | ||
return; | ||
} | ||
|
||
setFocusedIndex((prevIndex) => Math.min(prevIndex + 1, searchResults?.length - 1 ?? 0)); | ||
} else if (e.key === 'ArrowUp') { | ||
e.preventDefault(); | ||
|
||
setFocusedIndex((prevIndex) => Math.max(prevIndex - 1, 0)); | ||
} else if (e.key === 'Enter' && focusedIndex !== -1) { | ||
const postToGoTo = searchResults?.find((post, index) => focusedIndex === index); | ||
|
||
if (!postToGoTo) { | ||
return; | ||
} | ||
|
||
router.push(`/blog/${postToGoTo.attributes.slug}`); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
document.addEventListener('keydown', handleEscKey); | ||
|
||
return () => { | ||
document.removeEventListener('keydown', handleEscKey); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!debouncedSearchQuery || debouncedSearchQuery.length < 2) { | ||
setSearchResults([]); | ||
return; | ||
} | ||
|
||
fetchPosts(); | ||
}, [debouncedSearchQuery]); | ||
|
||
return ( | ||
<section className={styles.container} onBlur={handleBlur} onKeyDown={handleKeyDown}> | ||
<label className={styles.label}> | ||
<Image src={'/images/search/icon.svg'} | ||
priority | ||
alt={'Search bar'} | ||
width={16} | ||
height={16} | ||
className={styles.image} | ||
/> | ||
<input className={styles.input} | ||
type="text" | ||
placeholder={'Search blog posts'} | ||
onClick={() => setFormActive(true)} | ||
onFocus={() => setFormActive(true)} | ||
ref={searchInputRef} | ||
value={searchQuery} | ||
onChange={handleChange} | ||
/> | ||
</label> | ||
{Boolean(searchResults?.length && isActive) && ( | ||
<ul className={styles.results}> | ||
{searchResults?.map((post, index) => ( | ||
<li key={post.id} className={index === focusedIndex ? styles.focused : ''}> | ||
<Link href={`/blog/${post.attributes.slug}`}>{post.attributes.title}</Link> | ||
</li> | ||
))} | ||
</ul> | ||
)} | ||
</section> | ||
); | ||
}; | ||
|
||
export default SearchBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
@import '@/styles/vars'; | ||
|
||
.container { | ||
display: flex; | ||
width: 100%; | ||
background-color: $color-background; | ||
margin-bottom: 2rem; | ||
position: relative; | ||
|
||
.label { | ||
height: 36px; | ||
border-radius: .35rem; | ||
border: 1px solid darken($color-text, 40%); | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
padding-inline: .5rem; | ||
|
||
.input { | ||
background-color: inherit; | ||
outline: none; | ||
border: none; | ||
height: 100%; | ||
width: 100%; | ||
padding-left: .3rem; | ||
color: darken($color-text, 40%); | ||
font-weight: 300; | ||
|
||
&::placeholder { | ||
color: darken($color-text, 60%); | ||
} | ||
} | ||
} | ||
|
||
.results { | ||
overflow: hidden; | ||
display: flex; | ||
flex-direction: column; | ||
background-color: $color-background; | ||
border-radius: .35rem; | ||
border: 1px solid darken($color-text, 40%); | ||
width: 100%; | ||
position: absolute; | ||
translate: 0 100%; | ||
bottom: -1rem; | ||
left: 0; | ||
padding-block: .5rem; | ||
|
||
li { | ||
width: 100%; | ||
|
||
a { | ||
display: flex; | ||
width: 100%; | ||
padding-inline: 1rem; | ||
padding-block: .8rem; | ||
} | ||
|
||
&.focused, &:hover { | ||
background-color: lighten($color-background, 10%); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const useDebounce = <T, >(value: T, delay: number): T => { | ||
const [debouncedValue, setDebouncedValue] = useState<T>(value); | ||
|
||
useEffect(() => { | ||
const handler = setTimeout(() => { | ||
setDebouncedValue(value); | ||
}, delay); | ||
|
||
return () => { | ||
clearTimeout(handler); | ||
}; | ||
}, [value, delay]); // Only re-call effect if value or delay changes | ||
|
||
return debouncedValue; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 17 additions & 2 deletions
19
next/src/components/categories/category-link/styles.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
@import '@/styles/vars'; | ||
|
||
.link { | ||
color: darken($color-text, 40%); | ||
display: flex; | ||
align-items: center; | ||
|
||
.text { | ||
color: darken($color-text, 40%); | ||
} | ||
|
||
.cross { | ||
margin-left: .2rem; | ||
} | ||
|
||
&.active { | ||
color: $color-accent; | ||
border-radius: .35rem; | ||
padding: .5rem; | ||
background-color: darken($color-accent, 10%); | ||
|
||
.text { | ||
color: $color-text; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.categories { | ||
margin-bottom: 3rem; | ||
justify-content: flex-start; | ||
align-items: center; | ||
display: flex; | ||
gap: .5rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters