TinyComponents 0.4.0

dotnet add package TinyComponents --version 0.4.0                
NuGet\Install-Package TinyComponents -Version 0.4.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="TinyComponents" Version="0.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TinyComponents --version 0.4.0                
#r "nuget: TinyComponents, 0.4.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.
// Install TinyComponents as a Cake Addin
#addin nuget:?package=TinyComponents&version=0.4.0

// Install TinyComponents as a Cake Tool
#tool nuget:?package=TinyComponents&version=0.4.0                

TinyComponents

Welcome to TinyComponents! This tiny library will help you create XML and HTML components with a readable language that interacts with your code.

This library is aimed at rendering HTML/XML components securely, assigning attributes, classes, IDs and more to your elements, building your HTML in an elegant and formal syntax.

HtmlElement MyComponent(string name)
{
    return new HtmlElement("div")
        .WithContent($"Hey! Im {name}.");
}

var element = MyComponent("Dave");
Console.WriteLine(element);

Should output:

<div>Hey! Im Dave.</div>

The library is extremely simple to use, covering just a few very simple functions. This file will document all your resources here.

To correctly use this library, it is also interesting to note what it is and what it is not. TinyComponents is an library to help rendering XML/HTML with an fluent and functional syntax, which also support basic templating and components, but it's not an complete frontend framework with styles and scripts.

Installing

Install the experimental version of TinyComponents with at Nuget:

dotnet add package TinyComponents

Creating components

All components are rendered through ToString() present in each object. Any object can be rendered inside an HtmlElement or XmlNode. There are three primitive types for creating objects:

  • TinyComponents.HtmlElement
  • TinyComponents.XmlNode
  • TinyComponents.RenderableText

HtmlElement and XmlNode have very similar renderers, with the difference that:

  • HtmlElement has the Id, Name, TabIndex, Style, Title and ClassList properties.
  • When rendering HtmlElement with SelfClosed = true, XML is terminated with /> while HTML >.

Example 1:

var ul = new HtmlElement("ul");
ul.Style = new { backgroundColor = "red" };
ul.Id = "fruit-list";

ul.Children.Add(new HtmlElement("li", "Apple"));
ul.Children.Add(new HtmlElement("li", "Mango"));
ul.Children.Add(new HtmlElement("li", "Blueberry"));

Console.WriteLine(ul.ToString());

Renders to:

<ul id="fruit-list" style="background-color:red;">
    <li>Apple</li>
    <li>Mango</li>
    <li>Blueberry</li>
</ul>

Example 2:

public class Li : HtmlElement
{
    public Li(string content) : base("li", content)
    {
    }
}

var ul = new HtmlElement("ul");
ul.Style = new { backgroundColor = "red" };
ul.Id = "fruit-list";

ul.Children.Add(new Li("Apple"));
ul.Children.Add(new Li("Mango"));
ul.Children.Add(new Li("Blueberry"));

Console.WriteLine(ul.ToString());
<ul id="fruit-list" style="background-color:red;">
    <li>Apple</li>
    <li>Mango</li>
    <li>Blueberry</li>
</ul>

Example 3:

var form = new HtmlElement("form")
    .WithAttribute("method", "POST")
    .WithAttribute("action", "/contact.php")
    .WithContent(form =>
    {
        form += new HtmlElement("input")
            .SelfClosed()
            .WithName("name")
            .WithAttribute("type", "text")
            .WithAttribute("required")
            .WithAttribute("placeholder", "Your name");

        form += new HtmlElement("input")
            .SelfClosed()
            .WithName("message")
            .WithAttribute("type", "text")
            .WithAttribute("required")
            .WithAttribute("placeholder", "Message");

        form += new HtmlElement("button")
            .WithContent("Send message")
            .WithAttribute("type", "submit");
    });

Console.WriteLine(form);
<form method="POST" action="/contact.php">
    <input type="text" required placeholder="Your name" name="name">
    <input type="text" required placeholder="Message" name="message">
    <button type="submit">
        Send message
    </button>
</form>

Example 4:

var name = "<John>";
var formattableText = HtmlElement.Format($"""
    <h1>Hello, {name}!</h1>
    """);

Console.WriteLine(formattableText);
<h1>Hello, &lt;John&gt;!</h1>

Example 5:

class BlueButton : HtmlElement
{
    public BlueButton(string text)
    {
        TagName = "button";
        Style = new
        {
            backgroundColor = "blue",
            color = "white",
            border = "1px solid darkblue"
        };
        Children = [new RenderableText(text)];
    }
}

var button = new BlueButton("Click me!");
Console.WriteLine(button);
<button style="background-color:blue;color:white;border:1px solid darkblue;">Click me!</button>

Example 6:

static HtmlElement Html() => new HtmlElement("html");
static HtmlElement Body() => new HtmlElement("body");
static HtmlElement Ul() => new HtmlElement("ul");
static HtmlElement Li(string text) => new HtmlElement("li", text);
static HtmlElement H1(string text) => new HtmlElement("h1", text);

static void Main(string[] args)
{
    string[] colors = ["Red", "Blue", "Yellow"];

    var page = Html().WithContent(html =>
        {
            html += Body().WithContent(body =>
            {
                body += H1("List of colors:");

                var colorList = Ul();
                foreach (string color in colors)
                    colorList += Li(color);

                body += colorList;
            });
        });

    Console.WriteLine(page);
}
<html>
    <body>
        <h1>List of colors:</h1>
        <ul>
            <li>Red</li>
            <li>Blue</li>
            <li>Yellow</li>
        </ul>
    </body>
</html>
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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.1

    • 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.4.0 72 8/17/2024
0.3.0 88 7/6/2024
0.2.0 97 5/7/2024