XHP-JS
XHP-JS is a combination of Hack and Javascript libraries allowing you to easily
call Javascript functions or create Javascript classes that from XHP
components, and to pass these classes or references to the DOM nodes to other
Javascript code., (*1)
For convenience, an interface to construct React components is also included., (*2)
A short overview is available at https://code.facebook.com/posts/858739974205250, (*3)
Examples
A full example is available at https://github.com/hhvm/xhp-js-example, (*4)
Calling a Javascript function
xhp class JsCallExample extends HTML\element {
use XHPHTMLHelpers;
use XHPJSCall;
<<__Override>>
protected function renderAsync(): Awaitable<x\node> {
$this->jsCall(
'ModuleName',
'functionName',
'First argument',
// This passes the DOM node corresponding to the
below
$this->toJSElementRef(),
'Third argument',
);
return <div id={$this->getID()} />;
}
}
$xhp = <html><head /><body>
<x_js_scope><JsCallExample /><x_js_scope>
</body></html>;
echo await $xhp->toStringAsync();
Creating a Javascript Object
xhp class JSInstanceExample extends HTML\element {
use XHPHTMLHelpers;
use XHPJSCall;
<<__Override>>
protected function renderAsync(): Awaitable<x\node> {
$this->constructJSInstance(
'ClassName',
$this->toJSElementRef(),
// can pass through other arguments too
);
$this->jsCall(
'MyModule',
'myFunction',
// This passes the JS object created above
$this->toJSInstanceRef(),
);
return <div id={$this->getID()} />;
}
}
$xhp = <html><head /><body>
<x_js_scope><JSInstanceExample /><x_js_scope>
</body></html>;
Creating a React component
This functionality was based on an extremely old React version. The example has been removed., (*5)
Writing your JavaScript
We recommend writing your modules as CommonJS modules, and using Browserify., (*6)
Alternatively, you can create them as members of the window object., (*7)
XHP-JS looks for modules as members of the window object, and falls back to
attempting to call 'require("ModuleName")' - this requires a require() function
to be defined in the global scope., (*8)
For example:, (*9)
$this->jsCall('MyModule', 'myMethod', 'argument');
This Hack code can be thought of as creating the following Javascript:, (*10)
var module = window.MyModule ? window.MyModule : require('MyModule');
module.myMethod('argument');
In turn, your JavaScript may look like:, (*11)
var MyModule = {
myMethod: function() {
// ...
}
};
module.exports = MyModule; // if using CommonJS + Browserify
window.MyModule = MyModule; // if not
Installation
We recommend installing XHP-JS with Composer (for the Hack code) and npm +
Browserify for the Javascript code. Alternatively, you can include xhpjs.js or
xhpjs.min.js directly to declare an XHPJS object in the global scope., (*12)
See https://github.com/hhvm/xhp-js-example for a full example., (*13)
License
XHP-JS is MIT-licensed., (*14)