Skip to content

Commit c58e473

Browse files
authored
Blazor custom elements (#23314)
1 parent 03f4b57 commit c58e473

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

aspnetcore/blazor/components/index.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,20 +1035,64 @@ To render a Razor component from JS, register the component as a root component
10351035
options.RootComponents.RegisterForJavaScript<Counter>(identifier: "counter");
10361036
});
10371037
```
1038+
1039+
> [!NOTE]
1040+
> The preceding code example requires a namespace for the app's components (for example, `using BlazorSample.Pages;`) in the `Program.cs` file.
10381041
10391042
* In a Blazor WebAssembly app, call `RegisterForJavaScript` on <xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.RootComponents> in `Program.cs`:
10401043

10411044
```csharp
10421045
builder.RootComponents.RegisterForJavaScript<Counter>(identifier: "counter");
10431046
```
1047+
1048+
> [!NOTE]
1049+
> The preceding code example requires a namespace for the app's components (for example, `using BlazorSample.Pages;`) in the `Program.cs` file.
10441050
1045-
Load Blazor into your JS app (`blazor.server.js` or `blazor.webassembly.js`). Render the component from JS into a container element using the registered identifier, passing component parameters as needed:
1051+
Load Blazor into the JS app (`blazor.server.js` or `blazor.webassembly.js`). Render the component from JS into a container element using the registered identifier, passing component parameters as needed:
10461052

10471053
```javascript
10481054
let containerElement = document.getElementById('my-counter');
10491055
await Blazor.rootComponents.add(containerElement, 'counter', { incrementAmount: 10 });
10501056
```
10511057

1058+
## Blazor custom elements
1059+
1060+
Experimental support is available for building custom elements using the the [`Microsoft.AspNetCore.Components.CustomElements` NuGet package](https://www.nuget.org/packages/microsoft.aspnetcore.components.customelements). Custom elements use standard HTML interfaces to implement custom HTML elements.
1061+
1062+
Register a root component as a custom element:
1063+
1064+
* In a Blazor Server app, modify the call to <xref:Microsoft.Extensions.DependencyInjection.ComponentServiceCollectionExtensions.AddServerSideBlazor%2A> in `Program.cs`:
1065+
1066+
```csharp
1067+
builder.Services.AddServerSideBlazor(options =>
1068+
{
1069+
options.RootComponents.RegisterAsCustomElement<Counter>("my-counter");
1070+
});
1071+
```
1072+
1073+
> [!NOTE]
1074+
> The preceding code example requires a namespace for the app's components (for example, `using BlazorSample.Pages;`) in the `Program.cs` file.
1075+
1076+
* In a Blazor WebAssembly app, call `RegisterAsCustomElement` on <xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.RootComponents> in `Program.cs`:
1077+
1078+
```csharp
1079+
builder.RootComponents.RegisterAsCustomElement<Counter>("my-counter");
1080+
```
1081+
1082+
> [!NOTE]
1083+
> The preceding code example requires a namespace for the app's components (for example, `using BlazorSample.Pages;`) in the `Program.cs` file.
1084+
1085+
Use the custom element with any web framework. For example, the preceding counter custom element is used in a React app with the following markup:
1086+
1087+
```html
1088+
<my-counter increment-amount={incrementAmount}></my-counter>
1089+
```
1090+
1091+
For a complete example of how to create custom elements with Blazor, see the [Blazor Custom Elements sample project](https://github.com/aspnet/AspLabs/tree/main/src/BlazorCustomElements).
1092+
1093+
> [!WARNING]
1094+
> The custom elements feature is currently **experimental, unsupported, and subject to change or be removed at any time**. We welcome your feedback on how well this particular approach meets your requirements.
1095+
10521096
::: moniker-end
10531097

10541098
::: moniker range=">= aspnetcore-5.0 < aspnetcore-6.0"

0 commit comments

Comments
 (0)