@wordpress/components
Version:
UI components for WordPress.
1 lines • 3.34 kB
Source Map (JSON)
{"version":3,"file":"use-context-system.mjs","names":[],"sources":["../../src/context/use-context-system.ts"],"sourcesContent":["import clsx from 'clsx';\nimport type { ReactNode } from 'react';\nimport warn from '@wordpress/warning';\nimport { useComponentsContext } from './context-system-provider';\nimport { getNamespace, getConnectedNamespace } from './utils';\nimport { getStyledClassNameFromKey } from './get-styled-class-name-from-key';\nimport type { ConnectedProps } from './types';\n\n/**\n * Custom hook that derives registered props from the Context system.\n * These derived props are then consolidated with incoming component props.\n *\n * @param props Incoming props from the component.\n * @param namespace The namespace to register and to derive context props from.\n * @return The connected props.\n */\nexport function useContextSystem< P extends { className?: string } >(\n\tprops: P,\n\tnamespace: string\n) {\n\tconst contextSystemProps = useComponentsContext();\n\tif ( typeof namespace === 'undefined' ) {\n\t\twarn( 'useContextSystem: Please provide a namespace' );\n\t}\n\n\tconst contextProps = contextSystemProps?.[ namespace ] || {};\n\n\tconst finalComponentProps = {\n\t\t...getConnectedNamespace(),\n\t\t...getNamespace( namespace ),\n\t} as ConnectedProps< P >;\n\n\tconst { _overrides, ...otherContextProps } = contextProps;\n\tconst overrideProps = _overrides as Record< string, unknown >;\n\n\tconst initialMergedProps = Object.entries( otherContextProps ).length\n\t\t? Object.assign( {}, otherContextProps, props )\n\t\t: props;\n\tconst propsWithChildren: P & {\n\t\tchildren?: ReactNode;\n\t\trenderChildren?: ( props: P ) => ReactNode;\n\t} = initialMergedProps;\n\n\tconst classes = clsx(\n\t\tgetStyledClassNameFromKey( namespace ),\n\t\tprops.className\n\t);\n\n\t// Provides the ability to customize the render of the component.\n\tconst rendered =\n\t\ttypeof propsWithChildren.renderChildren === 'function'\n\t\t\t? propsWithChildren.renderChildren( initialMergedProps )\n\t\t\t: propsWithChildren.children;\n\n\tfor ( const key in initialMergedProps ) {\n\t\t( finalComponentProps as Record< string, unknown > )[ key ] =\n\t\t\tinitialMergedProps[ key ];\n\t}\n\n\tfor ( const key in overrideProps ) {\n\t\t( finalComponentProps as Record< string, unknown > )[ key ] =\n\t\t\toverrideProps[ key ];\n\t}\n\n\t// Setting an `undefined` explicitly can cause unintended overwrites\n\t// when a `cloneElement()` is involved.\n\tif ( rendered !== undefined ) {\n\t\t( finalComponentProps as Record< string, unknown > ).children =\n\t\t\trendered;\n\t}\n\n\tfinalComponentProps.className = classes;\n\n\treturn finalComponentProps;\n}\n"],"mappings":";;;;;;;;;;;;;;AAgBA,SAAgB,iBACf,OACA,WACC;CACD,MAAM,qBAAqB,qBAAqB;CAChD,IAAK,OAAO,cAAc,aACzB,KAAM,8CAA+C;CAGtD,MAAM,eAAe,qBAAsB,cAAe,CAAC;CAE3D,MAAM,sBAAsB;EAC3B,GAAG,sBAAsB;EACzB,GAAG,aAAc,SAAU;CAC5B;CAEA,MAAM,EAAE,YAAY,GAAG,sBAAsB;CAC7C,MAAM,gBAAgB;CAEtB,MAAM,qBAAqB,OAAO,QAAS,iBAAkB,CAAC,CAAC,SAC5D,OAAO,OAAQ,CAAC,GAAG,mBAAmB,KAAM,IAC5C;CACH,MAAM,oBAGF;CAEJ,MAAM,UAAU,KACf,0BAA2B,SAAU,GACrC,MAAM,SACP;CAGA,MAAM,WACL,OAAO,kBAAkB,mBAAmB,aACzC,kBAAkB,eAAgB,kBAAmB,IACrD,kBAAkB;CAEtB,KAAM,MAAM,OAAO,oBAClB,oBAAsD,OACrD,mBAAoB;CAGtB,KAAM,MAAM,OAAO,eAClB,oBAAsD,OACrD,cAAe;CAKjB,IAAK,aAAa,KAAA,GACjB,oBAAqD,WACpD;CAGF,oBAAoB,YAAY;CAEhC,OAAO;AACR"}