/* Copyright 2026 Marimo. All rights reserved. */ import type { JSX } from "react"; import type { ZodType } from "zod"; import type { IPlugin, IPluginProps } from "../types"; import type { FunctionSchemas, PluginFunctions } from "./rpc"; type Renderer = (props: IPluginProps) => JSX.Element; // If this simple builder pattern becomes unwieldy, we can switch to a more // complex builder pattern with individual classes. export function createPlugin( tagName: string, opts: { cssStyles?: string[]; } = {}, ) { return { /** * Data schema for the plugin. */ withData(validator: ZodType) { return { /** * Functions that the plugin can call. */ withFunctions( functions: FunctionSchemas, ) { return { /** * Render the plugin. */ renderer(renderer: Renderer): IPlugin { return { ...opts, tagName, validator, functions, render: renderer, }; }, }; }, /** * Render the plugin. */ renderer(renderer: Renderer): IPlugin { return { ...opts, tagName, validator, render: renderer, }; }, }; }, }; }