UNPKG

2 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11
12var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
13
14var React = _interopRequireWildcard(require("react"));
15
16var _default = function _default(stretch) {
17 var _React$useState = React.useState({
18 width: 0,
19 height: 0
20 }),
21 _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
22 targetSize = _React$useState2[0],
23 setTargetSize = _React$useState2[1];
24
25 function measureStretch(element) {
26 var tgtWidth = element.offsetWidth,
27 tgtHeight = element.offsetHeight;
28
29 var _element$getBoundingC = element.getBoundingClientRect(),
30 width = _element$getBoundingC.width,
31 height = _element$getBoundingC.height; // Rect is more accurate than offset, use if near
32
33
34 if (Math.abs(tgtWidth - width) < 1 && Math.abs(tgtHeight - height) < 1) {
35 tgtWidth = width;
36 tgtHeight = height;
37 }
38
39 setTargetSize({
40 width: tgtWidth,
41 height: tgtHeight
42 });
43 } // Merge stretch style
44
45
46 var style = React.useMemo(function () {
47 var sizeStyle = {};
48
49 if (stretch) {
50 var width = targetSize.width,
51 height = targetSize.height; // Stretch with target
52
53 if (stretch.indexOf('height') !== -1 && height) {
54 sizeStyle.height = height;
55 } else if (stretch.indexOf('minHeight') !== -1 && height) {
56 sizeStyle.minHeight = height;
57 }
58
59 if (stretch.indexOf('width') !== -1 && width) {
60 sizeStyle.width = width;
61 } else if (stretch.indexOf('minWidth') !== -1 && width) {
62 sizeStyle.minWidth = width;
63 }
64 }
65
66 return sizeStyle;
67 }, [stretch, targetSize]);
68 return [style, measureStretch];
69};
70
71exports.default = _default;
\No newline at end of file