lucid-ui
Version:
A UI component library from AppNexus.
100 lines • 5.27 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
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 __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_1 = __importDefault(require("lodash"));
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("react-peek/prop-types"));
var style_helpers_1 = require("../../util/style-helpers");
var component_types_1 = require("../../util/component-types");
var element_resize_detector_1 = __importDefault(require("element-resize-detector"));
var cx = style_helpers_1.lucidClassNames.bind('&-Resizer');
var func = prop_types_1.default.func, string = prop_types_1.default.string;
var Resizer = /** @class */ (function (_super) {
__extends(Resizer, _super);
function Resizer() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this._element = react_1.default.createRef();
_this.resizeDetector = element_resize_detector_1.default({ strategy: 'scroll' });
_this.state = {
width: 0,
height: 0,
};
_this.handleResize = function (_a) {
var offsetWidth = _a.offsetWidth, offsetHeight = _a.offsetHeight;
_this.setState({
width: offsetWidth,
height: offsetHeight,
});
};
return _this;
}
Resizer.prototype.componentDidMount = function () {
if (this._element.current) {
this.resizeDetector.listenTo(this._element.current, this.handleResize);
}
};
Resizer.prototype.componentWillUnmount = function () {
if (this._element.current) {
this.resizeDetector.removeListener(this._element.current, this.handleResize);
}
};
Resizer.prototype.render = function () {
var _a = this.props, className = _a.className, children = _a.children, passThroughs = __rest(_a, ["className", "children"]);
var _b = this.state, width = _b.width, height = _b.height;
return (react_1.default.createElement("div", __assign({}, component_types_1.omitProps(passThroughs, undefined, lodash_1.default.keys(Resizer.propTypes)), { className: cx('&', className), ref: this._element }), children && children(width, height)));
};
Resizer.displayName = 'Resizer';
Resizer.peek = {
description: "\n\t\t\tThis is a helper component used for getting the width and height of a\n\t\t\tcontaining element. This component doesn't take normal children. It\n\t\t\texpects you to pass a single function for children. It will then call\n\t\t\tthat function with new `width` and `height` values if the container\n\t\t\tsize changes.\n\t\t",
categories: ['utility'],
};
Resizer.propTypes = {
className: string(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\t\tAppended to the component-specific class names set on the root elements.\n\t\t"], ["\n\t\t\tAppended to the component-specific class names set on the root elements.\n\t\t"]))),
children: func(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\t\tA function that returns your rendered content with the signature:\n\t\t\t`(width, height) => {}`\n\t\t"], ["\n\t\t\tA function that returns your rendered content with the signature:\n\t\t\t\\`(width, height) => {}\\`\n\t\t"]))),
};
return Resizer;
}(react_1.default.Component));
exports.default = Resizer;
var templateObject_1, templateObject_2;
//# sourceMappingURL=Resizer.js.map