/
PageTitle.cs
35 lines (30 loc) · 1.2 KB
/
PageTitle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Sections;
namespace Microsoft.AspNetCore.Components.Web;
/// <summary>
/// Enables rendering an HTML <c><title></c> to a <see cref="HeadOutlet"/> component.
/// </summary>
public sealed class PageTitle : ComponentBase
{
/// <summary>
/// Gets or sets the content to be rendered as the document title.
/// </summary>
[Parameter]
public RenderFragment? ChildContent { get; set; }
/// <inheritdoc/>
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenComponent<SectionContent>(0);
builder.AddAttribute(1, nameof(SectionContent.Name), HeadOutlet.TitleSectionOutletName);
builder.AddAttribute(2, nameof(SectionContent.ChildContent), (RenderFragment)BuildTitleRenderTree);
builder.CloseComponent();
}
private void BuildTitleRenderTree(RenderTreeBuilder builder)
{
builder.OpenElement(0, "title");
builder.AddContent(1, ChildContent);
builder.CloseElement();
}
}