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

  • 1.3.2
  • next
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React Router Tabs

npm npm GitHub stars

Simple navigation tabs for React Router 4 and 5.

preview-gif

Why?

There are many plain React solutions for tabs that conditionally render content based on some local state. In a React Router app it would be preferable to keep rendering logic consistent by only using <Route /> components. This library exports a simple component called <NavTab /> which wraps React Router's Link and makes it behave more like a tab.

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
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";

// with default styles:
import "styles/react-router-tabs.css";

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

      <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

<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 for active styling
strictbooltrueTrailing slash considered for path match
disabledboolfalseDisables clicking on this tab
allowClickOnActiveboolfalseAllows clicking even when active
classNamestring'nav-tab'Custom className for this tab
activeClassNamestring'active'Custom activeClassName for this tab
styleobjectemptyCustom inline style
activeStyleobjectemptyCustom inline style when active

<RoutedTabs /> (optional)

Helper compound component to pass props to all child <NavTab> components. Renders as a div by default.

<RoutedTabs
  startPathWith={match.path}
  tabClassName="tab-link"
  activeTabClassName="active"
>
  <NavTab to="/admins">Admins</NavTab> // links to `${match.path}/admins`
  <NavTab to="/moderators">Moderators</NavTab>
  <NavTab to="/users">Users</NavTab>
  // etc
</RoutedTabs>

NB: NavTab must be the direct child of RoutedTabs for it to do anything. If a standard DOM element is detected, no props will be passed down. If your DOM tree requires more complex nesting, you probably don't want to use this helper.

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
elementTypestring'div'The element to render as the container
classNamestring'react-router-tabs'className for the container
styleobjectemptyStyles for the container
tabClassNamestring'nav-tab'className to be provided to each tab
activeTabClassNamestring'active'activeClassName to be provided to each tab
tabStyleobjectemptyStyles to be provided to each tab
activeTabStyleobjectemptyStyles to be provided to each tab
... othersanynoneAny other props RoutedTabs doesn't expect will be passed to its children

Styling

<NavTab> can take a className and activeClassName, and/or style and activeStyle props. If no classes are provided, it will be given defaults (see API above).

<RoutedTabs> can also be provided with tabClassName prop to pass to its children. This will be overwritten if the <NavTab> has its own className prop.

A default stylesheet is provided in both .css and .scss at:

node_modules/react-router-tabs/styles/react-router-tabs

Please note these styles have not been tested and are provided mainly as an example to work from.

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 08 Apr 2019

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