able-baker/run-browser.tsx

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-06-20 09:50:14 -04:00
import React from "react";
2023-10-12 22:47:45 -04:00
import * as TW from "@twind/core";
2023-06-21 17:21:36 -04:00
import TWPreTail from "https://esm.sh/v126/@twind/preset-tailwind@1.1.3/es2022/preset-tailwind.mjs";
import TWPreAuto from "https://esm.sh/v126/@twind/preset-autoprefix@1.0.7/es2022/preset-autoprefix.mjs";
2023-06-20 06:51:38 -04:00
2023-06-20 09:50:14 -04:00
const Configure =
{
2023-06-20 06:51:38 -04:00
theme: {},
presets: [TWPreTail(), TWPreAuto()],
hash: false
2023-06-20 06:51:38 -04:00
} as TW.TwindUserConfig;
2023-10-31 09:45:26 -04:00
export const Shadow =(inElement:HTMLElement, inConfig:TW.TwindUserConfig)=>
2023-06-20 06:51:38 -04:00
{
const ShadowDOM = inElement.attachShadow({ mode: "open" });
const ShadowDiv = document.createElement("div");
const ShadowCSS = document.createElement("style");
2023-06-20 09:50:14 -04:00
2023-06-20 06:51:38 -04:00
ShadowDOM.append(ShadowCSS);
ShadowDOM.append(ShadowDiv);
2023-10-31 09:45:26 -04:00
TW.observe(TW.twind(inConfig, TW.cssom(ShadowCSS)), ShadowDiv);
2023-06-20 06:51:38 -04:00
return ShadowDiv;
2023-06-20 09:50:14 -04:00
};
2023-10-31 09:14:22 -04:00
export default async(inSelector:string, inModulePath:string, inMemberApp="default", inMemberCSS="CSS", inShadow=false):Promise<(()=>void)|false>=>
2023-06-20 09:50:14 -04:00
{
2023-07-30 09:16:17 -04:00
if(!inModulePath)
{
return false;
}
let dom = document.querySelector(inSelector);
if(!dom)
{
console.log(`element "${inSelector}" not found.`);
return false;
}
const module = await import(inModulePath);
2023-10-31 09:14:22 -04:00
2023-10-31 09:45:26 -04:00
const merge = inMemberCSS ? {...Configure, ...module[inMemberCSS]} : Configure;
2023-10-31 09:14:22 -04:00
if(inShadow)
{
2023-10-31 09:45:26 -04:00
dom = Shadow(dom as HTMLElement, merge);
2023-10-31 09:14:22 -04:00
}
else
{
2023-10-31 09:45:26 -04:00
TW.install(merge);
2023-10-31 09:14:22 -04:00
}
2023-06-20 09:50:14 -04:00
const app = React.createElement(()=> React.createElement(module[inMemberApp], null), null);
2023-06-20 09:50:14 -04:00
if(React.render)
{
React.render(app, dom);
return ()=>dom && React.unmountComponentAtNode(dom);
2023-06-20 09:50:14 -04:00
}
else
{
const reactDom = await import(`https://esm.sh/react-dom@${React.version}/client`);
const root = reactDom.createRoot(dom);
root.render(app);
2023-06-20 09:50:14 -04:00
return root.unmount;
}
2023-06-20 21:40:49 -04:00
};