UNPKG

@wordpress/components

Version:
1 lines • 4.55 kB
{"version":3,"file":"context-system-provider.mjs","names":[],"sources":["../../src/context/context-system-provider.tsx"],"sourcesContent":["import deepmerge from 'deepmerge';\nimport fastDeepEqual from 'fast-deep-equal/es6/index.js';\nimport { isPlainObject } from 'is-plain-object';\nimport type { ReactNode } from 'react';\nimport {\n\tcreateContext,\n\tuseContext,\n\tuseRef,\n\tuseMemo,\n\tmemo,\n} from '@wordpress/element';\nimport warn from '@wordpress/warning';\nimport { useUpdateEffect } from '../utils';\n\ntype ContextSystemValue = Record<\n\tstring,\n\tRecord< string, unknown > | undefined\n>;\n\nexport const ComponentsContext = createContext< ContextSystemValue >( {} );\nComponentsContext.displayName = 'ComponentsContext';\n\nexport const useComponentsContext = () => useContext( ComponentsContext );\n\n/**\n * Consolidates incoming ContextSystem values with a (potential) parent ContextSystem value.\n *\n * Note: This function will warn if it detects an un-memoized `value`\n *\n * @param {Object} props\n * @param {Record<string, any>} props.value\n * @return {Record<string, any>} The consolidated value.\n */\nfunction useContextSystemBridge( {\n\tvalue,\n}: {\n\tvalue?: ContextSystemValue;\n} ): ContextSystemValue {\n\tconst parentContext = useComponentsContext();\n\n\tconst valueRef = useRef( value );\n\n\tuseUpdateEffect( () => {\n\t\tif (\n\t\t\t// Objects are equivalent.\n\t\t\tfastDeepEqual( valueRef.current, value ) &&\n\t\t\t// But not the same reference.\n\t\t\tvalueRef.current !== value\n\t\t) {\n\t\t\twarn( `Please memoize your context: ${ JSON.stringify( value ) }` );\n\t\t}\n\t}, [ value ] );\n\n\t// `parentContext` will always be memoized (i.e., the result of this hook itself)\n\t// or the default value from when the `ComponentsContext` was originally\n\t// initialized (which will never change, it's a static variable)\n\t// so this memoization will prevent `deepmerge()` from rerunning unless\n\t// the references to `value` change OR the `parentContext` has an actual material change\n\t// (because again, it's guaranteed to be memoized or a static reference to the empty object\n\t// so we know that the only changes for `parentContext` are material ones... i.e., why we\n\t// don't have to warn in the `useUpdateEffect` hook above for `parentContext` and we only\n\t// need to bother with the `value`). The `useUpdateEffect` above will ensure that we are\n\t// correctly warning when the `value` isn't being properly memoized. All of that to say\n\t// that this should be super safe to assume that `useMemo` will only run on actual\n\t// changes to the two dependencies, therefore saving us calls to `deepmerge()`!\n\tconst config = useMemo( () => {\n\t\t// Deep clone `parentContext` to avoid mutating it later.\n\t\treturn deepmerge( parentContext ?? {}, value ?? {}, {\n\t\t\tisMergeableObject: isPlainObject,\n\t\t} );\n\t}, [ parentContext, value ] );\n\n\treturn config;\n}\n\n/**\n * A Provider component that can modify props for connected components within\n * the Context system.\n *\n * @example\n * ```jsx\n * <ContextSystemProvider value={{ Button: { size: 'small' }}}>\n * <Button>...</Button>\n * </ContextSystemProvider>\n * ```\n *\n * @template {Record<string, any>} T\n * @param {Object} options\n * @param {React.ReactNode} options.children Children to render.\n * @param {T} options.value Props to render into connected components.\n * @return {React.JSX.Element} A Provider wrapped component.\n */\nconst BaseContextSystemProvider = ( {\n\tchildren,\n\tvalue,\n}: {\n\tchildren: ReactNode;\n\tvalue?: ContextSystemValue;\n} ) => {\n\tconst contextValue = useContextSystemBridge( { value } );\n\n\treturn (\n\t\t<ComponentsContext.Provider value={ contextValue }>\n\t\t\t{ children }\n\t\t</ComponentsContext.Provider>\n\t);\n};\n\nexport const ContextSystemProvider = memo( BaseContextSystemProvider );\n"],"mappings":";;;;;;;;AAmBA,MAAa,oBAAoB,cAAqC,CAAC,CAAE;AACzE,kBAAkB,cAAc;AAEhC,MAAa,6BAA6B,WAAY,iBAAkB;;;;;;;;;;AAWxE,SAAS,uBAAwB,EAChC,SAGuB;CACvB,MAAM,gBAAgB,qBAAqB;CAE3C,MAAM,WAAW,OAAQ,KAAM;CAE/B,sBAAuB;EACtB,IAEC,cAAe,SAAS,SAAS,KAAM,KAEvC,SAAS,YAAY,OAErB,KAAM,gCAAiC,KAAK,UAAW,KAAM,GAAK;CAEpE,GAAG,CAAE,KAAM,CAAE;CAqBb,OAPe,cAAe;EAE7B,OAAO,UAAW,iBAAiB,CAAC,GAAG,SAAS,CAAC,GAAG,EACnD,mBAAmB,cACpB,CAAE;CACH,GAAG,CAAE,eAAe,KAAM,CAEd;AACb;;;;;;;;;;;;;;;;;;AAmBA,MAAM,6BAA8B,EACnC,UACA,YAIM;CACN,MAAM,eAAe,uBAAwB,EAAE,MAAM,CAAE;CAEvD,OACC,oBAAC,kBAAkB,UAAnB;EAA4B,OAAQ;EACjC;CACyB,CAAA;AAE9B;AAEA,MAAa,wBAAwB,KAAM,yBAA0B"}