|
| 1 | +import React, { Component } from "react" |
| 2 | +import PropTypes from "prop-types" |
| 3 | +import { navigateTo } from "gatsby-link" |
| 4 | +import { rhythm } from "../utils/typography" |
| 5 | +import presets from "../utils/presets" |
| 6 | + |
| 7 | +import { css } from "glamor" |
| 8 | + |
| 9 | +// Override default search result styles |
| 10 | +css.global(`.searchWrap .algolia-docsearch-suggestion--highlight`, { |
| 11 | + backgroundColor: `${presets.lightPurple} !important`, |
| 12 | + boxShadow: `inset 0 -2px 0 0 ${presets.lightPurple} !important`, |
| 13 | + color: `black`, |
| 14 | + fontWeight: `bold`, |
| 15 | +}) |
| 16 | +css.global(`.searchWrap .algolia-docsearch-suggestion .algolia-docsearch-suggestion--subcategory-column`, { |
| 17 | + width: `100% !important`, |
| 18 | +}) |
| 19 | +css.global(`.searchWrap .algolia-docsearch-suggestion--subcategory-column-text:after`, { |
| 20 | + display: `none`, |
| 21 | +}) |
| 22 | +css.global( |
| 23 | + `.searchWrap .algolia-autocomplete .ds-dropdown-menu .ds-suggestion.ds-cursor .algolia-docsearch-suggestion:not(.suggestion-layout-simple) .algolia-docsearch-suggestion--content`, |
| 24 | + { backgroundColor: `${presets.brandLighter} !important` } |
| 25 | +) |
| 26 | +css.global( |
| 27 | + `.searchWrap .algolia-docsearch-suggestion .algolia-docsearch-suggestion--content.algolia-docsearch-suggestion--no-results`, |
| 28 | + { |
| 29 | + maxWidth: `100%`, |
| 30 | + paddingLeft: `0 !important`, |
| 31 | + width: `100% !important`, |
| 32 | + } |
| 33 | +) |
| 34 | +css.global( |
| 35 | + `.searchWrap .algolia-docsearch-suggestion .algolia-docsearch-suggestion--content.algolia-docsearch-suggestion--no-results:before`, |
| 36 | + { display: `none !important` } |
| 37 | +) |
| 38 | +css.global(`.searchWrap .algolia-autocomplete .ds-dropdown-menu`, { |
| 39 | + position: `fixed !important`, |
| 40 | + top: `${rhythm(2)} !important`, |
| 41 | + left: `${rhythm(0.5)} !important`, |
| 42 | + right: `${rhythm(0.5)} !important`, |
| 43 | + minWidth: `calc(100vw - ${rhythm(1)})`, |
| 44 | + maxWidth: `calc(100vw - 2rem)`, |
| 45 | + maxHeight: `calc(100vh - 5rem)`, |
| 46 | + display: `block`, |
| 47 | +}) |
| 48 | +css.global( |
| 49 | + `.searchWrap .algolia-autocomplete.algolia-autocomplete-right .ds-dropdown-menu, .algolia-autocomplete.algolia-autocomplete-left .ds-dropdown-menu`, |
| 50 | + { |
| 51 | + left: `${rhythm(0.5)} !important`, |
| 52 | + right: `${rhythm(0.5)} !important`, |
| 53 | + } |
| 54 | +) |
| 55 | +css.global( |
| 56 | + `.searchWrap .algolia-autocomplete.algolia-autocomplete-right .ds-dropdown-menu::before`, |
| 57 | + { |
| 58 | + right: rhythm(5), |
| 59 | + } |
| 60 | +) |
| 61 | +css.global( |
| 62 | + `.searchWrap .algolia-autocomplete.algolia-autocomplete-left .ds-dropdown-menu::before`, |
| 63 | + { |
| 64 | + left: rhythm(7), |
| 65 | + } |
| 66 | +) |
| 67 | + |
| 68 | +// use css.insert() for media query with global CSS |
| 69 | +css.insert(`@media ${presets.phablet}{ |
| 70 | + .searchWrap .algolia-autocomplete .algolia-docsearch-suggestion .algolia-docsearch-suggestion--subcategory-column { |
| 71 | + font-weight: 400; |
| 72 | + width: 30% !important; |
| 73 | + text-align: right; |
| 74 | + opacity: 1; |
| 75 | + padding: ${rhythm(0.5)} ${rhythm(1)} ${rhythm(0.5)} 0; |
| 76 | + } |
| 77 | + .searchWrap .algolia-autocomplete .algolia-docsearch-suggestion--subcategory-column:before { |
| 78 | + content: ""; |
| 79 | + position: absolute; |
| 80 | + display: block !important; |
| 81 | + top: 0; |
| 82 | + height: 100%; |
| 83 | + width: 1px; |
| 84 | + background: #ddd; |
| 85 | + right: 0; |
| 86 | + } |
| 87 | + .searchWrap .algolia-autocomplete .algolia-docsearch-suggestion--subcategory-column:after { |
| 88 | + display: none; |
| 89 | + } |
| 90 | + .searchWrap .algolia-autocomplete .algolia-docsearch-suggestion--content { |
| 91 | + width: 70% !important; |
| 92 | + max-width: 70%; |
| 93 | + display: block; |
| 94 | + padding: ${rhythm(0.5)} 0 ${rhythm(0.5)} ${rhythm(1)} !important; |
| 95 | + } |
| 96 | + .searchWrap .algolia-autocomplete .algolia-docsearch-suggestion--content:before { |
| 97 | + content: ""; |
| 98 | + position: absolute; |
| 99 | + display: block !important; |
| 100 | + top: 0; |
| 101 | + height: 100%; |
| 102 | + width: 1px; |
| 103 | + background: #ddd; |
| 104 | + left: -1px; |
| 105 | + } |
| 106 | +}`) |
| 107 | + |
| 108 | +css.insert(`@media ${presets.tablet}{ |
| 109 | + .searchWrap .algolia-autocomplete .ds-dropdown-menu { |
| 110 | + top: 100% !important; |
| 111 | + position: absolute !important; |
| 112 | + max-width: 600px !important; |
| 113 | + min-width: 500px !important; |
| 114 | + } |
| 115 | + .searchWrap .algolia-autocomplete.algolia-autocomplete-right .ds-dropdown-menu { |
| 116 | + right: 0 !important; |
| 117 | + left: inherit !important; |
| 118 | + } |
| 119 | + .searchWrap .algolia-autocomplete.algolia-autocomplete-right .ds-dropdown-menu::before { |
| 120 | + right: ${rhythm(2)}; |
| 121 | + } |
| 122 | +}`) |
| 123 | + |
| 124 | +class SearchForm extends Component { |
| 125 | + constructor() { |
| 126 | + super() |
| 127 | + this.state = { enabled: true } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Replace the default selection event, allowing us to do client-side |
| 132 | + * navigation thus avoiding a full page refresh. |
| 133 | + * |
| 134 | + * Ref: https://github.com/algolia/autocomplete.js#events |
| 135 | + */ |
| 136 | + autocompleteSelected(e) { |
| 137 | + e.stopPropagation() |
| 138 | + // Use an anchor tag to parse the absolute url (from autocomplete.js) into a relative url |
| 139 | + // eslint-disable-next-line no-undef |
| 140 | + const a = document.createElement(`a`) |
| 141 | + a.href = e._args[0].url |
| 142 | + navigateTo(`${a.pathname}${a.hash}`) |
| 143 | + } |
| 144 | + |
| 145 | + componentDidMount() { |
| 146 | + if ( |
| 147 | + typeof window === `undefined` || // eslint-disable-line no-undef |
| 148 | + typeof window.docsearch === `undefined` // eslint-disable-line no-undef |
| 149 | + ) { |
| 150 | + console.warn(`Search has failed to load and now is being disabled`) |
| 151 | + this.setState({ enabled: false }) |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + // eslint-disable-next-line no-undef |
| 156 | + window.addEventListener( |
| 157 | + `autocomplete:selected`, |
| 158 | + this.autocompleteSelected, |
| 159 | + true |
| 160 | + ) |
| 161 | + |
| 162 | + // eslint-disable-next-line no-undef |
| 163 | + window.docsearch({ |
| 164 | + apiKey: `71af1f9c4bd947f0252e17051df13f9c`, |
| 165 | + indexName: `gatsbyjs`, |
| 166 | + inputSelector: `#doc-search`, |
| 167 | + debug: false, |
| 168 | + }) |
| 169 | + } |
| 170 | + |
| 171 | + render() { |
| 172 | + const { enabled } = this.state |
| 173 | + const { styles } = this.props.styles |
| 174 | + return enabled ? ( |
| 175 | + <form |
| 176 | + css={{ |
| 177 | + ...styles, |
| 178 | + display: `flex`, |
| 179 | + flex: `0 0 auto`, |
| 180 | + flexDirection: `row`, |
| 181 | + alignItems: `center`, |
| 182 | + marginLeft: rhythm(1 / 2), |
| 183 | + marginBottom: 0, |
| 184 | + }} |
| 185 | + className="searchWrap" |
| 186 | + > |
| 187 | + <input |
| 188 | + id="doc-search" |
| 189 | + css={{ |
| 190 | + appearance: `none`, |
| 191 | + background: `transparent`, |
| 192 | + border: 0, |
| 193 | + color: presets.brand, |
| 194 | + paddingTop: rhythm(1 / 8), |
| 195 | + paddingRight: rhythm(1 / 4), |
| 196 | + paddingBottom: rhythm(1 / 8), |
| 197 | + paddingLeft: rhythm(1), |
| 198 | + backgroundImage: `url(/search.svg)`, |
| 199 | + backgroundSize: `16px 16px`, |
| 200 | + backgroundRepeat: `no-repeat`, |
| 201 | + backgroundPositionY: `center`, |
| 202 | + backgroundPositionX: `5px`, |
| 203 | + overflow: `hidden`, |
| 204 | + width: rhythm(1), |
| 205 | + transition: `width 0.2s ease`, |
| 206 | + |
| 207 | + ":focus": { |
| 208 | + outline: 0, |
| 209 | + backgroundColor: presets.brandLighter, |
| 210 | + borderRadius: presets.radiusLg, |
| 211 | + width: rhythm(5), |
| 212 | + }, |
| 213 | + |
| 214 | + [presets.Desktop]: { |
| 215 | + width: rhythm(5), |
| 216 | + }, |
| 217 | + }} |
| 218 | + type="search" |
| 219 | + placeholder="Search docs" |
| 220 | + aria-label="Search docs" |
| 221 | + /> |
| 222 | + </form> |
| 223 | + ) : null |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +SearchForm.propTypes = { |
| 228 | + styles: PropTypes.object, |
| 229 | +} |
| 230 | + |
| 231 | +export default SearchForm |
0 commit comments