Thank you so much for your example. I tried this, it would work perfectly if I was developing a function driven react app. My actual problem is using React Routing with PanelController for the Photoshop plugin. I can not integrate a code piece like yours into this panel controller logic.
Panel Controller
import ReactDOM from "react-dom";
const _id = Symbol("_id");
const _root = Symbol("_root");
const _attachment = Symbol("_attachment");
const _Component = Symbol("_Component");
const _menuItems = Symbol("_menuItems");
export class PanelController {
constructor(Component, { id, menuItems } = {}) {
this[_id] = null;
this[_root] = null;
this[_attachment] = null;
this[_Component] = null;
this[_menuItems] = [];
this[_Component] = Component;
this[_id] = id;
this[_menuItems] = menuItems || [];
this.menuItems = this[_menuItems].map(menuItem => ({
id: menuItem.id,
label: menuItem.label,
enabled: menuItem.enabled || true,
checked: menuItem.checked || false
}));
[ "create", "show", "hide", "destroy", "invokeMenu" ].forEach(fn => this[fn] = this[fn].bind(this));
}
create() {
this[_root] = document.createElement("div");
this[_root].style.height = "100vh";
this[_root].style.overflow = "auto";
this[_root].style.padding = "8px";
ReactDOM.render(this[_Component]({panel: this}), this[_root]);
return this[_root];
}
show(event) {
if (!this[_root]) this.create();
this[_attachment] = event;
this[_attachment].appendChild(this[_root]);
}
hide() {
if (this[_attachment] && this[_root]) {
this[_attachment].removeChild(this[_root]);
this[_attachment] = null;
}
}
destroy() { }
invokeMenu(id) {
const menuItem = this[_menuItems].find(c => c.id === id);
if (menuItem) {
const handler = menuItem.oninvoke;
if (handler) {
handler();
}
}
}
}
index.js
import React from "react";
import "./styles.css";
import { PanelController } from "./controllers/PanelController.jsx";
import { CommandController } from "./controllers/CommandController.jsx";
import { About } from "./components/About.jsx";
import { Demos } from "./panels/Demos.jsx";
import { MoreDemos } from "./panels/MoreDemos.jsx";
import { entrypoints } from "uxp";
// opens about
const aboutController = new CommandController(({ dialog }) => <About dialog={dialog}/>, { id: "showAbout", title: "React Starter Plugin Demo", size: { width: 480, height: 480 } });
// opens Demo panel
const demosController = new PanelController(() => <Demos/>, {id: "demos", menuItems: [
{ id: "reload1", label: "Reload Plugin", enabled: true, checked: false, oninvoke: () => location.reload() },
{ id: "dialog1", label: "About this Plugin", enabled: true, checked: false, oninvoke: () => aboutController.run() },
] });
// opens demo panel 2
const moreDemosController = new PanelController(() => <MoreDemos/>, { id: "moreDemos", menuItems: [
{ id: "reload2", label: "Reload Plugin", enabled: true, checked: false, oninvoke: () => location.reload() }
] });
entrypoints.setup({
plugin: {
create(plugin) {
/* optional */ console.log("created", plugin);
},
destroy() {
/* optional */ console.log("destroyed");
}
},
commands: {
showAbout: aboutController
},
panels: {
demos: demosController,
moreDemos: moreDemosController
}
});
How can I use the route components when I use the photoshop panel controller?