Skip to content

Commit ba32c83

Browse files
committed
add BitPageVisibility utility class bitfoundation#10231
1 parent 1f2c1ba commit ba32c83

File tree

6 files changed

+122
-8
lines changed

6 files changed

+122
-8
lines changed

src/BlazorUI/Bit.BlazorUI/Components/Lists/Carousel/BitCarousel.razor.cs

+49-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public partial class BitCarousel : BitComponentBase
2323

2424

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

2728

2829

@@ -128,6 +129,22 @@ public async Task GoTo(int index)
128129
await GotoPage(index - 1);
129130
}
130131

132+
/// <summary>
133+
/// Pauses the AutoPlay if enabled.
134+
/// </summary>
135+
public async Task Pause()
136+
{
137+
_autoPlayTimer?.Stop();
138+
}
139+
140+
/// <summary>
141+
/// Resumes the AutoPlay if enabled.
142+
/// </summary>
143+
public async Task Resume()
144+
{
145+
_autoPlayTimer?.Start();
146+
}
147+
131148

132149

133150
[JSInvokable("OnResize")]
@@ -179,6 +196,8 @@ protected override void OnInitialized()
179196
{
180197
_dotnetObj = DotNetObjectReference.Create(this);
181198

199+
_pageVisibility.OnChange += PageVisibilityChange;
200+
182201
base.OnInitialized();
183202
}
184203

@@ -195,17 +214,26 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
195214

196215
await base.OnAfterRenderAsync(firstRender);
197216

198-
if (firstRender is false) return;
199-
200-
await _js.BitObserversRegisterResize(UniqueId, RootElement, _dotnetObj);
201-
202-
if (AutoPlay)
217+
if (AutoPlay && _autoPlayTimer is null)
203218
{
204219
_autoPlayTimer = new System.Timers.Timer(AutoPlayInterval);
205220
_autoPlayTimer.Elapsed += AutoPlayTimerElapsed;
206221
_autoPlayTimer.Start();
207222
}
208223

224+
if (AutoPlay is false && _autoPlayTimer is not null)
225+
{
226+
_autoPlayTimer.Elapsed -= AutoPlayTimerElapsed;
227+
_autoPlayTimer.Dispose();
228+
_autoPlayTimer = null;
229+
}
230+
231+
if (firstRender is false) return;
232+
233+
await _pageVisibility.Init();
234+
235+
await _js.BitObserversRegisterResize(UniqueId, RootElement, _dotnetObj);
236+
209237
if (ScrollItemsCount > VisibleItemsCount)
210238
{
211239
_internalScrollItemsCount = VisibleItemsCount;
@@ -421,12 +449,28 @@ private async void AutoPlayTimerElapsed(object? sender, System.Timers.ElapsedEve
421449
await InvokeAsync(Next);
422450
}
423451

452+
private async Task PageVisibilityChange(bool hidden)
453+
{
454+
Console.WriteLine($"PageVisibilityChange: {hidden}");
455+
456+
if (hidden)
457+
{
458+
await Pause();
459+
}
460+
else
461+
{
462+
await Resume();
463+
}
464+
}
465+
424466

425467

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

472+
_pageVisibility.OnChange -= PageVisibilityChange;
473+
430474
if (_autoPlayTimer is not null)
431475
{
432476
_autoPlayTimer.Elapsed -= AutoPlayTimerElapsed;

src/BlazorUI/Bit.BlazorUI/Components/Notifications/SnackBar/BitSnackBar.razor.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text;
2-
3-
namespace Bit.BlazorUI;
1+
namespace Bit.BlazorUI;
42

53
/// <summary>
64
/// SnackBars provide brief notifications. The component is also known as a toast.

src/BlazorUI/Bit.BlazorUI/Extensions/IServiceCollectionExtensions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public static IServiceCollection AddBitBlazorUIServices(this IServiceCollection
99
{
1010
services.TryAddScoped<BitThemeManager>();
1111

12+
services.TryAddScoped<BitPageVisibility>();
13+
1214
return services;
1315
}
1416
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BitBlazorUI {
2+
export class PageVisibility {
3+
private static _isInitialized = false;
4+
5+
public static init(dotnetObj: DotNetObject) {
6+
if (PageVisibility._isInitialized) return;
7+
8+
PageVisibility._isInitialized = true;
9+
10+
document.addEventListener('visibilitychange', () => dotnetObj.invokeMethodAsync('VisibilityChanged', document.hidden));
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Bit.BlazorUI;
2+
3+
/// <summary>
4+
/// The Page Visibility API provides events you can watch for to know when a document becomes visible or hidden.
5+
/// <br />
6+
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API"/>
7+
/// </summary>
8+
public class BitPageVisibility(IJSRuntime js)
9+
{
10+
private bool _isInitialized;
11+
private DotNetObjectReference<BitPageVisibility>? _dotnetObj;
12+
13+
14+
15+
/// <summary>
16+
/// Fires when the content of the document has become visible or hidden.
17+
/// </summary>
18+
public event Func<bool, Task>? OnChange;
19+
20+
21+
22+
public async Task Init()
23+
{
24+
if (_isInitialized) return;
25+
26+
_isInitialized = true;
27+
28+
_dotnetObj = DotNetObjectReference.Create(this);
29+
30+
await js.InvokeVoid("BitBlazorUI.PageVisibility.init", _dotnetObj);
31+
}
32+
33+
34+
35+
[JSInvokable("VisibilityChanged")]
36+
public async Task _VisibilityChanged(bool hidden)
37+
{
38+
var onChange = OnChange;
39+
if (onChange is not null)
40+
{
41+
await onChange(hidden);
42+
}
43+
}
44+
45+
}

src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Lists/Carousel/BitCarouselDemo.razor.cs

+12
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ public partial class BitCarouselDemo
209209
Name = "GoTo",
210210
Type = "Task",
211211
Description = "Navigates to the given carousel item index.",
212+
},
213+
new()
214+
{
215+
Name = "Pause",
216+
Type = "Task",
217+
Description = "Pauses the AutoPlay if enabled.",
218+
},
219+
new()
220+
{
221+
Name = "Resume",
222+
Type = "Task",
223+
Description = "Resumes the AutoPlay if enabled.",
212224
}
213225
];
214226

0 commit comments

Comments
 (0)