Skip to content

Commit 6900a80

Browse files
author
Mischa Spelt
committed
Backdrop: combined UseBackdrop and UseStaticBackdrop to support all options
1 parent 1864d72 commit 6900a80

File tree

3 files changed

+217
-203
lines changed

3 files changed

+217
-203
lines changed

blazorbootstrap/Components/Offcanvas/Offcanvas.razor

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@
22
@inherits BlazorBootstrapComponentBase
33

44
<div @ref="@Element" id="@Id" class="@ClassNames" style="@StyleNames" @attributes="@AdditionalAttributes" tabindex="@TabIndex">
5-
@if (!string.IsNullOrWhiteSpace(Title) || HeaderTemplate != null || ShowCloseButton)
6-
{
7-
<div class="offcanvas-header @HeaderCssClass">
8-
@if (!string.IsNullOrWhiteSpace(title))
9-
{
10-
<h5>@title</h5>
11-
}
12-
else if (HeaderTemplate is not null)
13-
{
14-
@HeaderTemplate
15-
}
5+
@if( !string.IsNullOrWhiteSpace( Title ) || HeaderTemplate != null || ShowCloseButton )
6+
{
7+
<div class="offcanvas-header @HeaderCssClass">
8+
@if( !string.IsNullOrWhiteSpace( title ) )
9+
{
10+
<h5>@title</h5>
11+
}
12+
else if( HeaderTemplate is not null )
13+
{
14+
@HeaderTemplate
15+
}
1616

17-
@if (ShowCloseButton)
18-
{
19-
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
20-
}
21-
</div>
22-
}
23-
@if (childComponent is not null)
24-
{
25-
<div class="offcanvas-body @BodyCssClass">
26-
<DynamicComponent Type="@childComponent" Parameters="@parameters"/>
27-
</div>
28-
}
29-
else if (BodyTemplate != null)
30-
{
31-
<div class="offcanvas-body @BodyCssClass">
32-
@BodyTemplate
33-
</div>
34-
}
35-
@if (FooterTemplate != null)
36-
{
37-
<div class="offcanvas-footer @FooterCssClass">
38-
@FooterTemplate
39-
</div>
40-
}
17+
@if( ShowCloseButton )
18+
{
19+
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
20+
}
21+
</div>
22+
}
23+
@if( childComponent is not null )
24+
{
25+
<div class="offcanvas-body @BodyCssClass">
26+
<DynamicComponent Type="@childComponent" Parameters="@parameters" />
27+
</div>
28+
}
29+
else if( BodyTemplate != null )
30+
{
31+
<div class="offcanvas-body @BodyCssClass">
32+
@BodyTemplate
33+
</div>
34+
}
35+
@if( FooterTemplate != null )
36+
{
37+
<div class="offcanvas-footer @FooterCssClass">
38+
@FooterTemplate
39+
</div>
40+
}
4141
</div>

blazorbootstrap/Components/Offcanvas/Offcanvas.razor.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace BlazorBootstrap;
1+
using System.Reflection.Metadata;
2+
3+
namespace BlazorBootstrap;
24

35
public partial class Offcanvas : BlazorBootstrapComponentBase
46
{
@@ -40,7 +42,17 @@ protected override async ValueTask DisposeAsyncCore( bool disposing )
4042
protected override async Task OnAfterRenderAsync( bool firstRender )
4143
{
4244
if( firstRender )
43-
await JSRuntime.InvokeVoidAsync( "window.blazorBootstrap.offcanvas.initialize", Id, UseStaticBackdrop, CloseOnEscape, IsScrollable, objRef );
45+
{
46+
object backdrop = Backdrop switch
47+
{
48+
BackdropType.None => false,
49+
BackdropType.Visible => true,
50+
BackdropType.Static => "static",
51+
_ => throw new InvalidOperationException( "Invalid backdrop value" )
52+
};
53+
54+
await JSRuntime.InvokeVoidAsync( "window.blazorBootstrap.offcanvas.initialize", Id, backdrop, CloseOnEscape, IsScrollable, objRef );
55+
}
4456

4557
await base.OnAfterRenderAsync( firstRender );
4658
}
@@ -284,24 +296,26 @@ public bool IsVisible
284296
[Parameter]
285297
public string Title { get; set; } = default!;
286298

287-
[Obsolete( "Use `UseStaticBackdrop` parameter." )]
288-
/// <summary>
289-
/// Indicates whether to apply a backdrop on body while offcanvas is open.
290-
/// </summary>
291-
/// <remarks>
292-
/// Default value is true.
293-
/// </remarks>
294299
[Parameter]
295-
public bool UseBackdrop { get => this.UseStaticBackdrop; set => this.UseStaticBackdrop = value; }
300+
public BackdropType Backdrop { get; set; } = BackdropType.Visible;
296301

297-
/// <summary>
298-
/// When `UseStaticBackdrop` is set to true, the offcanvas will not close when clicking outside of it.
299-
/// </summary>
300-
/// <remarks>
301-
/// Default value is false.
302-
/// </remarks>
303-
[Parameter]
304-
public bool UseStaticBackdrop { get; set; } = true;
302+
public enum BackdropType
303+
{
304+
/// <summary>
305+
/// Do not use a backdrop, the page will stay visible.
306+
/// </summary>
307+
None,
308+
309+
/// <summary>
310+
/// Visible backdrop, the page will be covered by a backdrop
311+
/// </summary>
312+
Visible,
313+
314+
/// <summary>
315+
/// Static backdrop, the page will be covered by a backdrop and the offcanvas will not close when clicking outside of it.
316+
/// </summary>
317+
Static
318+
}
305319

306320
#endregion
307321
}

0 commit comments

Comments
 (0)