Skip to content

Commit 7588004

Browse files
m-allansonKyleAMathews
authored andcommitted
[www] add search input to home page (#3662)
* Experiment adding search input to home page * Prettier * Comment unused imports * Display social icons when viewport is wide enough * Use alternate colours at Hd breakpoint on home page
1 parent 4c9afab commit 7588004

File tree

4 files changed

+134
-68
lines changed

4 files changed

+134
-68
lines changed

www/src/components/navigation.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React from "react"
22
import Link from "gatsby-link"
3-
import GithubIcon from "react-icons/lib/go/mark-github"
43
import TwitterIcon from "react-icons/lib/fa/twitter"
5-
6-
import SearchForm from "../components/search-form"
74
import DiscordIcon from "../components/discord"
5+
import GithubIcon from "react-icons/lib/go/mark-github"
6+
import SearchForm from "../components/search-form"
87
import logo from "../logo.svg"
98
import typography, { rhythm, scale } from "../utils/typography"
109
import presets, { colors } from "../utils/presets"
@@ -163,9 +162,11 @@ export default ({ pathname }) => {
163162
marginLeft: `auto`,
164163
}}
165164
>
166-
{!isHomepage && (
167-
<SearchForm key="SearchForm" styles={{ ...navItemStyles }} />
168-
)}
165+
<SearchForm
166+
key="SearchForm"
167+
iconStyles={{ ...socialIconsStyles }}
168+
isHomepage={isHomepage}
169+
/>
169170
<a
170171
href="https://github.com/gatsbyjs/gatsby"
171172
title="GitHub"
@@ -176,27 +177,36 @@ export default ({ pathname }) => {
176177
>
177178
<GithubIcon style={{ verticalAlign: `text-top` }} />
178179
</a>
179-
<a
180-
href="https://discord.gg/0ZcbPKXt5bZjGY5n"
181-
title="Discord"
182-
css={{
183-
...navItemStyles,
184-
...socialIconsStyles,
185-
}}
186-
>
187-
<DiscordIcon overrideCSS={{ verticalAlign: `text-top` }} />
188-
</a>
189-
<a
190-
href="https://twitter.com/gatsbyjs"
191-
title="@gatsbyjs"
180+
181+
<div
192182
css={{
193-
...navItemStyles,
194-
...socialIconsStyles,
195-
paddingRight: 0,
183+
display: `none`,
184+
[presets.Desktop]: { display: !isHomepage && `inline-block` },
185+
[presets.Hd]: { display: `inline-block` },
196186
}}
197187
>
198-
<TwitterIcon style={{ verticalAlign: `text-top` }} />
199-
</a>
188+
<a
189+
href="https://discord.gg/0ZcbPKXt5bZjGY5n"
190+
title="Discord"
191+
css={{
192+
...navItemStyles,
193+
...socialIconsStyles,
194+
}}
195+
>
196+
<DiscordIcon overrideCSS={{ verticalAlign: `text-top` }} />
197+
</a>
198+
<a
199+
href="https://twitter.com/gatsbyjs"
200+
title="@gatsbyjs"
201+
css={{
202+
...navItemStyles,
203+
...socialIconsStyles,
204+
paddingRight: 0,
205+
}}
206+
>
207+
<TwitterIcon style={{ verticalAlign: `text-top` }} />
208+
</a>
209+
</div>
200210
</div>
201211
</div>
202212
</div>

www/src/components/search-form.js

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import React, { Component } from "react"
22
import PropTypes from "prop-types"
33
import { navigateTo } from "gatsby-link"
44
import { rhythm } from "../utils/typography"
5+
56
import presets, { colors } from "../utils/presets"
67
import hex2rgba from "hex2rgba"
8+
import SearchIcon from './search-icon'
79

810
import { css } from "glamor"
911

12+
const { curveDefault, speedDefault } = presets.animation
13+
1014
// Override default search result styles (docsearch.css)
1115
css.insert(`
1216
.algolia-autocomplete .ds-dropdown-menu {
@@ -227,7 +231,7 @@ css.insert(`
227231
class 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

356390
SearchForm.propTypes = {
357-
styles: PropTypes.object,
391+
isHomepage: PropTypes.bool,
392+
iconStyles: PropTypes.object,
358393
}
359394

360395
export default SearchForm

www/src/components/search-icon.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from "react"
2+
import PropTypes from "prop-types"
3+
4+
const SearchIcon = ({ overrideCSS }) => (
5+
<svg
6+
viewBox="0 0 40 40"
7+
xmlns="http://www.w3.org/2000/svg"
8+
fill="currentColor"
9+
css={{ ...overrideCSS }}
10+
>
11+
{/* Based on the 'search' icon in https://github.com/ionic-team/ionicons */}
12+
<g>
13+
<path d="m34.8 30.2c0.3 0.3 0.3 0.8 0 1.1l-3.4 3.5c-0.1 0.1-0.4 0.2-0.6 0.2s-0.4-0.1-0.6-0.2l-6.5-6.8c-2 1.2-4.1 1.8-6.3 1.8-6.8 0-12.4-5.5-12.4-12.4s5.6-12.4 12.4-12.4 12.4 5.5 12.4 12.4c0 2.1-0.6 4.2-1.7 6.1z m-17.4-20.4c-4.1 0-7.6 3.4-7.6 7.6s3.5 7.6 7.6 7.6 7.5-3.4 7.5-7.6-3.3-7.6-7.5-7.6z" />
14+
</g>
15+
</svg>
16+
)
17+
18+
SearchIcon.propTypes = {
19+
overrideCSS: PropTypes.object,
20+
}
21+
22+
export default SearchIcon

www/static/search.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)