Skip to content

Blazor-based component generator POC #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ charset = utf-8-bom
indent_size = 2
insert_final_newline = false

[*.razor]
indent_size = 2

[justfile]
indent_size = 2
5 changes: 5 additions & 0 deletions samples/Samples.MvcStarter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using GovUk.Frontend.AspNetCore;
using GovUk.Frontend.AspNetCore.Blazor;
using Joonasw.AspNetCore.SecurityHeaders;
using Microsoft.AspNetCore.Components.Web;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -16,6 +18,9 @@
//};
});

builder.Services.AddScoped<HtmlRenderer>();
builder.Services.AddScoped<BlazorComponentGenerator>();

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
14 changes: 13 additions & 1 deletion samples/Samples.MvcStarter/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
@{
@using GovUk.Frontend.AspNetCore.Blazor
@using GovUk.Frontend.AspNetCore.Components
@inject BlazorComponentGenerator ComponentGenerator
@{
ViewBag.Title = "MVC Starter";
}

<h1 class="govuk-heading-xl">MVC Starter</h1>

@await ComponentGenerator.GenerateWarningTextAsync(new WarningTextOptions()
{
Text = null,
Html = "hello world",
IconFallbackText = null,
Classes = "classes",
Attributes = null
})
26 changes: 26 additions & 0 deletions src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using GovUk.Frontend.AspNetCore.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Html;

namespace GovUk.Frontend.AspNetCore.Blazor;

public class BlazorComponentGenerator(HtmlRenderer htmlRenderer)

Check failure on line 8 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator.BlazorComponentGenerator(HtmlRenderer)'

Check failure on line 8 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator'

Check failure on line 8 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator.BlazorComponentGenerator(HtmlRenderer)'

Check failure on line 8 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator'

Check failure on line 8 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator.BlazorComponentGenerator(HtmlRenderer)'

Check failure on line 8 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator'

Check failure on line 8 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator.BlazorComponentGenerator(HtmlRenderer)'

Check failure on line 8 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator'
{
public async Task<IHtmlContent> GenerateWarningTextAsync(WarningTextOptions options)

Check failure on line 10 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator.GenerateWarningTextAsync(WarningTextOptions)'

Check failure on line 10 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator.GenerateWarningTextAsync(WarningTextOptions)'

Check failure on line 10 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator.GenerateWarningTextAsync(WarningTextOptions)'

Check failure on line 10 in src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Missing XML comment for publicly visible type or member 'BlazorComponentGenerator.GenerateWarningTextAsync(WarningTextOptions)'
{
ArgumentNullException.ThrowIfNull(options);

var parameters = ParameterView.FromDictionary(
new Dictionary<string, object?>()
{
{ "Options", options }
});

return await htmlRenderer.Dispatcher.InvokeAsync(async () =>
{
var output = await htmlRenderer.RenderComponentAsync<WarningText>(parameters);
return new HtmlString(output.ToHtmlString());
});
}
}
15 changes: 15 additions & 0 deletions src/GovUk.Frontend.AspNetCore/Blazor/WarningText.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@using System.Text.Encodings.Web
@using GovUk.Frontend.AspNetCore.Components

@code {
[Parameter, EditorRequired] public WarningTextOptions Options { get; set; } = null!;
}

<div class="govuk-warning-text @(Options.Classes)"
@*{{- govukAttributes(params.attributes) }}*@>
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text">
<span class="govuk-visually-hidden">@(Options.IconFallbackText ?? "Warning")</span>
@(Options.Html is not null ? new MarkupString(Options.Html.ToHtmlString(HtmlEncoder.Default)) : Options.Text)
</strong>
</div>
Loading