From 840a7b8a573ee4a5e031f73267f44aca12f376c3 Mon Sep 17 00:00:00 2001 From: James Gunn Date: Mon, 26 May 2025 17:57:22 +0100 Subject: [PATCH] Blazor-based component generator POC --- .editorconfig | 3 +++ samples/Samples.MvcStarter/Program.cs | 5 ++++ .../Views/Home/Index.cshtml | 14 +++++++++- .../Blazor/BlazorComponentGenerator.cs | 26 +++++++++++++++++++ .../Blazor/WarningText.razor | 15 +++++++++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs create mode 100644 src/GovUk.Frontend.AspNetCore/Blazor/WarningText.razor diff --git a/.editorconfig b/.editorconfig index 2052d3585..a9f75cc03 100644 --- a/.editorconfig +++ b/.editorconfig @@ -37,5 +37,8 @@ charset = utf-8-bom indent_size = 2 insert_final_newline = false +[*.razor] +indent_size = 2 + [justfile] indent_size = 2 \ No newline at end of file diff --git a/samples/Samples.MvcStarter/Program.cs b/samples/Samples.MvcStarter/Program.cs index 8b1cceafe..a2b2ff4fd 100644 --- a/samples/Samples.MvcStarter/Program.cs +++ b/samples/Samples.MvcStarter/Program.cs @@ -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); @@ -16,6 +18,9 @@ //}; }); +builder.Services.AddScoped(); +builder.Services.AddScoped(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/samples/Samples.MvcStarter/Views/Home/Index.cshtml b/samples/Samples.MvcStarter/Views/Home/Index.cshtml index f3aa2528e..28646c4e1 100644 --- a/samples/Samples.MvcStarter/Views/Home/Index.cshtml +++ b/samples/Samples.MvcStarter/Views/Home/Index.cshtml @@ -1,5 +1,17 @@ -@{ +@using GovUk.Frontend.AspNetCore.Blazor +@using GovUk.Frontend.AspNetCore.Components +@inject BlazorComponentGenerator ComponentGenerator +@{ ViewBag.Title = "MVC Starter"; }

MVC Starter

+ +@await ComponentGenerator.GenerateWarningTextAsync(new WarningTextOptions() +{ + Text = null, + Html = "hello world", + IconFallbackText = null, + Classes = "classes", + Attributes = null +}) diff --git a/src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs b/src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs new file mode 100644 index 000000000..75270847a --- /dev/null +++ b/src/GovUk.Frontend.AspNetCore/Blazor/BlazorComponentGenerator.cs @@ -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) +{ + public async Task GenerateWarningTextAsync(WarningTextOptions options) + { + ArgumentNullException.ThrowIfNull(options); + + var parameters = ParameterView.FromDictionary( + new Dictionary() + { + { "Options", options } + }); + + return await htmlRenderer.Dispatcher.InvokeAsync(async () => + { + var output = await htmlRenderer.RenderComponentAsync(parameters); + return new HtmlString(output.ToHtmlString()); + }); + } +} diff --git a/src/GovUk.Frontend.AspNetCore/Blazor/WarningText.razor b/src/GovUk.Frontend.AspNetCore/Blazor/WarningText.razor new file mode 100644 index 000000000..3ae93dd5f --- /dev/null +++ b/src/GovUk.Frontend.AspNetCore/Blazor/WarningText.razor @@ -0,0 +1,15 @@ +@using System.Text.Encodings.Web +@using GovUk.Frontend.AspNetCore.Components + +@code { + [Parameter, EditorRequired] public WarningTextOptions Options { get; set; } = null!; +} + +
+ + + @(Options.IconFallbackText ?? "Warning") + @(Options.Html is not null ? new MarkupString(Options.Html.ToHtmlString(HtmlEncoder.Default)) : Options.Text) + +