Summary: Doing some test-driven-development to support this feature, so I will start by adding a ton of tests to ensure the nuance of position: static is captured in Yoga. Specifically I have a slew of tests to capture: * Insets have no effect on static elements * Insets are relative to the nearest non-static ancestor * Percentage values for insets, padding, and margin of absolute children respect the correct dimension of the nearest non-static ancestor * Also added similar ones for static and relative children which should just respect their ancestor (static only because it is a flexbox by default) * This rule does NOT apply to border * The containing block for absolute children is the padding box of their nearest non-static ancestor * The containing block for static children is the content box of their parent (because all elements are flex containers in yoga, at least right now) Reviewed By: NickGerleman Differential Revision: D50475939 fbshipit-source-id: 7988ffc9bea3317875128dd1908d787b9b714a45
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.
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.
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.
// 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 so that platforms which can take advantage of WebAssembly use it by default.
Different entrypoints are exposed to choose a flavor explicitly.
import {loadYoga} from 'yoga-layout/wasm-async';
Using TypeScript
This package provides out-of-the-box TypeScript typings so long as tsc
is configured to support ESM resolution. It is recommended to set moduleResolution: 'bundler'
or moduleResolution: node16
in your tsconfig.json
according to your environment.
Contributing
Requirements
- Emscripten SDK
- CMake >= 3.13
- (Optional) ninja, for faster builds
Building
git clone https://github.com/facebook/yoga.git
cd yoga/javascript
yarn install
yarn build
Testing
# Build and test all entrypoints
yarn test
# Build and test a specific entrypoint
yarn test:asmjs-sync