SearchBox.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <div class="search-box">
  3. <input
  4. ref="input"
  5. aria-label="Search"
  6. :value="query"
  7. :class="{ 'focused': focused }"
  8. :placeholder="placeholder"
  9. autocomplete="off"
  10. spellcheck="false"
  11. @input="query = $event.target.value"
  12. @focus="focused = true"
  13. @blur="focused = false"
  14. @keyup.enter="go(focusIndex)"
  15. @keyup.up="onUp"
  16. @keyup.down="onDown"
  17. >
  18. <ul
  19. v-if="showSuggestions"
  20. class="suggestions"
  21. :class="{ 'align-right': alignRight }"
  22. @mouseleave="unfocus"
  23. >
  24. <li
  25. v-for="(s, i) in suggestions"
  26. :key="i"
  27. class="suggestion"
  28. :class="{ focused: i === focusIndex }"
  29. @mousedown="go(i)"
  30. @mouseenter="focus(i)"
  31. >
  32. <a
  33. :href="s.path"
  34. @click.prevent
  35. >
  36. <span class="page-title">{{ s.title || s.path }}</span>
  37. <span
  38. v-if="s.header"
  39. class="header"
  40. >&gt; {{ s.header.title }}</span>
  41. </a>
  42. </li>
  43. </ul>
  44. </div>
  45. </template>
  46. <script>
  47. import matchQuery from './match-query'
  48. /* global SEARCH_MAX_SUGGESTIONS, SEARCH_PATHS, SEARCH_HOTKEYS */
  49. export default {
  50. name: 'SearchBox',
  51. data () {
  52. return {
  53. query: '',
  54. focused: false,
  55. focusIndex: 0,
  56. placeholder: undefined
  57. }
  58. },
  59. computed: {
  60. showSuggestions () {
  61. return (
  62. this.focused
  63. && this.suggestions
  64. && this.suggestions.length
  65. )
  66. },
  67. suggestions () {
  68. const query = this.query.trim().toLowerCase()
  69. if (!query) {
  70. return
  71. }
  72. const { pages } = this.$site
  73. const max = this.$site.themeConfig.searchMaxSuggestions || SEARCH_MAX_SUGGESTIONS
  74. const localePath = this.$localePath
  75. const res = []
  76. for (let i = 0; i < pages.length; i++) {
  77. if (res.length >= max) break
  78. const p = pages[i]
  79. // filter out results that do not match current locale
  80. if (this.getPageLocalePath(p) !== localePath) {
  81. continue
  82. }
  83. // filter out results that do not match searchable paths
  84. if (!this.isSearchable(p)) {
  85. continue
  86. }
  87. if (matchQuery(query, p)) {
  88. res.push(p)
  89. } else if (p.headers) {
  90. for (let j = 0; j < p.headers.length; j++) {
  91. if (res.length >= max) break
  92. const h = p.headers[j]
  93. if (h.title && matchQuery(query, p, h.title)) {
  94. res.push(Object.assign({}, p, {
  95. path: p.path + '#' + h.slug,
  96. header: h
  97. }))
  98. }
  99. }
  100. }
  101. }
  102. return res
  103. },
  104. // make suggestions align right when there are not enough items
  105. alignRight () {
  106. const navCount = (this.$site.themeConfig.nav || []).length
  107. const repo = this.$site.repo ? 1 : 0
  108. return navCount + repo <= 2
  109. }
  110. },
  111. mounted () {
  112. this.placeholder = this.$site.themeConfig.searchPlaceholder || ''
  113. document.addEventListener('keydown', this.onHotkey)
  114. },
  115. beforeDestroy () {
  116. document.removeEventListener('keydown', this.onHotkey)
  117. },
  118. methods: {
  119. getPageLocalePath (page) {
  120. for (const localePath in this.$site.locales || {}) {
  121. if (localePath !== '/' && page.path.indexOf(localePath) === 0) {
  122. return localePath
  123. }
  124. }
  125. return '/'
  126. },
  127. isSearchable (page) {
  128. let searchPaths = SEARCH_PATHS
  129. // all paths searchables
  130. if (searchPaths === null) { return true }
  131. searchPaths = Array.isArray(searchPaths) ? searchPaths : new Array(searchPaths)
  132. return searchPaths.filter(path => {
  133. return page.path.match(path)
  134. }).length > 0
  135. },
  136. onHotkey (event) {
  137. if (event.srcElement === document.body && SEARCH_HOTKEYS.includes(event.key)) {
  138. this.$refs.input.focus()
  139. event.preventDefault()
  140. }
  141. },
  142. onUp () {
  143. if (this.showSuggestions) {
  144. if (this.focusIndex > 0) {
  145. this.focusIndex--
  146. } else {
  147. this.focusIndex = this.suggestions.length - 1
  148. }
  149. }
  150. },
  151. onDown () {
  152. if (this.showSuggestions) {
  153. if (this.focusIndex < this.suggestions.length - 1) {
  154. this.focusIndex++
  155. } else {
  156. this.focusIndex = 0
  157. }
  158. }
  159. },
  160. go (i) {
  161. if (!this.showSuggestions) {
  162. return
  163. }
  164. this.$router.push(this.suggestions[i].path)
  165. this.query = ''
  166. this.focusIndex = 0
  167. },
  168. focus (i) {
  169. this.focusIndex = i
  170. },
  171. unfocus () {
  172. this.focusIndex = -1
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="stylus">
  178. .search-box
  179. display inline-block
  180. position relative
  181. margin-right 1rem
  182. input
  183. cursor text
  184. width 10rem
  185. height: 2rem
  186. color lighten($textColor, 25%)
  187. display inline-block
  188. border 1px solid darken($borderColor, 10%)
  189. border-radius 2rem
  190. font-size 0.9rem
  191. line-height 2rem
  192. padding 0 0.5rem 0 2rem
  193. outline none
  194. transition all .2s ease
  195. background #fff url(search.svg) 0.6rem 0.5rem no-repeat
  196. background-size 1rem
  197. &:focus
  198. cursor auto
  199. border-color $accentColor
  200. .suggestions
  201. background #fff
  202. width 20rem
  203. position absolute
  204. top 2 rem
  205. border 1px solid darken($borderColor, 10%)
  206. border-radius 6px
  207. padding 0.4rem
  208. list-style-type none
  209. &.align-right
  210. right 0
  211. .suggestion
  212. line-height 1.4
  213. padding 0.4rem 0.6rem
  214. border-radius 4px
  215. cursor pointer
  216. a
  217. white-space normal
  218. color lighten($textColor, 35%)
  219. .page-title
  220. font-weight 600
  221. .header
  222. font-size 0.9em
  223. margin-left 0.25em
  224. &.focused
  225. background-color #f3f4f5
  226. a
  227. color $accentColor
  228. @media (max-width: $MQNarrow)
  229. .search-box
  230. input
  231. cursor pointer
  232. width 0
  233. border-color transparent
  234. position relative
  235. &:focus
  236. cursor text
  237. left 0
  238. width 10rem
  239. // Match IE11
  240. @media all and (-ms-high-contrast: none)
  241. .search-box input
  242. height 2rem
  243. @media (max-width: $MQNarrow) and (min-width: $MQMobile)
  244. .search-box
  245. .suggestions
  246. left 0
  247. @media (max-width: $MQMobile)
  248. .search-box
  249. margin-right 0
  250. input
  251. left 1rem
  252. .suggestions
  253. right 0
  254. @media (max-width: $MQMobileNarrow)
  255. .search-box
  256. .suggestions
  257. width calc(100vw - 4rem)
  258. input:focus
  259. width 8rem
  260. </style>