UNPKG

1.61 kBJavaScriptView Raw
1import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2import * as React from 'react';
3export default (function (stretch) {
4 var _React$useState = React.useState({
5 width: 0,
6 height: 0
7 }),
8 _React$useState2 = _slicedToArray(_React$useState, 2),
9 targetSize = _React$useState2[0],
10 setTargetSize = _React$useState2[1];
11
12 function measureStretch(element) {
13 var tgtWidth = element.offsetWidth,
14 tgtHeight = element.offsetHeight;
15
16 var _element$getBoundingC = element.getBoundingClientRect(),
17 width = _element$getBoundingC.width,
18 height = _element$getBoundingC.height; // Rect is more accurate than offset, use if near
19
20
21 if (Math.abs(tgtWidth - width) < 1 && Math.abs(tgtHeight - height) < 1) {
22 tgtWidth = width;
23 tgtHeight = height;
24 }
25
26 setTargetSize({
27 width: tgtWidth,
28 height: tgtHeight
29 });
30 } // Merge stretch style
31
32
33 var style = React.useMemo(function () {
34 var sizeStyle = {};
35
36 if (stretch) {
37 var width = targetSize.width,
38 height = targetSize.height; // Stretch with target
39
40 if (stretch.indexOf('height') !== -1 && height) {
41 sizeStyle.height = height;
42 } else if (stretch.indexOf('minHeight') !== -1 && height) {
43 sizeStyle.minHeight = height;
44 }
45
46 if (stretch.indexOf('width') !== -1 && width) {
47 sizeStyle.width = width;
48 } else if (stretch.indexOf('minWidth') !== -1 && width) {
49 sizeStyle.minWidth = width;
50 }
51 }
52
53 return sizeStyle;
54 }, [stretch, targetSize]);
55 return [style, measureStretch];
56});
\No newline at end of file