Skip to content

Commit 60296f4

Browse files
Provide CollapseOnOverlayTap support for Maui Bottom Sheet
1 parent a3038d6 commit 60296f4

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

maui/src/BottomSheet/SfBottomSheet.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,20 @@ public partial class SfBottomSheet : SfView, IParentThemeElement
455455
BindingMode.Default,
456456
propertyChanged: OnGrabberCornerRadiusPropertyChanged);
457457

458+
/// <summary>
459+
/// Identifies the <see cref="CollapseOnOverlayTap"/> bindable property.
460+
/// </summary>
461+
/// <value>
462+
/// The identifier for <see cref="CollapseOnOverlayTap"/> bindable property.
463+
/// </value>
464+
public static readonly BindableProperty CollapseOnOverlayTapProperty = BindableProperty.Create(
465+
nameof(CollapseOnOverlayTap),
466+
typeof(bool),
467+
typeof(SfBottomSheet),
468+
false,
469+
BindingMode.Default
470+
);
471+
458472
#endregion
459473

460474
#region Internal Bindable Properties
@@ -781,6 +795,18 @@ public CornerRadius GrabberCornerRadius
781795
set => SetValue(GrabberCornerRadiusProperty, value);
782796
}
783797

798+
/// <summary>
799+
/// Gets or sets whether the bottom sheet should collapse to its minimum height instead of closing when tapping outside.
800+
/// </summary>
801+
/// <value>
802+
/// It accepts Boolean values, and the default value is <c>false</c>.
803+
/// </value>
804+
public bool CollapseOnOverlayTap
805+
{
806+
get => (bool)GetValue(CollapseOnOverlayTapProperty);
807+
set => SetValue(CollapseOnOverlayTapProperty, value);
808+
}
809+
784810
#endregion
785811

786812
#region Internal Properties
@@ -1201,7 +1227,14 @@ void OnOverlayGridTapped(object? sender, EventArgs e)
12011227
{
12021228
if (_isSheetOpen)
12031229
{
1204-
Close();
1230+
if(CollapseOnOverlayTap)
1231+
{
1232+
State = BottomSheetState.Collapsed;
1233+
}
1234+
else
1235+
{
1236+
Close();
1237+
}
12051238
}
12061239
}
12071240

@@ -1524,7 +1557,7 @@ void UpdateStateChanged(BottomSheetState oldState, BottomSheetState newState)
15241557
_overlayGrid.IsVisible = (State is BottomSheetState.Collapsed) ? false : IsModal;
15251558
}
15261559

1527-
OnStateChanged(_stateChangedEventArgs);
1560+
OnStateChanged(_stateChangedEventArgs);
15281561
}
15291562
}
15301563

@@ -1616,7 +1649,7 @@ double GetCollapsedPosition()
16161649
_overlayGrid.IsVisible = false;
16171650
}
16181651

1619-
return targetPosition;
1652+
return targetPosition;
16201653
}
16211654

16221655
/// <summary>
@@ -1767,7 +1800,7 @@ bool ShouldRestrictMovement(double newTranslationY, double diffY)
17671800
return false;
17681801
}
17691802

1770-
bool isHalfExpandedAndRestricted = State is BottomSheetState.HalfExpanded &&
1803+
bool isHalfExpandedAndRestricted = State is BottomSheetState.HalfExpanded &&
17711804
AllowedState is BottomSheetAllowedState.HalfExpanded &&
17721805
_bottomSheet.TranslationY > newTranslationY;
17731806

maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Navigation/SfBottomSheetUnitTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,18 @@ public void CollapsedHeight(double input, double expected)
450450
Assert.Equal(expected, actual);
451451
}
452452

453+
[Theory]
454+
[InlineData(true, true)]
455+
[InlineData(false, false)]
456+
public void CollapseOnOverlayTap(bool input, bool expected)
457+
{
458+
_bottomSheet.CollapseOnOverlayTap = input;
459+
460+
var actual = _bottomSheet.CollapseOnOverlayTap;
461+
462+
Assert.Equal(expected, actual);
463+
}
464+
453465
#endregion
454466

455467
#region Internal Properties

0 commit comments

Comments
 (0)