react-autosize-container
Version:
React component that gives you feedback about container size (width & height)
98 lines • 4.09 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoSize = void 0;
var react_1 = __importStar(require("react"));
var debounce = function debounce(cb, wait) {
if (wait === void 0) { wait = 20; }
var h;
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
clearTimeout(h);
h = setTimeout(function () { return cb.apply(void 0, args); }, wait);
};
};
var AutoSize = function (_a) {
var style = _a.style, children = _a.children;
var container = react_1.useRef(null);
var _b = react_1.useState(function () {
var _a, _b;
return {
height: ((_a = container === null || container === void 0 ? void 0 : container.current) === null || _a === void 0 ? void 0 : _a.clientHeight) || 0,
width: ((_b = container === null || container === void 0 ? void 0 : container.current) === null || _b === void 0 ? void 0 : _b.clientWidth) || 0,
};
}), dimension = _b[0], setDimension = _b[1];
var handleResize = function () {
var _a, _b;
if (container.current) {
var width_1 = (_a = container.current) === null || _a === void 0 ? void 0 : _a.clientWidth;
var height_1 = (_b = container.current) === null || _b === void 0 ? void 0 : _b.clientHeight;
setDimension(function (prev) {
if (prev.width !== width_1 || prev.height !== height_1) {
return {
width: width_1,
height: height_1,
};
}
return prev;
});
}
};
react_1.useEffect(function () {
var observer;
var observedElement = container.current;
var debounced = debounce(handleResize, 20);
if (window.ResizeObserver && observedElement) {
observer = new ResizeObserver(debounced);
observer.observe(observedElement);
}
else {
// this is fallback if ResizeObserver is not supported
window.addEventListener('resize', debounced);
setTimeout(debounced, 100);
}
return function () {
if (window.ResizeObserver && observedElement) {
observer.unobserve(observedElement);
}
else {
window.removeEventListener('resize', debounced);
}
};
}, []);
return (react_1.default.createElement("div", { ref: container, style: __assign(__assign({}, style), { width: 'inherit', height: 'inherit' }) }, children(dimension)));
};
exports.AutoSize = AutoSize;
//# sourceMappingURL=index.js.map