-
Notifications
You must be signed in to change notification settings - Fork 524
/
Caret.js
158 lines (138 loc) · 3.25 KB
/
Caret.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react'
import PropTypes from 'prop-types'
import theme from './theme'
const oppositeEdge = {
top: 'Bottom',
right: 'Left',
bottom: 'Top',
left: 'Right'
}
const perpendicularEdge = {
top: 'Left',
right: 'Top',
bottom: 'Left',
left: 'Top'
}
function getPosition({edge, align}) {
const opposite = oppositeEdge[edge].toLowerCase()
const perp = perpendicularEdge[edge].toLowerCase()
return {
[opposite]: '100%',
[perp]: '50%'
}
}
export default function Caret({css, ...rest}) {
// TODO: should the svg switch even be configurable,
// or do we use feature detection here?
return css
? <CaretCSS {...rest} />
: <CaretSVG {...rest} />
}
Caret.defaultProps = {
align: 'center',
borderColor: theme.colors.gray[2],
borderWidth: 1,
edge: 'bottom',
fill: theme.colors.white,
position: 'absolute',
size: 8,
css: false
}
Caret.propTypes = {
align: PropTypes.oneOf(['start', 'center', 'end']),
borderColor: PropTypes.string,
borderWidth: PropTypes.number,
edge: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
fill: PropTypes.string,
position: PropTypes.oneOf(['absolute', 'relative']),
size: PropTypes.number,
css: PropTypes.bool
}
function CaretCSS(props) {
const {
align,
borderColor,
borderWidth,
edge,
fill,
position,
size
} = props
const opposite = oppositeEdge[edge]
const perp = perpendicularEdge[edge]
const style = {
pointerEvents: 'none',
position,
...getPosition({edge, align})
}
const common = {
borderStyle: 'solid',
borderWidth,
position: 'absolute',
[opposite.toLowerCase()]: '100%',
}
const after = {
...common,
borderColor: 'transparent',
borderWidth: size,
[`border${opposite}Color`]: fill,
[`margin${perp}`]: -size
}
const before = {
...common,
borderColor: 'transparent',
[`border${opposite}Color`]: borderColor,
borderWidth: borderWidth + size,
[`margin${perp}`]: -(size + borderWidth)
}
return (
<div style={style}>
<div style={before} />
<div style={after} />
</div>
)
}
function CaretSVG(props) {
const {
align,
borderColor,
borderWidth,
edge,
fill,
position,
size
} = props
const perp = perpendicularEdge[edge]
const style = {
pointerEvents: 'none',
position,
...getPosition({edge, align}),
[`margin${perp}`]: -size
}
// note: these arrays represent points in the form [x, y]
const a = [-size, 0]
const b = [0, size]
const c = [size, 0]
// spaces are optional in path `d` attribute, and points are
// represented in the form `x,y` -- which is what the arrays above
// become when stringified!
const triangle = `M${a}L${b}L${c}L${a}Z`
const line = `M${a}L${b}L${c}`
const transform = {
top: `translate(${[size, size * 2]}) rotate(180)`,
right: `translate(${[0, size]}) rotate(-90)`,
bottom: `translate(${[size, 0]})`,
left: `translate(${[size * 2, size]}) rotate(90)`
}[edge]
return (
<svg width={size * 2} height={size * 2} style={style}>
<g transform={transform}>
<path d={triangle} fill={fill} />
<path d={line}
fill='none'
stroke={borderColor}
stroke-width={borderWidth} />
</g>
</svg>
)
}