Skip to content

Commit

Permalink
[changed] Do not add "active" class by default
Browse files Browse the repository at this point in the history
Must use <Link activeClassName> or <Link activeStyle> in order to
enable "active" behavior on links. If neither of these are present,
links do not check if they are active.

Fixes remix-run#1873
  • Loading branch information
mjackson committed Sep 11, 2015
1 parent 7dcbfe0 commit 5fbe933
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
33 changes: 19 additions & 14 deletions modules/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ function isModifiedEvent(event) {
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
}

function isEmptyObject(object) {
for (var p in object)
if (object.hasOwnProperty(p))
return false;

return true;
}

/**
* A <Link> is used to create an <a> element that links to a route.
* When that route is active, the link gets an "active" class name
Expand Down Expand Up @@ -47,9 +55,8 @@ var Link = React.createClass({

getDefaultProps() {
return {
className: '',
activeClassName: 'active',
onlyActiveOnIndex: false,
className: '',
style: {}
};
},
Expand Down Expand Up @@ -83,26 +90,24 @@ var Link = React.createClass({
},

render() {
var { to, query, onlyActiveOnIndex } = this.props;

var props = {
...this.props,
onClick: this.handleClick
};

var { history } = this.context;
var { activeClassName, activeStyle, onlyActiveOnIndex, to, query, state, onClick, ...props } = this.props;

props.onClick = this.handleClick;

// Ignore if rendered outside the context
// of history, simplifies unit testing.
if (history) {
props.href = history.createHref(to, query);

if (history.isActive(to, query, onlyActiveOnIndex)) {
if (props.activeClassName)
props.className += props.className !== '' ? ` ${props.activeClassName}` : props.activeClassName;
if (activeClassName || (activeStyle != null && !isEmptyObject(activeStyle))) {
if (history.isActive(to, query, onlyActiveOnIndex)) {
if (activeClassName)
props.className += props.className === '' ? activeClassName : ` ${activeClassName}`;

if (props.activeStyle)
props.style = { ...props.style, ...props.activeStyle };
if (activeStyle)
props.style = { ...props.style, ...activeStyle };
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/__tests__/Link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ describe('A <Link>', function () {
render() {
return (
<div>
<Link to="/hello/michael">Michael</Link>
<Link to="/hello/ryan">Ryan</Link>
<Link to="/hello/michael" activeClassName="active">Michael</Link>
<Link to="/hello/ryan" activeClassName="active">Ryan</Link>
</div>
);
}
Expand Down

0 comments on commit 5fbe933

Please sign in to comment.