|
1 | | -Integrating React Banner with [React Sidebar][1] is easy and takes only minutes to set up. |
| 1 | +Top navigation bars are great when your site is being viewed on larger screens like desktops and laptops, however when users visit your site from a mobile phone you need to offer a more friendly alternative. There's a lot of options out there: [top-down menus][2], [full-screen overlay menus][3], [panels][4] and many more. Personally I think side panels are the most intuitive and, with [React Sidebar][1], very easy to configure. |
2 | 2 |
|
3 | | -*More information coming soon...* |
| 3 | +> Tip: View this page on a mobile screen (or with mobile dev tools) to see this in action. Clicking the hamburger menu in the top right corner will display the sidebar. |
4 | 4 |
|
5 | 5 |
|
6 | | -[1]: https://github.com/balloob/react-sidebar |
| 6 | +## Basic Setup |
| 7 | + |
| 8 | +Luckily, this package provides a built-in hamburger menu displayed on smaller screens that you can hook into to display the menu. Going forward with our original example, you would `npm install react-sidebar --save` and update the code as such: |
| 9 | + |
| 10 | +```javascript |
| 11 | +import React from 'react' |
| 12 | +import Banner from 'react-banner' |
| 13 | +import Sidebar from 'react-sidebar' // Import it |
| 14 | +import 'react-banner/dist/style.css' |
| 15 | + |
| 16 | +// Define sidebar content (here it's just an empty div) |
| 17 | +const SidebarContent = props => { |
| 18 | + return ( |
| 19 | + <div style={{ |
| 20 | + width: '80vw', |
| 21 | + height: '100vh', |
| 22 | + background: 'white' |
| 23 | + }} /> |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +// Use a full component class to maintain visible state |
| 28 | +export default class Site extends React.Component { |
| 29 | + state = { |
| 30 | + sidebarVisible: false |
| 31 | + } |
| 32 | + |
| 33 | + render() { |
| 34 | + return ( |
| 35 | + <Sidebar |
| 36 | + sidebar={ <} |
| 37 | + open={ this.state.sidebarVisible } |
| 38 | + onSetOpen={ this._toggleSidebar }> |
| 39 | + <Banner |
| 40 | + logo="My Logo" |
| 41 | + url={ window.location.pathname } |
| 42 | + onMenuClick={ this._openSidebar } |
| 43 | + links={[ |
| 44 | + { "title": "Example Link", "url": "/example" }, |
| 45 | + { "title": "Another", "url": "/another" }, |
| 46 | + { "title": "Link w/ Children", "url": "/children", "children": [ |
| 47 | + { "title": "John", "url": "/children/john" }, |
| 48 | + { "title": "Jill", "url": "/children/jill" }, |
| 49 | + { "title": "Jack", "url": "/children/jack" } |
| 50 | + ]} |
| 51 | + ]} /> |
| 52 | + |
| 53 | + <main> |
| 54 | + <h2>Hey, I'm some content</h2> |
| 55 | + </main> |
| 56 | + </Sidebar> |
| 57 | + ) |
| 58 | + } |
| 59 | +
|
| 60 | + _toggleSidebar = visible => { |
| 61 | + this.setState({ |
| 62 | + sidebarVisible: visible |
| 63 | + }) |
| 64 | + } |
| 65 | +
|
| 66 | + _openSidebar = () => { |
| 67 | + this._toggleSidebar(true) |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | +
|
| 72 | +> Note: The above example just shows an empty `<div>` for brevity, however in a real site you'd likely use the same link array passed to the `Banner` to generate a vertical navigation menu displayed within `SidebarContent`. |
| 73 | + |
| 74 | +Using [React Sidebar][1] is just one of many possibilities. Using the `onMenuClick` property you could pass a callback that triggers any kind of menu or behavior to occur. |
| 75 | + |
| 76 | + |
| 77 | +[1]: https://github.com/balloob/react-sidebar |
| 78 | +[2]: https://dribbble.com/shots/2225840-Simplified-Mobile-Navigation |
| 79 | +[3]: https://dribbble.com/shots/2193651-Mobile-menu |
| 80 | +[4]: https://dribbble.com/shots/2695255-Profile-and-side-panel-menu |
0 commit comments