AlgoliaSearchBox.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <form
  3. id="search-form"
  4. class="algolia-search-wrapper search-box"
  5. role="search"
  6. >
  7. <input
  8. id="algolia-search-input"
  9. class="search-query"
  10. :placeholder="placeholder"
  11. >
  12. </form>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'AlgoliaSearchBox',
  17. props: ['options'],
  18. data () {
  19. return {
  20. placeholder: undefined
  21. }
  22. },
  23. watch: {
  24. $lang (newValue) {
  25. this.update(this.options, newValue)
  26. },
  27. options (newValue) {
  28. this.update(newValue, this.$lang)
  29. }
  30. },
  31. mounted () {
  32. this.initialize(this.options, this.$lang)
  33. this.placeholder = this.$site.themeConfig.searchPlaceholder || ''
  34. },
  35. methods: {
  36. initialize (userOptions, lang) {
  37. Promise.all([
  38. import(/* webpackChunkName: "docsearch" */ 'docsearch.js/dist/cdn/docsearch.min.js'),
  39. import(/* webpackChunkName: "docsearch" */ 'docsearch.js/dist/cdn/docsearch.min.css')
  40. ]).then(([docsearch]) => {
  41. docsearch = docsearch.default
  42. const { algoliaOptions = {}} = userOptions
  43. docsearch(Object.assign(
  44. {},
  45. userOptions,
  46. {
  47. inputSelector: '#algolia-search-input',
  48. // #697 Make docsearch work well at i18n mode.
  49. algoliaOptions: {
  50. ...algoliaOptions
  51. },
  52. handleSelected: (input, event, suggestion) => {
  53. const { pathname, hash } = new URL(suggestion.url)
  54. const routepath = pathname.replace(this.$site.base, '/')
  55. const _hash = decodeURIComponent(hash)
  56. this.$router.push(`${routepath}${_hash}`)
  57. }
  58. }
  59. ))
  60. })
  61. },
  62. update (options, lang) {
  63. this.$el.innerHTML = '<input id="algolia-search-input" class="search-query">'
  64. this.initialize(options, lang)
  65. }
  66. }
  67. }
  68. </script>
  69. <style lang="stylus">
  70. .algolia-search-wrapper
  71. & > span
  72. vertical-align middle
  73. .algolia-autocomplete
  74. line-height normal
  75. .ds-dropdown-menu
  76. background-color #fff
  77. border 1px solid #ccc
  78. border-radius 4px
  79. font-size 16px
  80. max-width 800px
  81. min-width 650px
  82. margin 6px 0 0
  83. padding 4px
  84. text-align left
  85. &:before
  86. border-color #ccc
  87. [class*=ds-dataset-]
  88. border none
  89. padding 0
  90. .ds-suggestions
  91. margin-top 0
  92. .ds-suggestion
  93. border-bottom 1px solid $borderColor
  94. .algolia-docsearch-suggestion--highlight
  95. color #3eb07c
  96. .algolia-docsearch-suggestion
  97. border-color $borderColor
  98. padding 0
  99. .algolia-docsearch-suggestion--category-header
  100. padding 5px 10px
  101. margin-top 0
  102. background $accentColor
  103. color #fff
  104. font-weight 600
  105. .algolia-docsearch-suggestion--highlight
  106. background rgba(255, 255, 255, 0.6)
  107. .algolia-docsearch-suggestion--wrapper
  108. padding 0
  109. .algolia-docsearch-suggestion--title
  110. font-weight 600
  111. margin-bottom 0
  112. color $textColor
  113. .algolia-docsearch-suggestion--subcategory-column
  114. vertical-align top
  115. padding 5px 7px 5px 5px
  116. border-color $borderColor
  117. background #f1f3f5
  118. &:after
  119. display none
  120. .algolia-docsearch-suggestion--subcategory-column-text
  121. color #555
  122. .algolia-docsearch-footer
  123. border-color $borderColor
  124. .ds-cursor .algolia-docsearch-suggestion--content
  125. background-color #e7edf3 !important
  126. color $textColor
  127. @media (min-width: $MQMobile)
  128. .algolia-search-wrapper
  129. .algolia-autocomplete
  130. .algolia-docsearch-suggestion
  131. .algolia-docsearch-suggestion--subcategory-column
  132. float none
  133. width 150px
  134. min-width 150px
  135. display table-cell
  136. .algolia-docsearch-suggestion--content
  137. float none
  138. display table-cell
  139. width 100%
  140. vertical-align top
  141. .ds-dropdown-menu
  142. min-width 515px !important
  143. @media (max-width: $MQMobile)
  144. .algolia-search-wrapper
  145. .ds-dropdown-menu
  146. min-width calc(100vw - 4rem) !important
  147. max-width calc(100vw - 4rem) !important
  148. .algolia-docsearch-suggestion--wrapper
  149. padding 5px 7px 5px 5px !important
  150. .algolia-docsearch-suggestion--subcategory-column
  151. padding 0 !important
  152. background white !important
  153. .algolia-docsearch-suggestion--subcategory-column-text:after
  154. content " > "
  155. font-size 10px
  156. line-height 14.4px
  157. display inline-block
  158. width 5px
  159. margin -3px 3px 0
  160. vertical-align middle
  161. </style>