Skip to content

Commit

Permalink
Fixes for #617
Browse files Browse the repository at this point in the history
  • Loading branch information
jbomhold3 committed Sep 19, 2024
1 parent cb2654e commit 2be1ce8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/BlazorStrap/BlazorStrap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://blazorstrap.io/</PackageProjectUrl>
<RepositoryUrl>https://github.com/chanan/BlazorStrap</RepositoryUrl>
<RootNamespace>BlazorStrap</RootNamespace>
<PackageVersion>5.2.103-Beta1</PackageVersion>
<PackageVersion>5.2.103-Beta1a</PackageVersion>
<AnalysisLevel>6.0</AnalysisLevel><!--Next Use 5.2.200-Preview1-->
</PropertyGroup>

Expand Down
13 changes: 5 additions & 8 deletions src/BlazorStrap/Service/BlazorStrapInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ public async ValueTask ToastTimerAsync(ElementReference? elementReference, int t
if (elementReference is null || module is null) return;
await module.InvokeVoidAsync("toastTimer", cancellationToken ?? CancellationToken.None, elementReference, time, timeRemaining, rendered);
}
//TODO: Direct Points from old JS
/// <summary>
/// Triggers Carousel to animate
/// </summary>
Expand All @@ -303,12 +302,9 @@ public async ValueTask ToastTimerAsync(ElementReference? elementReference, int t
public async ValueTask<bool> AnimateCarouselAsync(string id, ElementReference? showElementReference, ElementReference? hideElementReference, bool back, bool v4, CancellationToken? cancellationToken = null)
{
var module = await GetModuleAsync();
if (module is null)
throw new NullReferenceException("Unable to load module.");
if (showElementReference == null)
throw new ArgumentNullException(nameof(showElementReference));
if (hideElementReference == null)
throw new ArgumentNullException(nameof(hideElementReference));
//If anything is null just return true. Animation will not trigger but the slide will still change.
if(module is null || showElementReference is null || hideElementReference is null)
return true;

return await module.InvokeAsync<bool>("animateCarousel", cancellationToken ?? CancellationToken.None, id, showElementReference, hideElementReference, back, v4, _objectReference);
}
Expand All @@ -322,7 +318,8 @@ public async ValueTask<bool> AnimateCarouselAsync(string id, ElementReference? s
/// <exception cref="NullReferenceException"></exception>
public async ValueTask PreloadModuleAsync(CancellationToken? cancellationToken = null)
{
_ = await GetModuleAsync() ?? throw new NullReferenceException("Unable to load module.");
//This might return null if this method is called be JS interop is ready. Since we are only preloading here we can ignore this.
_ = await GetModuleAsync();
}

/// <summary>
Expand Down
75 changes: 46 additions & 29 deletions src/BlazorStrap/Shared/Components/Common/BSCarouselBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,33 @@ internal async Task GotoChildSlide(BSCarouselItemBase item)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0045:Convert to conditional expression", Justification = "<Pending>")]
public async Task BackAsync()
{
if (ClickLocked) return;
ClickLocked = true;
try
{
if (ClickLocked) return;
ClickLocked = true;

var last = _active;
_active--;
var last = _active;
_active--;

if (_active < 0)
_active = Children.Count - 1;
if (last == 0)
_last = last;
if (_active < 0)
_active = Children.Count - 1;
if (last == 0)
_last = last;

else
_last = _active + 1;
await Children[_last].InternalHide();
await Children[_active].InternalShow();
await DoAnimations(true);
else
_last = _active + 1;
await Children[_last].InternalHide();
await Children[_active].InternalShow();
await DoAnimations(true);

await InvokeAsync(() => { IndicatorsRef?.Refresh(Children.Count, _active); });
ResetTransitionTimer(Children[_active].Interval);
await InvokeAsync(() => { IndicatorsRef?.Refresh(Children.Count, _active); });
ResetTransitionTimer(Children[_active].Interval);
}
catch (Exception e)
{
// When navigating to a different page, the carousel Children are removed from the DOM, causing an exception this can be ignored.
// The timer can tick just as this happens. This is why we need to catch the exception.
}
}

/// <summary>
Expand All @@ -160,25 +168,34 @@ public async Task BackAsync()
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0045:Convert to conditional expression", Justification = "<Pending>")]
public async Task NextAsync()
{
if (ClickLocked) return;
ClickLocked = true;
_active++;
if (_active > Children.Count - 1)
_active = 0;
try
{
if (ClickLocked) return;
ClickLocked = true;
_active++;
if (_active > Children.Count - 1)
_active = 0;

if (_active == 0)
_last = Children.Count - 1;
if (_active == 0)
_last = Children.Count - 1;

else
_last = _active - 1;
await Children[_last].InternalHide();
await Children[_active].InternalShow();
else
_last = _active - 1;
await Children[_last].InternalHide();
await Children[_active].InternalShow();

await DoAnimations(false);
await DoAnimations(false);


await InvokeAsync(() => { IndicatorsRef?.Refresh(Children.Count, _active); });
ResetTransitionTimer(Children[_active].Interval);
await InvokeAsync(() => { IndicatorsRef?.Refresh(Children.Count, _active); });
if (Children.Count > _active)
ResetTransitionTimer(Children[_active].Interval);
}
catch (Exception e)
{
// When navigating to a different page, the carousel Children are removed from the DOM, causing an exception this can be ignored.
// The timer can tick just as this happens. This is why we need to catch the exception.
}
}

protected abstract Task DoAnimations(bool back);
Expand Down

0 comments on commit 2be1ce8

Please sign in to comment.