useStretchStyle.js
1.54 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
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _default = function _default(stretch) {
var _React$useState = React.useState({
width: 0,
height: 0
}),
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
targetSize = _React$useState2[0],
setTargetSize = _React$useState2[1];
function measureStretch(element) {
setTargetSize({
width: element.offsetWidth,
height: element.offsetHeight
});
} // Merge stretch style
var style = React.useMemo(function () {
var sizeStyle = {};
if (stretch) {
var width = targetSize.width,
height = targetSize.height; // Stretch with target
if (stretch.indexOf('height') !== -1 && height) {
sizeStyle.height = height;
} else if (stretch.indexOf('minHeight') !== -1 && height) {
sizeStyle.minHeight = height;
}
if (stretch.indexOf('width') !== -1 && width) {
sizeStyle.width = width;
} else if (stretch.indexOf('minWidth') !== -1 && width) {
sizeStyle.minWidth = width;
}
}
return sizeStyle;
}, [stretch, targetSize]);
return [style, measureStretch];
};
exports.default = _default;