@wordpress/components
Version:
UI components for WordPress.
34 lines (33 loc) • 1.33 kB
JavaScript
import fastDeepEqual from "fast-deep-equal/es6/index.js";
import { useRef, useState } from "@wordpress/element";
import { createHigherOrderComponent, useIsomorphicLayoutEffect } from "@wordpress/compose";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/components/src/higher-order/with-fallback-styles/index.tsx
var with_fallback_styles_default = (mapNodeToProps) => createHigherOrderComponent((WrappedComponent) => {
return function WithFallbackStyles(props) {
const [fallbackStyles, setFallbackStyles] = useState(void 0);
const nodeRef = useRef(null);
useIsomorphicLayoutEffect(() => {
const node = props.node ?? nodeRef.current;
const grabStylesCompleted = !!fallbackStyles && Object.values(fallbackStyles).every(Boolean);
if (node && !grabStylesCompleted) {
const newFallbackStyles = mapNodeToProps(node, props);
if (!fastDeepEqual(newFallbackStyles, fallbackStyles)) setFallbackStyles(newFallbackStyles);
}
});
const wrappedComponent = /* @__PURE__ */ jsx(WrappedComponent, {
...props,
...fallbackStyles
});
return props.node ? wrappedComponent : /* @__PURE__ */ jsxs("div", {
ref: nodeRef,
children: [
" ",
wrappedComponent,
" "
]
});
};
}, "withFallbackStyles");
export { with_fallback_styles_default as default };
//# sourceMappingURL=index.mjs.map