@elliemae/ds-resizeable-container 2.3.0-alpha.8 → 2.3.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/Resizable.js +323 -0
- package/cjs/defaultProps.js +10 -0
- package/cjs/index.js +11 -0
- package/cjs/props.js +24 -0
- package/esm/Resizable.js +309 -0
- package/esm/defaultProps.js +6 -0
- package/esm/index.js +1 -0
- package/esm/props.js +20 -0
- package/package.json +25 -27
- package/types/Resizable.d.ts +55 -0
- package/types/defaultProps.d.ts +4 -0
- package/types/index.d.ts +2 -0
- package/types/props.d.ts +38 -0
- package/types/tests/DSResizableContainer.events.test.d.ts +1 -0
- package/dist/cjs/Resizable.js +0 -314
- package/dist/cjs/Resizable.js.map +0 -7
- package/dist/cjs/defaultProps.js +0 -38
- package/dist/cjs/defaultProps.js.map +0 -7
- package/dist/cjs/index.js +0 -36
- package/dist/cjs/index.js.map +0 -7
- package/dist/cjs/props.js +0 -42
- package/dist/cjs/props.js.map +0 -7
- package/dist/esm/Resizable.js +0 -285
- package/dist/esm/Resizable.js.map +0 -7
- package/dist/esm/defaultProps.js +0 -9
- package/dist/esm/defaultProps.js.map +0 -7
- package/dist/esm/index.js +0 -7
- package/dist/esm/index.js.map +0 -7
- package/dist/esm/props.js +0 -13
- package/dist/esm/props.js.map +0 -7
package/cjs/Resizable.js
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _jsx2 = require('@babel/runtime/helpers/jsx');
|
|
6
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
7
|
+
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
|
|
8
|
+
require('core-js/modules/esnext.async-iterator.map.js');
|
|
9
|
+
require('core-js/modules/esnext.iterator.map.js');
|
|
10
|
+
require('core-js/modules/web.dom-collections.iterator.js');
|
|
11
|
+
require('core-js/modules/esnext.async-iterator.every.js');
|
|
12
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
13
|
+
require('core-js/modules/esnext.iterator.every.js');
|
|
14
|
+
require('core-js/modules/esnext.async-iterator.filter.js');
|
|
15
|
+
require('core-js/modules/esnext.iterator.filter.js');
|
|
16
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
17
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
18
|
+
var React = require('react');
|
|
19
|
+
var reactDesc = require('react-desc');
|
|
20
|
+
var styled = require('styled-components');
|
|
21
|
+
var dsSystem = require('@elliemae/ds-system');
|
|
22
|
+
var dsGrid = require('@elliemae/ds-grid');
|
|
23
|
+
var props = require('./props.js');
|
|
24
|
+
var defaultProps = require('./defaultProps.js');
|
|
25
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
26
|
+
|
|
27
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
28
|
+
|
|
29
|
+
var _jsx2__default = /*#__PURE__*/_interopDefaultLegacy(_jsx2);
|
|
30
|
+
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
|
|
31
|
+
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefaultLegacy(_objectWithoutProperties);
|
|
32
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
33
|
+
var styled__default = /*#__PURE__*/_interopDefaultLegacy(styled);
|
|
34
|
+
|
|
35
|
+
const _excluded = ["innerRef"],
|
|
36
|
+
_excluded2 = ["children", "cols", "rows", "vertical", "horizontal"];
|
|
37
|
+
|
|
38
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
39
|
+
|
|
40
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
41
|
+
const safeDifference = 15;
|
|
42
|
+
const minDifference = 8;
|
|
43
|
+
const EVENT_TYPE = {
|
|
44
|
+
MOUSE: 'mouse',
|
|
45
|
+
KEY: 'arrows'
|
|
46
|
+
};
|
|
47
|
+
const DIRECTION = {
|
|
48
|
+
L: 'left',
|
|
49
|
+
R: 'right',
|
|
50
|
+
U: 'up',
|
|
51
|
+
D: 'down'
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function calcPosition(arrow, position) {
|
|
55
|
+
let offset;
|
|
56
|
+
|
|
57
|
+
switch (arrow) {
|
|
58
|
+
case DIRECTION.U:
|
|
59
|
+
offset = position - 3;
|
|
60
|
+
break;
|
|
61
|
+
|
|
62
|
+
case DIRECTION.D:
|
|
63
|
+
offset = position + 3;
|
|
64
|
+
break;
|
|
65
|
+
|
|
66
|
+
case DIRECTION.L:
|
|
67
|
+
offset = position - 3;
|
|
68
|
+
break;
|
|
69
|
+
|
|
70
|
+
case DIRECTION.R:
|
|
71
|
+
offset = position + 3;
|
|
72
|
+
break;
|
|
73
|
+
|
|
74
|
+
default:
|
|
75
|
+
offset = position;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return offset;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function cursorResolver(nextPx, px, isVertical) {
|
|
82
|
+
const orientation = {
|
|
83
|
+
vertical: 'row-resize',
|
|
84
|
+
horizontal: 'col-resize',
|
|
85
|
+
left: 'w-resize',
|
|
86
|
+
right: 'e-resize',
|
|
87
|
+
up: 'n-resize',
|
|
88
|
+
down: 's-resize'
|
|
89
|
+
};
|
|
90
|
+
if (isVertical && nextPx && nextPx <= safeDifference + 1) return orientation.up;
|
|
91
|
+
if (isVertical && px > safeDifference + 1) return orientation.vertical;
|
|
92
|
+
if (isVertical && px <= safeDifference + 1) return orientation.down;
|
|
93
|
+
if (!isVertical && nextPx && nextPx <= safeDifference + 1) return orientation.left;
|
|
94
|
+
if (!isVertical && px <= safeDifference + 1) return orientation.right;
|
|
95
|
+
if (!isVertical && px > safeDifference + 1) return orientation.horizontal;
|
|
96
|
+
return isVertical ? orientation.vertical : orientation.horizontal;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function ResizableItemComponent(_ref) {
|
|
100
|
+
let {
|
|
101
|
+
innerRef
|
|
102
|
+
} = _ref,
|
|
103
|
+
p = _objectWithoutProperties__default["default"](_ref, _excluded);
|
|
104
|
+
|
|
105
|
+
return /*#__PURE__*/jsxRuntime.jsx(dsGrid.Grid, _objectSpread({
|
|
106
|
+
ref: innerRef
|
|
107
|
+
}, p));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const ResizableGrid = /*#__PURE__*/styled__default["default"](dsGrid.Grid).withConfig({
|
|
111
|
+
componentId: "sc-forwbw-0"
|
|
112
|
+
})([""]);
|
|
113
|
+
const ResizableItem = /*#__PURE__*/styled__default["default"](ResizableItemComponent).withConfig({
|
|
114
|
+
componentId: "sc-forwbw-1"
|
|
115
|
+
})(["position:relative;"]);
|
|
116
|
+
const ResizableBar = /*#__PURE__*/styled__default["default"].div.withConfig({
|
|
117
|
+
componentId: "sc-forwbw-2"
|
|
118
|
+
})(["position:absolute;cursor:", ";", ""], _ref2 => {
|
|
119
|
+
let {
|
|
120
|
+
container,
|
|
121
|
+
nextContainer,
|
|
122
|
+
v
|
|
123
|
+
} = _ref2;
|
|
124
|
+
return cursorResolver(nextContainer, container, v);
|
|
125
|
+
}, props => {
|
|
126
|
+
if (props.v) {
|
|
127
|
+
return "\n bottom: calc(".concat(dsSystem.mapGap(props.gutter), " * -0.5);\n left: 0;\n width: 100%;\n height: 10px;\n margin-bottom: -5px;\n display: flex;\n flex-direction: column;\n justify-content:space-around;\n align-items: center;\n &:after{\n width: 100%;\n height: 1px;\n display: block;\n background: ").concat(props.theme.colors.neutral['300'], ";\n content: '';\n }\n &:hover{\n &:after{\n background: ").concat(props.theme.colors.brand['600'], ";\n } \n }\n ");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return "\n top: 0;\n right: calc(".concat(dsSystem.mapGap(props.gutter), " * -0.5);\n height: 100%;\n width: 10px;\n margin-right: -5px;\n display: flex;\n flex-direction: row;\n justify-content:space-around;\n align-items: center;\n &:after{\n height: 100%;\n width: 1px;\n display: block;\n background: ").concat(props.theme.colors.neutral['300'], ";\n content: '';\n }\n &:hover{\n &:after{\n background: ").concat(props.theme.colors.brand['600'], ";\n } \n }\n ");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const getWidths = list => list.map(l => l.clientWidth);
|
|
134
|
+
|
|
135
|
+
const getHeights = list => list.map(l => l.clientHeight);
|
|
136
|
+
|
|
137
|
+
function ResizableContainer(_ref3) {
|
|
138
|
+
let {
|
|
139
|
+
children,
|
|
140
|
+
cols,
|
|
141
|
+
rows,
|
|
142
|
+
vertical,
|
|
143
|
+
horizontal
|
|
144
|
+
} = _ref3,
|
|
145
|
+
rest = _objectWithoutProperties__default["default"](_ref3, _excluded2);
|
|
146
|
+
|
|
147
|
+
const refs = React.useRef([]);
|
|
148
|
+
const [containers, setContainers] = React.useState(cols);
|
|
149
|
+
const [blocks, setBlocks] = React.useState(rows);
|
|
150
|
+
const [isDragging, setDragging] = React.useState(false);
|
|
151
|
+
const [start, setStartPoint] = React.useState(0);
|
|
152
|
+
const [resetPoint, setResetPoint] = React.useState();
|
|
153
|
+
React.useEffect(() => {
|
|
154
|
+
if (horizontal) setContainers(getWidths(refs.current));
|
|
155
|
+
if (vertical) setBlocks(getHeights(refs.current));
|
|
156
|
+
}, [refs.current, horizontal, vertical]);
|
|
157
|
+
|
|
158
|
+
const addRef = node => {
|
|
159
|
+
refs.current.push(node);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const resizePanels = function () {
|
|
163
|
+
let diff = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
|
|
164
|
+
let v = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
165
|
+
const index = parseInt(start.index, 10);
|
|
166
|
+
|
|
167
|
+
if (!v) {
|
|
168
|
+
const newContainers = [...containers];
|
|
169
|
+
|
|
170
|
+
if (newContainers[index + 1] && newContainers[index + 1] > minDifference) {
|
|
171
|
+
newContainers[index + 1] = newContainers[index + 1] - diff;
|
|
172
|
+
newContainers[index] = diff + newContainers[index];
|
|
173
|
+
} else if (newContainers[index - 1] && newContainers[index - 1] > minDifference) {
|
|
174
|
+
newContainers[index - 1] = newContainers[index - 1] - diff;
|
|
175
|
+
newContainers[index] = diff + newContainers[index];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (newContainers.every(c => c > safeDifference)) setContainers(newContainers);
|
|
179
|
+
} else {
|
|
180
|
+
const newBlocks = [...blocks];
|
|
181
|
+
|
|
182
|
+
if (newBlocks[index + 1] && newBlocks[index + 1] > minDifference) {
|
|
183
|
+
newBlocks[index + 1] = newBlocks[index + 1] - diff;
|
|
184
|
+
newBlocks[index] = diff + newBlocks[index];
|
|
185
|
+
} else if (newBlocks[index - 1] && newBlocks[index - 1] > minDifference) {
|
|
186
|
+
newBlocks[index - 1] = newBlocks[index - 1] - diff;
|
|
187
|
+
newBlocks[index] = diff + newBlocks[index];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (newBlocks.every(c => c > safeDifference)) setBlocks(newBlocks);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const startResize = function (event, index) {
|
|
195
|
+
let v = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
196
|
+
let type = arguments.length > 3 ? arguments[3] : undefined;
|
|
197
|
+
setDragging(type);
|
|
198
|
+
setStartPoint({
|
|
199
|
+
x: event.clientX,
|
|
200
|
+
y: event.clientY,
|
|
201
|
+
index,
|
|
202
|
+
v
|
|
203
|
+
});
|
|
204
|
+
setResetPoint({
|
|
205
|
+
containers,
|
|
206
|
+
blocks
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const moveResize = (event, type) => {
|
|
211
|
+
if (isDragging && isDragging === type && !start.v) {
|
|
212
|
+
const diff = event.clientX - start.x;
|
|
213
|
+
if (diff === 0) return;
|
|
214
|
+
setStartPoint({
|
|
215
|
+
x: event.clientX,
|
|
216
|
+
index: start.index
|
|
217
|
+
});
|
|
218
|
+
resizePanels(diff);
|
|
219
|
+
} else if (isDragging && isDragging === type && start.v) {
|
|
220
|
+
const diff = event.clientY - start.y;
|
|
221
|
+
if (diff === 0) return;
|
|
222
|
+
setStartPoint(_objectSpread(_objectSpread({}, start), {}, {
|
|
223
|
+
y: event.clientY,
|
|
224
|
+
index: start.index
|
|
225
|
+
}));
|
|
226
|
+
resizePanels(diff, true);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const endResize = type => {
|
|
231
|
+
if (isDragging === type) {
|
|
232
|
+
setDragging(false);
|
|
233
|
+
setStartPoint();
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const handleKeyDown = function (e, index) {
|
|
238
|
+
let v = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
239
|
+
const keyCodes = {
|
|
240
|
+
37: DIRECTION.L,
|
|
241
|
+
Left: DIRECTION.L,
|
|
242
|
+
ArrowLeft: DIRECTION.L,
|
|
243
|
+
38: DIRECTION.U,
|
|
244
|
+
Up: DIRECTION.U,
|
|
245
|
+
ArrowUp: DIRECTION.U,
|
|
246
|
+
39: DIRECTION.R,
|
|
247
|
+
Right: DIRECTION.R,
|
|
248
|
+
ArrowRight: DIRECTION.R,
|
|
249
|
+
40: DIRECTION.D,
|
|
250
|
+
Down: DIRECTION.D,
|
|
251
|
+
ArrowDown: DIRECTION.D
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
if (keyCodes[e.key || e.keyCode]) {
|
|
255
|
+
e.preventDefault();
|
|
256
|
+
const {
|
|
257
|
+
top: y,
|
|
258
|
+
left: x
|
|
259
|
+
} = e.target.getBoundingClientRect();
|
|
260
|
+
|
|
261
|
+
if (!start) {
|
|
262
|
+
startResize({
|
|
263
|
+
clientX: x,
|
|
264
|
+
clientY: y
|
|
265
|
+
}, index, v, EVENT_TYPE.KEY);
|
|
266
|
+
} else {
|
|
267
|
+
const clientX = keyCodes[e.keyCode] === DIRECTION.L || keyCodes[e.keyCode] === DIRECTION.R ? calcPosition(keyCodes[e.keyCode], x) : x;
|
|
268
|
+
const clientY = keyCodes[e.keyCode] === DIRECTION.U || keyCodes[e.keyCode] === DIRECTION.D ? calcPosition(keyCodes[e.keyCode], y) : y;
|
|
269
|
+
moveResize({
|
|
270
|
+
clientX,
|
|
271
|
+
clientY
|
|
272
|
+
}, EVENT_TYPE.KEY);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const onLeave = type => {
|
|
278
|
+
if (isDragging === type) {
|
|
279
|
+
setDragging(false);
|
|
280
|
+
if (resetPoint && resetPoint.blocks) setBlocks(resetPoint.blocks);
|
|
281
|
+
if (resetPoint && resetPoint.containers) setContainers(resetPoint.containers);
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const count = React__default["default"].Children.count(children);
|
|
286
|
+
return /*#__PURE__*/jsxRuntime.jsx(ResizableGrid, _objectSpread(_objectSpread({}, rest), {}, {
|
|
287
|
+
cols: containers,
|
|
288
|
+
onKeyUp: () => endResize(EVENT_TYPE.KEY),
|
|
289
|
+
onMouseLeave: () => onLeave(EVENT_TYPE.MOUSE),
|
|
290
|
+
onMouseMove: e => moveResize(e, EVENT_TYPE.MOUSE),
|
|
291
|
+
onMouseUp: () => endResize(EVENT_TYPE.MOUSE),
|
|
292
|
+
rows: blocks,
|
|
293
|
+
children: React__default["default"].Children.map(children, (child, index) => /*#__PURE__*/_jsx2__default["default"](ResizableItem, {
|
|
294
|
+
"data-index": index,
|
|
295
|
+
innerRef: addRef
|
|
296
|
+
}, void 0, child, count - 1 !== index && horizontal && /*#__PURE__*/_jsx2__default["default"](ResizableBar, {
|
|
297
|
+
container: containers[index],
|
|
298
|
+
"data-index": index,
|
|
299
|
+
gutter: rest.gutter,
|
|
300
|
+
nextContainer: containers[index + 1],
|
|
301
|
+
onKeyDown: e => handleKeyDown(e, index, false),
|
|
302
|
+
onMouseDown: e => startResize(e, index, false, EVENT_TYPE.MOUSE),
|
|
303
|
+
tabIndex: 0
|
|
304
|
+
}), count - 1 !== index && vertical && /*#__PURE__*/_jsx2__default["default"](ResizableBar, {
|
|
305
|
+
container: blocks[index],
|
|
306
|
+
"data-index": index,
|
|
307
|
+
gutter: rest.gutter,
|
|
308
|
+
nextContainer: blocks[index + 1],
|
|
309
|
+
onKeyDown: e => handleKeyDown(e, index, true),
|
|
310
|
+
onMouseDown: e => startResize(e, index, true, EVENT_TYPE.MOUSE),
|
|
311
|
+
tabIndex: 0,
|
|
312
|
+
v: true
|
|
313
|
+
})))
|
|
314
|
+
}));
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
ResizableContainer.defaultProps = defaultProps.defaultProps;
|
|
318
|
+
const DSResizableContainerWithSchema = reactDesc.describe(ResizableContainer);
|
|
319
|
+
DSResizableContainerWithSchema.propTypes = props.props;
|
|
320
|
+
|
|
321
|
+
exports.DSResizableContainerWithSchema = DSResizableContainerWithSchema;
|
|
322
|
+
exports.ResizableContainer = ResizableContainer;
|
|
323
|
+
exports["default"] = ResizableContainer;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var Resizable = require('./Resizable.js');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
exports.DSResizableContainerWithSchema = Resizable.DSResizableContainerWithSchema;
|
|
10
|
+
exports.ResizableContainer = Resizable.ResizableContainer;
|
|
11
|
+
exports["default"] = Resizable.ResizableContainer;
|
package/cjs/props.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var reactDesc = require('react-desc');
|
|
6
|
+
|
|
7
|
+
const props = {
|
|
8
|
+
/** grid item content */
|
|
9
|
+
children: reactDesc.PropTypes.node.description('Grid item content'),
|
|
10
|
+
|
|
11
|
+
/** vertical resizing */
|
|
12
|
+
vertical: reactDesc.PropTypes.bool.description('vertical resizing'),
|
|
13
|
+
|
|
14
|
+
/** horizontal resizing */
|
|
15
|
+
horizontal: reactDesc.PropTypes.bool.description('horizontal resizing'),
|
|
16
|
+
|
|
17
|
+
/** Row layout cells */
|
|
18
|
+
rows: reactDesc.PropTypes.arrayOf(reactDesc.PropTypes.oneOfType([reactDesc.PropTypes.number, reactDesc.PropTypes.string])).description('Row layout cells'),
|
|
19
|
+
|
|
20
|
+
/** Column layout cells */
|
|
21
|
+
cols: reactDesc.PropTypes.arrayOf(reactDesc.PropTypes.oneOfType([reactDesc.PropTypes.number, reactDesc.PropTypes.string])).description('Column layout cells')
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
exports.props = props;
|
package/esm/Resizable.js
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import 'core-js/modules/esnext.async-iterator.filter.js';
|
|
2
|
+
import 'core-js/modules/esnext.iterator.filter.js';
|
|
3
|
+
import 'core-js/modules/esnext.async-iterator.for-each.js';
|
|
4
|
+
import 'core-js/modules/esnext.iterator.for-each.js';
|
|
5
|
+
import _jsx2 from '@babel/runtime/helpers/esm/jsx';
|
|
6
|
+
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
|
|
7
|
+
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
|
|
8
|
+
import 'core-js/modules/esnext.async-iterator.map.js';
|
|
9
|
+
import 'core-js/modules/esnext.iterator.map.js';
|
|
10
|
+
import 'core-js/modules/web.dom-collections.iterator.js';
|
|
11
|
+
import 'core-js/modules/esnext.async-iterator.every.js';
|
|
12
|
+
import 'core-js/modules/esnext.iterator.constructor.js';
|
|
13
|
+
import 'core-js/modules/esnext.iterator.every.js';
|
|
14
|
+
import React, { useRef, useState, useEffect } from 'react';
|
|
15
|
+
import { describe } from 'react-desc';
|
|
16
|
+
import styled from 'styled-components';
|
|
17
|
+
import { mapGap } from '@elliemae/ds-system';
|
|
18
|
+
import { Grid } from '@elliemae/ds-grid';
|
|
19
|
+
import { props } from './props.js';
|
|
20
|
+
import { defaultProps } from './defaultProps.js';
|
|
21
|
+
import { jsx } from 'react/jsx-runtime';
|
|
22
|
+
|
|
23
|
+
const _excluded = ["innerRef"],
|
|
24
|
+
_excluded2 = ["children", "cols", "rows", "vertical", "horizontal"];
|
|
25
|
+
|
|
26
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
27
|
+
|
|
28
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
29
|
+
const safeDifference = 15;
|
|
30
|
+
const minDifference = 8;
|
|
31
|
+
const EVENT_TYPE = {
|
|
32
|
+
MOUSE: 'mouse',
|
|
33
|
+
KEY: 'arrows'
|
|
34
|
+
};
|
|
35
|
+
const DIRECTION = {
|
|
36
|
+
L: 'left',
|
|
37
|
+
R: 'right',
|
|
38
|
+
U: 'up',
|
|
39
|
+
D: 'down'
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function calcPosition(arrow, position) {
|
|
43
|
+
let offset;
|
|
44
|
+
|
|
45
|
+
switch (arrow) {
|
|
46
|
+
case DIRECTION.U:
|
|
47
|
+
offset = position - 3;
|
|
48
|
+
break;
|
|
49
|
+
|
|
50
|
+
case DIRECTION.D:
|
|
51
|
+
offset = position + 3;
|
|
52
|
+
break;
|
|
53
|
+
|
|
54
|
+
case DIRECTION.L:
|
|
55
|
+
offset = position - 3;
|
|
56
|
+
break;
|
|
57
|
+
|
|
58
|
+
case DIRECTION.R:
|
|
59
|
+
offset = position + 3;
|
|
60
|
+
break;
|
|
61
|
+
|
|
62
|
+
default:
|
|
63
|
+
offset = position;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return offset;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function cursorResolver(nextPx, px, isVertical) {
|
|
70
|
+
const orientation = {
|
|
71
|
+
vertical: 'row-resize',
|
|
72
|
+
horizontal: 'col-resize',
|
|
73
|
+
left: 'w-resize',
|
|
74
|
+
right: 'e-resize',
|
|
75
|
+
up: 'n-resize',
|
|
76
|
+
down: 's-resize'
|
|
77
|
+
};
|
|
78
|
+
if (isVertical && nextPx && nextPx <= safeDifference + 1) return orientation.up;
|
|
79
|
+
if (isVertical && px > safeDifference + 1) return orientation.vertical;
|
|
80
|
+
if (isVertical && px <= safeDifference + 1) return orientation.down;
|
|
81
|
+
if (!isVertical && nextPx && nextPx <= safeDifference + 1) return orientation.left;
|
|
82
|
+
if (!isVertical && px <= safeDifference + 1) return orientation.right;
|
|
83
|
+
if (!isVertical && px > safeDifference + 1) return orientation.horizontal;
|
|
84
|
+
return isVertical ? orientation.vertical : orientation.horizontal;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function ResizableItemComponent(_ref) {
|
|
88
|
+
let {
|
|
89
|
+
innerRef
|
|
90
|
+
} = _ref,
|
|
91
|
+
p = _objectWithoutProperties(_ref, _excluded);
|
|
92
|
+
|
|
93
|
+
return /*#__PURE__*/jsx(Grid, _objectSpread({
|
|
94
|
+
ref: innerRef
|
|
95
|
+
}, p));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const ResizableGrid = /*#__PURE__*/styled(Grid).withConfig({
|
|
99
|
+
componentId: "sc-forwbw-0"
|
|
100
|
+
})([""]);
|
|
101
|
+
const ResizableItem = /*#__PURE__*/styled(ResizableItemComponent).withConfig({
|
|
102
|
+
componentId: "sc-forwbw-1"
|
|
103
|
+
})(["position:relative;"]);
|
|
104
|
+
const ResizableBar = /*#__PURE__*/styled.div.withConfig({
|
|
105
|
+
componentId: "sc-forwbw-2"
|
|
106
|
+
})(["position:absolute;cursor:", ";", ""], _ref2 => {
|
|
107
|
+
let {
|
|
108
|
+
container,
|
|
109
|
+
nextContainer,
|
|
110
|
+
v
|
|
111
|
+
} = _ref2;
|
|
112
|
+
return cursorResolver(nextContainer, container, v);
|
|
113
|
+
}, props => {
|
|
114
|
+
if (props.v) {
|
|
115
|
+
return "\n bottom: calc(".concat(mapGap(props.gutter), " * -0.5);\n left: 0;\n width: 100%;\n height: 10px;\n margin-bottom: -5px;\n display: flex;\n flex-direction: column;\n justify-content:space-around;\n align-items: center;\n &:after{\n width: 100%;\n height: 1px;\n display: block;\n background: ").concat(props.theme.colors.neutral['300'], ";\n content: '';\n }\n &:hover{\n &:after{\n background: ").concat(props.theme.colors.brand['600'], ";\n } \n }\n ");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return "\n top: 0;\n right: calc(".concat(mapGap(props.gutter), " * -0.5);\n height: 100%;\n width: 10px;\n margin-right: -5px;\n display: flex;\n flex-direction: row;\n justify-content:space-around;\n align-items: center;\n &:after{\n height: 100%;\n width: 1px;\n display: block;\n background: ").concat(props.theme.colors.neutral['300'], ";\n content: '';\n }\n &:hover{\n &:after{\n background: ").concat(props.theme.colors.brand['600'], ";\n } \n }\n ");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const getWidths = list => list.map(l => l.clientWidth);
|
|
122
|
+
|
|
123
|
+
const getHeights = list => list.map(l => l.clientHeight);
|
|
124
|
+
|
|
125
|
+
function ResizableContainer(_ref3) {
|
|
126
|
+
let {
|
|
127
|
+
children,
|
|
128
|
+
cols,
|
|
129
|
+
rows,
|
|
130
|
+
vertical,
|
|
131
|
+
horizontal
|
|
132
|
+
} = _ref3,
|
|
133
|
+
rest = _objectWithoutProperties(_ref3, _excluded2);
|
|
134
|
+
|
|
135
|
+
const refs = useRef([]);
|
|
136
|
+
const [containers, setContainers] = useState(cols);
|
|
137
|
+
const [blocks, setBlocks] = useState(rows);
|
|
138
|
+
const [isDragging, setDragging] = useState(false);
|
|
139
|
+
const [start, setStartPoint] = useState(0);
|
|
140
|
+
const [resetPoint, setResetPoint] = useState();
|
|
141
|
+
useEffect(() => {
|
|
142
|
+
if (horizontal) setContainers(getWidths(refs.current));
|
|
143
|
+
if (vertical) setBlocks(getHeights(refs.current));
|
|
144
|
+
}, [refs.current, horizontal, vertical]);
|
|
145
|
+
|
|
146
|
+
const addRef = node => {
|
|
147
|
+
refs.current.push(node);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const resizePanels = function () {
|
|
151
|
+
let diff = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
|
|
152
|
+
let v = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
153
|
+
const index = parseInt(start.index, 10);
|
|
154
|
+
|
|
155
|
+
if (!v) {
|
|
156
|
+
const newContainers = [...containers];
|
|
157
|
+
|
|
158
|
+
if (newContainers[index + 1] && newContainers[index + 1] > minDifference) {
|
|
159
|
+
newContainers[index + 1] = newContainers[index + 1] - diff;
|
|
160
|
+
newContainers[index] = diff + newContainers[index];
|
|
161
|
+
} else if (newContainers[index - 1] && newContainers[index - 1] > minDifference) {
|
|
162
|
+
newContainers[index - 1] = newContainers[index - 1] - diff;
|
|
163
|
+
newContainers[index] = diff + newContainers[index];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (newContainers.every(c => c > safeDifference)) setContainers(newContainers);
|
|
167
|
+
} else {
|
|
168
|
+
const newBlocks = [...blocks];
|
|
169
|
+
|
|
170
|
+
if (newBlocks[index + 1] && newBlocks[index + 1] > minDifference) {
|
|
171
|
+
newBlocks[index + 1] = newBlocks[index + 1] - diff;
|
|
172
|
+
newBlocks[index] = diff + newBlocks[index];
|
|
173
|
+
} else if (newBlocks[index - 1] && newBlocks[index - 1] > minDifference) {
|
|
174
|
+
newBlocks[index - 1] = newBlocks[index - 1] - diff;
|
|
175
|
+
newBlocks[index] = diff + newBlocks[index];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (newBlocks.every(c => c > safeDifference)) setBlocks(newBlocks);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const startResize = function (event, index) {
|
|
183
|
+
let v = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
184
|
+
let type = arguments.length > 3 ? arguments[3] : undefined;
|
|
185
|
+
setDragging(type);
|
|
186
|
+
setStartPoint({
|
|
187
|
+
x: event.clientX,
|
|
188
|
+
y: event.clientY,
|
|
189
|
+
index,
|
|
190
|
+
v
|
|
191
|
+
});
|
|
192
|
+
setResetPoint({
|
|
193
|
+
containers,
|
|
194
|
+
blocks
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const moveResize = (event, type) => {
|
|
199
|
+
if (isDragging && isDragging === type && !start.v) {
|
|
200
|
+
const diff = event.clientX - start.x;
|
|
201
|
+
if (diff === 0) return;
|
|
202
|
+
setStartPoint({
|
|
203
|
+
x: event.clientX,
|
|
204
|
+
index: start.index
|
|
205
|
+
});
|
|
206
|
+
resizePanels(diff);
|
|
207
|
+
} else if (isDragging && isDragging === type && start.v) {
|
|
208
|
+
const diff = event.clientY - start.y;
|
|
209
|
+
if (diff === 0) return;
|
|
210
|
+
setStartPoint(_objectSpread(_objectSpread({}, start), {}, {
|
|
211
|
+
y: event.clientY,
|
|
212
|
+
index: start.index
|
|
213
|
+
}));
|
|
214
|
+
resizePanels(diff, true);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const endResize = type => {
|
|
219
|
+
if (isDragging === type) {
|
|
220
|
+
setDragging(false);
|
|
221
|
+
setStartPoint();
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const handleKeyDown = function (e, index) {
|
|
226
|
+
let v = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
227
|
+
const keyCodes = {
|
|
228
|
+
37: DIRECTION.L,
|
|
229
|
+
Left: DIRECTION.L,
|
|
230
|
+
ArrowLeft: DIRECTION.L,
|
|
231
|
+
38: DIRECTION.U,
|
|
232
|
+
Up: DIRECTION.U,
|
|
233
|
+
ArrowUp: DIRECTION.U,
|
|
234
|
+
39: DIRECTION.R,
|
|
235
|
+
Right: DIRECTION.R,
|
|
236
|
+
ArrowRight: DIRECTION.R,
|
|
237
|
+
40: DIRECTION.D,
|
|
238
|
+
Down: DIRECTION.D,
|
|
239
|
+
ArrowDown: DIRECTION.D
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
if (keyCodes[e.key || e.keyCode]) {
|
|
243
|
+
e.preventDefault();
|
|
244
|
+
const {
|
|
245
|
+
top: y,
|
|
246
|
+
left: x
|
|
247
|
+
} = e.target.getBoundingClientRect();
|
|
248
|
+
|
|
249
|
+
if (!start) {
|
|
250
|
+
startResize({
|
|
251
|
+
clientX: x,
|
|
252
|
+
clientY: y
|
|
253
|
+
}, index, v, EVENT_TYPE.KEY);
|
|
254
|
+
} else {
|
|
255
|
+
const clientX = keyCodes[e.keyCode] === DIRECTION.L || keyCodes[e.keyCode] === DIRECTION.R ? calcPosition(keyCodes[e.keyCode], x) : x;
|
|
256
|
+
const clientY = keyCodes[e.keyCode] === DIRECTION.U || keyCodes[e.keyCode] === DIRECTION.D ? calcPosition(keyCodes[e.keyCode], y) : y;
|
|
257
|
+
moveResize({
|
|
258
|
+
clientX,
|
|
259
|
+
clientY
|
|
260
|
+
}, EVENT_TYPE.KEY);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const onLeave = type => {
|
|
266
|
+
if (isDragging === type) {
|
|
267
|
+
setDragging(false);
|
|
268
|
+
if (resetPoint && resetPoint.blocks) setBlocks(resetPoint.blocks);
|
|
269
|
+
if (resetPoint && resetPoint.containers) setContainers(resetPoint.containers);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const count = React.Children.count(children);
|
|
274
|
+
return /*#__PURE__*/jsx(ResizableGrid, _objectSpread(_objectSpread({}, rest), {}, {
|
|
275
|
+
cols: containers,
|
|
276
|
+
onKeyUp: () => endResize(EVENT_TYPE.KEY),
|
|
277
|
+
onMouseLeave: () => onLeave(EVENT_TYPE.MOUSE),
|
|
278
|
+
onMouseMove: e => moveResize(e, EVENT_TYPE.MOUSE),
|
|
279
|
+
onMouseUp: () => endResize(EVENT_TYPE.MOUSE),
|
|
280
|
+
rows: blocks,
|
|
281
|
+
children: React.Children.map(children, (child, index) => /*#__PURE__*/_jsx2(ResizableItem, {
|
|
282
|
+
"data-index": index,
|
|
283
|
+
innerRef: addRef
|
|
284
|
+
}, void 0, child, count - 1 !== index && horizontal && /*#__PURE__*/_jsx2(ResizableBar, {
|
|
285
|
+
container: containers[index],
|
|
286
|
+
"data-index": index,
|
|
287
|
+
gutter: rest.gutter,
|
|
288
|
+
nextContainer: containers[index + 1],
|
|
289
|
+
onKeyDown: e => handleKeyDown(e, index, false),
|
|
290
|
+
onMouseDown: e => startResize(e, index, false, EVENT_TYPE.MOUSE),
|
|
291
|
+
tabIndex: 0
|
|
292
|
+
}), count - 1 !== index && vertical && /*#__PURE__*/_jsx2(ResizableBar, {
|
|
293
|
+
container: blocks[index],
|
|
294
|
+
"data-index": index,
|
|
295
|
+
gutter: rest.gutter,
|
|
296
|
+
nextContainer: blocks[index + 1],
|
|
297
|
+
onKeyDown: e => handleKeyDown(e, index, true),
|
|
298
|
+
onMouseDown: e => startResize(e, index, true, EVENT_TYPE.MOUSE),
|
|
299
|
+
tabIndex: 0,
|
|
300
|
+
v: true
|
|
301
|
+
})))
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
ResizableContainer.defaultProps = defaultProps;
|
|
306
|
+
const DSResizableContainerWithSchema = describe(ResizableContainer);
|
|
307
|
+
DSResizableContainerWithSchema.propTypes = props;
|
|
308
|
+
|
|
309
|
+
export { DSResizableContainerWithSchema, ResizableContainer, ResizableContainer as default };
|
package/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DSResizableContainerWithSchema, ResizableContainer, ResizableContainer as default } from './Resizable.js';
|