UNPKG

@wordpress/components

Version:
91 lines (89 loc) 3.46 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/components/src/higher-order/with-fallback-styles/index.tsx var with_fallback_styles_exports = {}; __export(with_fallback_styles_exports, { default: () => with_fallback_styles_default }); module.exports = __toCommonJS(with_fallback_styles_exports); var import_es6 = __toESM(require("fast-deep-equal/es6")); var import_element = require("@wordpress/element"); var import_compose = require("@wordpress/compose"); var import_jsx_runtime = require("react/jsx-runtime"); var with_fallback_styles_default = (mapNodeToProps) => (0, import_compose.createHigherOrderComponent)((WrappedComponent) => { return class extends import_element.Component { constructor(props) { super(props); this.nodeRef = this.props.node; this.state = { fallbackStyles: void 0, grabStylesCompleted: false }; this.bindRef = this.bindRef.bind(this); } bindRef(node) { if (!node) { return; } this.nodeRef = node; } componentDidMount() { this.grabFallbackStyles(); } componentDidUpdate() { this.grabFallbackStyles(); } grabFallbackStyles() { const { grabStylesCompleted, fallbackStyles } = this.state; if (this.nodeRef && !grabStylesCompleted) { const newFallbackStyles = mapNodeToProps(this.nodeRef, this.props); if (!(0, import_es6.default)(newFallbackStyles, fallbackStyles)) { this.setState({ fallbackStyles: newFallbackStyles, grabStylesCompleted: Object.values(newFallbackStyles).every(Boolean) }); } } } render() { const wrappedComponent = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(WrappedComponent, { ...this.props, ...this.state.fallbackStyles }); return this.props.node ? wrappedComponent : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { ref: this.bindRef, children: [" ", wrappedComponent, " "] }); } }; }, "withFallbackStyles"); //# sourceMappingURL=index.js.map