vuepress-theme-reco/components/AlgoliaSearchBox.vue

171 lines
4.7 KiB
Vue
Raw Normal View History

2019-04-15 10:35:40 +08:00
<template>
<form
id="search-form"
class="algolia-search-wrapper search-box"
role="search"
>
2019-11-30 19:32:43 +08:00
<i class="iconfont reco-search"></i>
2019-04-15 10:35:40 +08:00
<input
id="algolia-search-input"
class="search-query"
2019-11-03 18:05:42 +08:00
:placeholder="placeholder"
2019-04-15 10:35:40 +08:00
>
</form>
</template>
<script>
export default {
props: ['options'],
2019-11-03 18:05:42 +08:00
data () {
return {
placeholder: undefined
}
},
2019-04-15 10:35:40 +08:00
mounted () {
this.initialize(this.options, this.$lang)
2019-11-03 18:05:42 +08:00
this.placeholder = this.$site.themeConfig.searchPlaceholder || ''
2019-04-15 10:35:40 +08:00
},
methods: {
initialize (userOptions, lang) {
Promise.all([
import(/* webpackChunkName: "docsearch" */ 'docsearch.js/dist/cdn/docsearch.min.js'),
import(/* webpackChunkName: "docsearch" */ 'docsearch.js/dist/cdn/docsearch.min.css')
]).then(([docsearch]) => {
docsearch = docsearch.default
const { algoliaOptions = {}} = userOptions
docsearch(Object.assign(
{},
userOptions,
{
inputSelector: '#algolia-search-input',
// #697 Make docsearch work well at i18n mode.
algoliaOptions: Object.assign({
'facetFilters': [`lang:${lang}`].concat(algoliaOptions.facetFilters || [])
2019-11-03 18:05:42 +08:00
}, algoliaOptions),
handleSelected: (input, event, suggestion) => {
const { pathname, hash } = new URL(suggestion.url)
this.$router.push(`${pathname}${hash}`)
}
2019-04-15 10:35:40 +08:00
}
))
})
},
update (options, lang) {
this.$el.innerHTML = '<input id="algolia-search-input" class="search-query">'
this.initialize(options, lang)
}
},
watch: {
$lang (newValue) {
this.update(this.options, newValue)
},
options (newValue) {
this.update(newValue, this.$lang)
}
}
}
</script>
<style lang="stylus">
@require '../styles/mode.styl'
2019-04-15 10:35:40 +08:00
.algolia-search-wrapper
& > span
vertical-align middle
.algolia-autocomplete
line-height normal
.ds-dropdown-menu
2019-11-30 19:32:43 +08:00
background-color var(--background-color)
border-radius $borderRadius
2019-11-12 22:25:53 +08:00
font-size 15px
2019-04-15 10:35:40 +08:00
margin 6px 0 0
padding 4px
text-align left
box-shadow var(--box-shadow)
2019-04-15 10:35:40 +08:00
&:before
2019-11-12 22:25:53 +08:00
display none
2019-04-15 10:35:40 +08:00
[class*=ds-dataset-]
2019-11-30 19:32:43 +08:00
background-color var(--background-color)
2019-04-15 10:35:40 +08:00
border none
padding 0
.ds-suggestions
margin-top 0
.ds-suggestion
2019-11-30 19:32:43 +08:00
border-bottom 1px solid var(--border-color)
2019-04-15 10:35:40 +08:00
.algolia-docsearch-suggestion--highlight
2019-11-30 19:32:43 +08:00
color $accentColor
2019-04-15 10:35:40 +08:00
.algolia-docsearch-suggestion
2019-11-30 19:32:43 +08:00
border-color var(--border-color)
2019-04-15 10:35:40 +08:00
padding 0
.algolia-docsearch-suggestion--category-header
padding 5px 10px
margin-top 0
background $accentColor
color #fff
font-weight 600
.algolia-docsearch-suggestion--highlight
background rgba(255, 255, 255, 0.6)
.algolia-docsearch-suggestion--wrapper
2019-11-30 19:32:43 +08:00
background var(--background-color)
2019-04-15 10:35:40 +08:00
padding 0
.algolia-docsearch-suggestion--title
font-weight 600
margin-bottom 0
2019-11-30 19:32:43 +08:00
color var(--text-color)
2019-04-15 10:35:40 +08:00
.algolia-docsearch-suggestion--subcategory-column
vertical-align top
padding 5px 7px 5px 5px
2019-11-30 19:32:43 +08:00
border-color var(--border-color)
background var(--background-color)
2019-04-15 10:35:40 +08:00
&:after
display none
.algolia-docsearch-suggestion--subcategory-column-text
2019-11-30 19:32:43 +08:00
color var(--text-color)
2019-04-15 10:35:40 +08:00
.algolia-docsearch-footer
2019-11-30 19:32:43 +08:00
border-color var(--border-color)
background var(--background-color)
2019-04-15 10:35:40 +08:00
.ds-cursor .algolia-docsearch-suggestion--content
background-color #e7edf3 !important
color $textColor
@media (min-width: $MQMobile)
.algolia-search-wrapper
.algolia-autocomplete
.algolia-docsearch-suggestion
.algolia-docsearch-suggestion--subcategory-column
float none
width 150px
min-width 150px
display table-cell
.algolia-docsearch-suggestion--content
float none
display table-cell
width 100%
vertical-align top
.ds-dropdown-menu
min-width 515px !important
@media (max-width: $MQMobile)
.algolia-search-wrapper
.ds-dropdown-menu
min-width calc(100vw - 4rem) !important
max-width calc(100vw - 4rem) !important
.algolia-docsearch-suggestion--wrapper
padding 5px 7px 5px 5px !important
.algolia-docsearch-suggestion--subcategory-column
padding 0 !important
2019-11-30 19:32:43 +08:00
background var(--border-color) !important
2019-04-15 10:35:40 +08:00
.algolia-docsearch-suggestion--subcategory-column-text:after
content " > "
font-size 10px
line-height 14.4px
display inline-block
width 5px
margin -3px 3px 0
vertical-align middle
</style>