@wordpress/components
Version:
UI components for WordPress.
20 lines (19 loc) • 832 B
JavaScript
import { useContext, useMemo, useSyncExternalStore } from "@wordpress/element";
import SlotFillContext from "../context.mjs";
// packages/components/src/slot-fill/bubbles-virtually/use-slot-fills.ts
function useObservableValueWithSelector(map, name, selector) {
const subscribe = useMemo(() => (listener) => map.subscribe(name, listener), [map, name]);
const getValue = () => selector(map.get(name));
return useSyncExternalStore(subscribe, getValue, getValue);
}
function getLength(array) {
return array?.length;
}
function useSlotFills(name) {
const length = useObservableValueWithSelector(useContext(SlotFillContext).fills, name, getLength);
return useMemo(() => {
return length !== void 0 ? Array.from({ length }) : void 0;
}, [length]);
}
export { useSlotFills as default };
//# sourceMappingURL=use-slot-fills.mjs.map