UNPKG

@wordpress/components

Version:
1 lines • 2.49 kB
{"version":3,"file":"slot.mjs","names":[],"sources":["../../../src/slot-fill/bubbles-virtually/slot.tsx"],"sourcesContent":["import type { ForwardedRef } from 'react';\nimport {\n\tuseRef,\n\tuseLayoutEffect,\n\tuseContext,\n\tforwardRef,\n} from '@wordpress/element';\nimport { useMergeRefs } from '@wordpress/compose';\nimport { View } from '../../view';\nimport SlotFillContext from '../context';\nimport type { WordPressComponentProps } from '../../context';\nimport type { SlotComponentProps } from '../types';\n\nfunction Slot(\n\tprops: WordPressComponentProps<\n\t\tOmit< SlotComponentProps, 'bubblesVirtually' >,\n\t\t'div'\n\t>,\n\tforwardedRef: ForwardedRef< any >\n) {\n\tconst {\n\t\tname,\n\t\tfillProps = {},\n\t\tas,\n\t\t// `children` is not allowed. However, if it is passed,\n\t\t// it will be displayed as is, so remove `children`.\n\t\tchildren,\n\t\t...restProps\n\t} = props;\n\n\tconst registry = useContext( SlotFillContext );\n\tconst instanceRef = useRef( {} );\n\tconst ref = useRef< HTMLElement >( null );\n\n\tconst fillPropsRef = useRef( fillProps );\n\tuseLayoutEffect( () => {\n\t\tfillPropsRef.current = fillProps;\n\t}, [ fillProps ] );\n\n\tuseLayoutEffect( () => {\n\t\tconst instance = instanceRef.current;\n\t\tregistry.registerSlot( name, {\n\t\t\ttype: 'portal',\n\t\t\tinstance,\n\t\t\tref,\n\t\t\tfillProps: fillPropsRef.current,\n\t\t} );\n\t\treturn () => registry.unregisterSlot( name, instance );\n\t}, [ registry, name ] );\n\n\tuseLayoutEffect( () => {\n\t\tregistry.updateSlot( name, {\n\t\t\ttype: 'portal',\n\t\t\tinstance: instanceRef.current,\n\t\t\tref,\n\t\t\tfillProps: fillPropsRef.current,\n\t\t} );\n\t} );\n\n\treturn (\n\t\t<View\n\t\t\tas={ as }\n\t\t\tref={ useMergeRefs( [ forwardedRef, ref ] ) }\n\t\t\t{ ...restProps }\n\t\t/>\n\t);\n}\n\nexport default forwardRef( Slot );\n"],"mappings":";;;;;;AAaA,SAAS,KACR,OAIA,cACC;CACD,MAAM,EACL,MACA,YAAY,CAAC,GACb,IAGA,UACA,GAAG,cACA;CAEJ,MAAM,WAAW,WAAY,eAAgB;CAC7C,MAAM,cAAc,OAAQ,CAAC,CAAE;CAC/B,MAAM,MAAM,OAAuB,IAAK;CAExC,MAAM,eAAe,OAAQ,SAAU;CACvC,sBAAuB;EACtB,aAAa,UAAU;CACxB,GAAG,CAAE,SAAU,CAAE;CAEjB,sBAAuB;EACtB,MAAM,WAAW,YAAY;EAC7B,SAAS,aAAc,MAAM;GAC5B,MAAM;GACN;GACA;GACA,WAAW,aAAa;EACzB,CAAE;EACF,aAAa,SAAS,eAAgB,MAAM,QAAS;CACtD,GAAG,CAAE,UAAU,IAAK,CAAE;CAEtB,sBAAuB;EACtB,SAAS,WAAY,MAAM;GAC1B,MAAM;GACN,UAAU,YAAY;GACtB;GACA,WAAW,aAAa;EACzB,CAAE;CACH,CAAE;CAEF,OACC,oBAAC,MAAD;EACM;EACL,KAAM,aAAc,CAAE,cAAc,GAAI,CAAE;EAC1C,GAAK;CACL,CAAA;AAEH;AAEA,IAAA,eAAe,WAAY,IAAK"}