https://github.com/hhvm/xhp-js
Easily create JS controllers for XHP elements, and XHP wrappers for React elements
https://github.com/hhvm/xhp-js
hack hacklang javascript react xhp
Last synced: 5 months ago
JSON representation
Easily create JS controllers for XHP elements, and XHP wrappers for React elements
- Host: GitHub
- URL: https://github.com/hhvm/xhp-js
- Owner: hhvm
- License: mit
- Created: 2015-04-10T16:40:35.000Z (about 11 years ago)
- Default Branch: main
- Last Pushed: 2021-11-18T20:57:17.000Z (over 4 years ago)
- Last Synced: 2025-05-22T17:49:58.987Z (about 1 year ago)
- Topics: hack, hacklang, javascript, react, xhp
- Language: Hack
- Size: 186 KB
- Stars: 56
- Watchers: 25
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# 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.
For convenience, an interface to construct React components is also included.
A short overview is available at https://code.facebook.com/posts/858739974205250
## Examples
A full example is available at https://github.com/hhvm/xhp-js-example
### Calling a Javascript function
```Hack
xhp class JsCallExample extends HTML\element {
use XHPHTMLHelpers;
use XHPJSCall;
<<__Override>>
protected function renderAsync(): Awaitable {
$this->jsCall(
'ModuleName',
'functionName',
'First argument',
// This passes the DOM node corresponding to the
below
$this->toJSElementRef(),
'Third argument',
);
return
getID()} />;
}
}
$xhp =
;
echo await $xhp->toStringAsync();
```
### Creating a Javascript Object
```Hack
xhp class JSInstanceExample extends HTML\element {
use XHPHTMLHelpers;
use XHPJSCall;
<<__Override>>
protected function renderAsync(): Awaitable {
$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
getID()} />;
}
}
$xhp =
;
```
### Creating a React component
*This functionality was based on an extremely old React version. The example has been removed.*
## Writing your JavaScript
We recommend writing your modules as CommonJS modules, and using Browserify.
Alternatively, you can create them as members of the window object.
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.
For example:
```Hack
$this->jsCall('MyModule', 'myMethod', 'argument');
```
This Hack code can be thought of as creating the following Javascript:
```Javascript
var module = window.MyModule ? window.MyModule : require('MyModule');
module.myMethod('argument');
```
In turn, your JavaScript may look like:
```Javascript
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.
See https://github.com/hhvm/xhp-js-example for a full example.
## License
XHP-JS is [MIT-licensed](LICENSE).