dotnet / dotnet/aspnetcore

Blazor builds DOM top-down, but some custom web components may rely on child content already being present when first attached to DOM

Open
#48,501 6 comments 0 reactions 0 assignees View on GitHub
area-blazor enhancement
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Consider a simple Web Component that requires some HTML content:

```html

Foo

```

```js
// BlazorWebCompDemo.lib.module.js or any file that registers the component:
customElements.define("my-component", class extends HTMLElement {

#important = this.querySelector(".important");

constructor() {
super();

this.#important.textContent = "Component initialized";
}

});
```

The above code works fine. However, once any Blazor-specific binding/expression is involved, the component shows up without any inner content:

```html


Foo

@(foo)

Foo

Foo

Foo

@code {
int foo = 42;
}
```

![image](https://github.com/dotnet/aspnetcore/assets/6388546/c77f0929-5366-4ef2-8397-4622801d9b56)

Some of our Web Components are external and we cannot change the behavior and they expect the full HTML content when the component is created. This should not happen if it is in a Shadow DOM. Currently we have to write custom Javascript to render them instead of using Blazor as a workaround.

### Expected Behavior

The DOM Element should have all of its content before being attached to the DOM tree.

### Steps To Reproduce

The above code should be enough to reproduce the problem but here is a repo with the code: https://github.com/datvm/BlazorEmptyWebCompDemo

![image](https://github.com/dotnet/aspnetcore/assets/6388546/95ddda8e-d716-49c4-a611-c7c2535c83d9)

(`Foo` means the Web Component wasn't initialized successfully)

### Exceptions (if any)

_No response_

### .NET Version

7.0.302

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.