Giter VIP home page Giter VIP logo

react-router-active-component's Introduction

React Router Active Component

Travis Codecov npm package

A factory function for creating React components which get a special className when a specified React Router route is active, and can also handle creating a link to the route.

The primary use case for this module is making it convenient to create components which contain a link to a route but put the active class name on the container rather than the link:

Using React Router's Link component:

<ul>                                      <ul>
  <li>                                      <li>
    <Link to="/route"                 =>      <a href="/route" class="active">
          activeClassName="active">             Link Text
      Link Text

Using the activeComponent() factory function:

var NavLink = activeComponent('li')
<ul>                           <ul>
  <NavLink to="/route">   =>     <li class="active">
    Link Text                      <a href="/route">
                                     Link Text

The demo shows:

  • A NavLink component which creates a navigation <li> (including its <a>) which gets an "active" class, as required by Bootstrap's CSS.

    var NavLink = activeComponent('li')
  • An ActivePara component which creates a <p> which only gets a "special" class for a specific route.

    var ActivePara = activeComponent('p', {link: false})
    <ActivePara to="/path/to" activeClassName="special">...</ActivePara>

Install

npm install react-router-active-component
import activeComponent from 'react-router-active-component'
// or
var activeComponent = require('react-router-active-component')

API

activeComponent(component[, options])

Creates a ReactComponent which takes the same props as React Router's Link component to configure when it is considered active and which class it will be given when active.

The component's contents will be wrapped in the given tag name or component - by default, its children will be used as contents for a <Link/>.

component: String | ReactComponent

This can be anything that can be passed as the first argument to React.createElement() - a tag name string or a ReactComponent which will be used to wrap the component's children.

var NavItem = activeComponent('li')

If a custom React component is given, the following additional props will be passed to it when rendering:

  • active: Boolean - true if the specified route is active, false otherwise.
  • className: String - contains any className passed to the component plus its activeClassName, if active.
  • style: Object - the activeStyle object passed to the component, if active.
options: Object

An options object with the following properties:

link: Boolean (default: true)

If true, the component's props and children be used to create a <Link/>. Otherwise, its children will just be wrapped in the specified component.

var ActiveParagraph = activeComponent('p', {link: false})
linkClassName : String

A default className for the nested <Link/>.

Component Props

The component created by activeComponent() takes the same props as React Router's Link component. See the React Router Link API docs for details.

One difference is that activeClassName will default to active if not provided, since determining if a component is active is the whole point of using activeComponent()!

Use the onlyActiveOnIndex boolean prop to control when a component is considered active when its URL path matches part of a deeper path - for example, if you have a "Home" navigation link which you only want to display as active when the current path is '/', you should use this prop:

<NavLink to="/" onlyActiveOnIndex>Home</NavLink>
<NavLink to="/tasks">Tasks</NavLink>

Pass a linkProps object to provide additional props when rendering a nested <Link/>:

<NavLink to="/special" linkProps={{className: 'special', onClick: this.handleSpecial}}>
  Special Link
</NavLink>

MIT Licensed

react-router-active-component's People

Contributors

adamcharnock avatar insin avatar nioushajafari avatar signalwerk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-router-active-component's Issues

Is this package maintained?

Hi, I am thinking about submitting a PR for react-v16's

  1. React.createClass is deprecated.
  2. import t from 'prop-types'
  3. Add eslint and prettier config

But it seems this package is no longer maintained, as I see #8 is not merged for longer than one year.

Is this package maintained?

Error: Can't resolve 'react-router/lib/Link'

I have an error at compile time inside react-router-active-component:
Module not found: Error: Can't resolve 'react-router/lib/Link',
this module does not even work, because it can not find the /lib/ directory in the latest major version of
react-router.
Current dependency ver. of the react-router is ^2.4.0, but the last is 4.2.0.
I suggest upgrading to the latest version of this dependency.

react-router 1.0.0

react-router is now nearing 1.0.0 and 1.0.0-beta3 is currently on npm. This may have to be updated due to the API changes that have happened.

Warning: "Unknown props `active`, `activeKey`, `activeHref"

I am getting the following annoying warnings in the browser console:

warning.js:19 Warning: Unknown props active, activeKey, activeHref on < li> tag. Remove these props from the element. For details, see https://fb.me/react-unknown-prop
in li (created by ActiveComponent)
in ActiveComponent (created by withRouter(ActiveComponent))
in withRouter(ActiveComponent) (created by MainLayout)
in ul (created by Nav)
in Nav (created by MainLayout)
in div (created by NavbarCollapse)
in Transition (created by Collapse)
in Collapse (created by NavbarCollapse)
in NavbarCollapse (created by MainLayout)
in div (created by Grid)
in Grid (created by Navbar)
in nav (created by Navbar)
in Navbar (created by Uncontrolled(Navbar))
in Uncontrolled(Navbar) (created by MainLayout)
in div (created by MainLayout)
in MainLayout (created by RouterContext)
in RouterContext (created by Router)
in Router (created by App)
in App

According to react-bootstrap/react-bootstrap#1994, react-router-active-component should filter out irrelevant props coming from parent components. Unless the culprit is react-router?

On another note, it would be good if you could publish the typings to allow easy usage with TypeScript.

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.