UNPKG

@wordpress/components

Version:
1 lines • 2.8 kB
{"version":3,"file":"slot.cjs","names":["useContext","SlotFillContext","useRef","View","useMergeRefs","forwardRef"],"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,YAAA,GAAWA,mBAAAA,WAAAA,CAAYC,cAAAA,OAAgB;CAC7C,MAAM,eAAA,GAAcC,mBAAAA,OAAAA,CAAQ,CAAC,CAAE;CAC/B,MAAM,OAAA,GAAMA,mBAAAA,OAAAA,CAAuB,IAAK;CAExC,MAAM,gBAAA,GAAeA,mBAAAA,OAAAA,CAAQ,SAAU;CACvC,CAAA,GAAA,mBAAA,gBAAA,OAAuB;EACtB,aAAa,UAAU;CACxB,GAAG,CAAE,SAAU,CAAE;CAEjB,CAAA,GAAA,mBAAA,gBAAA,OAAuB;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,CAAA,GAAA,mBAAA,gBAAA,OAAuB;EACtB,SAAS,WAAY,MAAM;GAC1B,MAAM;GACN,UAAU,YAAY;GACtB;GACA,WAAW,aAAa;EACzB,CAAE;CACH,CAAE;CAEF,OACC,iBAAA,GAAA,kBAAA,IAAA,CAACC,oBAAAA,MAAD;EACM;EACL,MAAA,GAAMC,mBAAAA,aAAAA,CAAc,CAAE,cAAc,GAAI,CAAE;EAC1C,GAAK;CACL,CAAA;AAEH;AAEA,IAAA,gBAAA,GAAeC,mBAAAA,WAAAA,CAAY,IAAK"}