index.html

Material UI Responsive Search in AppBar

Word count: 619Reading time: 3 min
2020/03/07 Share

Problem

The Material UI official example of search field looks like this on a mobile device:

A search field take over the whole app bar is not what I want. What I managed to achieve is a single search button on the right which will expand the search field on click, as the following picture shows.

Code

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import Typography from '@material-ui/core/Typography';
import {fade, withStyles} from '@material-ui/core/styles';
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
import InputBase from "@material-ui/core/InputBase";
import SearchIcon from "@material-ui/icons/Search";
import {TextField} from "@material-ui/core";
import ApplicationContext from "./ApplicationContext";

const styles = theme => ({
menuButton: {
marginRight: theme.spacing(2),
},
title: {
flexGrow: 1,
display: 'none',
[theme.breakpoints.up('xs')]: {
display: 'block',
},
},
SearchDesktop: {
position: 'relative',
borderRadius: theme.shape.borderRadius,
backgroundColor: fade(theme.palette.common.white, 0.15),
'&:hover': {
backgroundColor: fade(theme.palette.common.white, 0.25),
},
marginLeft: 0,
width: '100%',
display: 'none',
[theme.breakpoints.up('sm')]: {
display: 'block',
marginLeft: theme.spacing(1),
width: 'auto'
},
},
SearchDesktopIcon: {
width: theme.spacing(7),
height: '100%',
position: 'absolute',
pointerEvents: 'none',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
SearchDesktopInputRoot: {
color: 'inherit',
},
SearchDesktopInput: {
padding: theme.spacing(1, 1, 1, 7),
transition: theme.transitions.create('width'),
width: '100%',
[theme.breakpoints.up('sm')]: {
width: 120,
'&:focus': {
width: 200,
}
}
},
SearchMobileIcon: {
color: 'inherit',
[theme.breakpoints.up('sm')]: {
display: 'none'
}
},
SearchMobileInput: {
flexGrow: 1,
'& > *': {
color: 'inherit'
}
},
SearchMobileInputRoot: {
color: 'inherit'
},
});

class MyAppBar extends React.PureComponent {
static propTypes = {
title: PropTypes.string,
showSearchBar: PropTypes.bool
};

static defaultProps = {
showSearchBar: false
};

constructor(props) {
super(props);
this.state = {
searchExpanded: false
}
}

expandSearch = () => {
this.setState({
searchExpanded: true
})
};

handleBackClick = () => {
if (this.state.searchExpanded) {
this.setState({
searchExpanded: false
})
} else {
// TODO: Go back to last activity
}
};

onSearchInputChange = () => {
// TODO: search
};

render() {
const {classes} = this.props;
const context = this.context;
return (
<>
<AppBar position="static">
<Toolbar>
{// Back arrow button
(context.activity !== 'home' || this.state.searchExpanded) &&
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="open drawer"
onClick={this.handleBackClick}
>
<ArrowBackIcon/>
</IconButton>}
{// Title
!this.state.searchExpanded &&
<Typography className={classes.title} variant="h6" noWrap>
{this.props.title}
</Typography>
}
{// Search bar sm-
this.props.showSearchBar && this.state.searchExpanded &&
<TextField
className={classes.SearchMobileInput}
autoComplete={"off"}
onChange={this.onSearchInputChange}
color={"secondary"}
autoFocus={true}/>
}
{// Search button sm-
this.props.showSearchBar && !this.state.searchExpanded &&
<IconButton
className={classes.SearchMobileIcon}
onClick={this.expandSearch}>
<SearchIcon/>
</IconButton>
}
{// Search bar sm+
this.props.showSearchBar && !this.state.searchExpanded &&
<div className={classes.SearchDesktop}>
<div className={classes.SearchDesktopIcon}>
<SearchIcon/>
</div>
<InputBase
placeholder="Search…"
classes={{
root: classes.SearchDesktopInputRoot,
input: classes.SearchDesktopInput
}}
inputProps={{'aria-label': 'search'}}
onChange={this.onSearchInputChange}/>
</div>
}
</Toolbar>
</AppBar>
</>
);
}
}

export default withStyles(styles)(MyAppBar);

CATALOG
  1. 1. Problem
  2. 2. Code