-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sample): using a service to push out of bound toast messages
- Loading branch information
Showing
6 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandHtmxLayout.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
@inherits HtmxLayoutComponentBase | ||
<SectionOutlet SectionName="toast" /> | ||
<SectionOutlet SectionName="message" /> | ||
|
||
@Body | ||
|
||
<ToastOutlet /> |
6 changes: 5 additions & 1 deletion
6
samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/OutOfBandMainLayout.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
@inherits LayoutComponentBase | ||
<header class="container p-3 bg-secondary text-white shadow"> | ||
This header contains content that can be updated out of bounds: | ||
<SectionOutlet SectionName="toast" /> | ||
<SectionOutlet SectionName="message" /> | ||
</header> | ||
|
||
<main class="container p-3"> | ||
@Body | ||
</main> | ||
|
||
<ToastOutlet /> | ||
|
39 changes: 39 additions & 0 deletions
39
samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastOutlet.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
@inherits ConditionalComponentBase | ||
@inject ToastService ToastService | ||
@code { | ||
protected override void OnInitialized() => ToastService.OnMessagesChanged = StateHasChanged; | ||
} | ||
<div id="toast-container" class="toast-container position-fixed bottom-0 end-0 p-3"> | ||
<HtmxFragment> | ||
@if (ToastService.Messages.Count > 0) | ||
{ | ||
<div hx-swap-oob="@SwapStyles.AfterBegin:#toast-container"> | ||
@foreach (var msg in ToastService.Messages) | ||
{ | ||
<div class="toast align-items-center" role="alert" aria-live="assertive" aria-atomic="true"> | ||
<div class="d-flex"> | ||
<div class="toast-body"> | ||
@msg | ||
</div> | ||
<button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button> | ||
</div> | ||
</div> | ||
} | ||
</div> | ||
} | ||
</HtmxFragment> | ||
</div> | ||
<script> | ||
document.body.addEventListener('htmx:oobAfterSwap', function (evt) { | ||
const toasts = evt.detail.target.querySelectorAll(".toast"); | ||
for (let i = 0; i < toasts.length; i++) { | ||
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toasts[i]) | ||
if (!toastBootstrap.isShown()) { | ||
toastBootstrap.show(); | ||
toasts[i].addEventListener('hidden.bs.toast', () => { | ||
toasts[i].remove(); | ||
}); | ||
} | ||
} | ||
}); | ||
</script> |
18 changes: 18 additions & 0 deletions
18
samples/HtmxorExamples/Components/Pages/Examples/OutOfBandOutlets/ToastService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace HtmxorExamples.Components.Pages.Examples.OutOfBandOutlets; | ||
|
||
public sealed class ToastService | ||
{ | ||
private List<string>? messages; | ||
|
||
internal Action? OnMessagesChanged { get; set; } | ||
|
||
public IReadOnlyList<string> Messages | ||
=> messages as IReadOnlyList<string> ?? Array.Empty<string>(); | ||
|
||
public void AddToast(string message) | ||
{ | ||
messages ??= new(); | ||
messages.Add(message); | ||
OnMessagesChanged?.Invoke(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters