TaffySharp 0.1.0

dotnet add package TaffySharp --version 0.1.0
                    
NuGet\Install-Package TaffySharp -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TaffySharp" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TaffySharp" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="TaffySharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TaffySharp --version 0.1.0
                    
#r "nuget: TaffySharp, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TaffySharp@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TaffySharp&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=TaffySharp&version=0.1.0
                    
Install as a Cake Tool

TaffySharp

Build License: MIT

TaffySharp is a UI layout engine implementing CSS Flexbox, CSS Grid and block layout (with floats). It is a pure C# port of taffy (Rust, MIT), ported from v0.15.1 (commit 0064bd5). The port is released under the MIT license.

Chorizite/TaffySharp on GitHub is a native binding to the Rust library; this project is an unrelated, fully managed port with no native dependency.

Targets netstandard2.0, netstandard2.1, net8.0, and net10.0, so it can be used from .NET Framework 4.6.2+, .NET Core 2.0+, .NET 5+, Unity 2021.2+, and Mono.

Installation

dotnet add package TaffySharp

Usage

using TaffySharp;

TaffyTree tree = new();

NodeId child = tree.NewLeaf(new Style
{
    Size = new Size<Dimension>(Dimension.Percent(0.5f), Dimension.Auto),
});

NodeId root = tree.NewWithChildren(new Style
{
    Display = Display.Flex,
    Size = new Size<Dimension>(Dimension.Length(100f), Dimension.Length(100f)),
    JustifyContent = AlignContent.Center,
}, child);

tree.ComputeLayout(root, new Size<AvailableSpace>(AvailableSpace.Definite(100f), AvailableSpace.Definite(100f)));

Layout layout = tree.Layout(child); // Location = (25, 0), Size = 50 x 100

Leaves with intrinsic content (text, images) get a node context and a measure function:

TaffyTree<string> tree = new();
NodeId text = tree.NewLeafWithContext(new Style(), "Hello");
tree.ComputeLayoutWithMeasure(text, Size.MaxContent, (inputs, node, context, style) =>
    Compute.ComputeLeafLayout(inputs, style, (_, _) => 0f, (known, available) =>
        new Size<float>(known.Width ?? context!.Length * 8f, known.Height ?? 16f)));

The API mirrors taffy's: Style has the same properties (in PascalCase), percentages are fractions (0.5f is 50%), and the low-level Compute.* functions and tree interfaces let you run the algorithms on your own tree type. Not ported: CSS string parsing (taffy's parse feature) and serde support.

Rect.FromLength/FromPercent and Size.FromLengths/FromPercent build Dimension values (for Size, MinSize, MaxSize). The other length types have their own helpers: Rect.LengthPercentageFromLength for Padding and Border, Rect.LengthPercentageAutoFromLength for Margin and Inset, Size.LengthPercentageFromLengths for Gap (plus the ...FromPercent variants).

Things to know

  • Styles are stored by reference. NewLeaf, NewWithChildren and SetStyle keep the Style instance you pass. Use one instance per node (or style.Clone()): two nodes sharing an instance both change when it is mutated, but only the node you pass to MarkDirty is re-laid out. To change a style in place, mutate tree.Style(node) and call tree.MarkDirty(node), or pass a new instance to SetStyle.

  • Position defaults to Static, as in CSS (older taffy versions defaulted to relative). An absolute box is positioned against its containing block, the nearest ancestor with a non-static Position (or the root), and its Layout.Location is relative to that ancestor, not to its parent. A renderer that sums parent offsets must use tree.HoistedChildren(node), which lists the out-of-flow boxes each containing block positioned.

  • Nesting depth is limited by the thread's stack. Layout is recursive and uses about 8 KB of stack per nesting level, so a 1 MB thread handles roughly 100-120 levels. A deeper tree overflows the stack, which terminates the process. Lay out deep trees (generated UIs, nested comment threads, tree views) on a thread with a larger stack:

    Thread thread = new(() => tree.ComputeLayout(root, Size.MaxContent), 16 * 1024 * 1024);
    thread.Start();
    thread.Join();
    

Credits

Algorithm and original implementation: taffy by the Taffy Authors and Visly Inc. (MIT). The conformance tests in tests/TaffySharp.Tests/Generated are converted from taffy's Chrome-generated XML fixtures by tests/tools/convert_generated.py.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 106 11/1/2026