Code Monkey home page Code Monkey logo

react-sidenav's Introduction

react-sidenav

npm version Travis

Side Navigation Component for React

alt tag

Usage

Install via npm

npm install --save react-sidenav

Then import it using es6 modules

import React from 'react';
import SideNav, { Nav, NavIcon, NavText } from 'react-sidenav';
import SvgIcon from 'react-icons-kit';

import { ic_aspect_ratio } from 'react-icons-kit/md/ic_aspect_ratio';
import { ic_business } from 'react-icons-kit/md/ic_business';


//specify the base color/background of the parent container if needed
const MySideNav = () => (
    <div style={{background: '#2c3e50', color: '#FFF', width: 220}}> 
        <SideNav highlightColor='#E91E63' highlightBgColor='#00bcd4' defaultSelected='sales'>       
            <Nav id='dashboard'>
                <NavIcon><SvgIcon size={20} icon={ic_aspect_ratio}/></NavIcon>    
                <NavText> Dashboard </NavText>
            </Nav>
            <Nav id='sales'>
                <NavIcon><SvgIcon size={20} icon={ic_business}/></NavIcon>
                <NavText> Sales </NavText>
            </Nav>
        </SideNav>
    </div>
)

Item Selection

To highlight any item selected, you can either let the SideNav instance manage it by specifying defaultSelected property, or by passing selected property. If you pass the selected property then the SideNav is in what we say stateless mode.

//managed SideNav, state is handled within the component
<SideNav defaultSelected='xyz'>
  ...
</SideNav>
//stateless SideNav, state is handled within the component
<SideNav selected='xyz'>
  ...
</SideNav>

Item Id

Note that a sub nav id only needs to be unique within its siblings. SideNav automatically uses / to identify sub navigation.

Nav id='sales' --- Nav id='list'

We can select list by setting the selected property to 'sales/list'

Listening to item change

Register an onItemSelection props

<SideNav selected='xyz' onItemSelection={ (id, parent) => {}}>
  <Nav />
  <Nav />
</SideNav>

If its a top level nav, then parent is null.

Props

SideNav

Property Description Type
highlightColor color when an item is selected string
highlightBgColor background color when an item is selected string
hoverBgColor background color on hover. Defaults to highlightBgColor string
hoverColor color on hover. Defaults to highlightColor or inherit string
selected selected item, will be stateless string
defaultSelected default id of selected item, will auto manage state string
onItemSelection function called when an item is clicked (id, parent) function

React Router 4 Integration

To use with React Router 4, you can use the hoc withRR4 to create a SideNav. Please see playground folder for a full example

import { withRR4 } from 'react-sidenav';
import { BrowserRouter as Router } from 'react-router';

const SideNav = withRR4();

export const Side = () => (
    <Router>
        <SideNav>
            //nav items and route will automatically updated upon selection
        </SideNav>
    </Router>
);

NavIcon and NavText

These 2 components now support style and className props

Examples

The source code for the screenshot is under playground/index.js

Development

  1. Clone the repo
git clone https://github.com/wmira/react-sidenav.git
  1. Run npm install
cd react-sidenav
npm install
  1. Run playground script. The script below starts the dev server on port 8080.
npm run playground -- playground/index.js

To change the port, pass --port

npm run playground -- --port=8181 playground/index.js

Contributing

Contributions are welcome in any form.

react-sidenav's People

Contributors

wmira avatar keul avatar alexkovalevych avatar hagbarth avatar timothystewart6 avatar wozacosta avatar

Watchers

James Cloos avatar Vagabondan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.