Skip to content

Commit 6a4a776

Browse files
committed
Implement search
1 parent 1926a85 commit 6a4a776

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

resources/css/custom.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ svg .secondary {
4040
nav.dashboard-nav a.active {
4141
@apply border-green-primary text-green-primary;
4242
}
43+
44+
a > span > em {
45+
@apply bg-green-light;
46+
}

resources/js/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "cookieconsent/build/cookieconsent.min.css";
88
import "choices.js/public/assets/styles/choices.css";
99

1010
require('./bootstrap');
11+
require('./search');
1112

1213
// Initialise cookie consent.
1314
document.addEventListener('DOMContentLoaded', () => {

resources/js/search.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import algoliasearch from 'algoliasearch/lite';
2+
3+
const client = algoliasearch(process.env.MIX_ALGOLIA_APP_ID, process.env.MIX_ALGOLIA_SECRET)
4+
const index = client.initIndex(process.env.MIX_ALGOLIA_INDEX)
5+
6+
window.search = (event) => {
7+
// If the input is empty, return no results.
8+
if (event.target.value.length === 0) {
9+
return Promise.resolve({ hits: [] })
10+
}
11+
12+
// Perform the search using the provided input.
13+
return index.search(event.target.value, {
14+
hitsPerPage: 5,
15+
attributesToSnippet: [
16+
'body:10'
17+
]
18+
});
19+
}

resources/views/layouts/_nav.blade.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,38 @@
6262
</div>
6363
</div>
6464
<div class="flex-1 flex items-center justify-center px-2 lg:ml-6 lg:justify-end">
65-
<div class="max-w-lg w-full lg:max-w-xs">
66-
<form action="{{ route('forum') }}" method="GET">
67-
<label for="search" class="sr-only">Search</label>
68-
<div class="relative">
69-
<div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
70-
<svg class="h-5 w-5 text-gray-400" fill="currentColor" viewBox="0 0 20 20">
71-
<path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
72-
</svg>
73-
</div>
74-
<input type="text" name="search" id="search" class="nav-search block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md leading-5 bg-white placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:border-blue-300 focus:shadow-outline-blue sm:text-sm transition duration-150 ease-in-out" placeholder="Search for threads..." />
65+
<div class="max-w-lg w-full lg:max-w-xs" x-data="{ results: [] }">
66+
<label for="search" class="sr-only">Search</label>
67+
<div class="relative">
68+
<div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
69+
<svg class="h-5 w-5 text-gray-400" fill="currentColor" viewBox="0 0 20 20">
70+
<path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
71+
</svg>
7572
</div>
76-
</form>
73+
<input
74+
@click.away="results = []"
75+
@keyup="search(event).then(function({ hits }) { results = hits; })"
76+
type="text"
77+
name="search"
78+
id="search"
79+
class="nav-search block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md leading-5 bg-white placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:border-blue-300 focus:shadow-outline-blue sm:text-sm transition duration-150 ease-in-out"
80+
placeholder="Search for threads..."
81+
/>
82+
<template x-if="results.length > 0">
83+
<div x-transition:enter="transition ease-out duration-100" x-transition:enter-start="transform opacity-0 scale-95" x-transition:enter-end="transform opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="transform opacity-100 scale-100" x-transition:leave-end="transform opacity-0 scale-95" class="mt-2 origin-top-right absolute right-0 w-full rounded-md shadow-lg">
84+
<div class="rounded-md bg-white shadow-xs w-full">
85+
<div class="py-1">
86+
<template x-for="result in results" :key="result.subject">
87+
<a :href="result.url" class="block px-4 py-2 text-base leading-5 text-gray-700 hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus:bg-gray-100 focus:text-gray-900">
88+
<span class="block font-bold" x-html="result._highlightResult.subject.value"></span>
89+
<span class="block text-sm txt-gray-400" x-html="result._snippetResult.body.value"></span>
90+
</a>
91+
</template>
92+
</div>
93+
</div>
94+
</div>
95+
</template>
96+
</div>
7797
</div>
7898
</div>
7999
<div class="flex items-center lg:hidden">

tailwind.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
extend: {
66
colors: {
77
green: {
8+
light: '#baebe1',
89
primary: '#18bc9c',
910
dark: '#15a589',
1011
darker: '#12826c',

0 commit comments

Comments
 (0)