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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public partial class BitMessage : BitComponentBase
{
private bool _isExpanded;
private CancellationTokenSource? _autoDismissCts;



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

/// <summary>
/// Enables the auto-dismiss feature and sets the time to automatically call the OnDismiss callback.
/// </summary>
[Parameter] public TimeSpan? AutoDismissTime { get; set; }

/// <summary>
/// The content of message.
/// </summary>
Expand Down Expand Up @@ -178,7 +184,33 @@ protected override void RegisterCssClasses()
});
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender is false) return;
if (OnDismiss.HasDelegate is false) return;
if (AutoDismissTime is not { } delay || delay <= TimeSpan.Zero) return;

_autoDismissCts?.Cancel();
_autoDismissCts = new CancellationTokenSource();
_ = AutoDismissAsync(delay, _autoDismissCts.Token);
}



private async Task AutoDismissAsync(TimeSpan delay, CancellationToken ct)
{
try
{
await Task.Delay(delay, ct);

if (ct.IsCancellationRequested || OnDismiss.HasDelegate is false) return;

await InvokeAsync(OnDismiss.InvokeAsync);
}
catch (TaskCanceledException) { }
}

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

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



protected override async ValueTask DisposeAsync(bool disposing)
{
if (IsDisposed || disposing is false) return;

_autoDismissCts?.Cancel();
_autoDismissCts?.Dispose();

await base.DisposeAsync(disposing);
}



private static Dictionary<BitColor, string> _IconMap = new()
{
[BitColor.Primary] = "Info",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@
{
<BitButton OnClick="() => isDismissed = false">Dismissed, click to reset</BitButton>
}

<br/><br/>

@if (isAutoDismissed is false)
{
<BitMessage AutoDismissTime="TimeSpan.FromSeconds(5)" OnDismiss="() => isAutoDismissed = true">
Auto-Dismiss option enabled by adding the <strong>AutoDismissTime</strong> parameter alongside OnDismiss.
</BitMessage>
}
else
{
<BitButton OnClick="() => isAutoDismissed = false">Auto-Dismissed, click to reset</BitButton>
}
</DemoExample>

<DemoExample Title="Actions" RazorCode="@example9RazorCode" Id="example9">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public partial class BitMessageDemo
Href = "#alignment-enum",
},
new()
{
Name = "AutoDismissTime",
Type = "TimeSpan?",
DefaultValue = "null",
Description = "Enables the auto-dismiss feature and sets the time to automatically call the OnDismiss callback.",
},
new()
{
Name = "ChildContent",
Type = "RenderFragment?",
Expand Down Expand Up @@ -474,6 +481,8 @@ public partial class BitMessageDemo


private bool isDismissed;
private bool isAutoDismissed;

private double elevation = 7;
private bool isErrorDismissed;
private bool isWarningDismissed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,21 @@ Dismiss option enabled by adding <strong>OnDismiss</strong> parameter.
else
{
<BitButton OnClick=""() => isDismissed = false"">Dismissed, click to reset</BitButton>
}

@if (isAutoDismissed is false)
{
<BitMessage AutoDismissTime=""TimeSpan.FromSeconds(5)"" OnDismiss=""() => isAutoDismissed = true"">
Auto-Dismiss option enabled by adding <strong>AutoDismissTime</strong> parameter alongside of OnDismiss.
</BitMessage>
}
else
{
<BitButton OnClick=""() => isAutoDismissed = false"">Auto-Dismissed, click to reset</BitButton>
}";
private readonly string example8CsharpCode = @"
private bool isDismissed;";
private bool isDismissed;
private bool isAutoDismissed;";

private readonly string example9RazorCode = @"
<BitMessage>
Expand Down
Loading