On 12/21/20 2:50 PM, Diego de la Hera wrote:
> I'm writing a bootstrapped plugin for Zotero and I'm trying to stay
> away as much as possible from XUL, and using React/HTML instead.
> I would like the plugin to have the Zotero's look and feel, and I
> would like to reuse some of the React components already available in
> Zotero's codebase, such as those used by the Tags Box (e.g., Editable,
> Input, TextAreaInput).
> […] Would there be a way to use these native components from the
> plugin without having to copy them to the plugin's code?
You can, but note that Zotero's use of React is still fairly limited,
and we haven't made extensive use of those low-level components, so they
may not do all that much for you. But you can try. Editable specifically
could be useful if you hook it up correctly.
I haven't tested this, but the main thing would be making sure you're
using Zotero's require(), so that you can require the bundled
components. That require() should be available as long as you've
included chrome://zotero/content/include.js in your window.
You'd do something like this (assuming you're using Babel or similar to
transpile ES6 modules to CommonJS — if not, you can forego the ES6
imports/exports and use `require()` and such explicitly):
main.js:
```
import React from 'react';
import ReactDOM from 'react-dom';
import Foo from 'foo.js';
var Plugin = new function () {
this.onLoad = function () {
ReactDOM.render(
<Foo/>,
document.getElementById('foo')
);
}
}
```
foo.js:
```
import React, { useState } from 'react';
import Editable from 'components/editable.js';
function Foo() {
const [active, setActive] = useState(false);
function handleClick() {
setActive(true);
}
return (
<div>
<Editable
isActive={active}
onClick={handleClick}
value="Foo"
/>
</div>
);
}
export default Foo;
```
This would be mostly broken but shows the basic idea. Sorry we don't yet
have a sample extension that takes advantage of this stuff.
- Dan