piral-core
Version:
The core library for creating a Piral instance.
29 lines • 1.22 kB
JavaScript
import * as React from 'react';
import { isfunc } from 'piral-base';
import { useGlobalState } from '../hooks';
import { defaultRender, none } from '../utils';
function defaultOrder(extensions) {
return extensions;
}
/**
* The extension slot component to be used when the available
* extensions of a given name should be rendered at a specific
* location.
*/
export function ExtensionSlot(props) {
const { name, render = defaultRender, empty, params, children, emptySkipsRender = false, order = defaultOrder, } = props;
const extensions = useGlobalState((s) => s.registry.extensions[name] || none);
const isEmpty = extensions.length === 0 && isfunc(empty);
const content = isEmpty
? [defaultRender(empty(params), 'empty')]
: order(extensions).map(({ component: Component, reference, defaults = {} }, i) => (React.createElement(Component, { key: `${reference?.displayName || '_'}${i}`, children: children, params: {
...defaults,
...params,
} })));
if (isEmpty && emptySkipsRender) {
return content[0];
}
return render(content);
}
ExtensionSlot.displayName = `ExtensionSlot`;
//# sourceMappingURL=ExtensionSlot.js.map