Skip to content
GitHub11 stars

Use Render

Gives components of your own the same Render parameter as the primitives.

Text component rendered as a paragraph tag

Text component rendered as a strong tag
<div class="use-render-container">
    <UseRender DefaultTagName="p" class="use-render-text">Text component rendered as a paragraph tag</UseRender>
    <UseRender class="use-render-text use-render-strong">
        <Render Context="text">
            <strong @attributes="text.Attributes">Text component rendered as a strong tag</strong>
        </Render>
    </UseRender>
</div>

UseRender builds a part of your own: it renders DefaultTagName, or your own element through Render, with your attributes. Its State becomes data attributes: true renders data-name, other values data-name="value", and false and null nothing.

Anatomy

@using Blazix.Primitives

<UseRender DefaultTagName="p" State="@state">
    <Render Context="text">
        <strong @attributes="text.Attributes">...</strong>
    </Render>
</UseRender>

Parts of your own

For a part with state of its own, derive from PrimitivePart<TRenderProps>, as the primitives do. Add the part's attributes in BuildAttributes with an AttributeBuilder, and describe its state for Render in CreateRenderProps:

Badge.cs
public sealed record BadgeRenderProps(IReadOnlyDictionary<string, object> Attributes, bool Active)
    : RenderProps(Attributes);

public class Badge : PrimitivePart<BadgeRenderProps>
{
    [Parameter] public bool Active { get; set; }

    protected override string Tag => "span";

    protected override void BuildAttributes(AttributeBuilder attributes) =>
        attributes.Flag("data-active", Active);

    protected override BadgeRenderProps CreateRenderProps(IReadOnlyDictionary<string, object> attributes) =>
        new(attributes, Active);
}

API Reference

UseRender

Renders a <div> element.

ParameterTypeDefault
DefaultTagName string "div"
State IReadOnlyDictionary<string, object?>? -
Render RenderFragment<UseRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.