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
27 changes: 11 additions & 16 deletions src/BootstrapBlazor.Server/Components/Samples/Drawers.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
</section>

<Drawer Placement="@DrawerAlign" @bind-IsOpen="@IsOpen" AllowResize="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 => IsOpen = false)">@Localizer["Close"]</button>
</div>
@RenderDrawerContent
</Drawer>
</DemoBlock>

Expand All @@ -40,10 +37,7 @@
<button type="button" class="btn btn-primary" @onclick="@OpenNoBackdropDrawer">@Localizer["Open"]</button>
</section>
<Drawer Placement="Placement.Left" @bind-IsOpen="@IsShowBackdropOpen" ShowBackdrop="false">
<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 => IsShowBackdropOpen = false)">@Localizer["Close"]</button>
</div>
@RenderDrawerContent
</Drawer>
</DemoBlock>

Expand All @@ -52,10 +46,7 @@
<button type="button" class="btn btn-primary" @onclick="@OpenKeyboardDrawer">@Localizer["Open"]</button>
</section>
<Drawer Placement="Placement.Left" @bind-IsOpen="@IsKeyboardOpen" IsKeyboard="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 => IsKeyboardOpen = false)">@Localizer["Close"]</button>
</div>
@RenderDrawerContent
</Drawer>
</DemoBlock>

Expand All @@ -64,10 +55,7 @@
<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>
@RenderDrawerContent
</Drawer>
</DemoBlock>

Expand All @@ -79,3 +67,10 @@
</DemoBlock>

<AttributeTable Type="typeof(Drawer)" />

@code {
private RenderFragment RenderDrawerContent =>@<div class="d-flex justify-content-center align-items-center flex-column" style="height: 290px;">
<p>@Localizer["Content"]</p>
<DialogCloseButton Color="Color.Primary" Text="@Localizer["Close"]" />
</div>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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([email protected]) Website: https://www.blazor.zone
Expand Down Expand Up @@ -64,7 +64,7 @@ private void OpenDrawer()
private async Task DrawerServiceShow() => await DrawerService.Show(new DrawerOption()
{
Placement = Placement.Right,
ChildContent = builder => builder.AddContent(0, "Test"),
ChildContent = RenderDrawerContent,
ShowBackdrop = true,
AllowResize = true,
IsBackdrop = true,
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Drawer/Drawer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@inherits BootstrapModuleComponentBase
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]

<div @attributes="@AdditionalAttributes" class="@ClassString" style="@StyleString" id="@Id"
<div @attributes="@AdditionalAttributes" class="@ClassString" style="@StyleString" id="@Id" tabindex="-1"
data-bb-keyboard="@KeyboardString" data-bb-scroll="@BodyScrollString">
@if (ShowBackdrop)
{
Expand Down
62 changes: 38 additions & 24 deletions src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,44 +216,58 @@ private RenderFragment RenderBackdrop() => builder =>
/// <para lang="zh">点击背景遮罩方法</para>
/// <para lang="en">Backdrop Click Method</para>
/// </summary>
public async Task OnContainerClick()
{
if (OnClickBackdrop != null)
{
await OnClickBackdrop();
}
await Close();
}
public Task OnContainerClick() => CloseAsync(true);

private bool _isClosing;

/// <summary>
/// <para lang="zh">关闭抽屉方法</para>
/// <para lang="en">Close Drawer Method</para>
/// </summary>
[JSInvokable]
public async Task Close()
public Task Close() => CloseAsync(false);

private async Task CloseAsync(bool clickBackdrop)
{
if (OnClosingAsync != null && !await OnClosingAsync())
if (_isClosing || !IsOpen)
{
return;
}

IsOpen = false;
var animation = await InvokeAsync<bool>("execute", Id, false);
if (animation)
{
await Task.Delay(300);
}
if (OnCloseAsync != null)
{
await OnCloseAsync();
}
if (IsOpenChanged.HasDelegate)
_isClosing = true;
try
{
await IsOpenChanged.InvokeAsync(IsOpen);
if (clickBackdrop && OnClickBackdrop != null)
{
await OnClickBackdrop();
}
if (OnClosingAsync != null && !await OnClosingAsync())
{
return;
}

IsOpen = false;
var animation = await InvokeAsync<bool>("execute", Id, false);
if (animation)
{
await Task.Delay(300);
}
if (OnCloseAsync != null)
{
Comment thread
ArgoZhang marked this conversation as resolved.
Resolved
await OnCloseAsync();
}
if (IsOpenChanged.HasDelegate)
{
await IsOpenChanged.InvokeAsync(IsOpen);
}
else
{
StateHasChanged();
}
}
else
finally
{
StateHasChanged();
_isClosing = false;
}
}
}
61 changes: 47 additions & 14 deletions src/BootstrapBlazor/Components/Drawer/Drawer.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import Data from "../../modules/data.js"
import Drag from "../../modules/drag.js"
import EventHandler from "../../modules/event-handler.js"

const restoreBodyScroll = (el, body) => {
const hasOpenDrawer = [...document.querySelectorAll('.drawer.show')]
.some(drawer => drawer !== el && drawer.getAttribute('data-bb-scroll') !== 'true');
if (!hasOpenDrawer) {
body.classList.remove('drawer-overflow-hidden');
}
}

const initDrag = el => {
let originX = 0
let originY = 0;
Expand Down Expand Up @@ -76,7 +84,9 @@ export function init(id, invoke, method) {

const dw = {
el,
body: document.querySelector('body')
body: document.querySelector('body'),
open: null,
animationId: null
}
Data.set(id, dw)

Expand All @@ -99,24 +109,47 @@ export function execute(id, open) {
const drawerBackdrop = el.querySelector('.drawer-backdrop')
const animationFrame = getComputedStyle(drawerBody).getPropertyValue('transition') !== 'none';

if (open) {
const scroll = el.getAttribute('data-bb-scroll') === "true";
if (scroll === false) {
body.classList.add('drawer-overflow-hidden');
}
else {
restoreBodyScroll(el, body);
}
}

// Renders in the same state must not restart a pending animation.
if (dw.animationId && dw.open === open) {
return animationFrame;
}
dw.open = open;

if (dw.animationId) {
cancelAnimationFrame(dw.animationId);
dw.animationId = null;
}

let start = void 0
const show = ts => {
if (start === void 0) {
start = ts
}
const elapsed = ts - start;
if (elapsed < 20) {
requestAnimationFrame(show);
dw.animationId = requestAnimationFrame(show);
}
else {
showDrawer();
}
}

const showDrawer = () => {
dw.animationId = null;
drawerBody.classList.add('show');
if (drawerBackdrop) {
drawerBackdrop.classList.add('show');
const backdrop = el.querySelector('.drawer-backdrop');
if (backdrop) {
backdrop.classList.add('show');
}
el.focus();
}
Expand All @@ -127,23 +160,20 @@ export function execute(id, open) {
}
const elapsed = ts - start;
if (elapsed < 320) {
requestAnimationFrame(hide);
dw.animationId = requestAnimationFrame(hide);
}
else {
dw.animationId = null;
el.classList.remove('show');
restoreBodyScroll(el, body);
}
}

if (open) {
el.classList.add('show')

const scroll = el.getAttribute('data-bb-scroll') === "true";
if (scroll === false) {
body.classList.add('drawer-overflow-hidden');
}

if (animationFrame) {
requestAnimationFrame(show)
dw.animationId = requestAnimationFrame(show)
}
else {
showDrawer();
Expand All @@ -154,9 +184,9 @@ export function execute(id, open) {
if (drawerBackdrop) {
drawerBackdrop.classList.remove('show');
}
body.classList.remove('drawer-overflow-hidden');
restoreBodyScroll(el, body);
Comment thread
ArgoZhang marked this conversation as resolved.
Resolved
if (animationFrame) {
requestAnimationFrame(hide)
dw.animationId = requestAnimationFrame(hide)
}
else {
el.classList.remove('show');
Expand All @@ -170,6 +200,9 @@ export function dispose(id) {
Data.remove(id);

const { el, body } = dw;
if (dw.animationId) {
cancelAnimationFrame(dw.animationId);
}
EventHandler.off(el, 'keyup');

if (el.classList.contains('show')) {
Expand All @@ -179,8 +212,8 @@ export function dispose(id) {
if (drawerBody) {
drawerBody.classList.remove('show')
}
body.classList.remove('overflow-hidden')
}
restoreBodyScroll(el, body);

const drawerBody = el.querySelector('.drawer-body');
if (drawerBody !== null) {
Expand Down
7 changes: 5 additions & 2 deletions src/BootstrapBlazor/Components/Drawer/DrawerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ private async Task OnCloseAsync(DrawerOption option)
await option.OnCloseAsync();
}

_option = null;
StateHasChanged();
if (ReferenceEquals(_option, option))
{
_option = null;
Comment thread
ArgoZhang marked this conversation as resolved.
Resolved
StateHasChanged();
}
}

/// <summary>
Expand Down
51 changes: 50 additions & 1 deletion test/UnitTest/Components/DrawerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public void Width_Ok()
pb.Add(a => a.Width, "");
});
Assert.DoesNotContain("--bb-drawer-width", cut.Markup);
Assert.DoesNotContain("tabindex", cut.Markup);
}

[Fact]
Expand Down Expand Up @@ -76,6 +75,15 @@ public void IsOpenChanged_Ok()
Assert.False(isOpen);
}

[Fact]
public void Esc_Ok()
{
// 通过 tabindex="-1" 让组件可以获取焦点, ESC 事件才能触发
var cut = Context.Render<Drawer>();

cut.Contains("tabindex=\"-1\"");
}

[Fact]
public void OnClickBackdrop_Ok()
{
Expand Down Expand Up @@ -291,6 +299,47 @@ public async Task OnClosingAsync_ReturnTrue()
Assert.Equal(["closing", "close", "changed"], callbacks);
}

[Fact]
public async Task Close_IgnoresConcurrentCall()
{
var closingStarted = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var continueClosing = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
var closingCount = 0;
var closeCount = 0;
var changedCount = 0;
var cut = Context.Render<Drawer>(builder =>
{
builder.Add(a => a.IsOpen, true);
builder.Add(a => a.OnClosingAsync, async () =>
{
closingCount++;
closingStarted.SetResult(true);
await continueClosing.Task;
return true;
});
builder.Add(a => a.OnCloseAsync, () =>
{
closeCount++;
return Task.CompletedTask;
});
builder.Add(a => a.IsOpenChanged, EventCallback.Factory.Create<bool>(this, _ => changedCount++));
});

var firstClose = cut.InvokeAsync(cut.Instance.Close);
await closingStarted.Task;
await cut.InvokeAsync(cut.Instance.Close);

Assert.Equal(1, closingCount);
Assert.True(cut.Instance.IsOpen);

continueClosing.SetResult(true);
await firstClose;

Assert.False(cut.Instance.IsOpen);
Assert.Equal(1, closeCount);
Assert.Equal(1, changedCount);
}

class MockContent : ComponentBase
{
[CascadingParameter(Name = "BodyContext")]
Expand Down
Loading