refactor: asjust few styling of algolia search box

pull/165/head
Kia King Ishii 4 years ago
parent 077784fa34
commit 7aa3fb261b

@ -11,18 +11,17 @@ module.exports = {
editLinkText: 'Edit this page on GitHub', editLinkText: 'Edit this page on GitHub',
lastUpdated: 'Last Updated', lastUpdated: 'Last Updated',
algolia: {
apiKey: 'c57105e511faa5558547599f120ceeba',
indexName: 'vitepress'
},
carbonAds: { carbonAds: {
carbon: 'CEBDT27Y', carbon: 'CEBDT27Y',
custom: 'CKYD62QM', custom: 'CKYD62QM',
placement: 'vuejsorg' placement: 'vuejsorg'
}, },
algolia: {
apiKey: 'c57105e511faa5558547599f120ceeba',
indexName: 'vitepress'
// algoliaOptions: { facetFilters: ['tags:guide,api'] }
},
nav: [ nav: [
{ text: 'Guide', link: '/' }, { text: 'Guide', link: '/' },
{ text: 'Config Reference', link: '/config/basics' }, { text: 'Config Reference', link: '/config/basics' },

@ -131,10 +131,3 @@ watch(route, hideSidebar)
// TODO: route only changes when the pathname changes // TODO: route only changes when the pathname changes
// listening to hashchange does nothing because it's prevented in router // listening to hashchange does nothing because it's prevented in router
</script> </script>
<style>
/* remove margin added by user agent */
.DocSearch-SearchBar form {
margin-block-end: 0;
}
</style>

@ -1,5 +1,5 @@
<template> <template>
<div id="docsearch"></div> <div class="algolia-search-box" id="docsearch" />
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -7,6 +7,20 @@ import type { AlgoliaSearchOptions } from 'algoliasearch'
import { useRoute, useRouter } from 'vitepress' import { useRoute, useRouter } from 'vitepress'
import { defineProps, getCurrentInstance, onMounted, watch } from 'vue' import { defineProps, getCurrentInstance, onMounted, watch } from 'vue'
const { options } = defineProps<{
options: AlgoliaSearchOptions
}>()
const vm = getCurrentInstance()
const route = useRoute()
const router = useRouter()
watch(() => options, (value) => { update(value) })
onMounted(() => {
initialize(options)
})
function isSpecialClick(event: MouseEvent) { function isSpecialClick(event: MouseEvent) {
return ( return (
event.button === 1 || event.button === 1 ||
@ -19,31 +33,13 @@ function isSpecialClick(event: MouseEvent) {
function getRelativePath(absoluteUrl: string) { function getRelativePath(absoluteUrl: string) {
const { pathname, hash } = new URL(absoluteUrl) const { pathname, hash } = new URL(absoluteUrl)
// const url = pathname.replace(this.$site.base, '/') + hash
const url = pathname + hash
return url return pathname + hash
} }
const { options } = defineProps<{
// Using a regular import breaks at runtime
options: AlgoliaSearchOptions
}>()
const route = useRoute()
const router = useRouter()
const vm = getCurrentInstance()
watch(
() => options,
(newValue) => {
update(newValue)
}
)
function update(options: any) { function update(options: any) {
if (vm && vm.vnode.el) { if (vm && vm.vnode.el) {
vm.vnode.el.innerHTML = '<div id="docsearch"></div>' vm.vnode.el.innerHTML = '<div class="algolia-search-box" id="docsearch"></div>'
initialize(options) initialize(options)
} }
} }
@ -52,33 +48,21 @@ function initialize(userOptions: any) {
Promise.all([ Promise.all([
import('@docsearch/js'), import('@docsearch/js'),
import('@docsearch/css') import('@docsearch/css')
// import(/* webpackChunkName: "docsearch" */ '@docsearch/js'),
// Promise.resolve(docsearch),
// import(/* webpackChunkName: "docsearch" */ '@docsearch/css'),
]).then(([docsearch]) => { ]).then(([docsearch]) => {
docsearch = docsearch.default docsearch.default(
docsearch(
Object.assign({}, userOptions, { Object.assign({}, userOptions, {
container: '#docsearch', container: '#docsearch',
// #697 Make DocSearch work well in i18n mode.
searchParameters: Object.assign( searchParameters: Object.assign({}, userOptions.searchParameters),
{},
// lang && {
// facetFilters: [`lang:${lang}`].concat(
// userOptions.facetFilters || []
// )
// },
userOptions.searchParameters
),
navigator: { navigator: {
navigate: ({ suggestionUrl }: { suggestionUrl: string }) => { navigate: ({ suggestionUrl }: { suggestionUrl: string }) => {
const { pathname: hitPathname } = new URL( const { pathname: hitPathname } = new URL(
window.location.origin + suggestionUrl window.location.origin + suggestionUrl
) )
// Vue Router doesn't handle same-page navigation so we use // Router doesn't handle same-page navigation so we use the native
// the native browser location API for anchor navigation. // browser location API for anchor navigation
if (route.path === hitPathname) { if (route.path === hitPathname) {
window.location.assign(window.location.origin + suggestionUrl) window.location.assign(window.location.origin + suggestionUrl)
} else { } else {
@ -86,6 +70,7 @@ function initialize(userOptions: any) {
} }
} }
}, },
transformItems: (items) => { transformItems: (items) => {
return items.map((item) => { return items.map((item) => {
return Object.assign({}, item, { return Object.assign({}, item, {
@ -93,6 +78,7 @@ function initialize(userOptions: any) {
}) })
}) })
}, },
hitComponent: ({ hit, children }) => { hitComponent: ({ hit, children }) => {
const relativeHit = hit.url.startsWith('http') const relativeHit = hit.url.startsWith('http')
? getRelativePath(hit.url as string) ? getRelativePath(hit.url as string)
@ -110,15 +96,15 @@ function initialize(userOptions: any) {
return return
} }
// We rely on the native link scrolling when user is // we rely on the native link scrolling when user is already on
// already on the right anchor because Vue Router doesn't // the right anchor because Router doesn't support duplicated
// support duplicated history entries. // history entries
if (route.path === relativeHit) { if (route.path === relativeHit) {
return return
} }
// If the hits goes to another page, we prevent the native link behavior // if the hits goes to another page, we prevent the native link
// to leverage the Vue Router loading feature. // behavior to leverage the Router loading feature
if (route.path !== relativeHit) { if (route.path !== relativeHit) {
event.preventDefault() event.preventDefault()
} }
@ -133,16 +119,37 @@ function initialize(userOptions: any) {
) )
}) })
} }
onMounted(() => {
initialize(options)
})
</script> </script>
<style> <style>
.algolia-search-box {
padding-top: 1px;
}
@media (min-width: 720px) {
.algolia-search-box {
padding-left: 8px;
}
}
@media (min-width: 751px) {
.algolia-search-box {
padding-left: 8px;
}
.algolia-search-box .DocSearch-Button-Placeholder {
padding-left: 8px;
font-size: .9rem;
font-weight: 500;
}
}
.DocSearch { .DocSearch {
--docsearch-primary-color: #42b983; --docsearch-primary-color: #42b983;
--docsearch-highlight-color: var(--docsearch-primary-color); --docsearch-highlight-color: var(--docsearch-primary-color);
--docsearch-searchbox-shadow: inset 0 0 0 2px var(--docsearch-primary-color); --docsearch-searchbox-shadow: inset 0 0 0 2px var(--docsearch-primary-color);
--docsearch-text-color: var(--c-text-light);
--docsearch-muted-color: var(--c-text-lighter);
--docsearch-searchbox-background: #f2f2f2;
} }
</style> </style>

@ -195,6 +195,10 @@ blockquote > p {
margin: 0; margin: 0;
} }
form {
margin: 0;
}
.theme { .theme {
font-size: 16px; font-size: 16px;
color: var(--c-text); color: var(--c-text);

@ -26,8 +26,10 @@
--font-family-mono: source-code-pro, Menlo, Monaco, Consolas, "Courier New", monospace; --font-family-mono: source-code-pro, Menlo, Monaco, Consolas, "Courier New", monospace;
/** /**
* Z Indexes Algolia SearchBox has a z-index of 200, so make sure not to go * Z Indexes
* above that value. *
* Algolia SearchBox has a z-index of 200, so make sure not to go above
* that value.
* --------------------------------------------------------------------- */ * --------------------------------------------------------------------- */
--z-index-navbar: 10; --z-index-navbar: 10;

Loading…
Cancel
Save