-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.tsx
127 lines (119 loc) · 3.03 KB
/
index.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
125
126
127
/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';
/**
* External dependencies
*/
import classnames from 'classnames';
import { Resizable } from 're-resizable';
import type { ResizableProps } from 're-resizable';
import type { ReactNode, ForwardedRef } from 'react';
/**
* Internal dependencies
*/
import ResizeTooltip from './resize-tooltip';
const HANDLE_CLASS_NAME = 'components-resizable-box__handle';
const SIDE_HANDLE_CLASS_NAME = 'components-resizable-box__side-handle';
const CORNER_HANDLE_CLASS_NAME = 'components-resizable-box__corner-handle';
const HANDLE_CLASSES = {
top: classnames(
HANDLE_CLASS_NAME,
SIDE_HANDLE_CLASS_NAME,
'components-resizable-box__handle-top'
),
right: classnames(
HANDLE_CLASS_NAME,
SIDE_HANDLE_CLASS_NAME,
'components-resizable-box__handle-right'
),
bottom: classnames(
HANDLE_CLASS_NAME,
SIDE_HANDLE_CLASS_NAME,
'components-resizable-box__handle-bottom'
),
left: classnames(
HANDLE_CLASS_NAME,
SIDE_HANDLE_CLASS_NAME,
'components-resizable-box__handle-left'
),
topLeft: classnames(
HANDLE_CLASS_NAME,
CORNER_HANDLE_CLASS_NAME,
'components-resizable-box__handle-top',
'components-resizable-box__handle-left'
),
topRight: classnames(
HANDLE_CLASS_NAME,
CORNER_HANDLE_CLASS_NAME,
'components-resizable-box__handle-top',
'components-resizable-box__handle-right'
),
bottomRight: classnames(
HANDLE_CLASS_NAME,
CORNER_HANDLE_CLASS_NAME,
'components-resizable-box__handle-bottom',
'components-resizable-box__handle-right'
),
bottomLeft: classnames(
HANDLE_CLASS_NAME,
CORNER_HANDLE_CLASS_NAME,
'components-resizable-box__handle-bottom',
'components-resizable-box__handle-left'
),
};
// Removes the inline styles in the drag handles.
const HANDLE_STYLES_OVERRIDES = {
width: undefined,
height: undefined,
top: undefined,
right: undefined,
bottom: undefined,
left: undefined,
};
const HANDLE_STYLES = {
top: HANDLE_STYLES_OVERRIDES,
right: HANDLE_STYLES_OVERRIDES,
bottom: HANDLE_STYLES_OVERRIDES,
left: HANDLE_STYLES_OVERRIDES,
topLeft: HANDLE_STYLES_OVERRIDES,
topRight: HANDLE_STYLES_OVERRIDES,
bottomRight: HANDLE_STYLES_OVERRIDES,
bottomLeft: HANDLE_STYLES_OVERRIDES,
};
type ResizableBoxProps = ResizableProps & {
className: string;
children: ReactNode;
showHandle: boolean;
__experimentalShowTooltip: boolean;
__experimentalTooltipProps: Parameters< typeof ResizeTooltip >[ 0 ];
};
function ResizableBox(
{
className,
children,
showHandle = true,
__experimentalShowTooltip: showTooltip = false,
__experimentalTooltipProps: tooltipProps = {},
...props
}: ResizableBoxProps,
ref: ForwardedRef< Resizable >
): JSX.Element {
return (
<Resizable
className={ classnames(
'components-resizable-box__container',
showHandle && 'has-show-handle',
className
) }
handleClasses={ HANDLE_CLASSES }
handleStyles={ HANDLE_STYLES }
ref={ ref }
{ ...props }
>
{ children }
{ showTooltip && <ResizeTooltip { ...tooltipProps } /> }
</Resizable>
);
}
export default forwardRef( ResizableBox );