Skip to content

Commit

Permalink
docs: fix DocSearch not loading in SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
hasparus committed Jun 7, 2021
1 parent 6ab641d commit f3484a4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
4 changes: 4 additions & 0 deletions packages/docs/gatsby-browser.js
Expand Up @@ -5,3 +5,7 @@ import * as React from 'react'
import { WrapPageElement, setDocSearchComponents } from './src'

export const wrapPageElement = (props) => <WrapPageElement {...props} />

export const onRenderBody = (args) => {
setDocSearchComponents(args)
}
11 changes: 5 additions & 6 deletions packages/docs/src/components/search-input.js
Expand Up @@ -38,6 +38,7 @@ const algoliaStyles = {
color: 'primary',
'--shadow-color': (theme) => theme.colors.muted,
boxShadow: 'inset 0 -2px 0 0 var(--shadow-color)',
padding: 0,
},
'.algolia-docsearch-suggestion--subcategory-column-text': {},
'.algolia-docsearch-suggestion--text': {
Expand All @@ -48,9 +49,6 @@ const algoliaStyles = {
fontWeight: 'bold',
color: 'text',
fontSize: 1,
'.algolia-docsearch-suggestion--highlight': {
verticalAlign: 'middle',
},
},
'.algolia-docsearch-suggestion--category-header': {
display: 'none !important',
Expand All @@ -65,9 +63,10 @@ const algoliaStyles = {
'.algolia-docsearch-suggestion--subcategory-column:before': {
display: 'none',
},
'.ds-dropdown-menu .ds-suggestion.ds-cursor .algolia-docsearch-suggestion:not(.suggestion-layout-simple) .algolia-docsearch-suggestion--content': {
backgroundColor: 'muted',
},
'.ds-dropdown-menu .ds-suggestion.ds-cursor .algolia-docsearch-suggestion:not(.suggestion-layout-simple) .algolia-docsearch-suggestion--content':
{
backgroundColor: 'muted',
},
'@media (max-width: 768px)': {
// todo: make docsearch autocomplete dropdown responsive
},
Expand Down
40 changes: 21 additions & 19 deletions packages/docs/src/index.js
Expand Up @@ -41,27 +41,29 @@ export const setDocSearchComponents = ({
type="text/javascript"
dangerouslySetInnerHTML={{
__html: `
var lastSearchInput = null;
var observer = new MutationObserver(function () {
var searchSelector = "#algolia-docs-search";
var searchInput = document.querySelector(searchSelector);
if (searchInput) {
docsearch({
apiKey: "84ed820927eee5fa5018c9f1abe70390",
indexName: "theme-ui",
inputSelector: searchSelector,
debug: true
})
observer.disconnect()
observer = null
}
});
// start observing
document.addEventListener("DOMContentLoaded", function() {
observer.observe(document, {
childList: true,
subtree: true
var searchSelector = "#algolia-docs-search";
var searchInput = document.querySelector(searchSelector);
if (lastSearchInput !== searchInput) {
docsearch({
apiKey: "84ed820927eee5fa5018c9f1abe70390",
indexName: "theme-ui",
inputSelector: searchSelector,
debug: true
})
lastSearchInput = searchInput;
}
});
});
document.addEventListener("DOMContentLoaded", function() {
observer.observe(document, {
childList: true,
subtree: true
});
});
`,
}}
/>,
Expand Down

1 comment on commit f3484a4

@vercel
Copy link

@vercel vercel bot commented on f3484a4 Jun 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.