@rsc-labs/nocto-plugin-system
Version:
Pluggable registry system for Nocto plugins
23 lines (22 loc) • 1.77 kB
JavaScript
// / <reference types="vite/types/importMeta.d.ts" />
import React from "react";
import { useNoctoPluginContext } from "../providers/nocto-context";
const Note = ({ text, children }) => {
return (React.createElement("div", { className: "relative w-full mb-4" },
React.createElement("div", { className: "absolute left-4 -top-3 px-2 text-sm font-medium z-10\n border-t border-l border-r rounded-t-md\n bg-white dark:bg-gray-900\n text-gray-800 dark:text-gray-100" }, text),
React.createElement("div", { className: "border rounded-md pt-4 border-gray-300 dark:border-gray-700" }, children)));
};
export const NoctoSlot = ({ pluginId, name, runtimeProps = {}, fallback = null }) => {
const showSlots = import.meta.env.VITE_NOCTO_SHOW_SLOTS;
const { slotsRegistry } = useNoctoPluginContext();
const before = slotsRegistry.get(pluginId, `${name}.before`);
const main = slotsRegistry.get(pluginId, name);
const after = slotsRegistry.get(pluginId, `${name}.after`);
const hasMain = main.length > 0;
return (React.createElement(React.Fragment, null,
before.map(({ component: Component, staticProps }, i) => (React.createElement(Component, { key: `before-${i}`, ...staticProps, ...runtimeProps }))),
hasMain
? main.map(({ component: Component, staticProps }, i) => (React.createElement(Component, { key: `main-${i}`, ...staticProps, ...runtimeProps })))
: (showSlots ? React.createElement(Note, { text: `Plugin: ${pluginId}, slot name: ${name}` }, fallback) : fallback),
after.map(({ component: Component, staticProps }, i) => (React.createElement(Component, { key: `after-${i}`, ...staticProps, ...runtimeProps })))));
};