rc-trigger
Version:
base abstract trigger component for react
56 lines (45 loc) • 1.61 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
export default (function (stretch) {
var _React$useState = React.useState({
width: 0,
height: 0
}),
_React$useState2 = _slicedToArray(_React$useState, 2),
targetSize = _React$useState2[0],
setTargetSize = _React$useState2[1];
function measureStretch(element) {
var tgtWidth = element.offsetWidth,
tgtHeight = element.offsetHeight;
var _element$getBoundingC = element.getBoundingClientRect(),
width = _element$getBoundingC.width,
height = _element$getBoundingC.height; // Rect is more accurate than offset, use if near
if (Math.abs(tgtWidth - width) < 1 && Math.abs(tgtHeight - height) < 1) {
tgtWidth = width;
tgtHeight = height;
}
setTargetSize({
width: tgtWidth,
height: tgtHeight
});
} // 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];
});