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 move-in animation #511

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
16 changes: 16 additions & 0 deletions samples/InteractiveServer/Components/Pages/Animation.razor
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<button @onclick="AnimationDefault" class="btn btn-primary">Fade-in Fade-Out (Default)</button>
<button @onclick="AnimationPopInOut" class="btn btn-primary">Pop-in Pop-Out</button>
<button @onclick="AnimationPopIn" class="btn btn-primary">Pop-in</button>
<button @onclick="AnimationFadeIn" class="btn btn-primary">Fade-in</button>
<button @onclick="AnimationMoveInOut" class="btn btn-primary">Move-in Move-Out</button>
<button @onclick="NoAnimation" class="btn btn-secondary">None</button>

<p>
Expand Down Expand Up @@ -65,6 +67,20 @@
Modal.Show<Confirm>("Animation Type: PopIn", options);
}

void AnimationFadeIn()
{
var options = new ModalOptions { AnimationType = ModalAnimationType.FadeIn };

Modal.Show<Confirm>("Animation Type: FadeIn", options);
}

void AnimationMoveInOut()
{
var options = new ModalOptions { AnimationType = ModalAnimationType.MoveInOut };

Modal.Show<Confirm>("Animation Type: MoveInOut", options);
}

void NoAnimation()
{
var options = new ModalOptions { AnimationType = ModalAnimationType.None };
Expand Down
13 changes: 12 additions & 1 deletion samples/InteractiveServer/Components/Pages/CustomLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,26 @@

<button @onclick="ShowModalCustomLayout" class="btn btn-primary">Show Modal</button>

<button @onclick="ShowAnimatedModalCustomLayout" class="btn btn-primary">Show Animated Modal</button>

@code {

[CascadingParameter] public IModalService Modal { get; set; } = default!;

void ShowModalCustomLayout()
{
var options = new ModalOptions { UseCustomLayout = true };
var parameters = new ModalParameters {{nameof(CustomBootstrapModal.Message), "Hello custom modal!!"}};
var parameters = new ModalParameters();
parameters.Add(nameof(CustomBootstrapModal.Message), "Hello Bootstrap modal!!");
Modal.Show<CustomBootstrapModal>("Custom Layout", parameters, options);
}

void ShowAnimatedModalCustomLayout()
{
var options = new ModalOptions { UseCustomLayout = true, AnimationType = ModalAnimationType.MoveInOut };
var parameters = new ModalParameters();
parameters.Add(nameof(CustomBootstrapModal.Message), "Hello Bootstrap modal!!");
Modal.Show<CustomAnimatedBootstrapModal>("Custom Layout", parameters, options);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="modal d-block @BlazoredModal.OverlayAnimationClass" tabindex="-1" role="dialog">
<div class="modal-backdrop fade show" @onclick="Cancel"></div>
<div class="modal-dialog @BlazoredModal.ModalAnimationClass" style="z-index: 1050">
<!-- Pop it above the backdrop -->
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="Close"></button>
</div>
<div class="modal-body">
<p>@Message</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" @onclick="Close">Close</button>
</div>
</div>
</div>
</div>

@code {

[CascadingParameter] BlazoredModalInstance BlazoredModal { get; set; } = default!;

[Parameter] public string? Message { get; set; }

async Task Close() => await BlazoredModal.CloseAsync(ModalResult.Ok(true));
async Task Cancel() => await BlazoredModal.CancelAsync();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" aria-label="Close" @onclick="Close">
<span aria-hidden="true">&times;</span>
</button>
<button type="button" class="btn-close" aria-label="Close" @onclick="Close"></button>
</div>
<div class="modal-body">
<p>@Message</p>
Expand Down
16 changes: 16 additions & 0 deletions samples/InteractiveWebAssembly/Pages/Animation.razor
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<button @onclick="AnimationDefault" class="btn btn-primary">Fade-in Fade-Out (Default)</button>
<button @onclick="AnimationPopInOut" class="btn btn-primary">Pop-in Pop-Out</button>
<button @onclick="AnimationPopIn" class="btn btn-primary">Pop-in</button>
<button @onclick="AnimationFadeIn" class="btn btn-primary">Fade-in</button>
<button @onclick="AnimationMoveInOut" class="btn btn-primary">Move-in Move-Out</button>
<button @onclick="NoAnimation" class="btn btn-secondary">None</button>

<p>
Expand Down Expand Up @@ -63,6 +65,20 @@
var options = new ModalOptions { AnimationType = ModalAnimationType.PopIn };

Modal.Show<Confirm>("Animation Type: PopIn", options);
}

void AnimationFadeIn()
{
var options = new ModalOptions { AnimationType = ModalAnimationType.FadeIn };

Modal.Show<Confirm>("Animation Type: FadeIn", options);
}

void AnimationMoveInOut()
{
var options = new ModalOptions { AnimationType = ModalAnimationType.MoveInOut };

Modal.Show<Confirm>("Animation Type: MoveInOut", options);
}

void NoAnimation()
Expand Down
10 changes: 10 additions & 0 deletions samples/InteractiveWebAssembly/Pages/CustomLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

<button @onclick="ShowModalCustomLayout" class="btn btn-primary">Show Modal</button>

<button @onclick="ShowAnimatedModalCustomLayout" class="btn btn-primary">Show Animated Modal</button>

@code {

[CascadingParameter] public IModalService Modal { get; set; } = default!;
Expand All @@ -45,4 +47,12 @@
Modal.Show<CustomBootstrapModal>("Custom Layout", parameters, options);
}

void ShowAnimatedModalCustomLayout()
{
var options = new ModalOptions { UseCustomLayout = true, AnimationType = ModalAnimationType.MoveInOut };
var parameters = new ModalParameters();
parameters.Add(nameof(CustomBootstrapModal.Message), "Hello Bootstrap modal!!");
Modal.Show<CustomAnimatedBootstrapModal>("Custom Layout", parameters, options);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="modal d-block @BlazoredModal.OverlayAnimationClass" tabindex="-1" role="dialog">
<div class="modal-backdrop fade show" @onclick="Cancel"></div>
<div class="modal-dialog @BlazoredModal.ModalAnimationClass" style="z-index: 1050">
<!-- Pop it above the backdrop -->
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="btn-close" aria-label="Close" @onclick="Close"></button>
</div>
<div class="modal-body">
<p>@Message</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" @onclick="Close">Close</button>
</div>
</div>
</div>
</div>

@code {

[CascadingParameter] BlazoredModalInstance BlazoredModal { get; set; } = default!;

[Parameter] public string? Message { get; set; }

async Task Close() => await BlazoredModal.CloseAsync(ModalResult.Ok(true));
async Task Cancel() => await BlazoredModal.CancelAsync();

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<div class="modal fade show d-block" tabindex="-1" role="dialog">
<div class="modal d-block" tabindex="-1" role="dialog">
<div class="modal-backdrop fade show" @onclick="Cancel"></div>
<div class="modal-dialog" style="z-index: 1050">
<!-- Pop it above the backdrop -->
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" aria-label="Close" @onclick="Close">
<span aria-hidden="true">&times;</span>
</button>
<button type="button" class="btn-close" aria-label="Close" @onclick="Close"></button>
</div>
<div class="modal-body">
<p>@Message</p>
Expand Down
11 changes: 6 additions & 5 deletions src/Blazored.Modal/BlazoredModalInstance.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@if (UseCustomLayout)
{
<CascadingValue Value="this" IsFixed="true">
@Content
</CascadingValue>
<div>
<CascadingValue Value="this" IsFixed="true">
@Content
</CascadingValue>
</div>
Comment on lines +3 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the div tag needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing it doesn't seem to effect anything

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for the css to be applied to CustomLayouts.

Copy link
Member

@chrissainty chrissainty Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what way? Just when it's removed everything looks to continue working as it did before and there doesn't seem to be any references to this element in the CSS, unless I've missed it?

Copy link
Author

@byOsta byOsta Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the div...

This is without the div:
https://github.com/Blazored/Modal/assets/8792259/b1a629dd-fad1-4f51-9284-cc8765527ad1

This is with the div
https://github.com/Blazored/Modal/assets/8792259/4d12827a-e820-425f-bb6d-7191d8bcb945

Why does this happen?
Because of the Blazor CSS isolation
Without this change the custom layout that we have for the modals wont have the custom BlazoredModalInstance.razor.css styles applied...
I also had to add the following selectors on the BlazoredModalInstance.razor.css:
image

}
else
{
Expand All @@ -13,7 +15,7 @@ else
@onclick="StopListeningToBackgroundClick">

<FocusTrap @ref="FocusTrap" IsActive="ActivateFocusTrap">
<div class="@ModalClass" role="dialog" aria-modal="true" @onmouseup:stopPropagation="true" @onmousedown:stopPropagation="true">
<div class="@ModalClass @ModalAnimationClass" role="dialog" aria-modal="true" @onmouseup:stopPropagation="true" @onmousedown:stopPropagation="true">
@if (!HideHeader)
{
<div class="bm-header">
Expand All @@ -36,6 +38,5 @@ else
</div>
</div>
</FocusTrap>

</div>
}
51 changes: 35 additions & 16 deletions src/Blazored.Modal/BlazoredModalInstance.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public partial class BlazoredModalInstance : IDisposable
private bool HideHeader { get; set; }
private bool HideCloseButton { get; set; }
private bool DisableBackgroundCancel { get; set; }
private string? OverlayAnimationClass { get; set; }
public string? ModalAnimationClass { get; set; }
public string? OverlayAnimationClass { get; set; }
private string? OverlayCustomClass { get; set; }
private ModalAnimationType? AnimationType { get; set; }
private bool ActivateFocusTrap { get; set; }
Expand Down Expand Up @@ -95,20 +96,28 @@ public async Task CloseAsync()
public async Task CloseAsync(ModalResult modalResult)
{
// Fade out the modal, and after that actually remove it
if (AnimationType is ModalAnimationType.FadeInOut)
if (AnimationType is not ModalAnimationType.None && AnimationType is not ModalAnimationType.FadeIn &&
AnimationType is not ModalAnimationType.MoveIn && AnimationType is not ModalAnimationType.PopIn)
{
OverlayAnimationClass += " fade-out";
OverlayAnimationClass = AnimationType switch
{
ModalAnimationType.FadeInOut or ModalAnimationType.FadeOut or
ModalAnimationType.MoveInOut or ModalAnimationType.MoveOut or
ModalAnimationType.PopInOut => "blazored-fade-out",
byOsta marked this conversation as resolved.
Show resolved Hide resolved
_ => string.Empty,
};

ModalAnimationClass = AnimationType switch
{
ModalAnimationType.FadeInOut or ModalAnimationType.FadeOut => "blazored-fade-out",
ModalAnimationType.MoveInOut or ModalAnimationType.MoveOut => "blazored-move-out",
ModalAnimationType.PopInOut => "blazored-pop-out",
byOsta marked this conversation as resolved.
Show resolved Hide resolved
_ => string.Empty,
};
StateHasChanged();

await Task.Delay(400); // Needs to be a bit more than the animation time because of delays in the animation being applied between server and client (at least when using blazor server side), I think.
}
else if (AnimationType is ModalAnimationType.PopInOut)
{
OverlayAnimationClass += " pop-out";
StateHasChanged();

await Task.Delay(400);
}

await Parent.DismissInstance(Id, modalResult);
}
Expand All @@ -127,16 +136,17 @@ public async Task CancelAsync<TPayload>(TPayload payload)

private void ConfigureInstance()
{
UseCustomLayout = SetUseCustomLayout();
AnimationType = SetAnimation();
Position = SetPosition();
ModalClass = SetModalClass();
HideHeader = SetHideHeader();
HideCloseButton = SetHideCloseButton();
DisableBackgroundCancel = SetDisableBackgroundCancel();
UseCustomLayout = SetUseCustomLayout();
OverlayCustomClass = SetOverlayCustomClass();
ActivateFocusTrap = SetActivateFocusTrap();
OverlayAnimationClass = SetAnimationClass();
OverlayAnimationClass = SetOverlayAnimationClass();
ModalAnimationClass = SetModalAnimationClass();
Parent.OnModalClosed += AttemptFocus;
}

Expand Down Expand Up @@ -275,12 +285,21 @@ private string SetModalClass()
}

private ModalAnimationType SetAnimation()
=> Options.AnimationType ?? GlobalModalOptions.AnimationType ?? ModalAnimationType.FadeInOut;
=> Options.AnimationType ?? GlobalModalOptions.AnimationType ?? (!UseCustomLayout ? ModalAnimationType.FadeInOut : ModalAnimationType.None);

private string SetOverlayAnimationClass() => AnimationType switch
{
ModalAnimationType.FadeInOut or ModalAnimationType.FadeIn or
ModalAnimationType.MoveInOut or ModalAnimationType.MoveIn or
ModalAnimationType.PopInOut or ModalAnimationType.PopIn => "blazored-fade-in",
_ => string.Empty
};

private string SetAnimationClass() => AnimationType switch
private string SetModalAnimationClass() => AnimationType switch
{
ModalAnimationType.FadeInOut => "fade-in",
ModalAnimationType.PopInOut or ModalAnimationType.PopIn => "pop-in",
ModalAnimationType.FadeInOut or ModalAnimationType.FadeIn => "blazored-fade-in",
ModalAnimationType.MoveInOut or ModalAnimationType.MoveIn => "blazored-move-in",
ModalAnimationType.PopInOut or ModalAnimationType.PopIn => "blazored-pop-in",
byOsta marked this conversation as resolved.
Show resolved Hide resolved
_ => string.Empty,
};

Expand Down
Loading
Loading