Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions src/BootstrapBlazor.Server/Components/Samples/Drawers.razor
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
</Drawer>
</DemoBlock>

<DemoBlock Title="@Localizer["BodyScrollTitle"]" Introduction="@Localizer["BodyScrollIntro"]" Name="BodyScroll">
<section ignore>
<button type="button" class="btn btn-primary" @onclick="@OpenBodyScrollDrawer">@Localizer["Open"]</button>
</section>
<Drawer Placement="Placement.Left" @bind-IsOpen="@IsBodyScrollOpen" BodyScroll="true">
<div class="d-flex justify-content-center align-items-center flex-column" style="height: 290px;">
<p>@Localizer["Content"]</p>
<button type="button" class="btn btn-primary" @onclick="@(e => IsBodyScrollOpen = false)">@Localizer["Close"]</button>
</div>
</Drawer>
</DemoBlock>

<DemoBlock Title="@Localizer["DrawerServiceTitle"]" Introduction="@Localizer["DrawerServiceIntro"]" Name="DrawerService">
<Button OnClickWithoutRender="@DrawerServiceShow" Text="@Localizer["Open"]"></Button>
</DemoBlock>
Expand Down
12 changes: 12 additions & 0 deletions src/BootstrapBlazor.Server/Components/Samples/Drawers.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ private void OpenDrawer()

private void OpenKeyboardDrawer() => IsKeyboardOpen = true;

private bool IsBodyScrollOpen { get; set; }

private void OpenBodyScrollDrawer() => IsBodyScrollOpen = true;

private async Task DrawerServiceShow() => await DrawerService.Show(new DrawerOption()
{
Placement = Placement.Right,
Expand Down Expand Up @@ -137,6 +141,14 @@ private static AttributeItem[] GetAttributes() =>
DefaultValue = " — "
},
new()
{
Name = "BodyScroll",
Description = "Where the enable body scrolling when drawer is shown",
Type = "bool",
ValueList = "true|false",
DefaultValue = "false"
},
new()
{
Name = "ChildContent",
Description = "Subassembly",
Expand Down
2 changes: 2 additions & 0 deletions src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@
"DrawerServiceIntro": "Open the drawer pop-up window by calling the <code>DrawerService</code> service",
"IsKeyboardTitle": "ESC",
"IsKeyboardIntro": "By default, the component uses the <kbd>ESC</kbd> key to close the drawer popup. You can enable this function by <code>IsKeyboard=\"true\"</code>",
"BodyScrollTitle": "Body Scroll",
"BodyScrollIntro": "By setting <code>BodyScroll</code>, you can control whether scrolling is allowed when the drawer pop-up window is displayed. <code>Body</code> is false by default, scrolling is not allowed",
"Open": "click me to open",
"Content": "The layout, components, etc. in the drawer are fully customizable",
"Close": "close the drawer",
Expand Down
2 changes: 2 additions & 0 deletions src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,8 @@
"DrawerServiceIntro": "通过调用 <code>DrawerService</code> 服务打开抽屉弹窗",
"IsKeyboardTitle": "ESC 按键支持",
"IsKeyboardIntro": "组件默认使用 <kbd>ESC</kbd> 按键关闭抽屉弹窗,可通过 <code>IsKeyboard=\"true\"</code> 开启此功能",
"BodyScrollTitle": "页面滚动",
"BodyScrollIntro": "通过设置 <code>BodyScroll</code> 控制抽屉弹窗显示时是否允许滚动 <code>Body</code> 默认 false 不允许滚动",
"Open": "点我打开",
"Content": "抽屉内布局、组件等完全可以自定义",
"Close": "关闭抽屉",
Expand Down
3 changes: 2 additions & 1 deletion src/BootstrapBlazor/Components/Drawer/Drawer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
@inherits BootstrapModuleComponentBase
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]

<div @attributes="@AdditionalAttributes" tabindex="-1" class="@ClassString" style="@StyleString" id="@Id" data-bb-keyboard="@KeyboardString">
<div @attributes="@AdditionalAttributes" tabindex="-1" class="@ClassString" style="@StyleString" id="@Id"
data-bb-keyboard="@KeyboardString" data-bb-scroll="@BodyScrollString">
@if (ShowBackdrop)
{
<div tabindex="-1" class="drawer-backdrop" @onclick="@OnContainerClick">
Expand Down
8 changes: 8 additions & 0 deletions src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,16 @@ public partial class Drawer
[Parameter]
public bool IsKeyboard { get; set; }

/// <summary>
/// 获得/设置 抽屉显示时是否允许滚动 body 默认为 false 不滚动
/// </summary>
[Parameter]
public bool BodyScroll { get; set; }

private string? KeyboardString => IsKeyboard ? "true" : null;

private string? BodyScrollString => BodyScroll ? "true" : null;

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand Down
8 changes: 6 additions & 2 deletions src/BootstrapBlazor/Components/Drawer/Drawer.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,17 @@ export function execute(id, open) {
}
else {
el.classList.remove('show')
body.classList.remove('overflow-hidden')
body.classList.remove('drawer-overflow-hidden')
}
}

if (open) {
el.classList.add('show')
body.classList.add('overflow-hidden')

const scroll = el.getAttribute('data-bb-scroll') === "true";
if (scroll === false) {
body.classList.add('drawer-overflow-hidden');
}
requestAnimationFrame(show)
}
else if (el.classList.contains('show')) {
Expand Down
4 changes: 4 additions & 0 deletions src/BootstrapBlazor/Components/Drawer/Drawer.razor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,7 @@
}
}
}

.drawer-overflow-hidden {
overflow: hidden;
}
1 change: 1 addition & 0 deletions src/BootstrapBlazor/Components/Drawer/DrawerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private Dictionary<string, object> GetParameters(DrawerOption option)
{
[nameof(Drawer.IsOpen)] = true,
[nameof(Drawer.IsKeyboard)] = option.IsKeyboard,
[nameof(Drawer.BodyScroll)] = option.BodyScroll,
[nameof(Drawer.IsBackdrop)] = option.IsBackdrop,
[nameof(Drawer.ShowBackdrop)] = option.ShowBackdrop,
[nameof(Drawer.Placement)] = option.Placement,
Expand Down
5 changes: 5 additions & 0 deletions src/BootstrapBlazor/Components/Drawer/DrawerOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class DrawerOption
/// </summary>
public bool ShowBackdrop { get; set; } = true;

/// <summary>
/// 获得/设置 抽屉显示时是否允许滚动 body 默认为 false 不滚动
/// </summary>
public bool BodyScroll { get; set; }

/// <summary>
/// 获得/设置 组件出现位置 默认显示在 Left 位置
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions test/UnitTest/Components/DrawerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ public void IsKeyboard_Ok()
cut.DoesNotContain("data-bb-keyboard=\"true\"");
}

[Fact]
public void BodyScroll_Ok()
{
var cut = Context.RenderComponent<Drawer>(builder =>
{
builder.Add(a => a.BodyScroll, true);
builder.Add(a => a.ChildContent, s =>
{
s.OpenComponent<Button>(0);
s.CloseComponent();
});
});
cut.Contains("data-bb-scroll=\"true\"");
}

class MockContent : ComponentBase
{
[CascadingParameter(Name = "BodyContext")]
Expand Down
3 changes: 2 additions & 1 deletion test/UnitTest/Services/DrawerServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public async Task Show_Ok()
Placement = Placement.Bottom,
ShowBackdrop = true,
BodyContext = "test-body-context",
IsKeyboard = true
IsKeyboard = true,
BodyScroll = true
};
var service = Context.Services.GetRequiredService<DrawerService>();
var cut = Context.RenderComponent<BootstrapBlazorRoot>();
Expand Down