Yoga facebook出品的引擎
Laying out a Yoga tree
Each box in Yoga is represented as a Yoga Node. These nodes form a tree which is used to store both input styles, and output layout results.
Building a Yoga tree
Yoga nodes may be created, styled, and linked together. See Styling for a more comprehensive reference of how to style a Yoga Node.
- C/C++
- Java
- JavaScript
import Yoga, {Edge, FlexDirection, PositionType} from 'yoga-layout';
const root = Yoga.Node.create();
root.setFlexDirection(FlexDirection.Row);
root.setWidth(100);
root.setHeight(100);
const child0 = Yoga.Node.create();
child0.setFlexGrow(1);
child0.setMargin(Edge.Right, 10);
root.insertChild(child0, 0);
const child1 = Yoga.Node.create();
child1.setFlexGrow(1);
root.insertChild(child1, 1);
Yoga Nodes are not freed automatically and should be discarded when no longer needed. Individual nodes may be freed by calling node.free()
, or an entire Yoga tree may be freed by calling node.freeRecursive()
.
A future revision of JavaScript bindings for Yoga may move to garbage collection to remove this requirement.
Laying out the tree
The full tree of Yoga nodes is laid out all at once. This layout may be constrained to a passed availableWidth
and availableHeight
, or may be allowed to expand infinitely in a given axis by passing Undefined.
- C/C++
- Java
- JavaScript
root.calculateLayout(undefined, undefined, Direction.LTR);
Reading layout results
Layout results are now written to each Yoga node. This includes an offset relative to the border box of the node's parent, along with dimensions, and the resolved values for margin, border, and padding for each physical edge.
- C/C++
- Java
- JavaScript
const left = child0.getComputedLeft();
const height = child0.getComputedHeight();