UNPKG

@wordpress/components

Version:
37 lines (36 loc) • 1.89 kB
import clsx from "clsx"; import warn from "@wordpress/warning"; import { useComponentsContext } from "./context-system-provider.mjs"; import { getConnectedNamespace, getNamespace } from "./utils.mjs"; import { getStyledClassNameFromKey } from "./get-styled-class-name-from-key.mjs"; // packages/components/src/context/use-context-system.ts /** * Custom hook that derives registered props from the Context system. * These derived props are then consolidated with incoming component props. * * @param props Incoming props from the component. * @param namespace The namespace to register and to derive context props from. * @return The connected props. */ function useContextSystem(props, namespace) { const contextSystemProps = useComponentsContext(); if (typeof namespace === "undefined") warn("useContextSystem: Please provide a namespace"); const contextProps = contextSystemProps?.[namespace] || {}; const finalComponentProps = { ...getConnectedNamespace(), ...getNamespace(namespace) }; const { _overrides, ...otherContextProps } = contextProps; const overrideProps = _overrides; const initialMergedProps = Object.entries(otherContextProps).length ? Object.assign({}, otherContextProps, props) : props; const propsWithChildren = initialMergedProps; const classes = clsx(getStyledClassNameFromKey(namespace), props.className); const rendered = typeof propsWithChildren.renderChildren === "function" ? propsWithChildren.renderChildren(initialMergedProps) : propsWithChildren.children; for (const key in initialMergedProps) finalComponentProps[key] = initialMergedProps[key]; for (const key in overrideProps) finalComponentProps[key] = overrideProps[key]; if (rendered !== void 0) finalComponentProps.children = rendered; finalComponentProps.className = classes; return finalComponentProps; } export { useContextSystem }; //# sourceMappingURL=use-context-system.mjs.map