From fd7feab818635f4348c7a8adcec5a6e2f3563f26 Mon Sep 17 00:00:00 2001 From: Egil Hansen Date: Wed, 8 May 2024 17:26:50 +0000 Subject: [PATCH] feat(sample): using a service to push out of bound toast messages --- .../Examples/OutOfBandOutlets/Index.razor | 10 +++-- .../OutOfBandHtmxLayout.razor | 5 ++- .../OutOfBandMainLayout.razor | 6 ++- .../OutOfBandOutlets/ToastOutlet.razor | 39 +++++++++++++++++++ .../Examples/OutOfBandOutlets/ToastService.cs | 18 +++++++++ samples/HtmxorExamples/Program.cs | 2 + 6 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastOutlet.razor create mode 100644 samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastService.cs diff --git a/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/Index.razor b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/Index.razor index e68d9d0..8c62300 100644 --- a/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/Index.razor +++ b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/Index.razor @@ -2,6 +2,7 @@ @attribute [HtmxLayout(typeof(OutOfBandHtmxLayout), Priority = 1)] @layout OutOfBandMainLayout @page "/examples/out-of-band-outlets" +@inject ToastService toasts @code { [SupplyParameterFromForm] private int CurrentCount { get; set; } = 0; @@ -9,6 +10,7 @@ private void IncrementCount(HtmxEventArgs args) { CurrentCount++; + toasts.AddToast($"Counter is now {CurrentCount}"); } } Htmxor - Examples - Out of Band Outlets @@ -16,9 +18,9 @@

This is an example of a counter updating and a element in the header also updating out of bounds.

- - - + + + The count is @(CurrentCount % 2 == 0 ? "even" : "odd")! @@ -33,5 +35,5 @@ @onput="IncrementCount"> Click me - +
diff --git a/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandHtmxLayout.razor b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandHtmxLayout.razor index e646a1c..ebcec26 100644 --- a/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandHtmxLayout.razor +++ b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandHtmxLayout.razor @@ -1,3 +1,6 @@ @inherits HtmxLayoutComponentBase - + + @Body + + diff --git a/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandMainLayout.razor b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandMainLayout.razor index 6843358..5a05432 100644 --- a/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandMainLayout.razor +++ b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandMainLayout.razor @@ -1,8 +1,12 @@ @inherits LayoutComponentBase
This header contains content that can be updated out of bounds: - +
+
@Body
+ + + diff --git a/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastOutlet.razor b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastOutlet.razor new file mode 100644 index 0000000..f90a27d --- /dev/null +++ b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastOutlet.razor @@ -0,0 +1,39 @@ +@inherits ConditionalComponentBase +@inject ToastService ToastService +@code { + protected override void OnInitialized() => ToastService.OnMessagesChanged = StateHasChanged; +} +
+ + @if (ToastService.Messages.Count > 0) + { +
+ @foreach (var msg in ToastService.Messages) + { + + } +
+ } +
+
+ diff --git a/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastService.cs b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastService.cs new file mode 100644 index 0000000..d1c824f --- /dev/null +++ b/samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastService.cs @@ -0,0 +1,18 @@ +namespace HtmxorExamples.Components.Pages.Examples.OutOfBandOutlets; + +public sealed class ToastService +{ + private List? messages; + + internal Action? OnMessagesChanged { get; set; } + + public IReadOnlyList Messages + => messages as IReadOnlyList ?? Array.Empty(); + + public void AddToast(string message) + { + messages ??= new(); + messages.Add(message); + OnMessagesChanged?.Invoke(); + } +} diff --git a/samples/HtmxorExamples/Program.cs b/samples/HtmxorExamples/Program.cs index 1634400..4131227 100644 --- a/samples/HtmxorExamples/Program.cs +++ b/samples/HtmxorExamples/Program.cs @@ -1,8 +1,10 @@ using HtmxorExamples.Components; +using HtmxorExamples.Components.Pages.Examples.OutOfBandOutlets; var builder = WebApplication.CreateBuilder(args); // Add services to the container. +builder.Services.AddScoped(); builder.Services.AddRazorComponents().AddHtmx(options => { // Enabled to support out of band updates