Composition
A guide to composing the primitives with your own components.
Composing your own components
Every part that renders an element takes a Render fragment. The part gives the fragment its attributes (the id, role, ARIA and data-*
attributes, and its event handlers) as Attributes, and you spread them onto whatever you render.
For example, most triggers render a <button> by default. Render your own button component instead:
<MenuTrigger>
<Render Context="trigger">
<MyButton @attributes="trigger.Attributes" Size="md">Open menu</MyButton>
</Render>
</MenuTrigger>
A component you render must pass the attributes on to its root element, usually with a parameter that has
CaptureUnmatchedValues:
<button class="@($"my-button {Size}")" @attributes="AdditionalAttributes">@ChildContent</button>
@code {
[Parameter] public string Size { get; set; } = "md";
[Parameter] public RenderFragment? ChildContent { get; set; }
[Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }
}Follow these rules:
- Spread
Attributesonto the focusable element. The keyboard and focus handling look for it by the id the attributes carry. - Name each
Contextwhen you nest them (Context="trigger",Context="item"...). Blazor's default name,context, collides with itself. - You don't set
NativeButton. A<span>or<a>trigger needsrole="button", a tab stop, Enter and Space, andaria-disabled, and a real<button>must not get them. The browser reports the element after the first interactive render, and the part renders again to match. Set it only to skip that check, so prerendered HTML is right from the first byte.
Composing several parts
A Render fragment can hold another part, as deep as you need. A button that's a tooltip trigger, a dialog
trigger and a menu trigger at once is a common case:
<DialogRoot>
<TooltipRoot>
<TooltipTrigger>
<Render Context="tooltip">
<DialogTrigger @attributes="tooltip.Attributes">
<Render Context="dialog">
<MenuTrigger @attributes="dialog.Attributes">
<Render Context="menu">
<MyButton @attributes="menu.Attributes" Size="md">Open menu</MyButton>
</Render>
</MenuTrigger>
</Render>
</DialogTrigger>
</Render>
</TooltipTrigger>
<TooltipPopup>...</TooltipPopup>
</TooltipRoot>
<DialogPopup>...</DialogPopup>
</DialogRoot>How attributes are merged
Attributes you put on a part are merged into the Attributes it hands its Render fragment:
- Your
class,data-*and other attributes are kept, unless the part sets the same one. - Your event handlers run before the part's own, and both run.
- Your
stylecomes after the part's, so your declarations win.
An attribute you write after the spread on your own element replaces the part's. Parameters such as
Disabled are read by each part on its own, so one part's parameter doesn't change another's.
AttributeBuilder does this merging. Use it, with UseRender,
to give parts of your own the same Render support.
Changing the default element
Render also replaces the element a part renders. For example, AccordionHeader renders an
<h3>; render an <h2> where the page's heading levels need one:
<AccordionRoot>
<AccordionItem>
<AccordionHeader>
<Render Context="header">
<h2 @attributes="header.Attributes">
<AccordionTrigger>What is Blazix.Primitives?</AccordionTrigger>
</h2>
</Render>
</AccordionHeader>
<AccordionPanel>Blazix.Primitives is a library of unstyled Blazor components.</AccordionPanel>
</AccordionItem>
</AccordionRoot>Each part renders the most fitting element by default, so render a different one only when you have a reason to.
Rendering from state
The fragment's props also carry the part's state, such as Open on a trigger, or
Highlighted, Checked and Disabled on items. Use it to render different
content for each state:
<SwitchThumb>
<Render Context="thumb">
<span @attributes="thumb.Attributes">
@(thumb.Checked ? "On" : "Off")
</span>
</Render>
</SwitchThumb>