@@ -8,39 +8,32 @@ type Props = {
8
8
const { placeholder = ' 搜索' } = Astro .props ;
9
9
---
10
10
11
- <astro-sarch>
12
- <div >
13
- <div class =" mt-2" >
14
- <div class =" flex items-center rounded-md bg-white pl-3 outline-1 -outline-offset-1 outline-gray-300 has-[input:focus-within]:outline-2 has-[input:focus-within]:-outline-offset-2 has-[input:focus-within]:outline-blue-600" >
15
- <SearchSVG class =" shrink-0 text-base text-gray-500 select-none sm:text-sm/6" />
16
- <input type =" text" class =" block min-w-0 grow py-1.5 pr-3 pl-1 text-base text-gray-900 placeholder:text-gray-400 focus:outline-none sm:text-sm/6" placeholder ={ placeholder } />
17
- <div class =" flex shrink-0 focus-within:relative" >
18
- <select class =" col-start-1 row-start-1 w-full appearance-none rounded-md py-1.5 pr-7 pl-3 text-base text-gray-500 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-blue-600 sm:text-sm/6" >
19
- <option >GitHub</option >
20
- <option >Google</option >
21
- </select >
22
- </div >
23
- </div >
11
+ <div class =" mt-2" >
12
+ <div class =" flex items-center rounded-md bg-white pl-3 outline-1 -outline-offset-1 outline-gray-300 has-[input:focus-within]:outline-2 has-[input:focus-within]:-outline-offset-2 has-[input:focus-within]:outline-blue-600" >
13
+ <SearchSVG class =" shrink-0 text-base text-gray-500 select-none sm:text-sm/6" />
14
+ <input data-astro-search-input type =" text" class =" block min-w-0 grow py-1.5 pr-3 pl-1 text-base text-gray-900 placeholder:text-gray-400 focus:outline-none sm:text-sm/6" placeholder ={ placeholder } />
15
+ <div class =" flex shrink-0 focus-within:relative" >
16
+ <select data-astro-search-select class =" col-start-1 row-start-1 w-full appearance-none rounded-md py-1.5 pr-7 pl-3 text-base text-gray-500 placeholder:text-gray-400 focus:outline-2 focus:-outline-offset-2 focus:outline-blue-600 sm:text-sm/6" >
17
+ <option >GitHub</option >
18
+ <option >Google</option >
19
+ </select >
20
+ <button data-astro-search-button class =" flex items-center text-white bg-blue-500 rounded-md px-2" >Search</button >
24
21
</div >
25
22
</div >
26
- </astro-sarch >
23
+ </div >
27
24
28
25
<script >
29
- // 为我们的新 HTML 自定义元素定义行为。
30
- class AstroSearch extends HTMLElement {
31
- connectedCallback() {
32
- const searchInput = this.querySelector('input') as HTMLInputElement;
33
- const select = this.querySelector('select');
34
- searchInput.addEventListener('keydown', (e) => {
35
- if (e.key === 'Enter') {
36
- window.open(select?.value === 'GitHub'
37
- ? `https://github.com/ckvv/ckvv.github.io/search?l=Markdown&q=${searchInput.value}`
38
- : `https://www.google.com/search?q=site:${window.location.host} ${searchInput.value}`);
39
- }
40
- });
41
- }
26
+ const searchInput = document.querySelector('[data-astro-search-input]') as HTMLInputElement;
27
+ const select = document.querySelector('[data-astro-search-select]') as HTMLSelectElement;
28
+ function search() {
29
+ window.open(select?.value === 'GitHub'
30
+ ? `https://github.com/ckvv/ckvv.github.io/search?l=Markdown&q=${searchInput.value}`
31
+ : `https://www.google.com/search?q=site:${window.location.host} ${searchInput.value}`);
32
+ }
33
+ searchInput.addEventListener('keyup', (e) => {
34
+ if (e.key === 'Enter') {
35
+ search();
42
36
}
43
-
44
- // 告诉浏览器将我们的 AstroHeart 类用于 <astro-heart> 元素。
45
- customElements.define('astro-sarch', AstroSearch);
37
+ });
38
+ (document.querySelector('[data-astro-search-button]') as HTMLButtonElement).addEventListener('click', search);
46
39
</script >
0 commit comments