Socket
Socket
Sign inDemoInstall

react-sidebar

Package Overview
Dependencies
5
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-sidebar

A sidebar component for React.


Version published
Maintainers
1
Install size
47.9 kB
Created

Readme

Source

React Sidebar 2.0 npm version Build Status

If you're on React 0.13, use React Sidebar 1.1.0.
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 nameTypeDefaultDescription
childrenAnything React can rendern/aThe main content
rootClassNamestringn/aAdd a custom class to the root component
sidebarClassNamestringn/aAdd a custom class to the sidebar
contentClassNamestringn/aAdd a custom class to the content
overlayClassNamestringn/aAdd a custom class to the overlay
sidebarAnything React can rendern/aThe sidebar content
onSetOpenfunctionn/aCallback 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.
dockedbooleanfalseIf the sidebar should be always visible
openbooleanfalseIf the sidebar should be open
transitionsbooleantrueIf transitions should be enabled
touchbooleantrueIf touch gestures should be enabled
touchHandleWidthnumber20Width in pixels you can start dragging from the edge when the sidebar is closed.
dragToggleDistancenumber30Distance the sidebar has to be dragged before it will open/close after it is released.
pullRightbooleanfalsePlace the sidebar on the right
shadowbooleantrueEnable/Disable sidebar shadow
stylesobjectSee belowInline 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

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.

Keywords

FAQs

Last updated on 20 Jul 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc