/
BoxAnchorControl.tsx
124 lines (113 loc) · 3.59 KB
/
BoxAnchorControl.tsx
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
import React from 'react'
import styled from 'styled-components'
import { Control } from './Control'
import { PropertyHeader } from './PropertyHeader'
import { Help } from './Help'
import { ChartProperty, Flavor } from '../../types'
import { BoxAnchorControlConfig } from './types'
const boxWidth = 80
const boxHeight = 50
const boxPadding = 10
const outlineRadius = 8
const anchors: [string, number, number][] = [
['center', boxWidth / 2, boxHeight / 2],
['top-left', 0, 0],
['top', boxWidth / 2, 0],
['top-right', boxWidth, 0],
['right', boxWidth, boxHeight / 2],
['bottom-right', boxWidth, boxHeight],
['bottom', boxWidth / 2, boxHeight],
['bottom-left', 0, boxHeight],
['left', 0, boxHeight / 2],
]
const Row = styled.div`
display: flex;
align-items: center;
`
const Rect = styled.rect`
fill: none;
stroke: ${({ theme }) => theme.colors.textLight};
stroke-width: 2px;
stroke-opacity: 0.6;
`
const Dot = styled.circle<{
isSelected: boolean
}>`
fill: ${({ isSelected, theme }) => (isSelected ? theme.colors.accent : theme.colors.textLight)};
`
const DotOutline = styled.circle<{
isSelected: boolean
}>`
fill: red;
fill-opacity: 0;
stroke-width: 2px;
stroke: ${({ isSelected, theme }) =>
isSelected ? theme.colors.accent : theme.colors.textLight};
stroke-opacity: ${({ isSelected }) => (isSelected ? 1 : 0)};
&:hover {
stroke-opacity: 1;
}
`
const Value = styled.span`
margin-left: 20px;
`
interface BoxAnchorControlProps {
id: string
property: ChartProperty
flavors: Flavor[]
currentFlavor: Flavor
value: string
config: BoxAnchorControlConfig
onChange: (value: string) => void
context?: any
}
export const BoxAnchorControl = ({
id,
property,
flavors,
currentFlavor,
value,
onChange,
}: BoxAnchorControlProps) => {
return (
<Control
id={id}
description={property.description}
flavors={flavors}
currentFlavor={currentFlavor}
supportedFlavors={property.flavors}
>
<PropertyHeader {...property} />
<Row>
<svg width={boxWidth + boxPadding * 2} height={boxHeight + boxPadding * 2}>
<g transform={`translate(${boxPadding},${boxPadding})`}>
<Rect width={boxWidth} height={boxHeight} />
{anchors.map(anchor => {
const isSelected = value === anchor[0]
return (
<g
key={anchor[0]}
transform={`translate(${anchor[1]},${anchor[2]})`}
>
<Dot isSelected={isSelected} r={isSelected ? 3 : 2} />
<DotOutline
isSelected={isSelected}
r={outlineRadius}
style={{
cursor: 'pointer',
}}
onClick={() => {
onChange(anchor[0])
}}
/>
</g>
)
})}
</g>
</svg>
<Value>{value}</Value>
</Row>
<Help>{property.help}</Help>
</Control>
)
}