In the SUSI Web Chat application we wanted to make few static pages such as: Overview, Terms, Support, Docs, Blog, Team, Settings and About. The idea was to show them in app bar. Requirements were to have the capability to collapse on small viewports and to use Material UI components. In this blog post I’m going to elaborate how we built the responsive app bar Using Material UI components.
First we added usual Material UI app bar component like this.
<header className="nav-down" id="headerSection"> <AppBar className="topAppBar" title={<a href={this.state.baseUrl} ><img src="susi-white.svg" alt="susi-logo" className="siteTitle"/></a>} style={{backgroundColor:'#0084ff'}} onLeftIconButtonTouchTap={this.handleDrawer} iconElementRight={<TopMenu />} /> </header>
We added SUSI logo instead of the text title using below code snippet and linked it to the home page like this.
title={<a href={this.state.baseUrl} ><img src="susi-white.svg" alt="susi-logo" className="siteTitle"/></a>}
We have defined “this.state.baseUrl” in constructor and it gets the base url of the web application.
this.state = { openDrawer: false, baseUrl: window.location.protocol + '//' + window.location.host + '/' };
We need to open the right drawer when we click on the button on top left corner. So we have to define two methods to open and close drawer as below.
handleDrawer = () => this.setState({openDrawer: !this.state.openDrawer}); handleDrawerClose = () => this.setState({openDrawer: false});
Now we have to add components that we need to show on the right side of the app bar. We connect those elements to the app bar like this. “iconElementRight={}”
We defined “TopMenu” Items like this.
const TopMenu = (props) => ( <div> <div className="top-menu"> <FlatButton label="Overview" href="/overview" style={{color:'#fff'}} className="topMenu-item"/> <FlatButton label="Team" href="/team" style={{color:'#fff'}} className="topMenu-item"/> </div>
We added FlatButtons to place links to other static pages. After all we needed a FlatButton that gives IconMenu to show login and signup options.
<IconMenu {...props} iconButtonElement={ <IconButton iconStyle={{color:'#fff'}} ><MoreVertIcon /></IconButton> }> <MenuItem primaryText="Chat" containerElement={<Link to="/logout" />} rightIcon={<Chat/>}/> </IconMenu> </div> );
After adding all these correctly you will see this kind of an app bar in your application.
Now our app bar is ready. But it does not collapse on small viewports.
So we planned to hide flat buttons on small sized screens and show the menu button. For that we used media queries.
@media only screen and (max-width: 800px){ .topMenu-item{ display: none !important; } .topAppBar button{ display: block !important; } }
This is how we built the responsive app bar using Material UI components. You can check the preview from this url. If you are willing to contribute to SUSI Web Chat here is the GitHub repository.
Resources:
- Material UI Components: http://www.material-ui.com/#/components/
- Learn More about media queries: https://www.w3schools.com/css/css_rwd_mediaqueries.asp