Summary: Pull Request resolved: https://github.com/facebook/yoga/pull/1285 Enums are currently exposed to the JS package as constants (e.g. `import {ERRATA_NONE} from 'yoga-layout'`). This exports enums in the form of `import {Errata} from 'yoga-layout'` then `Errata.None`. It would be more ergonomic for these to be string union based enums instead, but right now it is a pretty thin wrapper around the native API, we need ordinal values to do things with bit masks, and folks have wanted to serialize them before. Reviewed By: yungsters Differential Revision: D45570417 fbshipit-source-id: dbfd330e939051d0c16460a4d2a996f88f98875c
77 lines
1.8 KiB
Markdown
77 lines
1.8 KiB
Markdown
# yoga-layout
|
|
|
|
This package provides prebuilt JavaScript bindings for the Yoga layout engine. Both WebAssembly and asm.js variants are packaged, with the optimal loaded based on platform.
|
|
|
|
## Usage
|
|
|
|
The default entrypoint provides an asynchronous loader function to return a Yoga instance.
|
|
|
|
```ts
|
|
import {loadYoga, Align} from 'yoga-layout';
|
|
|
|
const Yoga = await loadYoga();
|
|
|
|
const node = Yoga.Node.create();
|
|
node.setAlignContent(Align.Center);
|
|
```
|
|
|
|
An alternative synchronous API is provided for compatibility, but requires using asm.js in browsers instead of WebAssembly, leading to worse performance and larger assets.
|
|
|
|
```ts
|
|
import Yoga, {Align} from 'yoga-layout/sync';
|
|
|
|
const node = Yoga.Node.create();
|
|
node.setAlignContent(Align.Center);
|
|
```
|
|
|
|
Objects created by `Yoga.<>.create()` are not automatically garbage collected and should be freed once they are no longer in use.
|
|
|
|
```ts
|
|
// Free a config
|
|
config.free();
|
|
|
|
// Free a tree of Nodes
|
|
node.freeRecursive();
|
|
|
|
// Free a single Node
|
|
node.free();
|
|
```
|
|
|
|
## Selecting WebAssembly or asm.js
|
|
|
|
For better performance and smaller packages, WebAssembly is preferred to asm.js where available. `yoga-layout` tries to provide the right default using [export maps](https://webpack.js.org/guides/package-exports/#conditional-syntax) so that platforms which can take advantage of WebAssembly use it by default.
|
|
|
|
A specific entrypoint may be specified on platforms which do not understand export conditions.
|
|
|
|
```ts
|
|
import {loadYoga} from 'yoga-layout/dist/entrypoint/wasm-async';
|
|
```
|
|
|
|
|
|
## Contributing
|
|
|
|
### Requirements
|
|
|
|
1. Emscripten SDK
|
|
1. CMake >= 3.13
|
|
1. (Optional) ninja, for faster builds
|
|
|
|
### Building
|
|
|
|
```bash
|
|
git clone https://github.com/facebook/yoga.git
|
|
cd yoga/javascript
|
|
yarn install
|
|
yarn build
|
|
```
|
|
|
|
### Testing
|
|
|
|
```bash
|
|
# Build and test all entrypoints
|
|
yarn test
|
|
|
|
# Build and test a specific entrypoint
|
|
yarn test:asmjs-sync
|
|
```
|