Skip to content

Commit 57ad6b5

Browse files
committed
feat: show search bar when scrolled
1 parent 8c6043f commit 57ad6b5

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

components/Navbar.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useEffect, useState } from 'react'
1111
import { Nullable, User, UserCache } from '@types'
1212
import Fetch from '@utils/Fetch'
1313
import { redirectTo } from '@utils/Tools'
14+
import Search from './Search'
1415

1516
const DiscordAvatar = dynamic(() => import('@components/DiscordAvatar'))
1617

@@ -51,9 +52,20 @@ const Navbar: React.FC<NavbarProps> = ({ token }) => {
5152
setUserCache(null)
5253
}
5354
}, [token])
55+
const [scrolled, setScrolled] = useState(false)
56+
57+
useEffect(() => {
58+
const handleScroll = () => setScrolled(window.scrollY > 80)
59+
window.addEventListener('scroll', handleScroll)
60+
return () => window.removeEventListener('scroll', handleScroll)
61+
}, [])
62+
5463
return (
55-
<>
56-
<nav className='fixed top-0 z-40 flex w-full flex-wrap items-center justify-between bg-discord-blurple px-2 py-3 text-gray-100 dark:bg-discord-black lg:absolute'>
64+
<div className='fixed top-0 left-0 right-0 z-50'>
65+
<nav className={`fixed top-0 z-40 flex w-full flex-wrap items-center justify-between px-2 py-2 text-gray-100 transition-colors duration-300 backdrop-blur-sm ${
66+
scrolled || dev ? 'bg-discord-blurple dark:bg-discord-black' : 'bg-discord-blurple/30 dark:bg-discord-black/30'
67+
} lg:absolute`}>
68+
5769
<div className='container mx-auto flex flex-wrap items-center justify-between px-4'>
5870
<div className='relative flex w-full justify-between lg:w-auto lg:justify-start'>
5971
<Link
@@ -168,7 +180,12 @@ const Navbar: React.FC<NavbarProps> = ({ token }) => {
168180
</li>
169181
</ul>
170182
</div>
171-
<div className='hidden grow items-center bg-white lg:flex lg:bg-transparent lg:shadow-none'>
183+
{scrolled && (
184+
<div className='hidden grow items-center justify-center px-6 lg:flex'>
185+
<Search compact />
186+
</div>
187+
)}
188+
<div className='hidden lg:flex lg:items-center lg:bg-transparent lg:shadow-none'>
172189
<ul className='flex list-none flex-col lg:ml-auto lg:flex-row'>
173190
<li
174191
className='flex items-center outline-none'
@@ -376,7 +393,7 @@ const Navbar: React.FC<NavbarProps> = ({ token }) => {
376393
)}
377394
</div>
378395
</div>
379-
</>
396+
</div>
380397
)
381398
}
382399

components/Search.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import { useEffect, useRef, useState } from 'react'
2-
import { useRouter } from 'next/router'
3-
import Link from 'next/link'
4-
import dynamic from 'next/dynamic'
5-
import { makeBotURL, makeServerURL, redirectTo } from '@utils/Tools'
1+
import { Bot, ResponseProps, Server } from '@types'
62
import Fetch from '@utils/Fetch'
7-
import { Bot, Server, ResponseProps } from '@types'
3+
import { makeBotURL, makeServerURL, redirectTo } from '@utils/Tools'
4+
import dynamic from 'next/dynamic'
5+
import Link from 'next/link'
6+
import { useRouter } from 'next/router'
7+
import { useEffect, useRef, useState } from 'react'
88

99
import Day from '@utils/Day'
1010
import useOutsideClick from '@utils/useOutsideClick'
1111

1212
const DiscordAvatar = dynamic(() => import('@components/DiscordAvatar'))
1313
const ServerIcon = dynamic(() => import('@components/ServerIcon'))
1414

15-
const Search: React.FC = () => {
15+
interface SearchProps {
16+
compact?: boolean
17+
}
18+
19+
const Search: React.FC<SearchProps> = ({ compact = false }) => {
1620
const router = useRouter()
1721
const ref = useRef()
1822
const [query, setQuery] = useState('')
@@ -101,12 +105,13 @@ const Search: React.FC = () => {
101105
}
102106

103107
return (
104-
<div onFocus={() => setHidden(false)} ref={ref}>
105-
<div className='relative z-10 mt-5 flex w-full rounded-lg bg-white text-black dark:bg-very-black dark:text-gray-100'>
108+
<div className={`relative w-full z-10 ${compact ? '' : 'mt-5'}`}>
109+
<div onFocus={() => setHidden(false)} ref={ref}>
110+
<div className={`relative z-10 flex w-full items-center bg-white text-black dark:bg-very-black dark:text-gray-100 ${compact ? 'h-10 px-3 rounded-md' : 'rounded-lg'}`}>
106111
<input
107112
type='search'
108113
maxLength={50}
109-
className='h-16 grow border-0 border-none bg-transparent px-7 py-3 pr-20 text-xl shadow outline-none'
114+
className={`grow border-0 border-none bg-transparent pr-20 shadow outline-none ${compact ? 'h-10 text-sm' : 'h-16 px-7 py-3 text-xl'}`}
110115
placeholder='검색...'
111116
value={query}
112117
onChange={(e) => {
@@ -119,14 +124,14 @@ const Search: React.FC = () => {
119124
}}
120125
/>
121126
<button
122-
className='cusor-pointer absolute right-0 top-0 mr-5 mt-5 outline-none'
127+
className='absolute right-4 top-1/2 -translate-y-1/2 outline-none'
123128
onClick={onSubmit}
124129
>
125-
<i className='fas fa-search text-2xl text-gray-600 hover:text-gray-700' />
130+
<i className={`fas fa-search text-gray-600 hover:text-gray-700 ${compact ? 'text-sm' : 'text-2xl'}`} />
126131
</button>
127132
</div>
128133
<div className={`relative ${hidden ? 'hidden' : 'block'} z-50`}>
129-
<div className='pin-t pin-l absolute my-2 h-60 w-full overflow-y-scroll rounded bg-white text-black shadow-md dark:bg-very-black dark:text-gray-100 md:h-80'>
134+
<div className='absolute top-full mt-2 max-h-60 w-full overflow-y-auto rounded bg-white text-black shadow-md dark:bg-very-black dark:text-gray-100 md:h-80'>
130135
<ul>
131136
{data && data.code === 200 ? (
132137
<div className='grid lg:grid-cols-2'>
@@ -172,7 +177,7 @@ const Search: React.FC = () => {
172177
<li className='px-3 py-3.5'>검색중입니다...</li>
173178
</ul>
174179
) : (
175-
<ul>
180+
<ul className='px-3 py-3.5'>
176181
{query && data ? (
177182
data.message?.includes('문법') ? (
178183
<li className='px-3 py-3.5'>
@@ -194,10 +199,10 @@ const Search: React.FC = () => {
194199
)
195200
) : query.length === 0 ? (
196201
!recentSearch || !Array.isArray(recentSearch) || recentSearch.length === 0 ? (
197-
<li className='px-3 py-3.5'>최근 검색 기록이 없습니다.</li>
202+
<li >최근 검색 기록이 없습니다.</li>
198203
) : (
199204
<>
200-
<li className='h-15 cursor-pointer px-3 py-2 font-semibold'>
205+
<li className='h-15 cursor-pointer font-semibold'>
201206
최근 검색어
202207
<button
203208
className='absolute right-0 pr-10 text-sm text-red-500 hover:opacity-90'
@@ -236,6 +241,7 @@ const Search: React.FC = () => {
236241
</div>
237242
</div>
238243
</div>
244+
</div>
239245
)
240246
}
241247

0 commit comments

Comments
 (0)