Skip to content

Commit 5fbe933

Browse files
committed
[changed] Do not add "active" class by default
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
1 parent 7dcbfe0 commit 5fbe933

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

modules/Link.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ function isModifiedEvent(event) {
1111
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
1212
}
1313

14+
function isEmptyObject(object) {
15+
for (var p in object)
16+
if (object.hasOwnProperty(p))
17+
return false;
18+
19+
return true;
20+
}
21+
1422
/**
1523
* A <Link> is used to create an <a> element that links to a route.
1624
* When that route is active, the link gets an "active" class name
@@ -47,9 +55,8 @@ var Link = React.createClass({
4755

4856
getDefaultProps() {
4957
return {
50-
className: '',
51-
activeClassName: 'active',
5258
onlyActiveOnIndex: false,
59+
className: '',
5360
style: {}
5461
};
5562
},
@@ -83,26 +90,24 @@ var Link = React.createClass({
8390
},
8491

8592
render() {
86-
var { to, query, onlyActiveOnIndex } = this.props;
87-
88-
var props = {
89-
...this.props,
90-
onClick: this.handleClick
91-
};
92-
9393
var { history } = this.context;
94+
var { activeClassName, activeStyle, onlyActiveOnIndex, to, query, state, onClick, ...props } = this.props;
95+
96+
props.onClick = this.handleClick;
9497

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

100-
if (history.isActive(to, query, onlyActiveOnIndex)) {
101-
if (props.activeClassName)
102-
props.className += props.className !== '' ? ` ${props.activeClassName}` : props.activeClassName;
103+
if (activeClassName || (activeStyle != null && !isEmptyObject(activeStyle))) {
104+
if (history.isActive(to, query, onlyActiveOnIndex)) {
105+
if (activeClassName)
106+
props.className += props.className === '' ? activeClassName : ` ${activeClassName}`;
103107

104-
if (props.activeStyle)
105-
props.style = { ...props.style, ...props.activeStyle };
108+
if (activeStyle)
109+
props.style = { ...props.style, ...activeStyle };
110+
}
106111
}
107112
}
108113

modules/__tests__/Link-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ describe('A <Link>', function () {
7171
render() {
7272
return (
7373
<div>
74-
<Link to="/hello/michael">Michael</Link>
75-
<Link to="/hello/ryan">Ryan</Link>
74+
<Link to="/hello/michael" activeClassName="active">Michael</Link>
75+
<Link to="/hello/ryan" activeClassName="active">Ryan</Link>
7676
</div>
7777
);
7878
}

0 commit comments

Comments
 (0)