-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
DocSearch.js
115 lines (105 loc) · 2.89 KB
/
DocSearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* @emails react-core
* @flow
*/
import React, {Component} from 'react';
import {colors, media} from 'theme';
type State = {
enabled: boolean,
};
class DocSearch extends Component<{}, State> {
state = {
enabled: true,
};
componentDidMount() {
// Initialize Algolia search.
// TODO Is this expensive? Should it be deferred until a user is about to search?
// eslint-disable-next-line no-undef
if (window.docsearch) {
window.docsearch({
apiKey: '36221914cce388c46d0420343e0bb32e',
indexName: 'react',
inputSelector: '#algolia-doc-search',
});
} else {
console.warn('Search has failed to load and now is being disabled');
this.setState({enabled: false});
}
}
render() {
const {enabled} = this.state;
return enabled ? (
<form
css={{
display: 'flex',
flex: '0 0 auto',
flexDirection: 'row',
alignItems: 'center',
paddingLeft: '0.5rem',
paddingRight: '0.5rem',
[media.lessThan('small')]: {
justifyContent: 'flex-end',
},
[media.lessThan('large')]: {
marginRight: 10,
},
[media.between('small', 'medium')]: {
width: 'calc(100% / 3)',
},
[media.between('medium', 'xlarge')]: {
width: 'calc(100% / 6)',
},
[media.greaterThan('small')]: {
minWidth: 120,
},
}}>
<input
css={{
appearance: 'none',
background: 'transparent',
border: 0,
color: colors.white,
fontSize: 18,
fontWeight: 300,
fontFamily: 'inherit',
position: 'relative',
padding: '5px 5px 5px 29px',
backgroundImage: 'url(/search.svg)',
backgroundSize: '16px 16px',
backgroundRepeat: 'no-repeat',
backgroundPositionY: 'center',
backgroundPositionX: '5px',
':focus': {
outline: 0,
backgroundColor: colors.lighter,
borderRadius: '0.25rem',
},
[media.lessThan('large')]: {
fontSize: 16,
},
[media.greaterThan('small')]: {
width: '100%',
},
[media.lessThan('small')]: {
width: '16px',
transition: 'width 0.2s ease, padding 0.2s ease',
paddingLeft: '16px',
':focus': {
paddingLeft: '29px',
width: '8rem',
outline: 'none',
},
},
}}
id="algolia-doc-search"
type="search"
placeholder="Search docs"
aria-label="Search docs"
/>
</form>
) : null;
}
}
export default DocSearch;