@@ -2,11 +2,15 @@ import React, { Component } from "react"
22import PropTypes from "prop-types"
33import { navigateTo } from "gatsby-link"
44import { rhythm } from "../utils/typography"
5+
56import presets , { colors } from "../utils/presets"
67import hex2rgba from "hex2rgba"
8+ import SearchIcon from './search-icon'
79
810import { css } from "glamor"
911
12+ const { curveDefault, speedDefault } = presets . animation
13+
1014// Override default search result styles (docsearch.css)
1115css . insert ( `
1216 .algolia-autocomplete .ds-dropdown-menu {
@@ -227,7 +231,7 @@ css.insert(`
227231class SearchForm extends Component {
228232 constructor ( ) {
229233 super ( )
230- this . state = { enabled : true }
234+ this . state = { enabled : true , focussed : false }
231235 this . autocompleteSelected = this . autocompleteSelected . bind ( this )
232236 this . focusSearchInput = this . focusSearchInput . bind ( this )
233237 }
@@ -293,12 +297,12 @@ class SearchForm extends Component {
293297 }
294298
295299 render ( ) {
296- const { enabled } = this . state
297- const { styles } = this . props . styles
300+ const { enabled, focussed } = this . state
301+ const { iconStyles, isHomepage } = this . props
302+
298303 return enabled ? (
299304 < form
300305 css = { {
301- ...styles ,
302306 display : `flex` ,
303307 flex : `0 0 auto` ,
304308 flexDirection : `row` ,
@@ -309,52 +313,83 @@ class SearchForm extends Component {
309313 className = "searchWrap"
310314 onSubmit = { e => e . preventDefault ( ) }
311315 >
312- < input
313- id = "doc-search"
314- css = { {
315- appearance : `none` ,
316- background : `transparent` ,
317- border : 0 ,
318- color : colors . gatsby ,
319- paddingTop : rhythm ( 1 / 8 ) ,
320- paddingRight : rhythm ( 1 / 4 ) ,
321- paddingBottom : rhythm ( 1 / 8 ) ,
322- paddingLeft : rhythm ( 1 ) ,
323- backgroundImage : `url(/search.svg)` ,
324- backgroundSize : `16px 16px` ,
325- backgroundRepeat : `no-repeat` ,
326- backgroundPositionY : `center` ,
327- backgroundPositionX : `5px` ,
328- overflow : `hidden` ,
329- width : rhythm ( 1 ) ,
330- transition : `width 0.2s ease` ,
331-
332- ":focus" : {
333- outline : 0 ,
334- backgroundColor : colors . ui . light ,
316+ < label css = { { position : `relative` } } >
317+ < input
318+ id = "doc-search"
319+ css = { {
320+ appearance : `none` ,
321+ backgroundColor : `transparent` ,
322+ border : 0 ,
335323 borderRadius : presets . radiusLg ,
336- width : rhythm ( 5 ) ,
337- } ,
338-
339- [ presets . Desktop ] : {
340- width : rhythm ( 5 ) ,
341- } ,
342- } }
343- type = "search"
344- placeholder = "Search docs"
345- aria-label = "Search docs"
346- title = "Hit 's' to search docs"
347- ref = { input => {
348- this . searchInput = input
349- } }
350- />
324+ color : colors . gatsby ,
325+ paddingTop : rhythm ( 1 / 8 ) ,
326+ paddingRight : rhythm ( 1 / 4 ) ,
327+ paddingBottom : rhythm ( 1 / 8 ) ,
328+ paddingLeft : rhythm ( 1 ) ,
329+ overflow : `hidden` ,
330+ width : rhythm ( 1 ) ,
331+ transition : `width ${ speedDefault } ${ curveDefault } , background-color ${ speedDefault } ${ curveDefault } ` ,
332+ ":focus" : {
333+ outline : 0 ,
334+ backgroundColor : colors . ui . light ,
335+ borderRadius : presets . radiusLg ,
336+ width : rhythm ( 5 ) ,
337+ transition : `width ${ speedDefault } ${ curveDefault } , background-color ${ speedDefault } ${ curveDefault } ` ,
338+ } ,
339+
340+ [ presets . Desktop ] : {
341+ backgroundColor : ! isHomepage && `#fff` ,
342+ color : colors . gatsby ,
343+ width : ! isHomepage && rhythm ( 5 ) ,
344+ ":focus" : {
345+ backgroundColor : colors . ui . light ,
346+ color : colors . gatsby ,
347+ } ,
348+ } ,
349+
350+ [ presets . Hd ] : {
351+ backgroundColor : isHomepage && colors . lilac ,
352+ color : isHomepage && colors . ui . light ,
353+ width : isHomepage && rhythm ( 5 ) ,
354+ } ,
355+ } }
356+ type = "search"
357+ placeholder = "Search docs"
358+ aria-label = "Search docs"
359+ title = "Hit 's' to search docs"
360+ onFocus = { ( ) => this . setState ( { focussed : true } ) }
361+ onBlur = { ( ) => this . setState ( { focussed : false } ) }
362+ ref = { input => {
363+ this . searchInput = input
364+ } }
365+ />
366+ < SearchIcon
367+ overrideCSS = { {
368+ ...iconStyles ,
369+ fill : focussed && colors . gatsby ,
370+ position : `absolute` ,
371+ left : `5px` ,
372+ top : `50%` ,
373+ width : `16px` ,
374+ height : `16px` ,
375+ pointerEvents : `none` ,
376+ transition : `fill ${ speedDefault } ${ curveDefault } ` ,
377+ transform : `translateY(-50%)` ,
378+
379+ [ presets . Hd ] : {
380+ fill : focussed && isHomepage && colors . gatsby ,
381+ } ,
382+ } }
383+ />
384+ </ label >
351385 </ form >
352386 ) : null
353387 }
354388}
355389
356390SearchForm . propTypes = {
357- styles : PropTypes . object ,
391+ isHomepage : PropTypes . bool ,
392+ iconStyles : PropTypes . object ,
358393}
359394
360395export default SearchForm
0 commit comments