-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLink.js
47 lines (39 loc) · 929 Bytes
/
Link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React from 'react';
export default function Link( props ){
var attrs = {};
for( var prop in props ){
if( prop === 'to' ){
attrs.href = translate( props.to );
}
else if( prop === 'children' ){
// Nothing, children are passed to the createElement method directly
}
else {
attrs[prop] = props[prop];
}
}
attrs.onClick = function(e){ onClick(e, props.to, props.onClick) };
return React.createElement('a', attrs, props.children);
}
function isHash(){
return document.location.hash.slice(0,2) === '#/';
}
function translate( path ){
if( isHash() ){
return '#' + path;
}
return path;
}
function onClick( e, to, onClick ){
if ( onClick ) onClick(e);
if ( !e.defaultPrevented ){
e.preventDefault();
if( isHash() ){
document.location.hash = '#' + to;
}
else {
history.pushState({},'',to);
window.onpopstate();
}
}
}