Skip to content

Commit 69179e7

Browse files
authored
feat(LayoutSidebar): add LayoutSidebar component (#5438)
* feat: 增加 ShowSidebar 参数 控制是否显示 LayoutSidebar 组件 * feat: 增加 LayoutSidebar 组件 * doc: 更新拖动栏组件 * chore: 增加样式 * refactor: 更新参数 * refactor: 精简代码 * style: 更新样式
1 parent 4759e19 commit 69179e7

File tree

10 files changed

+156
-58
lines changed

10 files changed

+156
-58
lines changed

src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
<span class="sidebar-text">Bootstrap Blazor</span>
1111
</div>
1212
<NavMenu />
13-
<div class="sidebar-bar">
14-
<div class="sidebar-body"></div>
15-
</div>
13+
<LayoutSidebar Min="250" Max="380" ContainerSelector=".section"></LayoutSidebar>
1614
</aside>
1715

1816
<section class="main">

src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor.css

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
padding: var(--bb-main-pading);
33
}
44

5-
.section {
6-
--bb-sidebar-width: 0;
7-
}
8-
95
.sidebar-title {
106
height: 50px;
117
align-items: center;
@@ -78,29 +74,31 @@
7874

7975
@media (min-width: 768px) {
8076
.section {
81-
--bb-sidebar-width: 300px;
77+
--bb-layout-sidebar-width: 300px;
8278
display: flex;
8379
flex-direction: row;
8480
-webkit-font-smoothing: antialiased;
8581
}
8682

83+
.main {
84+
flex: 1;
85+
width: 1%;
86+
min-width: 0;
87+
}
88+
8789
.sidebar-title {
8890
display: flex;
8991
}
9092

9193
.sidebar {
92-
width: var(--bb-sidebar-width);
94+
width: var(--bb-layout-sidebar-width);
9395
height: calc(100vh);
9496
position: sticky;
9597
top: 0;
9698
border-right: solid 1px var(--bs-border-color);
9799
margin-top: calc(var(--bs-header-height) * -1);
98100
}
99101

100-
.main {
101-
width: calc(100% - var(--bb-sidebar-width));
102-
}
103-
104102
.sidebar-bar {
105103
display: block;
106104
}
Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import Data from "../../_content/BootstrapBlazor/modules/data.js"
2-
import Drag from "../../_content/BootstrapBlazor/modules/drag.js"
32
import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js"
43

54
export function init(id) {
65
const navmenu = {
76
navbar: document.querySelector('.navbar-toggler'),
8-
menu: document.querySelector('.sidebar-content'),
9-
bar: document.querySelector('.sidebar-body')
7+
menu: document.querySelector('.sidebar-content')
108
}
119
Data.set(id, navmenu)
1210

@@ -20,27 +18,6 @@ export function init(id) {
2018
navmenu.menu.classList.remove('show')
2119
}
2220
})
23-
let originX = 0
24-
let section = document.querySelector('section');
25-
let width = 0
26-
Drag.drag(navmenu.bar,
27-
e => {
28-
navmenu.bar.classList.add('drag')
29-
width = parseInt(getComputedStyle(section).getPropertyValue('--bb-sidebar-width'))
30-
originX = e.clientX || e.touches[0].clientX
31-
},
32-
e => {
33-
const eventX = e.clientX || (e.touches.length > 0 && e.touches[0].clientX)
34-
const moveX = eventX - originX
35-
const newWidth = width + moveX
36-
if (newWidth >= 250 && newWidth <= 380) {
37-
section.style.setProperty('--bb-sidebar-width', `${newWidth}px`)
38-
}
39-
},
40-
e => {
41-
navmenu.bar.classList.remove('drag')
42-
}
43-
)
4421
}
4522

4623
export function dispose(id) {
@@ -49,5 +26,4 @@ export function dispose(id) {
4926

5027
EventHandler.off(data.navbar, 'click');
5128
EventHandler.off(data.menu, 'click', '.nav-link');
52-
Drag.dispose(data.bar)
5329
}

src/BootstrapBlazor/Components/Layout/Layout.razor

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@
2222
}
2323
else if (IsFullSide)
2424
{
25-
<aside class="@SideClassString" style="@SideStyleString">
26-
@if (Side != null)
27-
{
28-
@Side
29-
}
30-
@if (Menus != null)
31-
{
32-
@RenderMenu
33-
}
34-
</aside>
25+
@RenderSide
3526
<section class="layout-right">
3627
@if (Header != null)
3728
{
@@ -51,16 +42,7 @@
5142
@RenderHeader(ShowCollapseBar)
5243
}
5344
<section class="has-sidebar">
54-
<aside class="@SideClassString" style="@SideStyleString">
55-
@if (Side != null)
56-
{
57-
@Side
58-
}
59-
@if (Menus != null)
60-
{
61-
@RenderMenu
62-
}
63-
</aside>
45+
@RenderSide
6446
@RenderMain
6547
</section>
6648
@if (ShowFooter && Footer != null)
@@ -100,6 +82,22 @@
10082
@Header
10183
</header>;
10284

85+
RenderFragment RenderSide =>
86+
@<aside class="@SideClassString" style="@SideStyleString">
87+
@if (Side != null)
88+
{
89+
@Side
90+
}
91+
@if (ShowSplitBar)
92+
{
93+
<LayoutSidebar></LayoutSidebar>
94+
}
95+
@if (Menus != null)
96+
{
97+
@RenderMenu
98+
}
99+
</aside>;
100+
103101
RenderFragment RenderMenu =>
104102
@<div class="layout-menu">
105103
@if (IsFixedTabHeader || IsFullSide)

src/BootstrapBlazor/Components/Layout/Layout.razor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public partial class Layout : IHandlerException
6565
[Parameter]
6666
public RenderFragment? Side { get; set; }
6767

68+
/// <summary>
69+
/// 获得/设置 是否显示分割栏 默认 false 不显示
70+
/// 仅在 左右布局时有效
71+
/// </summary>
72+
[Parameter]
73+
public bool ShowSplitBar { get; set; }
74+
6875
/// <summary>
6976
/// 获得/设置 NotAuthorized 模板 默认 null NET6.0/7.0 有效
7077
/// </summary>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@namespace BootstrapBlazor.Components
2+
@inherits BootstrapModuleComponentBase
3+
@attribute [BootstrapModuleAutoLoader("Layout/LayoutSidebar.razor.js")]
4+
5+
<div id="@Id" class="layout-sidebar"
6+
data-bb-min="@_minWidthString" data-bb-max="@_maxWidthString" data-bb-selector="@ContainerSelector">
7+
<div class="layout-sidebar-body"></div>
8+
</div>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// LayoutSidebar 组件
10+
/// </summary>
11+
public partial class LayoutSidebar
12+
{
13+
/// <summary>
14+
/// 获得/设置 容器选择器 默认 null 未设置
15+
/// 组件拖动后设置容器 style="--bb-layout-sidebar-width: 200px;" 用于宽度调整
16+
/// </summary>
17+
[Parameter]
18+
public string? ContainerSelector { get; set; }
19+
20+
/// <summary>
21+
/// 获得/设置 最小宽度 默认 null 未设置
22+
/// </summary>
23+
[Parameter]
24+
public int? Min { get; set; }
25+
26+
/// <summary>
27+
/// 获得/设置 最大宽度 默认 null 未设置
28+
/// </summary>
29+
[Parameter]
30+
public int? Max { get; set; }
31+
32+
private string? _minWidthString => Min.HasValue ? $"{Min}" : null;
33+
34+
private string? _maxWidthString => Max.HasValue ? $"{Max}" : null;
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Drag from "../../modules/drag.js"
2+
import Data from "../../modules/data.js"
3+
4+
export function init(id) {
5+
const el = document.getElementById(id);
6+
if (el === null) {
7+
return;
8+
}
9+
10+
const min = parseFloat(el.getAttribute("data-bb-min") ?? "0");
11+
const max = parseFloat(el.getAttribute("data-bb-max") ?? "0");
12+
const selector = el.getAttribute("data-bb-selector");
13+
const section = document.querySelector(selector);
14+
const bar = el.querySelector(".layout-sidebar-body");
15+
let originX = 0;
16+
let width = 0;
17+
Drag.drag(bar,
18+
e => {
19+
bar.classList.add('drag')
20+
width = parseInt(getComputedStyle(section).getPropertyValue('--bb-layout-sidebar-width'))
21+
originX = e.clientX || e.touches[0].clientX
22+
},
23+
e => {
24+
const eventX = e.clientX || (e.touches.length > 0 && e.touches[0].clientX)
25+
const moveX = eventX - originX
26+
const newWidth = width + moveX
27+
if (newWidth >= min && newWidth <= max) {
28+
section.style.setProperty('--bb-layout-sidebar-width', `${newWidth}px`)
29+
}
30+
},
31+
e => {
32+
bar.classList.remove('drag')
33+
}
34+
)
35+
}
36+
37+
export function dispose(id) {
38+
const el = document.getElementById(id);
39+
if (el) {
40+
const bar = el.querySelector(".layout-sidebar-body");
41+
if (bar) {
42+
Drag.dispose(bar);
43+
}
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.layout-sidebar {
2+
width: 1px;
3+
position: absolute;
4+
top: 0;
5+
right: -1px;
6+
bottom: 0;
7+
background-color: var(--bs-border-color);
8+
display: none;
9+
10+
.layout-sidebar-body {
11+
position: absolute;
12+
inset: 0px -2px;
13+
cursor: col-resize;
14+
background-color: transparent;
15+
border-radius: 4px;
16+
17+
&:hover {
18+
background-color: var(--bb-sidebar-body-hover-bg);
19+
}
20+
21+
&.drag,
22+
&.drag:hover {
23+
background-color: var(--bb-sidebar-body-drag-hover-bg);
24+
}
25+
}
26+
}
27+
28+
@media(min-width: 768px) {
29+
.layout-sidebar {
30+
display: block;
31+
}
32+
}

src/BootstrapBlazor/wwwroot/scss/components.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
@import "../../Components/Input/FloatingLabel.razor.scss";
5858
@import "../../Components/IpAddress/IpAddress.razor.scss";
5959
@import "../../Components/Layout/Layout.razor.scss";
60+
@import "../../Components/Layout/LayoutSidebar.razor.scss";
6061
@import "../../Components/Light/Light.razor.scss";
6162
@import "../../Components/ListGroup/ListGroup.razor.scss";
6263
@import "../../Components/ListView/ListView.razor.scss";

0 commit comments

Comments
 (0)