mhink / react-ionize
- четверг, 13 апреля 2017 г. в 03:11:34
JavaScript
A React renderer for building your entire Electron app
react-ionize is a library which lets you build the "non-browser" parts of an Electron app using React components to manage your application's state.
Electron applications consist of two types of process: a main process which manages the lifecycle of the application, and several renderer processes, which display webpages which comprise the application's GUI. While it's fairly common to use React and ReactDOM to build an HTML/CSS/JS interface in the renderer process, react-ionize
runs in the main process, managing things like window size/position, menu contents, and application-wide events.
react-ionize is still an EXPERIMENTAL, PRE-ALPHA library, and is not yet suitable for for use in a production app! It's a custom renderer built on top of the React Fiber reconciliation API, which itself is still under active development. (Not to mention, I've got a whole crop of Electron features yet to add.)
* npm install --save electron
* npm install --save react@16.0.0-alpha.5
* npm install --save react-ionize
Take a look at Ionize Example App to get started.
const React = require('react');
const Ionize = require('react-ionize');
const path = require('path');
const fs = require('fs');
const INDEX_HTML_PATH = path.resolve(__dirname, 'index.html');
const INDEX_HTML_SOURCE = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello, Electron!</title>
</head>
<body>
<h1>Hello, Electron!</h1>
</body>
</html>
`;
fs.writeFileSync(INDEX_HTML_PATH, INDEX_HTML_SOURCE);
Ionize.start(
<app>
<window show file={INDEX_HTML_PATH} />
</app>
);
(Normally, you'd build and distribute an index.html
along with your JS during your build process, but I wanted this example to be as independent of build processes as possible.)
Ionize.start(element, [callback])
Starts up an Electron application under Ionize. (Note: this will wait on the 'ready' Electron event before starting to render any elements.)
Generally speaking, the presence of an Ionize element in your component tree
indicates that you want it to be there, and that Ionize should ensure its
presence when rendering. This can lead to slightly surprising behavior if
you're unfamiliar with React- for instance, if you want a window to actually
go away when you close it, you need to make sure that the corresponding <window/>
element actually gets unmounted!
<app>
Attachment point for event handlers related to the global app. Not strictly necessary if you don't need to register any of these (since React Fiber now supports multiple children without a parent element).
Generally speaking, children of <app>
are things related to the entire
application: browser windows, dialogs, tray elements, and so forth. (Or at
least, they will be once I get a chance to implement them.)
<window>
Represents an Electron BrowserWindow object.
file
show
onReadyToShow
ready-to-show
event is triggered. You can use this to keep the window hidden until it's ready.showDevTools
size
<input>
element in traditional React apps. That is to say, if you provide size
by itself, the user will not be able to resize the window- it will simply be that size, unless you provide an onResize
handler to update the value provided.onResize
size
prop, it will allow the window to be resized (although you will need to update the value of the size
prop accordingly).defaultSize
defaultSize
prop to set the window to an initial size when it's mounted.position
onMove
onMoved
defaultPosition
size
, onResize
, and defaultSize
, except they represent the position on the screen.<menu>
The <menu>
element defines an Electron application menu. By nesting <submenu>
s inside it, you can construct your menu.
TODO: When nested inside a <window>
element, this should attach the menu to
that window, specifically- and I'd like to have something like a <contextmenu>
element for right-clicks.
<submenu>
<submenu>
and <item>
elements only!<item>
and relatedPretty much equivalent to new MenuItem({ type: 'normal' })
in vanilla Electron.
label
onClick
There are a couple special-case elements related to <item>
.
<sep>
A separator menu item. Equivalent to new MenuItem({ type: 'separator' })
in vanilla Electron.
In the Electron API, you can create MenuItems with a "role", which assigns them some OS-native functionality (think copy, paste, select all, and so on.) As a shorthand, you can use these directly as an element name within a <submenu>
. For instance,
<pasteandmatchstyle />
is equivalent to
new MenuItem({ role: 'pasteandmatchstyle' })
in vanilla Electron.
Feedback is welcome! Add an issue or hit me up on Twitter. I'm still working out how all this should work, but I would love to hear your suggestions.
If you'd like to contribute, right now we could really use some more feature implementations. Basically, what I've been doing is picking Electron APIs and figuring out how to turn them into either elements or props on existing elements. For instance, the app.setBadge(text)
API call would make for a pretty good prop on <app />
.
To assist the would-be contributor, I've included a bunch of documentation and notes about React Fiber in the comments of IonizeHostConfig.js
. For further reading, I'd basically recommend cloning down the React repo and digging through its /src/renderers
directory, starting with /src/renderers/shared/fiber/ReactFiberReconciler.js
and going from there.