QuickSearch.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <template>
  2. <div class="quick-search">
  3. <div class="search-container">
  4. <input
  5. ref="searchInput"
  6. v-model="searchQuery"
  7. type="text"
  8. class="search-input"
  9. :placeholder="placeholder"
  10. @input="onSearch"
  11. @focus="onFocus"
  12. @blur="onBlur"
  13. @keydown="onKeydown"
  14. />
  15. <div class="search-icon">
  16. <HugeiconsIcon :icon="SearchIcon" width="1em" height="1em" />
  17. </div>
  18. </div>
  19. <div v-if="showResults && filteredItems.length > 0" class="search-results">
  20. <div
  21. v-for="(item, index) in filteredItems"
  22. :key="item.id"
  23. :class="['search-result-item', { active: selectedIndex === index }]"
  24. @click="selectItem(item)"
  25. @mouseenter="selectedIndex = index"
  26. >
  27. <div class="result-content">
  28. <div class="result-title" v-html="highlightText(item.title, searchQuery)"></div>
  29. <div v-if="item.description" class="result-description" v-html="highlightText(item.description, searchQuery)"></div>
  30. <div v-if="item.category" class="result-category">{{ item.category }}</div>
  31. </div>
  32. <div v-if="item.icon" class="result-icon">
  33. <HugeiconsIcon :icon="item.icon" width="1.2em" height="1.2em" />
  34. </div>
  35. </div>
  36. </div>
  37. <div v-if="showResults && searchQuery && filteredItems.length === 0" class="no-results">
  38. <div class="no-results-content">
  39. <HugeiconsIcon :icon="SearchRemoveIcon" width="2em" height="2em" />
  40. <p>No results found for "{{ searchQuery }}"</p>
  41. </div>
  42. </div>
  43. </div>
  44. </template>
  45. <script setup>
  46. import { ref, computed, watch, nextTick, onMounted, onUnmounted } from 'vue'
  47. import { useRouter } from 'vue-router'
  48. import { HugeiconsIcon } from '@hugeicons/vue'
  49. import { SearchIcon } from '@hugeicons/core-free-icons'
  50. import { SearchRemoveIcon } from '@hugeicons/core-free-icons'
  51. import { ViewIcon } from '@hugeicons/core-free-icons'
  52. const props = defineProps({
  53. placeholder: {
  54. type: String,
  55. default: 'Search...'
  56. },
  57. items: {
  58. type: Array,
  59. default: () => []
  60. },
  61. searchFields: {
  62. type: Array,
  63. default: () => ['title', 'description']
  64. },
  65. maxResults: {
  66. type: Number,
  67. default: 10
  68. },
  69. debounceMs: {
  70. type: Number,
  71. default: 300
  72. },
  73. enableGlobalShortcut: {
  74. type: Boolean,
  75. default: true
  76. },
  77. autoImportRoutes: {
  78. type: Boolean,
  79. default: true
  80. }
  81. })
  82. const emit = defineEmits(['select', 'search', 'focus', 'blur'])
  83. const router = useRouter()
  84. const searchInput = ref(null)
  85. const searchQuery = ref('')
  86. const showResults = ref(false)
  87. const selectedIndex = ref(-1)
  88. const items = ref([...props.items])
  89. const debounceTimer = ref(null)
  90. function importRoutesFromRouter() {
  91. const routeItems = router.getRoutes()
  92. .filter(route => route.name) // Exclude unnamed routes
  93. .map(route => ({
  94. id: `route-${route.name}`,
  95. title: route.meta.title || route.name,
  96. description: `Navigate to ${route.path}`,
  97. category: 'Navigation',
  98. path: route.path,
  99. icon: route.meta.icon || ViewIcon,
  100. type: 'route'
  101. }))
  102. // Add the items to the search
  103. routeItems.forEach(item => addItem(item))
  104. }
  105. const filteredItems = computed(() => {
  106. if (!searchQuery.value.trim()) {
  107. return []
  108. }
  109. const query = searchQuery.value.toLowerCase()
  110. const filtered = items.value.filter(item => {
  111. return props.searchFields.some(field => {
  112. const value = item[field]
  113. return value && value.toLowerCase().includes(query)
  114. })
  115. })
  116. return filtered.slice(0, props.maxResults)
  117. })
  118. function handleGlobalKeydown(event) {
  119. // Check if Ctrl+K is pressed
  120. if (event.ctrlKey && event.key === 'k') {
  121. event.preventDefault()
  122. // Don't trigger if user is typing in an input/textarea
  123. if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') {
  124. return
  125. }
  126. focus()
  127. }
  128. }
  129. function onSearch() {
  130. clearTimeout(debounceTimer.value)
  131. debounceTimer.value = setTimeout(() => {
  132. selectedIndex.value = -1
  133. showResults.value = true
  134. emit('search', searchQuery.value)
  135. }, props.debounceMs)
  136. }
  137. function onFocus() {
  138. showResults.value = true
  139. emit('focus')
  140. }
  141. function onBlur() {
  142. // Delay hiding results to allow for click events
  143. setTimeout(() => {
  144. showResults.value = false
  145. selectedIndex.value = -1
  146. emit('blur')
  147. }, 150)
  148. }
  149. function onKeydown(event) {
  150. if (!showResults.value || filteredItems.value.length === 0) return
  151. switch (event.key) {
  152. case 'ArrowDown':
  153. event.preventDefault()
  154. selectedIndex.value = Math.min(selectedIndex.value + 1, filteredItems.value.length - 1)
  155. break
  156. case 'ArrowUp':
  157. event.preventDefault()
  158. selectedIndex.value = Math.max(selectedIndex.value - 1, -1)
  159. break
  160. case 'Enter':
  161. event.preventDefault()
  162. if (selectedIndex.value >= 0) {
  163. selectItem(filteredItems.value[selectedIndex.value])
  164. }
  165. break
  166. case 'Escape':
  167. showResults.value = false
  168. selectedIndex.value = -1
  169. searchInput.value.blur()
  170. break
  171. }
  172. }
  173. function selectItem(item) {
  174. switch(item.type) {
  175. case 'route':
  176. router.push({ path: item.path })
  177. break
  178. case 'callback':
  179. item.callback()
  180. break
  181. default:
  182. emit('select', item)
  183. }
  184. searchQuery.value = ''
  185. showResults.value = false
  186. selectedIndex.value = -1
  187. }
  188. function highlightText(text, query) {
  189. if (!query || !text) return text
  190. const regex = new RegExp(`(${query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi')
  191. return text.replace(regex, '<mark>$1</mark>')
  192. }
  193. // API Methods
  194. function addItem(item) {
  195. const existingIndex = items.value.findIndex(i => i.id === item.id)
  196. if (existingIndex >= 0) {
  197. items.value[existingIndex] = { ...item }
  198. } else {
  199. items.value.push({ ...item })
  200. }
  201. }
  202. function removeItem(itemId) {
  203. items.value = items.value.filter(item => item.id !== itemId)
  204. }
  205. function clearItems() {
  206. items.value = []
  207. }
  208. function getItems() {
  209. return [...items.value]
  210. }
  211. function setItems(newItems) {
  212. items.value = [...newItems]
  213. }
  214. function focus() {
  215. searchInput.value?.focus()
  216. }
  217. function blur() {
  218. searchInput.value?.blur()
  219. }
  220. function clear() {
  221. searchQuery.value = ''
  222. showResults.value = false
  223. selectedIndex.value = -1
  224. }
  225. function refreshRoutes() {
  226. if (props.autoImportRoutes) {
  227. importRoutesFromRouter()
  228. }
  229. }
  230. // Global shortcut management
  231. onMounted(() => {
  232. if (props.enableGlobalShortcut) {
  233. document.addEventListener('keydown', handleGlobalKeydown)
  234. }
  235. // Auto-import routes on mount
  236. if (props.autoImportRoutes) {
  237. importRoutesFromRouter()
  238. }
  239. })
  240. onUnmounted(() => {
  241. if (props.enableGlobalShortcut) {
  242. document.removeEventListener('keydown', handleGlobalKeydown)
  243. }
  244. })
  245. // Watch for external items changes
  246. watch(() => props.items, (newItems) => {
  247. items.value = [...newItems]
  248. }, { deep: true })
  249. defineExpose({
  250. addItem,
  251. removeItem,
  252. clearItems,
  253. getItems,
  254. setItems,
  255. focus,
  256. blur,
  257. clear,
  258. refreshRoutes,
  259. searchQuery,
  260. filteredItems,
  261. showResults
  262. })
  263. </script>
  264. <style scoped>
  265. .quick-search {
  266. position: relative;
  267. width: 100%;
  268. max-width: 400px;
  269. }
  270. .search-container {
  271. position: relative;
  272. display: flex;
  273. align-items: center;
  274. }
  275. .search-input {
  276. width: 100%;
  277. padding: 0.4em;
  278. border: none;
  279. border-radius: .4em;
  280. font-size: 1rem;
  281. outline: none;
  282. transition: all 0.2s ease;
  283. background-color: #666;
  284. color: #fff;
  285. }
  286. .search-input::placeholder {
  287. color: #bbb;
  288. }
  289. .search-input:focus {
  290. border-color: #3b82f6;
  291. box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
  292. }
  293. .search-icon {
  294. position: absolute;
  295. right: 0.75rem;
  296. color: #6b7280;
  297. pointer-events: none;
  298. }
  299. .search-results {
  300. position: absolute;
  301. top: 100%;
  302. left: 0;
  303. right: 0;
  304. background: #fff;
  305. border: 2px solid #e1e5e9;
  306. border-top: none;
  307. border-radius: 0 0 8px 8px;
  308. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  309. max-height: 300px;
  310. overflow-y: auto;
  311. z-index: 1000;
  312. }
  313. .search-result-item {
  314. display: flex;
  315. align-items: center;
  316. padding: 0.75rem 1rem;
  317. cursor: pointer;
  318. transition: background-color 0.15s ease;
  319. border-bottom: 1px solid #f3f4f6;
  320. }
  321. .search-result-item:last-child {
  322. border-bottom: none;
  323. }
  324. .search-result-item:hover,
  325. .search-result-item.active {
  326. background-color: #f8fafc;
  327. }
  328. .result-content {
  329. flex: 1;
  330. min-width: 0;
  331. }
  332. .result-title {
  333. font-weight: 500;
  334. color: #1f2937;
  335. margin-bottom: 0.25rem;
  336. }
  337. .result-description {
  338. font-size: 0.875rem;
  339. color: #6b7280;
  340. margin-bottom: 0.25rem;
  341. }
  342. .result-category {
  343. font-size: 0.75rem;
  344. color: #9ca3af;
  345. text-transform: uppercase;
  346. letter-spacing: 0.05em;
  347. }
  348. .result-icon {
  349. margin-left: 0.75rem;
  350. color: #6b7280;
  351. flex-shrink: 0;
  352. }
  353. .no-results {
  354. position: absolute;
  355. top: 100%;
  356. left: 0;
  357. right: 0;
  358. background: #fff;
  359. border: 2px solid #e1e5e9;
  360. border-top: none;
  361. border-radius: 0 0 8px 8px;
  362. box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  363. z-index: 1000;
  364. }
  365. .no-results-content {
  366. display: flex;
  367. flex-direction: column;
  368. align-items: center;
  369. padding: 2rem 1rem;
  370. color: #6b7280;
  371. text-align: center;
  372. }
  373. .no-results-content p {
  374. margin: 0.5rem 0 0 0;
  375. font-size: 0.875rem;
  376. }
  377. mark {
  378. background-color: #fef3c7;
  379. color: #92400e;
  380. padding: 0;
  381. border-radius: 2px;
  382. }
  383. /* Dark theme */
  384. @media (prefers-color-scheme: dark) {
  385. .search-input {
  386. background-color: #1f2937;
  387. border-color: #374151;
  388. color: #f9fafb;
  389. }
  390. .search-input:focus {
  391. border-color: #60a5fa;
  392. box-shadow: 0 0 0 3px rgba(96, 165, 250, 0.1);
  393. }
  394. .search-icon {
  395. color: #9ca3af;
  396. }
  397. .search-results {
  398. background: #1f2937;
  399. border-color: #374151;
  400. }
  401. .search-result-item {
  402. border-bottom-color: #374151;
  403. }
  404. .search-result-item:hover,
  405. .search-result-item.active {
  406. background-color: #374151;
  407. }
  408. .result-title {
  409. color: #f9fafb;
  410. }
  411. .result-description {
  412. color: #d1d5db;
  413. }
  414. .result-category {
  415. color: #9ca3af;
  416. }
  417. .result-icon {
  418. color: #9ca3af;
  419. }
  420. .no-results {
  421. background: #1f2937;
  422. border-color: #374151;
  423. }
  424. .no-results-content {
  425. color: #9ca3af;
  426. }
  427. mark {
  428. background-color: #451a03;
  429. color: #fbbf24;
  430. }
  431. }
  432. /* Responsive */
  433. @media (max-width: 640px) {
  434. .quick-search {
  435. max-width: 100%;
  436. }
  437. .search-results {
  438. max-height: 250px;
  439. }
  440. }
  441. </style>