@simonklee/yoga
v0.2.30
Published
A fast FFI wrapper for Facebook's Yoga layout engine for Bun, providing a yoga-layout compatible API
Readme
Yoga bindings
Fast Bun FFI bindings for Facebook's Yoga layout
engine, providing a yoga-layout compatible API built on Zig.
Supported Platforms
Pre-built binaries are included in the npm package for:
| Platform | Architecture | Binary |
| -------- | --------------------- | --------------- |
| macOS | ARM64 (Apple Silicon) | libyoga.dylib |
| macOS | x64 (Intel) | libyoga.dylib |
| Linux | x64 | libyoga.so |
| Linux | ARM64 | libyoga.so |
| Windows | x64 | yoga.dll |
The correct binary is automatically loaded at runtime based on your platform.
Installation
bun add @simonklee/yogaUsage
import Yoga, { Node, Config, Edge, FlexDirection } from "@simonklee/yoga";
const config = Config.create();
const root = Node.create(config);
root.setFlexDirection(FlexDirection.Column);
root.setWidth(100);
root.setHeight(100);
const child = Node.create(config);
child.setFlexGrow(1);
root.insertChild(child, 0);
root.calculateLayout(100, 100);
console.log(root.getComputedLayout());
// { left: 0, top: 0, right: 0, bottom: 0, width: 100, height: 100 }
root.freeRecursive();
config.free();API
The API mirrors yoga-layout. All enums, constants, and methods are compatible.
Building from source
Requirements: Bun and Zig 0.15.2+.
# Build the native library (debug)
zig build
# Build native library and dist placeholders (debug)
bun run build:native:dev
# Build the native library (release)
zig build -Doptimize=ReleaseFast
# Build native library and dist placeholders (release, for bun --compile)
bun run build:native
# Build TypeScript to dist/
bun run build:ts
# or
bun run build
# Tests
zig build test
bun run build:native:dev # Prepare dist placeholders for bun test
bun testBenchmarks
bun run benchDevelopment notes
src/yoga_ffi.zigexposes Yoga's C API via Zigexport fn.src/index.tsusesbun:ffiand prefers a localzig-outlibrary when present, falling back to the platform-specific binary indist/.
License
MIT
