Skip to content

Support for Blazor Custom elements to handle nesting of components.ย #45421

@DanielCordell

Description

@DanielCordell

Is there an existing issue for this?

  • I have searched the existing issues

There's this issue #38369 but it refers to angular specifically, but I'm focusing on a pure HTML context & trying to consider more blazor functionality (arbitrarily named RenderFragments, Cascading Parameters). If it's a duplicate my apologies.

Is your feature request related to a problem? Please describe the problem.

I built a very simple test-bench for custom elements, with the idea that I could use them in place of building some code in razor. The feature isn't very extensively documented so I wanted to see if I could find some limitations. I notice that I can't nest components inside each-other, even if the outer component has a RenderFragment ChildContent parameter.

If I have the HTML:

<data-grid>
    <data-column name="Col1" />
    <data-column name="Col2" />
    <data-column name="Col3" />
</data-grid>

(where data-grid is a Custom Element defined in Blazor, with a RenderFragment ChildContent parameter, and data-column is another component that simply renders the text passed into the name attribute.)

It does initially render as such if I put a DOM breakpoint in, but then after the blazor lifecycle has run, all of the data-column elements get removed from the DOM (and therefore the blazor components get destroyed). What you're left with is the actual output of rendering the DataGrid razor component. It would be nice if there was such a way of having nested components work as expected (possibly with Shadow DOM & slots for other render fragments).

Describe the solution you'd like

Ideally, nesting elements with renderfragments in Blazor should be able to be replicated in HTML with custom elements. Possibly having each renderfragment create a 'slot' in the shadow dom?

Would also be very cool if cascading parameters (not type parameters) worked this way as well, although I suspect that's a much harder problem, and likely not feasible.

Additional context

I built a really hacked-together demo of a PoC of simple nesting, by declaring my own window.registerCustomBlazorElement function. https://github.com/DanielCordell/CustomElementsDemo/blob/main/wwwroot/index.html#L34

However it's definitely not the way to go about doing this, as it basically uses the fact that on element creation blazor hasn't overwritten the DOM yet to shove the child nodes of the element into the shadow DOM.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing one

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions