Skip to content

MilllerTime/react-sidebar

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Sidebar 2.0 npm version Build Status

Breaking change since v1.1.0 is that the sidebar no longer has a white background.

React Sidebar is a sidebar component for React 0.14+. It offers the following features:

  • Have the sidebar slide over main content
  • Dock the sidebar on the left of the content
  • Touch enabled: swipe to open and close the sidebar
  • Easy to combine with media queries for auto-docking (see example)
  • Sidebar and content passed in as PORCs (Plain Old React Components)
  • MIT license
  • Only dependency is React.

See a demo here.

Change log

2.2

  • Move from onTouchTap to onClick for React 15.2 compatibility (@factorize)
  • Fix accessibility issues (@cristian-sima)

2.1.4

  • Update included ES5 build with 2.1.3 changes

2.1.3

  • Added optional classNames (@sugarshin)

2.1.2

  • Fix server side rendering (@elliottsj)

2.1

  • Allow overriding embedded styles (@kulesa)

2.0.1

  • Allow adding className to sidebar using sidebarClassName prop (@lostdalek)

2.0.0

  • React 0.14 release

Touch specifics

The touch interaction of the React Sidebar mimics the interactions that are supported by Android apps that implement the material design spec:

  • When the sidebar is closed, dragging from the left side of the screen will have the right side of the sidebar follow your finger.
  • When the sidebar is open, sliding your finger over the screen will only affect the sidebar once your finger is over the sidebar.
  • On release, it will call onSetOpen prop if the distance the sidebar was dragged is more than the dragToggleDistance prop.

Supported props

Property name Type Default Description
children Anything React can render n/a The main content
rootClassName string n/a Add a custom class to the root component
sidebarClassName string n/a Add a custom class to the sidebar
contentClassName string n/a Add a custom class to the content
overlayClassName string n/a Add a custom class to the overlay
sidebar Anything React can render n/a The sidebar content
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.
docked boolean false If the sidebar should be always visible
open boolean false If the sidebar should be open
transitions boolean true If transitions should be enabled
touch boolean true If touch gestures should be enabled
touchHandleWidth number 20 Width in pixels you can start dragging from the edge when the sidebar is closed.
dragToggleDistance number 30 Distance the sidebar has to be dragged before it will open/close after it is released.
pullRight boolean false Place the sidebar on the right
shadow boolean true Enable/Disable sidebar shadow
styles object See below Inline styles. These styles are merged with the defaults and applied to the respective elements.

Installation

React Sidebar is available on NPM. Install the package into your project: npm install react-sidebar --save

If you use TypeScript, typings are available on DefinitlyTyped and can be installed with: npm install --save @types/react-sidebar

Getting started

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.

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.

The minimum React component to integrate React Sidebar looks like this:

var React = require('react');
var Sidebar = require('react-sidebar').default;

var App = React.createClass({
  getInitialState: function() {
    return {sidebarOpen: false};
  },

  onSetSidebarOpen: function(open) {
    this.setState({sidebarOpen: open});
  },

  render: function() {
    var sidebarContent = <b>Sidebar content</b>;

    return (
      <Sidebar sidebar={sidebarContent}
               open={this.state.sidebarOpen}
               onSetOpen={this.onSetSidebarOpen}>
        <b>Main content</b>
      </Sidebar>
    );
  }
});

module.exports = App;

Responsive sidebar

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. 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.

var React = require('react');
var Sidebar = require('react-sidebar');

var App = React.createClass({
  getInitialState() {
    return {sidebarOpen: false, sidebarDocked: false};
  },

  onSetSidebarOpen: function(open) {
    this.setState({sidebarOpen: open});
  },

  componentWillMount: function() {
    var mql = window.matchMedia(`(min-width: 800px)`);
    mql.addListener(this.mediaQueryChanged);
    this.setState({mql: mql, sidebarDocked: mql.matches});
  },

  componentWillUnmount: function() {
    this.state.mql.removeListener(this.mediaQueryChanged);
  },

  mediaQueryChanged: function() {
    this.setState({sidebarDocked: this.state.mql.matches});
  },

  render: function() {
    var sidebarContent = <b>Sidebar content</b>;

    return (
      <Sidebar sidebar={sidebarContent}
               open={this.state.sidebarOpen}
               docked={this.state.sidebarDocked}
               onSetOpen={this.onSetSidebarOpen}>
        <b>Main content</b>
      </Sidebar>
    );
  }
});

module.exports = App;

Styles

Styles are passed as an object with 5 keys, root, sidebar, content, overlay and dragHandle, and merged to the defaults:

{
  root: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    overflow: 'hidden',
  },
  sidebar: {
    zIndex: 2,
    position: 'absolute',
    top: 0,
    bottom: 0,
    transition: 'transform .3s ease-out',
    WebkitTransition: '-webkit-transform .3s ease-out',
    willChange: 'transform',
    overflowY: 'auto',
  },
  content: {
    position: 'absolute',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    overflow: 'auto',
    transition: 'left .3s ease-out, right .3s ease-out',
  },
  overlay: {
    zIndex: 1,
    position: 'fixed',
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    opacity: 0,
    visibility: 'hidden',
    transition: 'opacity .3s ease-out',
    backgroundColor: 'rgba(0,0,0,.3)',
  },
  dragHandle: {
    zIndex: 1,
    position: 'fixed',
    top: 0,
    bottom: 0,
  },
}

Acknowledgements

My goal was to make a React Component that implements the material design spec for navigation drawers. My initial attempt was to improve hamburger-basement by arnemart but I quickly figured that I better start from scratch. Still, that project helped me a ton to get started.

Packages

No packages published

Languages

  • JavaScript 98.3%
  • HTML 1.7%