UNPKG

@wordpress/components

Version:
1 lines • 2.63 kB
{"version":3,"file":"fill.mjs","names":[],"sources":["../../src/slot-fill/fill.tsx"],"sourcesContent":["import { useObservableValue } from '@wordpress/compose';\nimport {\n\tuseContext,\n\tuseLayoutEffect,\n\tuseRef,\n\tcreatePortal,\n} from '@wordpress/element';\nimport SlotFillContext from './context';\nimport type { FillComponentProps } from './types';\nimport StyleProvider from '../style-provider';\n\nexport default function Fill( { name, children }: FillComponentProps ) {\n\tconst registry = useContext( SlotFillContext );\n\tconst instanceRef = useRef( {} );\n\tconst childrenRef = useRef( children );\n\n\tuseLayoutEffect( () => {\n\t\tchildrenRef.current = children;\n\t}, [ children ] );\n\n\tuseLayoutEffect( () => {\n\t\tconst instance = instanceRef.current;\n\t\tregistry.registerFill( name, {\n\t\t\tinstance,\n\t\t\tchildren: childrenRef.current,\n\t\t} );\n\t\treturn () => registry.unregisterFill( name, instance );\n\t}, [ registry, name ] );\n\n\tuseLayoutEffect( () => {\n\t\tregistry.updateFill( name, {\n\t\t\tinstance: instanceRef.current,\n\t\t\tchildren,\n\t\t} );\n\t}, [ registry, name, children ] );\n\n\tconst slot = useObservableValue( registry.slots, name );\n\n\tif ( ! slot ) {\n\t\treturn null;\n\t}\n\n\tif ( slot.type === 'children' ) {\n\t\treturn null;\n\t}\n\n\tconst portalEl = slot.ref.current;\n\tif ( ! portalEl ) {\n\t\treturn null;\n\t}\n\n\tconst wrappedChildren =\n\t\ttypeof children === 'function'\n\t\t\t? children( slot.fillProps ?? {} )\n\t\t\t: children;\n\n\t// When using a `Fill`, the `children` will be rendered in the document of the\n\t// `Slot`. Wrap the children in a `StyleProvider` so CSS Module styles target\n\t// that document/iframe instead of the document containing the `Fill`.\n\treturn createPortal(\n\t\t<StyleProvider document={ portalEl.ownerDocument }>\n\t\t\t{ wrappedChildren }\n\t\t</StyleProvider>,\n\t\tportalEl\n\t);\n}\n"],"mappings":";;;;;;AAWA,SAAwB,KAAM,EAAE,MAAM,YAAiC;CACtE,MAAM,WAAW,WAAY,eAAgB;CAC7C,MAAM,cAAc,OAAQ,CAAC,CAAE;CAC/B,MAAM,cAAc,OAAQ,QAAS;CAErC,sBAAuB;EACtB,YAAY,UAAU;CACvB,GAAG,CAAE,QAAS,CAAE;CAEhB,sBAAuB;EACtB,MAAM,WAAW,YAAY;EAC7B,SAAS,aAAc,MAAM;GAC5B;GACA,UAAU,YAAY;EACvB,CAAE;EACF,aAAa,SAAS,eAAgB,MAAM,QAAS;CACtD,GAAG,CAAE,UAAU,IAAK,CAAE;CAEtB,sBAAuB;EACtB,SAAS,WAAY,MAAM;GAC1B,UAAU,YAAY;GACtB;EACD,CAAE;CACH,GAAG;EAAE;EAAU;EAAM;CAAS,CAAE;CAEhC,MAAM,OAAO,mBAAoB,SAAS,OAAO,IAAK;CAEtD,IAAK,CAAE,MACN,OAAO;CAGR,IAAK,KAAK,SAAS,YAClB,OAAO;CAGR,MAAM,WAAW,KAAK,IAAI;CAC1B,IAAK,CAAE,UACN,OAAO;CAGR,MAAM,kBACL,OAAO,aAAa,aACjB,SAAU,KAAK,aAAa,CAAC,CAAE,IAC/B;CAKJ,OAAO,aACN,oBAAC,eAAD;EAAe,UAAW,SAAS;EAChC,UAAA;CACY,CAAA,GACf,QACD;AACD"}