Skip to content
Merged
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
21 changes: 20 additions & 1 deletion examples/Demo/Shared/Pages/MessageBar/MessageBarPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@
)
</CodeSnippet>

<h2 id="sections">Sections</h2>
<p>
You can use multiple message bar providers in a single page/app. In the example below you can add a message bar on the top of the window, but also
in the notification center or in a dialog. An area to display messages in is called a <code>Section</code>.

You can specify in wich <code>Section</code> you want your message to appear in through the <code>MessageOptions</code>.

</p>

<h2 id="clear">Clear message bars on navigation</h2>
<p>
By default, message bars will not be removed when navigating to another page. This can be changed by setting the <code>ClearAfterNavigation</code>
parameter to true on a <code>FluentMenuProvider</code>. When using multiple providers (see Sections above), the parameter needs to be set for each
provider individually.
You can also set the behavior on a per message basis by using the same parameter of the <code>MessageOptions</code> class.
</p>

<h2 id="example">Examples</h2>

<DemoSection Title="Message Service" Component="typeof(MessageBarDefault)">
Expand All @@ -44,7 +61,7 @@
If you click on "Add in a dialog" multiple times, you will see that only one message is displayed.
Once you dismiss that one, the next one will be displayed.
</p>
<p>This is the advised way to use MessageBars </p>
<p>Using the <code>MessageService</code> is the advised way to use message bars.</p>
</DemoSection>

<DemoSection Title="Timed Messages" Component="typeof(MessageBarTimed)">
Expand Down Expand Up @@ -100,3 +117,5 @@
<ApiDocumentation Component="typeof(MessageService)" />

<ApiDocumentation Component="typeof(Message)" />

<ApiDocumentation Component="typeof(MessageOptions)" />
2 changes: 1 addition & 1 deletion examples/Demo/Shared/Shared/DemoMainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<div class="content">
<article id="article">

<FluentMessageBarProvider Section="@App.MESSAGES_TOP" />
<FluentMessageBarProvider Section="@App.MESSAGES_TOP" ClearAfterNavigation="true" />


<CascadingValue Value=@OnRefreshTableOfContents>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.FluentUI.AspNetCore.Components.Utilities;

namespace Microsoft.FluentUI.AspNetCore.Components;

/// <summary />
public partial class FluentMessageBarProvider : FluentComponentBase, IDisposable
{
[Inject]
private NavigationManager NavigationManager { get; set; } = default!;

/// <summary />
protected string? ClassValue => new CssBuilder(Class).Build();

Expand Down Expand Up @@ -83,7 +87,12 @@ protected override void OnInitialized()
{
MessageService.OnMessageItemsUpdated += OnMessageItemsUpdatedHandler;
MessageService.OnMessageItemsUpdatedAsync += OnMessageItemsUpdatedHandlerAsync;
base.OnInitialized();

if (ClearAfterNavigation)
{
NavigationManager.LocationChanged += ClearMessages;
}

}

/// <summary />
Expand All @@ -92,18 +101,32 @@ protected virtual void OnMessageItemsUpdatedHandler()
InvokeAsync(StateHasChanged);
}

protected async virtual Task OnMessageItemsUpdatedHandlerAsync()
protected virtual async Task OnMessageItemsUpdatedHandlerAsync()
{
await Task.Run(() =>
{
InvokeAsync(StateHasChanged);
});
}

private void ClearMessages(object? sender, LocationChangedEventArgs args)
{
if (AllMessagesForCategory.Any())
{
InvokeAsync(() =>
{
MessageService.Clear(Section);
StateHasChanged();
});
}
}

/// <summary />
public void Dispose()
{
MessageService.OnMessageItemsUpdated -= OnMessageItemsUpdatedHandler;
MessageService.OnMessageItemsUpdatedAsync -= OnMessageItemsUpdatedHandlerAsync;

NavigationManager.LocationChanged -= ClearMessages;
}
}
6 changes: 3 additions & 3 deletions src/Core/Components/MessageBar/Services/MessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public virtual IEnumerable<Message> MessagesToShow(int count = 5, string? sectio
MessageLock.EnterReadLock();
try
{
IEnumerable<Message>? messages = string.IsNullOrEmpty(section)
var messages = string.IsNullOrEmpty(section)
? MessageList
: MessageList.Where(x => x.Section == section);

Expand Down Expand Up @@ -301,11 +301,11 @@ private void RemoveMessageItems(string? section = null)
return;
}

IEnumerable<Message>? messages = string.IsNullOrEmpty(section)
var messages = string.IsNullOrEmpty(section)
? MessageList
: MessageList.Where(i => i.Section == section);

foreach (Message message in messages)
foreach (var message in messages)
{
message.OnClose -= Remove;
}
Expand Down