UNPKG

@wordpress/components

Version:
1 lines • 1.72 kB
{"version":3,"file":"use-slot-fills.mjs","names":[],"sources":["../../../src/slot-fill/bubbles-virtually/use-slot-fills.ts"],"sourcesContent":["import { useContext, useMemo, useSyncExternalStore } from '@wordpress/element';\nimport type { ObservableMap } from '@wordpress/compose';\nimport SlotFillContext from '../context';\nimport type { SlotKey } from '../types';\n\nfunction useObservableValueWithSelector< K, V, S >(\n\tmap: ObservableMap< K, V >,\n\tname: K,\n\tselector: ( v: V | undefined ) => S\n) {\n\tconst subscribe = useMemo(\n\t\t() => ( listener: () => void ) => map.subscribe( name, listener ),\n\t\t[ map, name ]\n\t);\n\tconst getValue = () => selector( map.get( name ) );\n\treturn useSyncExternalStore( subscribe, getValue, getValue );\n}\n\nfunction getLength< T >( array: T[] | undefined ) {\n\treturn array?.length;\n}\n\nexport default function useSlotFills( name: SlotKey ) {\n\tconst registry = useContext( SlotFillContext );\n\tconst length = useObservableValueWithSelector(\n\t\tregistry.fills,\n\t\tname,\n\t\tgetLength\n\t);\n\t// callers expect an opaque array with length `length`, so create that array\n\tconst fills = useMemo( () => {\n\t\treturn length !== undefined ? Array.from( { length } ) : undefined;\n\t}, [ length ] );\n\treturn fills;\n}\n"],"mappings":";;;AAKA,SAAS,+BACR,KACA,MACA,UACC;CACD,MAAM,YAAY,eACT,aAA0B,IAAI,UAAW,MAAM,QAAS,GAChE,CAAE,KAAK,IAAK,CACb;CACA,MAAM,iBAAiB,SAAU,IAAI,IAAK,IAAK,CAAE;CACjD,OAAO,qBAAsB,WAAW,UAAU,QAAS;AAC5D;AAEA,SAAS,UAAgB,OAAyB;CACjD,OAAO,OAAO;AACf;AAEA,SAAwB,aAAc,MAAgB;CAErD,MAAM,SAAS,+BADE,WAAY,eAErB,CAAC,CAAC,OACT,MACA,SACD;CAKA,OAHc,cAAe;EAC5B,OAAO,WAAW,KAAA,IAAY,MAAM,KAAM,EAAE,OAAO,CAAE,IAAI,KAAA;CAC1D,GAAG,CAAE,MAAO,CACD;AACZ"}