UNPKG

6.02 kBMarkdownView Raw
1React Sidebar 2.0 [![npm version](https://badge.fury.io/js/react-sidebar.svg)](http://badge.fury.io/js/react-sidebar) [![Build Status](https://travis-ci.org/balloob/react-sidebar.svg)](https://travis-ci.org/balloob/react-sidebar)
2=============
3
4#### [If you're on React 0.13, use React Sidebar 1.1.0.](https://github.com/balloob/react-sidebar/tree/63b5f19527afd50c9cae4705a5766f8f845a795d)
5#### Breaking change since v1.1.0 is that the sidebar no longer has a white background.
6
7React Sidebar is a sidebar component for React 0.14+. It offers the following features:
8
9 - Have the sidebar slide over main content
10 - Dock the sidebar on the left of the content
11 - Touch enabled: swipe to open and close the sidebar
12 - Easy to combine with media queries for auto-docking ([see example](http://balloob.github.io/react-sidebar/example/responsive_example.html))
13 - Sidebar and content passed in as PORCs (Plain Old React Components)
14 - MIT license
15 - Only dependency is React.
16
17[See a demo here.](http://balloob.github.io/react-sidebar/example/)
18
19Touch specifics
20---------------
21The touch interaction of the React Sidebar mimics the interactions that are supported by Android apps that implement the material design spec:
22
23 - When the sidebar is closed, dragging from the left side of the screen will have the right side of the sidebar follow your finger.
24 - When the sidebar is open, sliding your finger over the screen will only affect the sidebar once your finger is over the sidebar.
25 - On release, it will call `onSetOpen` prop if the distance the sidebar was dragged is more than the `dragToggleDistance` prop.
26
27Supported props
28---------------
29
30| Property name | Type | Default | Description |
31|---------------|------|---------|-------------|
32| children | Anything React can render | n/a | The main content |
33| sidebar | Anything React can render | n/a | The sidebar content |
34| onSetOpen | function | n/a | Callback called when the sidebar wants to change the open prop. Happens after sliding the sidebar and when the overlay is clicked when the sidebar is open. |
35| docked | boolean | false | If the sidebar should be always visible |
36| open | boolean | false | If the sidebar should be open |
37| transitions | boolean | true | If transitions should be enabled |
38| touch | boolean | true | If touch gestures should be enabled |
39| touchHandleWidth | number | 20 | Width in pixels you can start dragging from the edge when the sidebar is closed. |
40| dragToggleDistance | number | 30 | Distance the sidebar has to be dragged before it will open/close after it is released. |
41| pullRight | boolean | false | Place the sidebar on the right |
42| shadow | boolean | true | Enable/Disable sidebar shadow |
43
44Installation
45------------
46React Sidebar is available on NPM. Install the package into your project: `npm install react-sidebar --save`
47
48Getting started
49-----------------
50By design, React Sidebar does not keep track of whether it is open or not. This has to be done by the parent component. This allows the parent component to make changes to the sidebar and main content based on the open/docked state. An example could be to hide the "show menu" button from the main content when the sidebar is docked.
51
52Because React Sidebar can be toggled by dragging the sidebar into its open/closed position, you will have to pass in an `onSetOpen` method as a prop to allow the sidebar to control the open state in the parent.
53
54The minimum React component to integrate React Sidebar looks like this:
55
56```javascript
57var React = require('react');
58var Sidebar = require('react-sidebar');
59
60var App = React.createClass({
61 getInitialState: function() {
62 return {sidebarOpen: false};
63 },
64
65 onSetSidebarOpen: function(open) {
66 this.setState({sidebarOpen: open});
67 },
68
69 render: function() {
70 var sidebarContent = <b>Sidebar content</b>;
71
72 return (
73 <Sidebar sidebar={sidebarContent}
74 open={this.state.sidebarOpen}
75 onSetOpen={this.onSetSidebarOpen}>
76 <b>Main content</b>
77 </Sidebar>
78 );
79 }
80});
81
82module.exports = App;
83```
84
85Responsive sidebar
86------------------
87A common use case for a sidebar is to show it automatically when there is enough screen width available. This can be achieved using media queries via [`window.matchMedia`][mdn-matchmedia]. This again has to be integrated into the parent component so you can use the information to make changes to the sidebar and main content.
88
89[mdn-matchmedia]: https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
90
91```javascript
92var React = require('react');
93var Sidebar = require('react-sidebar');
94
95var App = React.createClass({
96 getInitialState() {
97 return {sidebarOpen: false, sidebarDocked: false};
98 },
99
100 onSetSidebarOpen: function(open) {
101 this.setState({sidebarOpen: open});
102 },
103
104 componentWillMount: function() {
105 var mql = window.matchMedia(`(min-width: 800px)`);
106 mql.addListener(this.mediaQueryChanged);
107 this.setState({mql: mql, docked: mql.matches});
108 },
109
110 componentWillUnmount: function() {
111 this.state.mql.removeListener(this.mediaQueryChanged);
112 },
113
114 mediaQueryChanged: function() {
115 this.setState({sidebarDocked: this.state.mql.matches});
116 },
117
118 render: function() {
119 var sidebarContent = <b>Sidebar content</b>;
120
121 return (
122 <Sidebar sidebar={sidebarContent}
123 open={this.state.sidebarOpen}
124 docked={this.state.sidebarDocked}
125 onSetOpen={this.onSetSidebarOpen}>
126 <b>Main content</b>
127 </Sidebar>
128 );
129 }
130});
131
132module.exports = App;
133```
134
135Acknowledgements
136----------------
137
138My goal was to make a React Component that implements the [material design spec for navigation drawers](http://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-content). My initial attempt was to improve [hamburger-basement by arnemart](https://github.com/arnemart/hamburger-basement) but I quickly figured that I better start from scratch. Still, that project helped me a ton to get started.