-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDropZoneAlignmentButtonWidget.tsx
More file actions
113 lines (103 loc) · 2.85 KB
/
DropZoneAlignmentButtonWidget.tsx
File metadata and controls
113 lines (103 loc) · 2.85 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
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
import * as React from 'react';
import { useRef, useState } from 'react';
import styled from '@emotion/styled';
import * as _ from 'lodash';
import {
Alignment,
useDroppableModel,
useMouseDragEvents,
WorkspaceEngine,
WorkspaceModel
} from '@projectstorm/react-workspaces-core';
export interface DropZoneAlignmentTheme {
thickness?: number;
thicknessIdle?: number;
lengthIdle?: number;
background?: string;
backgroundEntered?: string;
}
const DefaultDropZoneAlignmentTheme: DropZoneAlignmentTheme = {
thickness: 30,
thicknessIdle: 13,
lengthIdle: 60,
background: '#0096ff',
backgroundEntered: 'orange'
};
export interface DropZoneAlignmentButtonWidgetProps {
alignment: Alignment;
engine: WorkspaceEngine;
model: WorkspaceModel;
handleDrop: (model: WorkspaceModel) => any;
theme?: DropZoneAlignmentTheme;
}
export const DropZoneAlignmentButtonWidget: React.FC<DropZoneAlignmentButtonWidgetProps> = (props) => {
const [entered, setEntered] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const theme = _.merge({}, DefaultDropZoneAlignmentTheme, props.theme || {});
useMouseDragEvents({
forwardRef: ref,
mouseEnter: () => {
setEntered(true);
},
mouseExit: () => {
setEntered(false);
}
});
useDroppableModel({
forwardRef: ref,
engine: props.engine,
onDrop: props.handleDrop
});
const vertical = props.alignment === Alignment.LEFT || props.alignment === Alignment.RIGHT;
let width = theme.thicknessIdle;
let height = theme.lengthIdle;
if (!vertical) {
width = theme.lengthIdle;
height = theme.thicknessIdle;
}
return (
<S.SplitContainer
thickness={theme.thickness}
ref={ref}
vertical={vertical}
alignment={props.alignment}
hover={entered}
>
<S.SplitContainerIcon theme={theme} hover={entered} width={width} height={height} />
</S.SplitContainer>
);
};
namespace S {
export const SplitContainer = styled.div<{
alignment: Alignment;
hover: boolean;
vertical: boolean;
thickness: number;
}>`
${(p) => p.alignment}: 0;
position: absolute;
width: ${(p) => (p.vertical ? `${p.thickness}px` : '100%')};
height: ${(p) => (!p.vertical ? `${p.thickness}px` : '100%')};
pointer-events: all;
display: flex;
align-items: center;
justify-content: center;
`;
export const SplitContainerIcon = styled.div<{
width: number;
height: number;
hover: boolean;
theme: DropZoneAlignmentTheme;
}>`
border-radius: 2px;
background: ${(p) => (p.hover ? p.theme.backgroundEntered : p.theme.background)};
transition:
background 0.3s,
width ease-out 0.3s,
height ease-out 0.3s;
transition-delay: 0.1s;
pointer-events: none;
width: ${(p) => (p.hover ? '100%' : `${p.width}px`)};
height: ${(p) => (p.hover ? '100%' : `${p.height}px`)};
`;
}