Skip to content

Commit 0c204c4

Browse files
ArgoZhangice6
andauthored
feat(Drawer): add BodyScroll parameter (#5311)
* feat: 增加 BodyScroll 参数 * doc: 增加示例 * test: 增加单元测试 * doc: 增加示例代码资源文件 Co-Authored-By: ice6 <[email protected]>
1 parent 93817bc commit 0c204c4

File tree

12 files changed

+71
-4
lines changed

12 files changed

+71
-4
lines changed

src/BootstrapBlazor.Server/Components/Samples/Drawers.razor

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@
5959
</Drawer>
6060
</DemoBlock>
6161

62+
<DemoBlock Title="@Localizer["BodyScrollTitle"]" Introduction="@Localizer["BodyScrollIntro"]" Name="BodyScroll">
63+
<section ignore>
64+
<button type="button" class="btn btn-primary" @onclick="@OpenBodyScrollDrawer">@Localizer["Open"]</button>
65+
</section>
66+
<Drawer Placement="Placement.Left" @bind-IsOpen="@IsBodyScrollOpen" BodyScroll="true">
67+
<div class="d-flex justify-content-center align-items-center flex-column" style="height: 290px;">
68+
<p>@Localizer["Content"]</p>
69+
<button type="button" class="btn btn-primary" @onclick="@(e => IsBodyScrollOpen = false)">@Localizer["Close"]</button>
70+
</div>
71+
</Drawer>
72+
</DemoBlock>
73+
6274
<DemoBlock Title="@Localizer["DrawerServiceTitle"]" Introduction="@Localizer["DrawerServiceIntro"]" Name="DrawerService">
6375
<Button OnClickWithoutRender="@DrawerServiceShow" Text="@Localizer["Open"]"></Button>
6476
</DemoBlock>

src/BootstrapBlazor.Server/Components/Samples/Drawers.razor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ private void OpenDrawer()
5757

5858
private void OpenKeyboardDrawer() => IsKeyboardOpen = true;
5959

60+
private bool IsBodyScrollOpen { get; set; }
61+
62+
private void OpenBodyScrollDrawer() => IsBodyScrollOpen = true;
63+
6064
private async Task DrawerServiceShow() => await DrawerService.Show(new DrawerOption()
6165
{
6266
Placement = Placement.Right,
@@ -137,6 +141,14 @@ private static AttributeItem[] GetAttributes() =>
137141
DefaultValue = " — "
138142
},
139143
new()
144+
{
145+
Name = "BodyScroll",
146+
Description = "Where the enable body scrolling when drawer is shown",
147+
Type = "bool",
148+
ValueList = "true|false",
149+
DefaultValue = "false"
150+
},
151+
new()
140152
{
141153
Name = "ChildContent",
142154
Description = "Subassembly",

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,8 @@
894894
"DrawerServiceIntro": "Open the drawer pop-up window by calling the <code>DrawerService</code> service",
895895
"IsKeyboardTitle": "ESC",
896896
"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>",
897+
"BodyScrollTitle": "Body Scroll",
898+
"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",
897899
"Open": "click me to open",
898900
"Content": "The layout, components, etc. in the drawer are fully customizable",
899901
"Close": "close the drawer",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,8 @@
894894
"DrawerServiceIntro": "通过调用 <code>DrawerService</code> 服务打开抽屉弹窗",
895895
"IsKeyboardTitle": "ESC 按键支持",
896896
"IsKeyboardIntro": "组件默认使用 <kbd>ESC</kbd> 按键关闭抽屉弹窗,可通过 <code>IsKeyboard=\"true\"</code> 开启此功能",
897+
"BodyScrollTitle": "页面滚动",
898+
"BodyScrollIntro": "通过设置 <code>BodyScroll</code> 控制抽屉弹窗显示时是否允许滚动 <code>Body</code> 默认 false 不允许滚动",
897899
"Open": "点我打开",
898900
"Content": "抽屉内布局、组件等完全可以自定义",
899901
"Close": "关闭抽屉",

src/BootstrapBlazor/Components/Drawer/Drawer.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
@inherits BootstrapModuleComponentBase
33
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]
44

5-
<div @attributes="@AdditionalAttributes" tabindex="-1" class="@ClassString" style="@StyleString" id="@Id" data-bb-keyboard="@KeyboardString">
5+
<div @attributes="@AdditionalAttributes" tabindex="-1" class="@ClassString" style="@StyleString" id="@Id"
6+
data-bb-keyboard="@KeyboardString" data-bb-scroll="@BodyScrollString">
67
@if (ShowBackdrop)
78
{
89
<div tabindex="-1" class="drawer-backdrop" @onclick="@OnContainerClick">

src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,16 @@ public partial class Drawer
125125
[Parameter]
126126
public bool IsKeyboard { get; set; }
127127

128+
/// <summary>
129+
/// 获得/设置 抽屉显示时是否允许滚动 body 默认为 false 不滚动
130+
/// </summary>
131+
[Parameter]
132+
public bool BodyScroll { get; set; }
133+
128134
private string? KeyboardString => IsKeyboard ? "true" : null;
129135

136+
private string? BodyScrollString => BodyScroll ? "true" : null;
137+
130138
/// <summary>
131139
/// <inheritdoc/>
132140
/// </summary>

src/BootstrapBlazor/Components/Drawer/Drawer.razor.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,17 @@ export function execute(id, open) {
115115
}
116116
else {
117117
el.classList.remove('show')
118-
body.classList.remove('overflow-hidden')
118+
body.classList.remove('drawer-overflow-hidden')
119119
}
120120
}
121121

122122
if (open) {
123123
el.classList.add('show')
124-
body.classList.add('overflow-hidden')
124+
125+
const scroll = el.getAttribute('data-bb-scroll') === "true";
126+
if (scroll === false) {
127+
body.classList.add('drawer-overflow-hidden');
128+
}
125129
requestAnimationFrame(show)
126130
}
127131
else if (el.classList.contains('show')) {

src/BootstrapBlazor/Components/Drawer/Drawer.razor.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,7 @@
184184
}
185185
}
186186
}
187+
188+
.drawer-overflow-hidden {
189+
overflow: hidden;
190+
}

src/BootstrapBlazor/Components/Drawer/DrawerContainer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ private Dictionary<string, object> GetParameters(DrawerOption option)
6666
{
6767
[nameof(Drawer.IsOpen)] = true,
6868
[nameof(Drawer.IsKeyboard)] = option.IsKeyboard,
69+
[nameof(Drawer.BodyScroll)] = option.BodyScroll,
6970
[nameof(Drawer.IsBackdrop)] = option.IsBackdrop,
7071
[nameof(Drawer.ShowBackdrop)] = option.ShowBackdrop,
7172
[nameof(Drawer.Placement)] = option.Placement,

src/BootstrapBlazor/Components/Drawer/DrawerOption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class DrawerOption
4040
/// </summary>
4141
public bool ShowBackdrop { get; set; } = true;
4242

43+
/// <summary>
44+
/// 获得/设置 抽屉显示时是否允许滚动 body 默认为 false 不滚动
45+
/// </summary>
46+
public bool BodyScroll { get; set; }
47+
4348
/// <summary>
4449
/// 获得/设置 组件出现位置 默认显示在 Left 位置
4550
/// </summary>

0 commit comments

Comments
 (0)