Files
Netcatty/packages/plugin-sdk/src/hostReact.ts
T
陈大猫 d5a03606b7 feat: rebuild plugin UI with host rendering and standard settings (#4053)
* feat: rebuild plugin pages with host rendering and native settings

* fix(plugins): contain host-view errors, isolate editing, and migrate fixtures

* fix(plugins): migrate remaining permission fixture hosts
2026-10-28 23:08:31 -07:00

23 lines
1.2 KiB
TypeScript

import { createRoot } from 'react-dom/client';
import { createElement, useEffect, useRef, type ReactNode } from 'react';
import type { Disposable, PluginViews } from './index.js';
import { registerHostDomView } from './hostDom.js';
/** Author ordinary React while Netcatty owns the visible DOM. */
export function registerHostReactView(views: PluginViews, viewId: string, render: () => ReactNode, css = ''): Disposable {
return registerHostDomView(views, viewId, (container) => {
const root = createRoot(container); root.render(render()); return () => root.unmount();
}, css);
}
/** A host-positioned popup with freely authored content. */
export function PluginPopover({ anchor, open, onClose, children }: { anchor: string; open: boolean; onClose(): void; children: ReactNode }) {
const ref = useRef<HTMLElement>(null);
useEffect(() => {
const node = ref.current; if (!node) return;
node.addEventListener('netcatty-dismiss', onClose);
return () => node.removeEventListener('netcatty-dismiss', onClose);
}, [onClose]);
return createElement('div', { ref, 'data-plugin-popover': 'true', 'data-plugin-anchor': anchor, ...(open ? { open: true } : {}) }, children);
}