Skip to content

Commit 02f26f9

Browse files
authored
Merge pull request #2141 from lxw15337674/feat/SearchModal
全局搜索弹窗夜间模式样式优化、移动端自适应。
2 parents 4681ae1 + d2efd0b commit 02f26f9

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

components/AlgoliaSearchModal.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function AlgoliaSearchModal({ cRef }) {
2222
const [useTime, setUseTime] = useState(0)
2323
const inputRef = useRef(null)
2424
const [activeIndex, setActiveIndex] = useState(0)
25+
const [isLoading, setIsLoading] = useState(false)
2526
useHotkeys('ctrl+k', (e) => {
2627
e.preventDefault()
2728
setIsModalOpen(true)
@@ -105,14 +106,15 @@ export default function AlgoliaSearchModal({ cRef }) {
105106
if (!query || query === '') {
106107
return
107108
}
108-
109+
setIsLoading(true)
109110
try {
110111
const res = await index.search(query, { page, hitsPerPage: 10 })
111112
const { hits, nbHits, nbPages, processingTimeMS } = res
112113
setUseTime(processingTimeMS)
113114
setTotalPage(nbPages)
114115
setTotalHit(nbHits)
115116
setSearchResults(hits)
117+
setIsLoading(false)
116118
const doms = document.getElementById('search-wrapper').getElementsByClassName('replace')
117119

118120
setTimeout(() => {
@@ -175,12 +177,12 @@ export default function AlgoliaSearchModal({ cRef }) {
175177
<div
176178
id="search-wrapper"
177179
className={`${isModalOpen ? 'opacity-100' : 'invisible opacity-0 pointer-events-none'
178-
} z-30 fixed h-screen w-screen left-0 top-0 mt-12 flex items-start justify-center`}
180+
} z-30 fixed h-screen w-screen left-0 top-0 sm:mt-12 flex items-start justify-center mt-0`}
179181
>
180182
{/* 模态框 */}
181183
<div
182184
className={`${isModalOpen ? 'opacity-100' : 'invisible opacity-0 translate-y-10'
183-
} flex flex-col justify-between w-full min-h-[10rem] max-w-xl dark:bg-hexo-black-gray dark:border-gray-800 bg-white dark:bg- p-5 rounded-lg z-50 shadow border hover:border-blue-600 duration-300 transition-all `}
185+
} flex flex-col justify-between w-full min-h-[10rem] h-full md:h-fit max-w-xl dark:bg-hexo-black-gray dark:border-gray-800 bg-white dark:bg- p-5 rounded-lg z-50 shadow border hover:border-blue-600 duration-300 transition-all `}
184186
>
185187
<div className="flex justify-between items-center">
186188
<div className="text-2xl text-blue-600 dark:text-yellow-600 font-bold">搜索</div>
@@ -205,21 +207,23 @@ export default function AlgoliaSearchModal({ cRef }) {
205207
<TagGroups />
206208
</div>
207209
{
208-
searchResults.length === 0 && keyword && (
210+
searchResults.length === 0 && keyword && !isLoading && (
209211
<div>
210-
<p className=" text-slate-600 text-center my-4 text-base"> 无法找到相关结果
212+
<p className=" text-slate-600 text-center my-4 text-base"> 无法找到相关结果
211213
<span
212214
className='font-semibold'
213215
>&quot;{keyword}&quot;</span></p>
214216
</div>
215217
)
216218
}
217-
<ul>
219+
<ul className='flex-1 overflow-auto'>
218220
{searchResults.map((result, index) => (
219221
<li key={result.objectID}
220222
onMouseEnter={() => setActiveIndex(index)}
221223
onClick={() => onJumpSearchResult(index)}
222-
className={`cursor-pointer replace my-2 p-2 duration-100 ${activeIndex === index ? 'bg-blue-600 dark:bg-yellow-600' : ''}`}>
224+
className={`cursor-pointer replace my-2 p-2 duration-100
225+
rounded-lg
226+
${activeIndex === index ? 'bg-blue-600 dark:bg-yellow-600' : ''}`}>
223227
<a
224228
className={`${activeIndex === index ? ' text-white' : ' text-black dark:text-gray-300 '}`}
225229
>
@@ -229,15 +233,15 @@ export default function AlgoliaSearchModal({ cRef }) {
229233
))}
230234
</ul>
231235
<Pagination totalPage={totalPage} page={page} switchPage={switchPage} />
232-
<div className='flex items-center justify-between mt-2 text-sm dark:text-gray-300'>
236+
<div className='flex items-center justify-between mt-2 sm:text-sm text-xs dark:text-gray-300'>
233237
<div>
234238
{totalHit > 0 && (
235239
<p>
236240
共搜索到 {totalHit} 条结果,用时 {useTime} 毫秒
237241
</p>
238242
)}
239243
</div>
240-
<div className="text-gray-600 text-right">
244+
<div className="text-gray-600 dark:text-gray-300 text-right">
241245
<span >
242246
<i className="fa-brands fa-algolia"></i> Algolia 提供搜索服务
243247
</span>
@@ -294,7 +298,7 @@ function Pagination(props) {
294298
{Array.from({ length: totalPage }, (_, i) => {
295299
const classNames = page === i
296300
? 'font-bold text-white bg-blue-600 dark:bg-yellow-600 rounded'
297-
: 'hover:text-blue-600 hover:font-bold'
301+
: 'hover:text-blue-600 hover:font-bold dark:text-gray-300'
298302

299303
return (
300304
<div

0 commit comments

Comments
 (0)