Skip to content

Commit 8b97e71

Browse files
authored
feat(blazorui): add AutoDismissTime parameter to BitMessage #11426 (#11428)
1 parent 1fce940 commit 8b97e71

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

src/BlazorUI/Bit.BlazorUI/Components/Notifications/Message/BitMessage.razor.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public partial class BitMessage : BitComponentBase
77
{
88
private bool _isExpanded;
9+
private CancellationTokenSource? _autoDismissCts;
910

1011

1112

@@ -20,6 +21,11 @@ public partial class BitMessage : BitComponentBase
2021
[Parameter, ResetStyleBuilder]
2122
public BitAlignment? Alignment { get; set; }
2223

24+
/// <summary>
25+
/// Enables the auto-dismiss feature and sets the time to automatically call the OnDismiss callback.
26+
/// </summary>
27+
[Parameter] public TimeSpan? AutoDismissTime { get; set; }
28+
2329
/// <summary>
2430
/// The content of message.
2531
/// </summary>
@@ -178,7 +184,33 @@ protected override void RegisterCssClasses()
178184
});
179185
}
180186

187+
protected override async Task OnAfterRenderAsync(bool firstRender)
188+
{
189+
await base.OnAfterRenderAsync(firstRender);
190+
191+
if (firstRender is false) return;
192+
if (OnDismiss.HasDelegate is false) return;
193+
if (AutoDismissTime is not { } delay || delay <= TimeSpan.Zero) return;
181194

195+
_autoDismissCts?.Cancel();
196+
_autoDismissCts = new CancellationTokenSource();
197+
_ = AutoDismissAsync(delay, _autoDismissCts.Token);
198+
}
199+
200+
201+
202+
private async Task AutoDismissAsync(TimeSpan delay, CancellationToken ct)
203+
{
204+
try
205+
{
206+
await Task.Delay(delay, ct);
207+
208+
if (ct.IsCancellationRequested || OnDismiss.HasDelegate is false) return;
209+
210+
await InvokeAsync(OnDismiss.InvokeAsync);
211+
}
212+
catch (TaskCanceledException) { }
213+
}
182214

183215
private void ToggleExpand() => _isExpanded = _isExpanded is false;
184216

@@ -188,6 +220,18 @@ protected override void RegisterCssClasses()
188220

189221

190222

223+
protected override async ValueTask DisposeAsync(bool disposing)
224+
{
225+
if (IsDisposed || disposing is false) return;
226+
227+
_autoDismissCts?.Cancel();
228+
_autoDismissCts?.Dispose();
229+
230+
await base.DisposeAsync(disposing);
231+
}
232+
233+
234+
191235
private static Dictionary<BitColor, string> _IconMap = new()
192236
{
193237
[BitColor.Primary] = "Info",

src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Notifications/Message/BitMessageDemo.razor

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@
151151
{
152152
<BitButton OnClick="() => isDismissed = false">Dismissed, click to reset</BitButton>
153153
}
154+
155+
<br/><br/>
156+
157+
@if (isAutoDismissed is false)
158+
{
159+
<BitMessage AutoDismissTime="TimeSpan.FromSeconds(5)" OnDismiss="() => isAutoDismissed = true">
160+
Auto-Dismiss option enabled by adding the <strong>AutoDismissTime</strong> parameter alongside OnDismiss.
161+
</BitMessage>
162+
}
163+
else
164+
{
165+
<BitButton OnClick="() => isAutoDismissed = false">Auto-Dismissed, click to reset</BitButton>
166+
}
154167
</DemoExample>
155168

156169
<DemoExample Title="Actions" RazorCode="@example9RazorCode" Id="example9">

src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Notifications/Message/BitMessageDemo.razor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public partial class BitMessageDemo
2121
Href = "#alignment-enum",
2222
},
2323
new()
24+
{
25+
Name = "AutoDismissTime",
26+
Type = "TimeSpan?",
27+
DefaultValue = "null",
28+
Description = "Enables the auto-dismiss feature and sets the time to automatically call the OnDismiss callback.",
29+
},
30+
new()
2431
{
2532
Name = "ChildContent",
2633
Type = "RenderFragment?",
@@ -474,6 +481,8 @@ public partial class BitMessageDemo
474481

475482

476483
private bool isDismissed;
484+
private bool isAutoDismissed;
485+
477486
private double elevation = 7;
478487
private bool isErrorDismissed;
479488
private bool isWarningDismissed;

src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Notifications/Message/BitMessageDemo.razor.samples.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,21 @@ Dismiss option enabled by adding <strong>OnDismiss</strong> parameter.
106106
else
107107
{
108108
<BitButton OnClick=""() => isDismissed = false"">Dismissed, click to reset</BitButton>
109+
}
110+
111+
@if (isAutoDismissed is false)
112+
{
113+
<BitMessage AutoDismissTime=""TimeSpan.FromSeconds(5)"" OnDismiss=""() => isAutoDismissed = true"">
114+
Auto-Dismiss option enabled by adding <strong>AutoDismissTime</strong> parameter alongside of OnDismiss.
115+
</BitMessage>
116+
}
117+
else
118+
{
119+
<BitButton OnClick=""() => isAutoDismissed = false"">Auto-Dismissed, click to reset</BitButton>
109120
}";
110121
private readonly string example8CsharpCode = @"
111-
private bool isDismissed;";
122+
private bool isDismissed;
123+
private bool isAutoDismissed;";
112124

113125
private readonly string example9RazorCode = @"
114126
<BitMessage>

0 commit comments

Comments
 (0)