UNPKG

lucid-ui

Version:

A UI component library from Xandr.

103 lines 4.73 kB
"use strict"; 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) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 = require("lodash"); var react_1 = __importDefault(require("react")); var prop_types_1 = __importDefault(require("prop-types")); var style_helpers_1 = require("../../util/style-helpers"); 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 = (0, 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({}, (0, lodash_1.omit)(passThroughs, ['className', 'children'].concat(['initialState', 'callbackId'])), { className: cx('&', className), ref: this._element }), children && children(width, height))); }; Resizer.displayName = 'Resizer'; Resizer.peek = { description: "A helper component used for getting the width and height of a containing element. This component doesn't take normal children. It expects you to pass a single function for children. It will then call that function with new `width` and `height` values if the container size changes.", categories: ['utility'], }; Resizer.propTypes = { /** Appended to the component-specific class names set on the root elements. */ className: string, /** A function that returns your rendered content with the signature: \`(width, height) => {}\` */ children: func, }; return Resizer; }(react_1.default.Component)); exports.default = Resizer; //# sourceMappingURL=Resizer.js.map