Git Product home page Git Product logo

xhp-js's Introduction

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

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 <div /> 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.

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:

$this->jsCall('MyModule', 'myMethod', 'argument');

This Hack code can be thought of as creating the following Javascript:

var module = window.MyModule ? window.MyModule : require('MyModule');
module.myMethod('argument');

In turn, your JavaScript may look like:

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.

xhp-js's People

Contributors

aewing avatar facebook-github-bot avatar fredemmott avatar lexidor avatar simonwelsh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xhp-js's Issues

Pick a namespace for this project

All code in master and #9 lives at the root namespace.
We can now use xhp in namespaces ๐ŸŽ‰.
I have not decided on a namespace name just yet, so I have instead created an issue so we won't forget.

Figure out how to `constructReactInstance` in React 17

Older versions of React had a different way of creating components.
I am almost certain that this doesn't work in React 17.
Some research by someone who knows Reacts internals is required to bring this functionality into the modern era.

One of the things that might make things more difficult (or more easy, depending on how you look at it), is how React 17 handles event handlers.

React 17 release notes

Also a bit unrelated, but a bit less fun is that JSX can compile down to two "syntaxes".
New JS Transform

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.