Skip to content
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

Add BitPageVisibility utility class (#10231) #10241

Merged
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 @@ -23,6 +23,7 @@ public partial class BitCarousel : BitComponentBase


[Inject] private IJSRuntime _js { get; set; } = default!;
[Inject] private BitPageVisibility _pageVisibility { get; set; } = default!;



Expand Down Expand Up @@ -128,6 +129,22 @@ public async Task GoTo(int index)
await GotoPage(index - 1);
}

/// <summary>
/// Pauses the AutoPlay if enabled.
/// </summary>
public void Pause()
{
_autoPlayTimer?.Stop();
}

/// <summary>
/// Resumes the AutoPlay if enabled.
/// </summary>
public void Resume()
{
_autoPlayTimer?.Start();
}



[JSInvokable("OnResize")]
Expand Down Expand Up @@ -179,6 +196,8 @@ protected override void OnInitialized()
{
_dotnetObj = DotNetObjectReference.Create(this);

_pageVisibility.OnChange += PageVisibilityChange;

base.OnInitialized();
}

Expand All @@ -191,21 +210,30 @@ protected override void OnParametersSet()

protected override async Task OnAfterRenderAsync(bool firstRender)
{
_directionStyle = Dir == BitDir.Rtl ? "direction:rtl" : string.Empty;

await base.OnAfterRenderAsync(firstRender);

if (firstRender is false) return;

await _js.BitObserversRegisterResize(UniqueId, RootElement, _dotnetObj);
_directionStyle = Dir == BitDir.Rtl ? "direction:rtl" : string.Empty;

if (AutoPlay)
if (AutoPlay && _autoPlayTimer is null)
{
_autoPlayTimer = new System.Timers.Timer(AutoPlayInterval);
_autoPlayTimer.Elapsed += AutoPlayTimerElapsed;
_autoPlayTimer.Start();
}

if (AutoPlay is false && _autoPlayTimer is not null)
{
_autoPlayTimer.Elapsed -= AutoPlayTimerElapsed;
_autoPlayTimer.Dispose();
_autoPlayTimer = null;
}

if (firstRender is false) return;

await _pageVisibility.Init();

await _js.BitObserversRegisterResize(UniqueId, RootElement, _dotnetObj);

if (ScrollItemsCount > VisibleItemsCount)
{
_internalScrollItemsCount = VisibleItemsCount;
Expand Down Expand Up @@ -421,12 +449,28 @@ private async void AutoPlayTimerElapsed(object? sender, System.Timers.ElapsedEve
await InvokeAsync(Next);
}

private Task PageVisibilityChange(bool hidden)
{
if (hidden)
{
Pause();
}
else
{
Resume();
}

return Task.CompletedTask;
}



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

_pageVisibility.OnChange -= PageVisibilityChange;

if (_autoPlayTimer is not null)
{
_autoPlayTimer.Elapsed -= AutoPlayTimerElapsed;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text;

namespace Bit.BlazorUI;
namespace Bit.BlazorUI;

/// <summary>
/// SnackBars provide brief notifications. The component is also known as a toast.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static IServiceCollection AddBitBlazorUIServices(this IServiceCollection
{
services.TryAddScoped<BitThemeManager>();

services.TryAddScoped<BitPageVisibility>();

return services;
}
}
13 changes: 13 additions & 0 deletions src/BlazorUI/Bit.BlazorUI/Scripts/PageVisibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace BitBlazorUI {
export class PageVisibility {
private static _isInitialized = false;

public static init(dotnetObj: DotNetObject) {
if (PageVisibility._isInitialized) return;

PageVisibility._isInitialized = true;

document.addEventListener('visibilitychange', () => dotnetObj.invokeMethodAsync('VisibilityChanged', document.hidden));
}
}
}
48 changes: 48 additions & 0 deletions src/BlazorUI/Bit.BlazorUI/Utils/BitPageVisibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Bit.BlazorUI;

/// <summary>
/// The Page Visibility API provides events you can watch for to know when a document becomes visible or hidden.
/// <br />
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API"/>
/// </summary>
public class BitPageVisibility(IJSRuntime js)
{
private bool _isInitialized;
private DotNetObjectReference<BitPageVisibility>? _dotnetObj;



/// <summary>
/// Fires when the content of the document has become visible or hidden.
/// </summary>
public event Func<bool, Task>? OnChange;



/// <summary>
/// Initializes the js api of the page visibility utility.
/// </summary>
public async Task Init()
{
if (_isInitialized) return;

_isInitialized = true;

_dotnetObj = DotNetObjectReference.Create(this);

await js.InvokeVoid("BitBlazorUI.PageVisibility.init", _dotnetObj);
}



[JSInvokable("VisibilityChanged")]
public async Task _VisibilityChanged(bool hidden)
{
var onChange = OnChange;
if (onChange is not null)
{
await onChange(hidden);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ public partial class BitCarouselDemo
Name = "GoTo",
Type = "Task",
Description = "Navigates to the given carousel item index.",
},
new()
{
Name = "Pause",
Type = "Task",
Description = "Pauses the AutoPlay if enabled.",
},
new()
{
Name = "Resume",
Type = "Task",
Description = "Resumes the AutoPlay if enabled.",
}
];

Expand Down
Loading