Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add active class to link #1960

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const ErrorDebug = process.env.NODE_ENV === 'production'

export default class App extends Component {
static childContextTypes = {
headManager: PropTypes.object
headManager: PropTypes.object,
router: PropTypes.object
}

getChildContext () {
const { headManager } = this.props
return { headManager }
const { headManager, router } = this.props
return { headManager, router }
}

render () {
Expand Down
9 changes: 9 additions & 0 deletions lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default class Link extends Component {
]).isRequired
}

static contextTypes = {
router: PropTypes.object
}

componentWillReceiveProps (nextProps) {
this.formatUrls(nextProps)
}
Expand Down Expand Up @@ -106,6 +110,7 @@ export default class Link extends Component {
render () {
let { children } = this.props
let { href, as } = this
const {route: currentRoute} = this.context.router
// Deprecated. Warning shown by propType check. If the childen provided is a string (<Link>example</Link>) we wrap it in an <a> tag
if (typeof children === 'string') {
children = <a>{children}</a>
Expand All @@ -122,6 +127,10 @@ export default class Link extends Component {
props.href = as || href
}

if (currentRoute === href) {
props.className = (props.className || '') + ' active'
}

return React.cloneElement(child, props)
}
}
Expand Down