03a4410272
主要工作是共享同一套“画布内核与通用 UI 源码”,网站和 Tauri 只分别实现宿主适配层。 --------- Co-authored-by: 段舒康 <kdletters@qq.com> Co-authored-by: kdletters <kdletters@qq.com> Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/136 Co-authored-by: menghao <mh18530625731@163.com> Co-committed-by: menghao <mh18530625731@163.com>
42 lines
974 B
TypeScript
42 lines
974 B
TypeScript
import { createContext, type ReactNode, useContext } from 'react';
|
|
import { createPortal } from 'react-dom';
|
|
|
|
const CanvasPortalRootContext = createContext<HTMLElement | null>(null);
|
|
|
|
export function CanvasPortalProvider({
|
|
children,
|
|
root,
|
|
}: {
|
|
children: ReactNode;
|
|
root: HTMLElement | null;
|
|
}) {
|
|
return (
|
|
<CanvasPortalRootContext.Provider value={root}>
|
|
{children}
|
|
</CanvasPortalRootContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function CanvasPortal({
|
|
children,
|
|
className = '',
|
|
container,
|
|
}: {
|
|
children: ReactNode;
|
|
className?: string;
|
|
container?: HTMLElement | null;
|
|
}) {
|
|
const contextualRoot = useContext(CanvasPortalRootContext);
|
|
const portalRoot =
|
|
container ??
|
|
contextualRoot ??
|
|
(typeof document === 'undefined' ? null : document.body);
|
|
if (!portalRoot) return <>{children}</>;
|
|
return createPortal(
|
|
<div className={`genarrative-image-canvas__portal ${className}`.trim()}>
|
|
{children}
|
|
</div>,
|
|
portalRoot,
|
|
);
|
|
}
|