-
Notifications
You must be signed in to change notification settings - Fork 469
[dev-v5] Add NewsBar with dynamic notifications #4973
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6fb3518
Add DemoNewsBar component and implement notification height adjustments
dvoituron 32b2403
Implement dynamic news notification in DemoNewsBar with dismiss funct…
dvoituron 855aae9
Add news banner functionality to DemoNewsBar with content parsing and…
dvoituron aa3428a
Merge branch 'dev-v5' into users/dvoituron/dev-v5/demo-notification
dvoituron ecc0435
Merge branch 'dev-v5' into users/dvoituron/dev-v5/demo-notification
dvoituron c23723e
Refactor DemoNewsBar: Update news version message formatting, rename …
dvoituron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| --- | ||
| Title: New release available | ||
| Intent: Success | ||
| --- | ||
| Version RC4 is now available with many new components. | ||
| Check out the GitHub changelog for all the details and breaking changes. |
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
examples/Demo/FluentUI.Demo.Client/Layout/DemoNewsBar.razor
This file contains hidden or 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,44 @@ | ||
| @if (Visible) | ||
| { | ||
| <FluentMessageBar Intent="@NewsIntent" | ||
| AllowDismiss="false" | ||
| Class="demo-notification"> | ||
| <FluentStack VerticalAlignment="VerticalAlignment.Center"> | ||
| <div> | ||
| @if (!string.IsNullOrEmpty(NewsContent)) | ||
| { | ||
| <strong>@NewsTitle: </strong> | ||
| } | ||
| @NewsContent | ||
| </div> | ||
| <FluentSpacer /> | ||
| <FluentButton Appearance="ButtonAppearance.Transparent" | ||
| IconStart="@(new Icons.Regular.Size20.Dismiss().WithColor("currentColor"))" | ||
| IconOnly="true" | ||
| Title="Dismiss" | ||
| Size="ButtonSize.Small" | ||
| OnClick="@DismissClickAsync" /> | ||
| </FluentStack> | ||
|
|
||
| </FluentMessageBar> | ||
| } | ||
|
|
||
| <script> | ||
| // This function overrides the default max-width of the <fluent-message-bar> component. | ||
| // It is invoked from C# when the message bar becomes visible. | ||
| window.applyDemoNotificationStyle = function () { | ||
| const sheet = new CSSStyleSheet(); | ||
| sheet.replaceSync('.content { max-width: unset; }'); | ||
|
|
||
| // Wait until the <fluent-message-bar> custom element definition is loaded. | ||
| customElements.whenDefined('fluent-message-bar').then(() => { | ||
| const bar = document.querySelector('fluent-message-bar.demo-notification'); | ||
| if (bar && bar.shadowRoot) { | ||
| bar.shadowRoot.adoptedStyleSheets = [ | ||
| ...bar.shadowRoot.adoptedStyleSheets, | ||
| sheet | ||
| ]; | ||
| } | ||
| }); | ||
|
dvoituron marked this conversation as resolved.
|
||
| }; | ||
| </script> | ||
176 changes: 176 additions & 0 deletions
176
examples/Demo/FluentUI.Demo.Client/Layout/DemoNewsBar.razor.cs
This file contains hidden or 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,176 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // This file is licensed to you under the MIT License. | ||
| // ------------------------------------------------------------------------ | ||
|
|
||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using Microsoft.AspNetCore.Components; | ||
| using Microsoft.FluentUI.AspNetCore.Components; | ||
| using Microsoft.JSInterop; | ||
|
|
||
| namespace FluentUI.Demo.Client.Layout; | ||
|
|
||
| /// <summary> | ||
| /// A component that displays a news message bar in the demo application. | ||
| /// </summary> | ||
| public partial class DemoNewsBar | ||
| { | ||
| /* | ||
| -------------------------------------------------------------------- | ||
| NEWS-BANNER.md | ||
| -------------------------------------------------------------------- | ||
|
|
||
| --- | ||
| Title: New release available | ||
| Intent: Success | ||
| --- | ||
| Version RC4 is now available with many new components. | ||
| Check out the changelog for all the details and breaking changes. | ||
| */ | ||
|
|
||
| /// <summary> | ||
| /// The URI of the news content to display in the message bar. | ||
| /// </summary> | ||
| private static readonly Uri NewsUri = new("https://raw.githubusercontent.com/microsoft/fluentui-blazor/refs/heads/dev-v5/NEWS-BANNER.md"); | ||
|
dvoituron marked this conversation as resolved.
|
||
|
|
||
| private const string LocalStorageKey = "fluentui-demo-newsbar-sha"; | ||
|
|
||
| private string NewsTitle { get; set; } = "News"; | ||
|
|
||
| private MessageBarIntent NewsIntent { get; set; } = MessageBarIntent.Info; | ||
|
|
||
| private string? NewsContent { get; set; } | ||
|
|
||
| private string? NewsSha { get; set; } | ||
|
|
||
| private bool Visible { get; set; } | ||
|
|
||
| [Inject] | ||
| public required HttpClient HttpClient { get; set; } | ||
|
|
||
| [Inject] | ||
| public required IJSRuntime JSRuntime { get; set; } | ||
|
|
||
| protected override async Task OnAfterRenderAsync(bool firstRender) | ||
| { | ||
| if (!firstRender) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Read the news content. | ||
| var raw = ""; | ||
|
|
||
| try | ||
| { | ||
| raw = await HttpClient.GetStringAsync(NewsUri); | ||
| } | ||
| catch (HttpRequestException) | ||
| { | ||
| Console.WriteLine($"DemoNewsBar: Failed to read the news content from {NewsUri}"); | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(raw)) | ||
| { | ||
| Visible = false; | ||
| return; | ||
| } | ||
|
|
||
| // Extract the Title, Intent and content from the front matter. | ||
| ParseNewsBanner(raw); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(NewsContent)) | ||
| { | ||
| Visible = false; | ||
| return; | ||
| } | ||
|
|
||
| // Compute a small SHA of the whole file content. | ||
| NewsSha = ComputeSha(raw); | ||
|
|
||
| // If the stored SHA matches the current content, the user already | ||
| // read this message, so the message bar stays hidden. | ||
| var storedSha = await JSRuntime.InvokeAsync<string?>("localStorage.getItem", LocalStorageKey); | ||
| Visible = !string.Equals(storedSha, NewsSha, StringComparison.Ordinal); | ||
|
|
||
| StateHasChanged(); | ||
|
|
||
| // When the content is set and the message bar is visible, apply the | ||
| // notification style override defined in the razor script. | ||
| if (Visible) | ||
| { | ||
| await JSRuntime.InvokeVoidAsync("applyDemoNotificationStyle"); | ||
| } | ||
| } | ||
|
|
||
| private async Task DismissClickAsync() | ||
| { | ||
| // Save the SHA of the read message into the user local storage. | ||
| if (NewsSha is not null) | ||
| { | ||
| await JSRuntime.InvokeVoidAsync("localStorage.setItem", LocalStorageKey, NewsSha); | ||
| } | ||
|
|
||
| Visible = false; | ||
| } | ||
|
|
||
| private static string ComputeSha(string content) | ||
| { | ||
| var hash = SHA256.HashData(Encoding.UTF8.GetBytes(content)); | ||
|
|
||
| // Keep a small SHA: the first 16 bytes are enough to detect content changes. | ||
| return Convert.ToHexString(hash, 0, 16).ToLowerInvariant(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parses the news banner content using the front matter format: | ||
| /// a metadata block delimited by "---" lines containing the Title and | ||
| /// Intent, followed by the content body. | ||
| /// </summary> | ||
| /// <param name="raw">The raw file content to parse.</param> | ||
| private void ParseNewsBanner(string raw) | ||
| { | ||
| var lines = raw.Replace("\r\n", "\n").Split('\n'); | ||
| var index = 0; | ||
|
|
||
| // The front matter must start with a "---" delimiter line. | ||
| if (index < lines.Length && lines[index].Trim() == "---") | ||
| { | ||
| index++; | ||
|
|
||
| // Read the metadata until the closing "---" delimiter line. | ||
| while (index < lines.Length && lines[index].Trim() != "---") | ||
| { | ||
| var line = lines[index]; | ||
| var separator = line.IndexOf(':'); | ||
|
|
||
| if (separator > 0) | ||
| { | ||
| var key = line[..separator].Trim(); | ||
| var value = line[(separator + 1)..].Trim(); | ||
|
|
||
| if (string.Equals(key, "Title", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| NewsTitle = value; | ||
| } | ||
| else if (string.Equals(key, "Intent", StringComparison.OrdinalIgnoreCase) | ||
| && Enum.TryParse<MessageBarIntent>(value, ignoreCase: true, out var intent)) | ||
| { | ||
| NewsIntent = intent; | ||
| } | ||
| } | ||
|
|
||
| index++; | ||
| } | ||
|
|
||
| // Skip the closing "---" delimiter line. | ||
| if (index < lines.Length) | ||
| { | ||
| index++; | ||
| } | ||
| } | ||
|
|
||
| // The remaining lines are the content body. | ||
| NewsContent = string.Join(Environment.NewLine, lines[index..]).Trim(); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.