import React from 'react';
import PropTypes from 'prop-types';
import SearchIcon from '@material-ui/icons/Search';
import InputBase from '@material-ui/core/InputBase';
import { AccountCircle } from '@material-ui/icons';
import { withStyles, AppBar, Toolbar, Paper, Typography, IconButton } from '@material-ui/core';
const styles = (theme) => ({
root: {
borderRadius: theme.shape.borderRadius * 0.5,
backgroundColor: theme.palette.primary.main,
display: 'flex',
},
logo: {
borderRadius: theme.shape.borderRadius * 0.5,
backgroundColor: theme.palette.secondary.main,
marginTop: theme.spacing.unit * 2,
marginBottom: theme.spacing.unit * 2,
marginLeft: theme.spacing.unit * 3,
marginRight: theme.spacing.unit * 5,
flex: 0.5,
},
logoFont: {
color: theme.palette.primary.main,
fontWeight: 'bolder',
paddingTop: theme.spacing.unit * 0.5,
paddingBottom: theme.spacing.unit * 0.5,
paddingLeft: theme.spacing.unit * 2,
paddingRight: theme.spacing.unit * 2,
},
headerPads: {
paddingTop: theme.spacing.unit * 3,
paddingBottom: theme.spacing.unit * 2,
paddingRight: theme.spacing.unit * 10,
paddingLeft: theme.spacing.unit * 10,
},
containerHorizontalAlignment: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'end',
paddingTop: theme.spacing.unit,
paddingBottom: theme.spacing.unit,
flex: 10,
},
searchBar: {
marginTop: theme.spacing.unit,
marginBottom: theme.spacing.unit,
marginLeft: theme.spacing.unit,
marginRight: theme.spacing.unit * 5,
borderRadius: theme.shape.borderRadius * 0.5,
backgroundColor: theme.palette.secondary.main,
width: 'auto',
/* [theme.breakpoints.up('sm')]: {
marginLeft: theme.spacing.unit,
width: 'auto',
}, */
display: 'flex',
flexDirection: 'row',
},
searchIcon: {
color: theme.palette.primary.main,
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
marginLeft: 20,
height: '100%',
width: theme.spacing.unit * 7,
},
inputRoot: {
color: theme.palette.primary.main,
width: '100%',
},
inputInput: {
transition: theme.transitions.create('width'),
width: 'auto',
[theme.breakpoints.up('sm')]: {
width: '25%', //change it to 250(number)
'&:focus': {
width: '100%', //change it to 1000(number), it will work fine, but I need percentages.
},
},
},
loginButtonContainer: {
flex: 1,
},
loginButton: {
justifyContent: 'right',
marginTop: theme.spacing.unit * 0.5,
color: theme.palette.secondary.main,
},
});
function StylishSearchBar(props) {
const { classes} = props;
return (
<div className={classes.headerPads}>
<Paper square className={ classes.root }>
<div className={classes.logo}>
<Typography variant="h5" className={classes.logoFont}>
PlaceHolder
</Typography>
</div>
//Problematic div: the search bar
<div style={{border: '1px solid white'}} className={ classes.containerHorizontalAlignment }>
<div className={classes.searchBar}>
<div className={classes.searchIcon}>
<SearchIcon />
</div>
<InputBase
placeholder='Search'
classes={{
root: classes.inputRoot,
input: classes.inputInput,
}}
/>
</div>
</div>
<div className={classes.loginButton}>
<IconButton className={classes.loginButton}>
<AccountCircle fontSize='large'/>
</IconButton>
</div>
</Paper>
</div>
);
}
StylishSearchBar.propTypes = {
classes: PropTypes.object.isRequired,
}
export default withStyles(styles)(StylishSearchBar);
Issues:
in
styles.inputInput
a transition is created using material-ui'stheme.transition.create
, however when passing percentages as widths, the component has a fixed width with no expansion or transitions.If the width of the searchBar exceeds its parent's capacity, it won't push the 'PlaceHolder' out of the
Paper
element, but instead will extend beyond the right boundary of thePaper
. At the same time, it may also impact the position of the button at the end of thePaper
.
The use of theme.create.transitions()
from material-ui is incorporated here, but suggestions involving only CSS are welcome for resolving the mentioned issues.
Feel free to test it in the sandbox here, and maximize the browser window within the sandbox for optimal viewing.
Thank you