Skip to content

Commit 525eaac

Browse files
authored
fix(ModalDialog): not shown after close previous window when set IsDraggable and IsCenter to true (#4663)
* refactor: 移动可拖动垂直计算逻辑到 Dialog 脚本内 * feat: 增加 _firstRender 控制仅首次渲染增加样式逻辑 * chore: bump version 9.0.0-beta02
1 parent f477aa4 commit 525eaac

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.0.0-beta01</Version>
4+
<Version>9.0.0-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Modal/Modal.razor.js

-10
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ export function init(id, invoke, shownCallback, closeCallback) {
1717
Data.set(id, modal)
1818

1919
EventHandler.on(el, 'shown.bs.modal', () => {
20-
const dialog = el.querySelector('.modal-dialog');
21-
if (dialog.classList.contains('is-draggable-center')) {
22-
const width = dialog.offsetWidth / 2;
23-
const height = dialog.offsetHeight / 2;
24-
25-
dialog.style.setProperty("margin-left", `calc(50vw - ${width}px)`);
26-
dialog.style.setProperty("margin-top", `calc(50vh - ${height}px)`);
27-
dialog.classList.remove('is-draggable-center');
28-
}
29-
3020
invoke.invokeMethodAsync(shownCallback)
3121
})
3222
EventHandler.on(el, 'hidden.bs.modal', e => {

src/BootstrapBlazor/Components/Modal/ModalDialog.razor.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public partial class ModalDialog : IHandlerException
2424
.AddClass("modal-dialog-scrollable", IsScrolling)
2525
.AddClass("modal-fullscreen", MaximizeStatus)
2626
.AddClass("is-draggable", IsDraggable)
27-
.AddClass("is-draggable-center", IsCentered && IsDraggable)
27+
.AddClass("is-draggable-center", IsCentered && IsDraggable && _firstRender)
2828
.AddClass("d-none", !IsShown)
2929
.AddClass(Class, !string.IsNullOrEmpty(Class))
3030
.Build();
@@ -291,6 +291,8 @@ public partial class ModalDialog : IHandlerException
291291

292292
private DialogResult _result = DialogResult.Close;
293293

294+
private bool _firstRender = true;
295+
294296
/// <summary>
295297
/// OnInitialized 方法
296298
/// </summary>
@@ -329,6 +331,20 @@ protected override void OnParametersSet()
329331
MaximizeIconString = MaximizeWindowIcon;
330332
}
331333

334+
/// <summary>
335+
/// <inheritdoc/>
336+
/// </summary>
337+
/// <param name="firstRender"></param>
338+
protected override void OnAfterRender(bool firstRender)
339+
{
340+
base.OnAfterRender(firstRender);
341+
342+
if (firstRender)
343+
{
344+
_firstRender = false;
345+
}
346+
}
347+
332348
/// <summary>
333349
/// 设置 Header 文字方法
334350
/// </summary>

src/BootstrapBlazor/Components/Modal/ModalDialog.razor.js

+9
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ export function init(id) {
119119
}
120120
)
121121
}
122+
123+
if (el.classList.contains('is-draggable-center')) {
124+
const width = el.offsetWidth / 2;
125+
const height = el.offsetHeight / 2;
126+
127+
el.style.setProperty("margin-left", `calc(50vw - ${width}px)`);
128+
el.style.setProperty("margin-top", `calc(50vh - ${height}px)`);
129+
el.classList.remove('is-draggable-center');
130+
}
122131
}
123132

124133
export function dispose(id) {

0 commit comments

Comments
 (0)