I have a react app, and in one of the components I would like to render a Phosphor desktop (http://phosphorjs.github.io).
Web sources suggest the other way around, running React component inside a Phosphor Widget, but I need to wrap the Phosphor desktop with widgets inside a React component instead. Not sure how to go about it.
This is my first attempt:
import React, { Component } from 'react';
import './App.css';
import { DockPanel } from 'phosphor-dockpanel';
import { Message } from 'phosphor-messaging';
import { TabPanel } from 'phosphor-tabs';
import { ResizeMessage, Widget } from 'phosphor-widget';
class MyWidget extends Widget {
static createNode() {
console.log('widget:createNode');
var node = document.createElement('div');
var app = document.createElement('div');
app.className = 'todoapp';
node.appendChild(app);
return node;
}
constructor(model) {
super();
this.addClass('TodoWidget');
this._model = model;
console.log('widget:constructor');
}
get model() {
console.log('widget:getModel');
return this._model;
}
onAfterAttach(msg) {
console.log('onAfterAttach');
//this._model.subscribe(() => this.update());
this.update();
}
onUpdateRequest(msg) {
console.log('widget:onUpdateRequest');
var data = { model: this._model };
var host = this.node.firstChild;
//React.render(React.createElement(app.TodoApp, data), host);
}
}
class ReactDockPanel extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.panel = new DockPanel();
this.panel.id = this.props.id;
this.widget = new MyWidget();
this.panel.insertLeft(this.widget);
}
componentDidMount() {
this.panel.attach(this.node);
}
render() {
return (
<div ref={el=>this.node=el}>
</div>
);
}
}
class App extends Component {
render() {
return (
<ReactDockPanel id='main'>
</ReactDockPanel>
);
}
}
export default App;
I'm not sure how to replace React.render(React.createElement(app.TodoApp, data), host); Phosphor docs are for reference but not for learning so I'm going by their only example code.
Ideally I'd like to be able to render like this:
<ReactDockPanel id='mydesktop'>
<SomeWidget insert='left' />
<SomeOtherWidget insert='right' />
</ReactDockPanel>
Not sure it can be done.