diff --git a/src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor b/src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor index 507146aa844..ef2ce092a08 100644 --- a/src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor +++ b/src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor @@ -10,9 +10,7 @@ Bootstrap Blazor - +
diff --git a/src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor.css b/src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor.css index 88763b3961d..ba65bb28d5f 100644 --- a/src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor.css +++ b/src/BootstrapBlazor.Server/Components/Layout/MainLayout.razor.css @@ -2,10 +2,6 @@ padding: var(--bb-main-pading); } -.section { - --bb-sidebar-width: 0; -} - .sidebar-title { height: 50px; align-items: center; @@ -78,18 +74,24 @@ @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; @@ -97,10 +99,6 @@ margin-top: calc(var(--bs-header-height) * -1); } - .main { - width: calc(100% - var(--bb-sidebar-width)); - } - .sidebar-bar { display: block; } diff --git a/src/BootstrapBlazor.Server/Components/Layout/NavMenu.razor.js b/src/BootstrapBlazor.Server/Components/Layout/NavMenu.razor.js index 854cefcec85..c9a462db93b 100644 --- a/src/BootstrapBlazor.Server/Components/Layout/NavMenu.razor.js +++ b/src/BootstrapBlazor.Server/Components/Layout/NavMenu.razor.js @@ -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) @@ -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) { @@ -49,5 +26,4 @@ export function dispose(id) { EventHandler.off(data.navbar, 'click'); EventHandler.off(data.menu, 'click', '.nav-link'); - Drag.dispose(data.bar) } diff --git a/src/BootstrapBlazor/Components/Layout/Layout.razor b/src/BootstrapBlazor/Components/Layout/Layout.razor index 4084ac060d7..a8fc664ee77 100644 --- a/src/BootstrapBlazor/Components/Layout/Layout.razor +++ b/src/BootstrapBlazor/Components/Layout/Layout.razor @@ -22,16 +22,7 @@ } else if (IsFullSide) { - + @RenderSide
@if (Header != null) { @@ -51,16 +42,7 @@ @RenderHeader(ShowCollapseBar) }
- + @RenderSide @RenderMain
@if (ShowFooter && Footer != null) @@ -100,6 +82,22 @@ @Header ; + RenderFragment RenderSide => + @; + RenderFragment RenderMenu => @
@if (IsFixedTabHeader || IsFullSide) diff --git a/src/BootstrapBlazor/Components/Layout/Layout.razor.cs b/src/BootstrapBlazor/Components/Layout/Layout.razor.cs index bcf77282967..2e59a3738f8 100644 --- a/src/BootstrapBlazor/Components/Layout/Layout.razor.cs +++ b/src/BootstrapBlazor/Components/Layout/Layout.razor.cs @@ -65,6 +65,13 @@ public partial class Layout : IHandlerException [Parameter] public RenderFragment? Side { get; set; } + /// + /// 获得/设置 是否显示分割栏 默认 false 不显示 + /// 仅在 左右布局时有效 + /// + [Parameter] + public bool ShowSplitBar { get; set; } + /// /// 获得/设置 NotAuthorized 模板 默认 null NET6.0/7.0 有效 /// diff --git a/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor b/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor new file mode 100644 index 00000000000..4c78c2966d6 --- /dev/null +++ b/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor @@ -0,0 +1,8 @@ +@namespace BootstrapBlazor.Components +@inherits BootstrapModuleComponentBase +@attribute [BootstrapModuleAutoLoader("Layout/LayoutSidebar.razor.js")] + +
+
+
diff --git a/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs b/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs new file mode 100644 index 00000000000..af5b4b49975 --- /dev/null +++ b/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.cs @@ -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; + +/// +/// LayoutSidebar 组件 +/// +public partial class LayoutSidebar +{ + /// + /// 获得/设置 容器选择器 默认 null 未设置 + /// 组件拖动后设置容器 style="--bb-layout-sidebar-width: 200px;" 用于宽度调整 + /// + [Parameter] + public string? ContainerSelector { get; set; } + + /// + /// 获得/设置 最小宽度 默认 null 未设置 + /// + [Parameter] + public int? Min { get; set; } + + /// + /// 获得/设置 最大宽度 默认 null 未设置 + /// + [Parameter] + public int? Max { get; set; } + + private string? _minWidthString => Min.HasValue ? $"{Min}" : null; + + private string? _maxWidthString => Max.HasValue ? $"{Max}" : null; +} diff --git a/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.js b/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.js new file mode 100644 index 00000000000..a0c4b4a6c32 --- /dev/null +++ b/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.js @@ -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); + } + } +} diff --git a/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.scss b/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.scss new file mode 100644 index 00000000000..d340cfcbab2 --- /dev/null +++ b/src/BootstrapBlazor/Components/Layout/LayoutSidebar.razor.scss @@ -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; + } +} diff --git a/src/BootstrapBlazor/wwwroot/scss/components.scss b/src/BootstrapBlazor/wwwroot/scss/components.scss index 8beef795781..a5bc79b6a78 100644 --- a/src/BootstrapBlazor/wwwroot/scss/components.scss +++ b/src/BootstrapBlazor/wwwroot/scss/components.scss @@ -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";