|
| 1 | +<div x-data="{ |
| 2 | + init(){ |
| 3 | + window.axios('/index.json') |
| 4 | + .then(response => { |
| 5 | + this.fuse = new window.Fuse(response.data, { |
| 6 | + minMatchCharLength: 6, |
| 7 | + keys: ['title', 'snippet', 'categories'], |
| 8 | + }); |
| 9 | + }); |
| 10 | + }, |
| 11 | + get results() { |
| 12 | + return this.query ? this.fuse.search(this.query) : []; |
| 13 | + }, |
| 14 | + get isQuerying() { |
| 15 | + return Boolean( this.query ); |
| 16 | + }, |
| 17 | + fuse: null, |
| 18 | + searching: false, |
| 19 | + query: '', |
| 20 | + showInput() { |
| 21 | + this.searching = true; |
| 22 | + this.$nextTick(() => { |
| 23 | + this.$refs.search.focus(); |
| 24 | + }) |
| 25 | + }, |
| 26 | + reset() { |
| 27 | + this.query = ''; |
| 28 | + this.searching = false; |
| 29 | + }, |
| 30 | + }" |
| 31 | + x-cloak |
| 32 | + class="flex flex-1 justify-end items-center text-right px-4" |
| 33 | +> |
| 34 | + <div |
| 35 | + class="absolute md:relative w-full justify-end bg-white left-0 top-0 z-10 mt-7 md:mt-0 px-4 md:px-0" |
| 36 | + :class="{'hidden md:flex': ! searching}" |
| 37 | + > |
| 38 | + <label for="search" class="hidden">Search</label> |
| 39 | + |
| 40 | + <input |
| 41 | + id="search" |
| 42 | + x-model="query" |
| 43 | + x-ref="search" |
| 44 | + class="relative block h-10 w-full lg:w-1/2 lg:focus:w-3/4 bg-gray-100 border border-gray-500 focus:border-blue-400 outline-none cursor-pointer text-gray-700 px-4 pb-0 pt-px transition-all duration-200 ease-out bg-[url('/assets/img/magnifying-glass.svg')] bg-no-repeat bg-[0.8rem] indent-[1.2em]" |
| 45 | + :class="{ 'rounded-b-none rounded-t-lg': query, 'rounded-3xl': !query }" |
| 46 | + autocomplete="off" |
| 47 | + name="search" |
| 48 | + placeholder="Search" |
| 49 | + type="text" |
| 50 | + @keyup.esc="reset" |
| 51 | + @blur="reset" |
| 52 | + > |
| 53 | + |
| 54 | + <button |
| 55 | + x-show="query || searching" |
| 56 | + class="absolute top-0 right-0 leading-snug font-400 text-3xl text-blue-500 hover:text-blue-600 focus:outline-none pr-7 md:pr-3" |
| 57 | + @click="reset" |
| 58 | + >×</button> |
| 59 | + |
| 60 | + <div |
| 61 | + x-show="isQuerying" |
| 62 | + x-cloak |
| 63 | + x-transition:enter="transition ease-out duration-300" |
| 64 | + x-transition:enter-start="opacity-0" |
| 65 | + x-transition:enter-end="opacity-100" |
| 66 | + x-transition:leave="transition-none" |
| 67 | + x-transition:leave-start="opacity-100" |
| 68 | + x-transition:leave-end="opacity-0" |
| 69 | + class="absolute left-0 right-0 md:inset-auto w-full lg:w-3/4 text-left mb-4 md:mt-10" |
| 70 | + > |
| 71 | + <div class="flex flex-col bg-white border border-b-0 border-t-0 border-blue-400 rounded-b-lg shadow-search mx-4 md:mx-0"> |
| 72 | + <template x-for="(result, index) in results"> |
| 73 | + <a |
| 74 | + class="bg-white hover:bg-blue-100 border-b border-blue-400 text-xl cursor-pointer p-4" |
| 75 | + :class="{ 'rounded-b-lg': (index === results.length - 1) }" |
| 76 | + :href="result.item.link" |
| 77 | + :title="result.item.title" |
| 78 | + :key="result.link" |
| 79 | + @mousedown.prevent |
| 80 | + > |
| 81 | + <span x-html="result.item.title"></span> |
| 82 | + |
| 83 | + <span class="block font-normal text-gray-700 text-sm my-1" x-html="result.item.snippet"></span> |
| 84 | + </a> |
| 85 | + </template> |
| 86 | + <div |
| 87 | + x-show="! results.length" |
| 88 | + class="bg-white w-full hover:bg-blue-100 border-b border-blue-400 rounded-b-lg shadow cursor-pointer p-4" |
| 89 | + > |
| 90 | + <p class="my-0">No results for <strong x-html="query"></strong></p> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + |
| 96 | + <button |
| 97 | + title="Start searching" |
| 98 | + type="button" |
| 99 | + class="flex md:hidden bg-gray-100 hover:bg-blue-100 justify-center items-center border border-gray-500 rounded-full focus:outline-none h-10 px-3" |
| 100 | + @click.prevent="showInput" |
| 101 | + > |
| 102 | + <img src="/assets/img/magnifying-glass.svg" alt="search icon" class="h-4 w-4 max-w-none"> |
| 103 | + </button> |
| 104 | +</div> |
0 commit comments