UNPKG

@wordpress/components

Version:
1 lines • 3.89 kB
{"version":3,"file":"slot.cjs","names":["Children","cloneElement","useContext","SlotFillContext","useRef","useObservableValue","isEmptyElement"],"sources":["../../src/slot-fill/slot.tsx"],"sourcesContent":["import type { ReactElement, ReactNode, Key } from 'react';\nimport { useObservableValue } from '@wordpress/compose';\nimport {\n\tuseContext,\n\tuseLayoutEffect,\n\tuseRef,\n\tChildren,\n\tcloneElement,\n\tisEmptyElement,\n} from '@wordpress/element';\nimport SlotFillContext from './context';\nimport type { SlotComponentProps } from './types';\n\n/**\n * Whether the argument is a function.\n *\n * @param maybeFunc The argument to check.\n * @return True if the argument is a function, false otherwise.\n */\nfunction isFunction( maybeFunc: any ): maybeFunc is Function {\n\treturn typeof maybeFunc === 'function';\n}\n\nfunction addKeysToChildren( children: ReactNode ) {\n\treturn Children.map( children, ( child, childIndex ) => {\n\t\tif ( ! child || typeof child === 'string' ) {\n\t\t\treturn child;\n\t\t}\n\t\tlet childKey: Key = childIndex;\n\t\tif ( typeof child === 'object' && 'key' in child && child?.key ) {\n\t\t\tchildKey = child.key;\n\t\t}\n\n\t\treturn cloneElement( child as ReactElement, {\n\t\t\tkey: childKey,\n\t\t} );\n\t} );\n}\n\nfunction Slot( props: Omit< SlotComponentProps, 'bubblesVirtually' > ) {\n\tconst { name, children, fillProps = {} } = props;\n\n\tconst registry = useContext( SlotFillContext );\n\tconst instanceRef = useRef( {} );\n\n\tuseLayoutEffect( () => {\n\t\tconst instance = instanceRef.current;\n\t\tregistry.registerSlot( name, { type: 'children', instance } );\n\t\treturn () => registry.unregisterSlot( name, instance );\n\t}, [ registry, name ] );\n\n\tlet fills = useObservableValue( registry.fills, name ) ?? [];\n\tconst currentSlot = useObservableValue( registry.slots, name );\n\n\t// Fills should only be rendered in the currently registered instance of the slot.\n\tif ( ! currentSlot || currentSlot.instance !== instanceRef.current ) {\n\t\tfills = [];\n\t}\n\n\tconst renderedFills = fills\n\t\t.map( ( fill ) => {\n\t\t\tconst fillChildren = isFunction( fill.children )\n\t\t\t\t? fill.children( fillProps )\n\t\t\t\t: fill.children;\n\t\t\treturn addKeysToChildren( fillChildren );\n\t\t} )\n\t\t.filter(\n\t\t\t// In some cases fills are rendered only when some conditions apply.\n\t\t\t// This ensures that we only use non-empty fills when rendering, i.e.,\n\t\t\t// it allows us to render wrappers only when the fills are actually present.\n\t\t\t( element ) => ! isEmptyElement( element )\n\t\t);\n\n\treturn (\n\t\t<>\n\t\t\t{ isFunction( children )\n\t\t\t\t? children( renderedFills )\n\t\t\t\t: renderedFills }\n\t\t</>\n\t);\n}\n\nexport default Slot;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,SAAS,WAAY,WAAwC;CAC5D,OAAO,OAAO,cAAc;AAC7B;AAEA,SAAS,kBAAmB,UAAsB;CACjD,OAAOA,mBAAAA,SAAS,IAAK,WAAY,OAAO,eAAgB;EACvD,IAAK,CAAE,SAAS,OAAO,UAAU,UAChC,OAAO;EAER,IAAI,WAAgB;EACpB,IAAK,OAAO,UAAU,YAAY,SAAS,SAAS,OAAO,KAC1D,WAAW,MAAM;EAGlB,QAAA,GAAOC,mBAAAA,aAAAA,CAAc,OAAuB,EAC3C,KAAK,SACN,CAAE;CACH,CAAE;AACH;AAEA,SAAS,KAAM,OAAwD;CACtE,MAAM,EAAE,MAAM,UAAU,YAAY,CAAC,MAAM;CAE3C,MAAM,YAAA,GAAWC,mBAAAA,WAAAA,CAAYC,aAAAA,OAAgB;CAC7C,MAAM,eAAA,GAAcC,mBAAAA,OAAAA,CAAQ,CAAC,CAAE;CAE/B,CAAA,GAAA,mBAAA,gBAAA,OAAuB;EACtB,MAAM,WAAW,YAAY;EAC7B,SAAS,aAAc,MAAM;GAAE,MAAM;GAAY;EAAS,CAAE;EAC5D,aAAa,SAAS,eAAgB,MAAM,QAAS;CACtD,GAAG,CAAE,UAAU,IAAK,CAAE;CAEtB,IAAI,SAAA,GAAQC,mBAAAA,mBAAAA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