Skip to content

Commit eb1593f

Browse files
authored
Merge pull request #6 from srsng/main
home search box
2 parents af71e41 + 27dcb87 commit eb1593f

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ npm install
1111
```shell
1212
npm run docs:dev
1313
```
14+
1415
浏览器打开 <http://localhost:5173>
1516

1617
## 部署
@@ -20,7 +21,7 @@ npm run docs:build
2021
```
2122

2223
本地预览,执行下列命令后,生成一个本地静态 Web 服务 <http://localhost:4173>,该服务以 .vitepress/dist 作为源文件
24+
2325
```shell
2426
npm run docs:preview
2527
```
26-
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<!-- .vitepress/theme/components/SearchBox.vue -->
2+
<template>
3+
<div class="custom-search">
4+
<button
5+
ref="searchButton"
6+
class="search-button"
7+
@click="openSearch"
8+
aria-label="搜索文档"
9+
>
10+
<svg
11+
xmlns="http://www.w3.org/2000/svg"
12+
viewBox="0 0 24 24"
13+
fill="none"
14+
stroke="currentColor"
15+
stroke-width="2"
16+
>
17+
<circle cx="11" cy="11" r="8"></circle>
18+
<path d="M21 21l-4.35-4.35"></path>
19+
</svg>
20+
<span class="DocSearch-Button-Placeholder">搜索文档...</span>
21+
</button>
22+
</div>
23+
</template>
24+
25+
<script setup>
26+
const openSearch = async () => {
27+
const ele = document.getElementById("local-search").querySelector("button");
28+
console.log("search", ele);
29+
ele.click();
30+
};
31+
</script>
32+
33+
<style scoped>
34+
.custom-search {
35+
max-width: 580px;
36+
width: 100%;
37+
margin: 0 auto 32px;
38+
padding: 0 24px;
39+
}
40+
41+
.search-button {
42+
display: flex;
43+
align-items: center;
44+
width: 100%;
45+
padding: 10px 16px;
46+
background: var(--vp-c-bg-alt);
47+
border: 1px solid var(--vp-c-divider);
48+
border-radius: 8px;
49+
cursor: pointer;
50+
transition: all 0.25s ease;
51+
box-shadow: var(--vp-shadow-1);
52+
text-align: left;
53+
}
54+
55+
.search-button:hover {
56+
background: var(--vp-c-bg-soft);
57+
border-color: var(--vp-c-brand);
58+
box-shadow: var(--vp-shadow-2);
59+
}
60+
61+
.search-button:active {
62+
transform: translateY(1px);
63+
}
64+
65+
.search-button svg {
66+
width: 18px;
67+
height: 18px;
68+
margin-right: 12px;
69+
color: var(--vp-c-text-2);
70+
transition: color 0.25s ease;
71+
}
72+
73+
.search-button:hover svg {
74+
color: var(--vp-c-brand);
75+
}
76+
77+
.search-button span {
78+
font-size: 15px;
79+
color: var(--vp-c-text-2);
80+
transition: color 0.25s ease;
81+
}
82+
83+
.search-button:hover span {
84+
color: var(--vp-c-text-1);
85+
}
86+
87+
/* 在首页的特殊样式 */
88+
.VPHero.has-image .custom-search {
89+
margin-top: 24px;
90+
margin-bottom: 48px;
91+
}
92+
93+
/* 响应式调整 */
94+
@media (max-width: 768px) {
95+
.custom-search {
96+
padding: 0 16px;
97+
margin-bottom: 24px;
98+
}
99+
100+
.search-button {
101+
padding: 8px 14px;
102+
}
103+
104+
.search-button span {
105+
font-size: 14px;
106+
}
107+
}
108+
</style>

docs/.vitepress/theme/layout.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script setup>
22
import DefaultTheme from "vitepress/theme";
3+
import SearchBox from "./components/SearchBox.vue";
34
45
const { Layout } = DefaultTheme;
56
</script>
@@ -8,5 +9,8 @@ const { Layout } = DefaultTheme;
89
<Layout>
910
<!-- <template #not-found> 404 not-found </template> -->
1011
<!-- <template #home-hero-image> </template> -->
12+
<template #home-hero-after>
13+
<SearchBox></SearchBox>
14+
</template>
1115
</Layout>
1216
</template>

0 commit comments

Comments
 (0)