├── .babelrc
├── .eslintrc
├── .gitignore
├── .npmignore
├── .prettierignore
├── CHANGELOG.md
├── LICENSE
├── README.md
├── example
└── src
│ ├── index.html
│ ├── index.js
│ ├── material_title_panel.js
│ ├── responsive_example.html
│ ├── responsive_example.js
│ └── sidebar_content.js
├── package-lock.json
├── package.json
├── rollup.config.js
├── src
└── sidebar.js
└── webpack.config.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | ["@babel/env", { "loose": true, "modules": false }],
4 | "@babel/react"
5 | ],
6 | "plugins": [
7 | "@babel/plugin-transform-runtime",
8 | "@babel/plugin-proposal-object-rest-spread",
9 | ["transform-react-remove-prop-types", { "mode": "unsafe-wrap" }]
10 | ]
11 | }
12 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "browser": true,
4 | "es6": true
5 | },
6 | "extends": ["airbnb", "prettier", "prettier/react"],
7 | "parser": "babel-eslint",
8 | "parserOptions": {
9 | "ecmaFeatures": {
10 | "jsx": true
11 | },
12 | "sourceType": "module"
13 | },
14 | "rules": {
15 | "react/destructuring-assignment": 0,
16 | "react/jsx-filename-extension": 0,
17 | "jsx-a11y/anchor-is-valid": 0,
18 | "no-plusplus": 0,
19 | "react/require-default-props": 0,
20 | "react/forbid-prop-types": 0,
21 | "react/no-access-state-in-setstate": 0
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | _dev/
3 | node_modules/
4 | example/compiled
5 | .DS_Store
6 | npm-debug.log
7 | dist
8 | .vscode
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | .vscode
2 | example
3 | node_modules
4 | src
5 | .babelrc
6 | .eslintrc
7 | .gitignore
8 | .nvmrc
9 | .travis.yml
10 | CHANGELOG.md
11 | package-lock.json
12 | rollup.config.js
13 | webpack.config.js
--------------------------------------------------------------------------------
/.prettierignore:
--------------------------------------------------------------------------------
1 | dist
2 | example/dist
3 | package-lock.json
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change log
2 |
3 | ## 3.0.0
4 |
5 | - prop-types is now dependency instead of peer dependency (@markusenglund)
6 | - PropTypes are removed in production to save on size (@markusenglund)
7 | - Now using babel in loose mode & babel-runtime to reduce bundle size further (@markusenglund)
8 | - Made the library available as ES Module in addition to CommonJS (@markusenglund)
9 | - Remove tab-index from overlay (@markusenglund)
10 | - Remove scroll bar when not needed by changing default content overflowY from "scroll" to "auto"
11 | - Added new id props to let users give custom id values to all elements (@rluiten)
12 | - Remove touch functionality in IOS since it doesn't work due to swipe-to-go-back native behaviour.
13 | - Remove box-shadow when the sidebar is not visible, so it's not visible at the edge of the screen.
14 |
15 | ## 2.3.2
16 |
17 | - prop-types is now a peer dependency (@Fallenstedt)
18 |
19 | ## 2.3.1
20 |
21 | - Modify content styles to have momentum scrolling (@Fallenstedt)
22 | - Update examples to eliminate depreciation warnings(@Fallenstedt)
23 | - Update readme's examples(@Fallenstedt)
24 |
25 | ## 2.3
26 |
27 | - Replace findDOMNode by ref callback (@BDav24)
28 | - Allow setting initial sidebar width (@BDav24)
29 |
30 | ## 2.2
31 |
32 | - Move from onTouchTap to onClick for React 15.2 compatibility (@factorize)
33 | - Fix accessibility issues (@cristian-sima)
34 |
35 | ## 2.1.4
36 |
37 | - Update included ES5 build with 2.1.3 changes
38 |
39 | ## 2.1.3
40 |
41 | - Added optional classNames (@sugarshin)
42 |
43 | ## 2.1.2
44 |
45 | - Fix server side rendering (@elliottsj)
46 |
47 | ## 2.1
48 |
49 | - Allow overriding embedded styles (@kulesa)
50 |
51 | ## 2.0.1
52 |
53 | - Allow adding className to sidebar using sidebarClassName prop (@lostdalek)
54 |
55 | ## 2.0.0
56 |
57 | - React 0.14 release
58 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Paulus Schoutsen
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React Sidebar [](http://badge.fury.io/js/react-sidebar) [](https://travis-ci.org/balloob/react-sidebar)
2 |
3 | React Sidebar is a sidebar component for React 0.14+. It offers the following features:
4 |
5 | - The sidebar can slide over the main content or dock next to it.
6 | - Touch enabled: swipe to open and close the sidebar like on a native mobile app.
7 | - Easy to combine with media queries to show the sidebar only when there's enough screen-width ([see example](http://balloob.github.io/react-sidebar/example/responsive_example.html)).
8 | - Works on both the left and right side.
9 | - Tiny size: <2.5kB gzipped
10 | - MIT license
11 |
12 | [See a demo here.](http://balloob.github.io/react-sidebar/example/)
13 |
14 | ## Touch specifics
15 |
16 | The touch interaction of the React Sidebar mimics the interactions that are supported by Android apps that implement the material design spec:
17 |
18 | - When the sidebar is closed, dragging from the left side of the screen will have the right side of the sidebar follow your finger.
19 | - When the sidebar is open, sliding your finger over the screen will only affect the sidebar once your finger is over the sidebar.
20 | - On release, it will call `onSetOpen` prop if the distance the sidebar was dragged is more than the `dragToggleDistance` prop.
21 |
22 | Note: The sidebar touch functionality doesn't work on IOS because of the "swipe-to-go-back" feature. Therefore the functionality has been disabled on IOS.
23 |
24 | ## Installation
25 |
26 | `npm install react-sidebar`
27 |
28 | If you use TypeScript, typings are [available on DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-sidebar) and can be installed with:
29 |
30 | `npm install @types/react-sidebar`
31 |
32 | ## Getting started
33 |
34 | By 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.
35 |
36 | Because 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.
37 |
38 | The minimum React component to integrate React Sidebar looks like this:
39 |
40 | ```jsx
41 | import React from "react";
42 | import Sidebar from "react-sidebar";
43 |
44 | class App extends React.Component {
45 | constructor(props) {
46 | super(props);
47 | this.state = {
48 | sidebarOpen: true
49 | };
50 | this.onSetSidebarOpen = this.onSetSidebarOpen.bind(this);
51 | }
52 |
53 | onSetSidebarOpen(open) {
54 | this.setState({ sidebarOpen: open });
55 | }
56 |
57 | render() {
58 | return (
59 | Sidebar content}
61 | open={this.state.sidebarOpen}
62 | onSetOpen={this.onSetSidebarOpen}
63 | styles={{ sidebar: { background: "white" } }}
64 | >
65 |
68 |
69 | );
70 | }
71 | }
72 |
73 | export default App;
74 | ```
75 |
76 | ## Responsive sidebar
77 |
78 | A 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.
79 |
80 | [mdn-matchmedia]: https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
81 |
82 | ```jsx
83 | import React from "react";
84 | import Sidebar from "react-sidebar";
85 |
86 | const mql = window.matchMedia(`(min-width: 800px)`);
87 |
88 | class App extends React.Component {
89 | constructor(props) {
90 | super(props);
91 | this.state = {
92 | sidebarDocked: mql.matches,
93 | sidebarOpen: false
94 | };
95 |
96 | this.mediaQueryChanged = this.mediaQueryChanged.bind(this);
97 | this.onSetSidebarOpen = this.onSetSidebarOpen.bind(this);
98 | }
99 |
100 | componentWillMount() {
101 | mql.addListener(this.mediaQueryChanged);
102 | }
103 |
104 | componentWillUnmount() {
105 | mql.removeListener(this.mediaQueryChanged);
106 | }
107 |
108 | onSetSidebarOpen(open) {
109 | this.setState({ sidebarOpen: open });
110 | }
111 |
112 | mediaQueryChanged() {
113 | this.setState({ sidebarDocked: mql.matches, sidebarOpen: false });
114 | }
115 |
116 | render() {
117 | return (
118 | Sidebar content}
120 | open={this.state.sidebarOpen}
121 | docked={this.state.sidebarDocked}
122 | onSetOpen={this.onSetSidebarOpen}
123 | >
124 | Main content
125 |
126 | );
127 | }
128 | }
129 |
130 | export default App;
131 | ```
132 |
133 | ## Supported props
134 |
135 | | Property name | Type | Default | Description |
136 | | ------------------ | ------------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
137 | | children | Anything React can render | n/a | The main content |
138 | | rootClassName | string | n/a | Add a custom class to the root component |
139 | | sidebarClassName | string | n/a | Add a custom class to the sidebar |
140 | | contentClassName | string | n/a | Add a custom class to the content |
141 | | overlayClassName | string | n/a | Add a custom class to the overlay |
142 | | defaultSidebarWidth | number | 0 | Width in pixles of the sidebar on render. Use this to stop the sidebar from poping in after intial render. (Overrides transitions) |
143 | | sidebar | Anything React can render | n/a | The sidebar content |
144 | | 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. |
145 | | docked | boolean | false | If the sidebar should be always visible |
146 | | open | boolean | false | If the sidebar should be open |
147 | | transitions | boolean | true | If transitions should be enabled |
148 | | touch | boolean | true | If touch gestures should be enabled |
149 | | touchHandleWidth | number | 20 | Width in pixels you can start dragging from the edge when the sidebar is closed. |
150 | | dragToggleDistance | number | 30 | Distance the sidebar has to be dragged before it will open/close after it is released. |
151 | | pullRight | boolean | false | Place the sidebar on the right |
152 | | shadow | boolean | true | Enable/Disable sidebar shadow |
153 | | styles | object | [See below](#styles) | Inline styles. These styles are merged with the defaults and applied to the respective elements. |
154 | | rootId | string | n/a | Add an id to the root component |
155 | | sidebarId | string | n/a | Add an id to the sidebar |
156 | | contentId | string | n/a | Add an id to the content. The driving use case for adding an element id to content was to allow react-scroll to scroll the content area of the site using react-sidebar. |
157 | | overlayId | string | n/a | Add an an id to the overlay |
158 |
159 | ## Styles
160 |
161 | Styles are passed as an object with 5 keys, `root`, `sidebar`, `content`, `overlay` and `dragHandle`, and merged to the following defaults:
162 |
163 | ```javascript
164 | {
165 | root: {
166 | position: "absolute",
167 | top: 0,
168 | left: 0,
169 | right: 0,
170 | bottom: 0,
171 | overflow: "hidden"
172 | },
173 | sidebar: {
174 | zIndex: 2,
175 | position: "absolute",
176 | top: 0,
177 | bottom: 0,
178 | transition: "transform .3s ease-out",
179 | WebkitTransition: "-webkit-transform .3s ease-out",
180 | willChange: "transform",
181 | overflowY: "auto"
182 | },
183 | content: {
184 | position: "absolute",
185 | top: 0,
186 | left: 0,
187 | right: 0,
188 | bottom: 0,
189 | overflowY: "auto",
190 | WebkitOverflowScrolling: "touch",
191 | transition: "left .3s ease-out, right .3s ease-out"
192 | },
193 | overlay: {
194 | zIndex: 1,
195 | position: "fixed",
196 | top: 0,
197 | left: 0,
198 | right: 0,
199 | bottom: 0,
200 | opacity: 0,
201 | visibility: "hidden",
202 | transition: "opacity .3s ease-out, visibility .3s ease-out",
203 | backgroundColor: "rgba(0,0,0,.3)"
204 | },
205 | dragHandle: {
206 | zIndex: 1,
207 | position: "fixed",
208 | top: 0,
209 | bottom: 0
210 | }
211 | };
212 | ```
213 |
214 | ## Acknowledgements
215 |
216 | My goal was to make a React Component that implements the [material design spec for navigation drawers](https://material.io/design/components/navigation-drawer.html). 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.
217 |
--------------------------------------------------------------------------------
/example/src/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
91 | This example will automatically dock the sidebar if the page width
92 | is above 800px (which is currently {this.state.docked.toString()}
93 | ).
94 |
95 |
96 | This functionality should live in the component that renders the
97 | sidebar. This way you're able to modify the sidebar and main
98 | content based on the responsiveness data. For example, the menu
99 | button in the header of the content is now{" "}
100 | {this.state.docked ? "hidden" : "shown"} because the sidebar is{" "}
101 | {!this.state.docked && "not"} visible.
102 |