UNPKG

gl-react

Version:

Universal React library, write and compose WebGL shaders, implement complex effects using a descriptive paradigm

92 lines (76 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * A High Order Component (HOC) function that provides * the contextual `width` and `height` props to a GL Component. * It also merge optional width,height props to override the contextual size * @function connectSize * @param GLComponent - a React Component that receives width and height props * @returns {ReactClass<*>} a Component that merge width and height props * with context and renders `GLComponent`. * @example * const Foo = ({ width, height }) => <Node uniforms={{ width, height }} />; * const FooConnected = connectSize(Foo); * <FooConnected /> // you don't have to provide width, height. * <FooConnected width={64} height={64} /> // If you do, you override width,height in the context as well, so <Node> is implicitly receiving the new width/height. */ const connectSize = GLComponent => { var _class; return _class = class extends _react.Component { constructor(...args) { super(...args); _defineProperty(this, "context", void 0); } getGLSize() { const { props: { width, height }, context: { glSizable } } = this; if (width && height) return [width, height]; const [cw, ch] = glSizable.getGLSize(); return [width || cw, height || ch]; } getChildContext() { return { glSizable: this }; } render() { const { onConnectSizeComponentRef } = this.props; const [width, height] = this.getGLSize(); return /*#__PURE__*/_react.default.createElement(GLComponent, _extends({ ref: onConnectSizeComponentRef }, this.props, { width: width, height: height })); } }, _defineProperty(_class, "displayName", `connectSize(${GLComponent.displayName || GLComponent.name || "?"})`), _defineProperty(_class, "propTypes", { width: _propTypes.default.number, height: _propTypes.default.number }), _defineProperty(_class, "contextTypes", { glSizable: _propTypes.default.object.isRequired }), _defineProperty(_class, "childContextTypes", { glSizable: _propTypes.default.object.isRequired }), _class; }; var _default = connectSize; exports.default = _default; //# sourceMappingURL=connectSize.js.map