Skip to content

feat(LayoutSidebar): add LayoutSplitebar component #5438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 24, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
<span class="sidebar-text">Bootstrap Blazor</span>
</div>
<NavMenu />
<div class="sidebar-bar">
<div class="sidebar-body"></div>
</div>
<LayoutSidebar Min="250" Max="380" ContainerSelector=".section"></LayoutSidebar>
</aside>

<section class="main">
Expand Down
18 changes: 8 additions & 10 deletions src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
padding: var(--bb-main-pading);
}

.section {
--bb-sidebar-width: 0;
}

.sidebar-title {
height: 50px;
align-items: center;
Expand Down Expand Up @@ -78,29 +74,31 @@

@media (min-width: 768px) {
.section {
--bb-sidebar-width: 300px;
--bb-layout-sidebar-width: 300px;
display: flex;
flex-direction: row;
-webkit-font-smoothing: antialiased;
}

.main {
flex: 1;
width: 1%;
min-width: 0;
}

.sidebar-title {
display: flex;
}

.sidebar {
width: var(--bb-sidebar-width);
width: var(--bb-layout-sidebar-width);
height: calc(100vh);
position: sticky;
top: 0;
border-right: solid 1px var(--bs-border-color);
margin-top: calc(var(--bs-header-height) * -1);
}

.main {
width: calc(100% - var(--bb-sidebar-width));
}

.sidebar-bar {
display: block;
}
Expand Down
26 changes: 1 addition & 25 deletions src/BootstrapBlazor.Server/Components/Layout/NavMenu.razor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import Data from "../../_content/BootstrapBlazor/modules/data.js"
import Drag from "../../_content/BootstrapBlazor/modules/drag.js"
import EventHandler from "../../_content/BootstrapBlazor/modules/event-handler.js"

export function init(id) {
const navmenu = {
navbar: document.querySelector('.navbar-toggler'),
menu: document.querySelector('.sidebar-content'),
bar: document.querySelector('.sidebar-body')
menu: document.querySelector('.sidebar-content')
}
Data.set(id, navmenu)

Expand All @@ -20,27 +18,6 @@ export function init(id) {
navmenu.menu.classList.remove('show')
}
})
let originX = 0
let section = document.querySelector('section');
let width = 0
Drag.drag(navmenu.bar,
e => {
navmenu.bar.classList.add('drag')
width = parseInt(getComputedStyle(section).getPropertyValue('--bb-sidebar-width'))
originX = e.clientX || e.touches[0].clientX
},
e => {
const eventX = e.clientX || (e.touches.length > 0 && e.touches[0].clientX)
const moveX = eventX - originX
const newWidth = width + moveX
if (newWidth >= 250 && newWidth <= 380) {
section.style.setProperty('--bb-sidebar-width', `${newWidth}px`)
}
},
e => {
navmenu.bar.classList.remove('drag')
}
)
}

export function dispose(id) {
Expand All @@ -49,5 +26,4 @@ export function dispose(id) {

EventHandler.off(data.navbar, 'click');
EventHandler.off(data.menu, 'click', '.nav-link');
Drag.dispose(data.bar)
}
38 changes: 18 additions & 20 deletions src/BootstrapBlazor/Components/Layout/Layout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@
}
else if (IsFullSide)
{
<aside class="@SideClassString" style="@SideStyleString">
@if (Side != null)
{
@Side
}
@if (Menus != null)
{
@RenderMenu
}
</aside>
@RenderSide
<section class="layout-right">
@if (Header != null)
{
Expand All @@ -51,16 +42,7 @@
@RenderHeader(ShowCollapseBar)
}
<section class="has-sidebar">
<aside class="@SideClassString" style="@SideStyleString">
@if (Side != null)
{
@Side
}
@if (Menus != null)
{
@RenderMenu
}
</aside>
@RenderSide
@RenderMain
</section>
@if (ShowFooter && Footer != null)
Expand Down Expand Up @@ -100,6 +82,22 @@
@Header
</header>;

RenderFragment RenderSide =>
@<aside class="@SideClassString" style="@SideStyleString">
@if (Side != null)
{
@Side
}
@if (ShowSplitBar)
{

Check warning on line 92 in src/BootstrapBlazor/Components/Layout/Layout.razor

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Components/Layout/Layout.razor#L92

Added line #L92 was not covered by tests
<LayoutSidebar></LayoutSidebar>
}

Check warning on line 94 in src/BootstrapBlazor/Components/Layout/Layout.razor

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Components/Layout/Layout.razor#L94

Added line #L94 was not covered by tests
@if (Menus != null)
{
@RenderMenu
}
</aside>;

RenderFragment RenderMenu =>
@<div class="layout-menu">
@if (IsFixedTabHeader || IsFullSide)
Expand Down
7 changes: 7 additions & 0 deletions src/BootstrapBlazor/Components/Layout/Layout.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public partial class Layout : IHandlerException
[Parameter]
public RenderFragment? Side { get; set; }

/// <summary>
/// 获得/设置 是否显示分割栏 默认 false 不显示
/// 仅在 左右布局时有效
/// </summary>
[Parameter]
public bool ShowSplitBar { get; set; }

/// <summary>
/// 获得/设置 NotAuthorized 模板 默认 null NET6.0/7.0 有效
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@namespace BootstrapBlazor.Components
@inherits BootstrapModuleComponentBase
@attribute [BootstrapModuleAutoLoader("Layout/LayoutSidebar.razor.js")]

<div id="@Id" class="layout-sidebar"
data-bb-min="@_minWidthString" data-bb-max="@_maxWidthString" data-bb-selector="@ContainerSelector">
<div class="layout-sidebar-body"></div>
</div>
35 changes: 35 additions & 0 deletions src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone

namespace BootstrapBlazor.Components;

/// <summary>
/// LayoutSidebar 组件
/// </summary>
public partial class LayoutSidebar
{
/// <summary>
/// 获得/设置 容器选择器 默认 null 未设置
/// 组件拖动后设置容器 style="--bb-layout-sidebar-width: 200px;" 用于宽度调整
/// </summary>
[Parameter]
public string? ContainerSelector { get; set; }

Check warning on line 18 in src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs#L18

Added line #L18 was not covered by tests

/// <summary>
/// 获得/设置 最小宽度 默认 null 未设置
/// </summary>
[Parameter]
public int? Min { get; set; }

Check warning on line 24 in src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs#L24

Added line #L24 was not covered by tests

/// <summary>
/// 获得/设置 最大宽度 默认 null 未设置
/// </summary>
[Parameter]
public int? Max { get; set; }

Check warning on line 30 in src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs

View check run for this annotation

Codecov / codecov/patch

src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs#L30

Added line #L30 was not covered by tests

private string? _minWidthString => Min.HasValue ? $"{Min}" : null;

private string? _maxWidthString => Max.HasValue ? $"{Max}" : null;
}
45 changes: 45 additions & 0 deletions src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Drag from "../../modules/drag.js"
import Data from "../../modules/data.js"

export function init(id) {
const el = document.getElementById(id);
if (el === null) {
return;
}

const min = parseFloat(el.getAttribute("data-bb-min") ?? "0");
const max = parseFloat(el.getAttribute("data-bb-max") ?? "0");
const selector = el.getAttribute("data-bb-selector");
const section = document.querySelector(selector);
const bar = el.querySelector(".layout-sidebar-body");
let originX = 0;
let width = 0;
Drag.drag(bar,
e => {
bar.classList.add('drag')
width = parseInt(getComputedStyle(section).getPropertyValue('--bb-layout-sidebar-width'))
originX = e.clientX || e.touches[0].clientX
},
e => {
const eventX = e.clientX || (e.touches.length > 0 && e.touches[0].clientX)
const moveX = eventX - originX
const newWidth = width + moveX
if (newWidth >= min && newWidth <= max) {
section.style.setProperty('--bb-layout-sidebar-width', `${newWidth}px`)
}
},
e => {
bar.classList.remove('drag')
}
)
}

export function dispose(id) {
const el = document.getElementById(id);
if (el) {
const bar = el.querySelector(".layout-sidebar-body");
if (bar) {
Drag.dispose(bar);
}
}
}
32 changes: 32 additions & 0 deletions src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.layout-sidebar {
width: 1px;
position: absolute;
top: 0;
right: -1px;
bottom: 0;
background-color: var(--bs-border-color);
display: none;

.layout-sidebar-body {
position: absolute;
inset: 0px -2px;
cursor: col-resize;
background-color: transparent;
border-radius: 4px;

&:hover {
background-color: var(--bb-sidebar-body-hover-bg);
}

&.drag,
&.drag:hover {
background-color: var(--bb-sidebar-body-drag-hover-bg);
}
}
}

@media(min-width: 768px) {
.layout-sidebar {
display: block;
}
}
1 change: 1 addition & 0 deletions src/BootstrapBlazor/wwwroot/scss/components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
@import "../../Components/Input/FloatingLabel.razor.scss";
@import "../../Components/IpAddress/IpAddress.razor.scss";
@import "../../Components/Layout/Layout.razor.scss";
@import "../../Components/Layout/LayoutSidebar.razor.scss";
@import "../../Components/Light/Light.razor.scss";
@import "../../Components/ListGroup/ListGroup.razor.scss";
@import "../../Components/ListView/ListView.razor.scss";
Expand Down