-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathPin.tsx
More file actions
54 lines (47 loc) · 1.25 KB
/
Pin.tsx
File metadata and controls
54 lines (47 loc) · 1.25 KB
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
import * as React from 'react'
import styled from 'styled-components'
import { useDrag } from '../../shared/drag'
import { Position } from '../../types'
import { Pin as PinType } from './types'
const pinSize = 20
const Styles = styled.div<{ selected?: boolean }>`
width: ${pinSize}px;
height: ${pinSize}px;
box-sizing: border-box;
background: ${props => props.selected
? '#ffd92c'
: 'steelblue'};
border: 2px solid white;
border-radius: ${pinSize}px;
`
type Props = PinType & {
contextMenu(): void
translate(dx: number, dy: number): void
pointerdown(): void
pointer(): Position
}
export function Pin(props: Props) {
const drag = useDrag((dx, dy) => {
props.translate(dx, dy)
}, props.pointer)
const { x, y } = props.position
return (
<Styles
onPointerDown={(e: React.PointerEvent) => {
e.stopPropagation()
e.preventDefault()
drag.start(e)
props.pointerdown()
}}
onContextMenu={(e: React.MouseEvent) => {
e.stopPropagation()
e.preventDefault()
props.contextMenu()
}}
selected={props.selected}
style={{ position: 'absolute', top: `${y - pinSize / 2}px`, left: `${x - pinSize / 2}px` }}
data-testid="pin"
>
</Styles>
)
}