forked from rtsao/react-svg-arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (59 loc) · 1.85 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
var React = require('react');
var PropTypes = require('prop-types');
var svg = React.DOM.svg;
var path = React.DOM.path;
var ROOT_2 = Math.sqrt(2);
function Arrow(props) {
var landscape = props.direction === 'top' || props.direction === 'bottom';
var mirror = props.direction === 'left' || props.direction === 'top';
var borderDiagonal = props.borderWidth * ROOT_2;
var halfBorderDiagonal = borderDiagonal / 2;
var secondary = 2 * props.size + 2 * borderDiagonal;
var primary = props.size + borderDiagonal;
var primaryStart = mirror ? primary : 0;
var primaryEnd = mirror ? halfBorderDiagonal : primary - halfBorderDiagonal;
var secondaryStart = halfBorderDiagonal;
var secondaryMiddle = secondary / 2;
var secondaryEnd = secondary - halfBorderDiagonal;
var pathData = landscape ? [
'M', secondaryStart, primaryStart,
'L', secondaryMiddle, primaryEnd,
'L', secondaryEnd, primaryStart,
] : [
'M', primaryStart, secondaryStart,
'L', primaryEnd, secondaryMiddle,
'L', primaryStart, secondaryEnd
];
return (
svg({
xmlns: 'http://www.w3.org/svg/2000',
width: landscape ? secondary : primary,
height: landscape ? primary : secondary,
style: props.style,
className: props.className
},
path({
d: pathData.join(' '),
fill: props.color,
stroke: props.borderColor,
strokeWidth: props.borderWidth,
strokeLinecap: 'square'
})
)
);
}
Arrow.propTypes = {
color: PropTypes.string.isRequired,
size: PropTypes.number.isRequired,
direction: PropTypes.oneOf(['top', 'right', 'bottom', 'left']).isRequired,
borderWidth: PropTypes.number,
borderColor: PropTypes.string,
style: PropTypes.object,
className: PropTypes.string
};
Arrow.defaultProps = {
borderWidth: 0,
borderColor: 'transparent'
};
module.exports = Arrow;