UNPKG

@wordpress/components

Version:
69 lines (58 loc) 1.77 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { createElement } from "@wordpress/element"; /** * External dependencies */ import fastDeepEqual from 'fast-deep-equal/es6'; /** * WordPress dependencies */ import { Component } from '@wordpress/element'; import { createHigherOrderComponent } from '@wordpress/compose'; export default (mapNodeToProps => createHigherOrderComponent(WrappedComponent => { return class extends Component { constructor(props) { super(props); this.nodeRef = this.props.node; this.state = { fallbackStyles: undefined, 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 (!fastDeepEqual(newFallbackStyles, fallbackStyles)) { this.setState({ fallbackStyles: newFallbackStyles, grabStylesCompleted: Object.values(newFallbackStyles).every(Boolean) }); } } } render() { const wrappedComponent = createElement(WrappedComponent, _extends({}, this.props, this.state.fallbackStyles)); return this.props.node ? wrappedComponent : createElement("div", { ref: this.bindRef }, " ", wrappedComponent, " "); } }; }, 'withFallbackStyles')); //# sourceMappingURL=index.js.map