AlgoliaSearchBox.vue 4.4 KB

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