Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-router-tabs

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-router-tabs

Dead simple navigation tabs for React Router 4

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5K
decreased by-17.75%
Maintainers
1
Weekly downloads
 
Created
Source

React Router Tabs

npm npm GitHub stars

Simple navigation tabs for React Router 4.

preview-gif

Why?

There are many plain React solutions for tabs that conditionally render content based on some "activeTab" state. In a React Router app it would be preferable to keep rendering logic consistent by only using <Route>s. This library exports a simple wrapper component to turn any enclosing element into a path-aware navigation tab using React Router links.

Benefits of this library

  • Simplifies assigning tabs to paths
  • Abstracts matching, setting active styles and onClick handling
  • <RoutedTabs> compound component lets you set props for nested tabs (classnames, styles, path prefix, ...others)
  • Easy to use with different route setups (see examples)
  • Uses React Router's own API
  • No state, no assertions about tab content, just navigation

Installation

via yarn

$ yarn add react-router-tabs

or npm

$ npm install --save react-router-tabs
Strict dependencies
  • react-router-dom
  • prop-types
Peer dependencies
  • react

Usage

import React from 'react';
import { Route, Switch, Redirect } from 'react-router-dom';
import { RoutedTabs, NavTab } from 'react-router-tabs';
import { Admins, Moderators, Users } from './components';

// Using default styles:
import 'styles/react-router-tabs.css';

const UsersPage = ({ match }) => {
  return (
    <div>
      <RoutedTabs startPathWith={match.path}>
        <NavTab to="/admins">Admins</NavTab>
        <NavTab to="/moderators">Moderators</NavTab>
        <NavTab to="/users">Users</NavTab>
      </RoutedTabs>

      <Switch>
        <Route exact path={`${match.path}`} render={() => <Redirect replace to={`${match.path}/admins`} />} />
        <Route path={`${match.path}/admins`} component={Admins} />
        <Route path={`${match.path}/moderators`} component={Moderators} />
        <Route path={`${match.path}/users`} component={Users} />
      </Switch>
    </div>
  );
};

export default UsersPage;

Components

<RoutedTabs />

Compound component to set props on enclosing <NavTabs>s.

(optional but recommended)

PropTypeDefaultDescription
startPathWithstringemptyString to append to the start of every tab's path to simplify writing out full paths. In most cases this should be given props.match.path
classNamestring'react-router-tabs'Custom className for the enclosing <div>s
stylestringreact-router-tabs'Custom inline styles for the enclosing <div>s
tabClassNamestring'react-router-tabs__tab'Custom className to be provided to each tab
activeTabClassNamestring'active'Custom activeClassName to be provided to each tab
tabStylestringemptyCustom inline style to be provided to each tab
activeTabStylestringemptyCustom inline style to be provided to each tab
<NavTab />

Forked and adapted from React Router's <NavLink />. More info on below props in the official React Router docs.

PropTypeDefaultDescription
tostringrequiredPath to the route to be rendered
replacebooltrueReplace current browser path rather than adding to the history
exactboolfalseRequire exact path match (won't work for nested routes)
strictbooltrueTrailing slash considered for path match
disabledboolfalseSets disabled className/style and blocks onClick()
classNamestring'react-router-tabs'Custom className for this tab
activeClassNamestring'active'Custom activeClassName for this tab
stylestringemptyCustom inline style
activeStylestringemptyCustom inline style when active

Styling

Both components can take various classname and inline style props.

<RoutedTabs> will provide tabClassName/activeTabClassName and tabStyle/activeTabStyle to any nested <NavTabs>. Please see the API above for more detail.

If no classnames or styles are provided, the default classnames will be used. A default stylesheet is provided in this repo (CSS/SASS versions) and can be imported like below:

import 'node_modules/react-router-tabs/styles/react-router-tabs.css';

Note: the provided styles have not been tested.

Acknowledgements

Thanks to the React Router team. This module only exists to simplify one of a thousand use-cases for their great library.

Author

Chace Stewart (chacestew@gmail.com)

License

MIT

Keywords

FAQs

Package last updated on 30 Dec 2017

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc