Skip to content

Commit 8422a3f

Browse files
authored
feat(Cssbuilder): improve performance use StringBuilder (#6799)
* wip: 临时提交 * refactor: use AddClass instead AddStyle * refactor: 更新 Drawer 抽屉单元测试 * perf: 优化性能更新单元测试
1 parent 7c9a31d commit 8422a3f

File tree

14 files changed

+55
-164
lines changed

14 files changed

+55
-164
lines changed

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.AspNetCore.Components.Web.Virtualization;
77
using Microsoft.Extensions.Localization;
8+
using System.Globalization;
89

910
namespace BootstrapBlazor.Components;
1011

@@ -150,8 +151,8 @@ public partial class AutoFill<TValue>
150151
.AddClass($"text-danger", IsValid.HasValue && !IsValid.Value)
151152
.Build();
152153

153-
private string? PlaceHolderStyleString => RowHeight != 50f
154-
? CssBuilder.Default().AddStyle("height", $"{RowHeight}px").Build()
154+
private string? PlaceHolderStyleString => Math.Abs(RowHeight - 50f) > 0.1f
155+
? CssBuilder.Default().AddClass($"height: {RowHeight.ToString(CultureInfo.InvariantCulture)}px;").Build()
155156
: null;
156157

157158
/// <summary>

src/BootstrapBlazor/Components/Badge/ShieldBadge.razor.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ public partial class ShieldBadge
6969
.Build();
7070

7171
private string? StyleString => CssBuilder.Default()
72-
.AddStyle("--bb-shield-badge-icon-color", IconColor, !string.IsNullOrEmpty(IconColor))
73-
.AddStyle("--bb-shield-badge-label-color", LabelColor, !string.IsNullOrEmpty(LabelColor))
74-
.AddStyle("--bb-shield-badge-label-bg", LabelBackgroundColor, !string.IsNullOrEmpty(LabelBackgroundColor))
75-
.AddStyle("--bb-shield-badge-text-color", TextColor, !string.IsNullOrEmpty(TextColor))
76-
.AddStyle("--bb-shield-badge-text-bg", TextBackgroundColor, !string.IsNullOrEmpty(TextBackgroundColor))
77-
.AddStyle("--bb-shield-badge-radius", $"{Math.Max(0, Radius)}px", Radius != 3)
72+
.AddClass($"--bb-shield-badge-icon-color: {IconColor};", !string.IsNullOrEmpty(IconColor))
73+
.AddClass($"--bb-shield-badge-label-color: {LabelColor};", !string.IsNullOrEmpty(LabelColor))
74+
.AddClass($"--bb-shield-badge-label-bg: {LabelBackgroundColor};", !string.IsNullOrEmpty(LabelBackgroundColor))
75+
.AddClass($"--bb-shield-badge-text-color: {TextColor};", !string.IsNullOrEmpty(TextColor))
76+
.AddClass($"--bb-shield-badge-text-bg: {TextBackgroundColor};", !string.IsNullOrEmpty(TextBackgroundColor))
77+
.AddClass($"--bb-shield-badge-radius: {Math.Max(0, Radius)}px;", Radius != 3)
7878
.Build();
7979

8080
private string? IconString => CssBuilder.Default("shield-badge-icon")

src/BootstrapBlazor/Components/Captcha/Captcha.razor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public partial class Captcha
2020
/// 获得 组件宽度
2121
/// </summary>
2222
private string? StyleString => CssBuilder.Default()
23-
.AddStyle("width", $"{Width + 42}px", Width > 0)
23+
.AddClass($"width: {Width + 42}px;", Width > 0)
2424
.Build();
2525

2626
/// <summary>
2727
/// 获得 加载图片失败样式
2828
/// </summary>
2929
private string? FailedStyle => CssBuilder.Default()
30-
.AddStyle("width", $"{Width}px", Width > 0)
31-
.AddStyle("height", $"{Height}px", Height > 0)
30+
.AddClass($"width: {Width}px;", Width > 0)
31+
.AddClass($"height: {Height}px;", Height > 0)
3232
.Build();
3333

3434
/// <summary>
@@ -219,7 +219,7 @@ private CaptchaOption GetCaptchaOption()
219219
private static bool CalcStddev(List<int> trails)
220220
{
221221
var ret = false;
222-
if (trails.Any())
222+
if (trails.Count > 0)
223223
{
224224
var average = trails.Sum() * 1.0 / trails.Count;
225225
var dev = trails.Select(t => t - average);

src/BootstrapBlazor/Components/Card/Card.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public partial class Card
127127
private IIconTheme? IconTheme { get; set; }
128128

129129
private string? HeaderStyleString => CssBuilder.Default()
130-
.AddStyle("--bs-card-cap-padding-y", HeaderPaddingY)
130+
.AddClass($"--bs-card-cap-padding-y: {HeaderPaddingY};", !string.IsNullOrEmpty(HeaderPaddingY))
131131
.AddStyleFromAttributes(AdditionalAttributes)
132132
.Build();
133133

src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class Drawer
1919
.Build();
2020

2121
private string? StyleString => CssBuilder.Default()
22-
.AddStyle("--bb-drawer-position", Position)
22+
.AddClass($"--bb-drawer-position: {Position};")
2323
.AddClass($"--bb-drawer-zindex: {ZIndex};", ZIndex.HasValue)
2424
.AddStyleFromAttributes(AdditionalAttributes)
2525
.Build();
@@ -28,10 +28,12 @@ public partial class Drawer
2828
/// 获得 抽屉 Style 字符串
2929
/// </summary>
3030
private string? DrawerStyleString => CssBuilder.Default()
31-
.AddStyle("--bb-drawer-width", Width, Placement != Placement.Top && Placement != Placement.Bottom)
32-
.AddStyle("--bb-drawer-height", Height, Placement == Placement.Top || Placement == Placement.Bottom)
31+
.AddClass($"--bb-drawer-width: {Width};", !string.IsNullOrEmpty(Width) && !IsVertical)
32+
.AddClass($"--bb-drawer-height: {Height};", !string.IsNullOrEmpty(Height) && IsVertical)
3333
.Build();
3434

35+
private bool IsVertical => Placement == Placement.Top || Placement == Placement.Bottom;
36+
3537
/// <summary>
3638
/// 获得 抽屉样式
3739
/// </summary>

src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public partial class EditorForm<TModel> : IShowLabel
4444
.Build();
4545

4646
private string? FormStyleString => CssBuilder.Default()
47-
.AddStyle("--bb-row-label-width", $"{LabelWidth}px", LabelWidth.HasValue)
47+
.AddClass($"--bb-row-label-width: {LabelWidth}px;", LabelWidth.HasValue)
4848
.Build();
4949

5050
/// <summary>

src/BootstrapBlazor/Components/Label/BootstrapLabel.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public partial class BootstrapLabel
3939
.Build();
4040

4141
private string? StyleString => CssBuilder.Default()
42-
.AddStyle($"--bb-row-label-width", $"{LabelWidth}px", LabelWidth.HasValue)
42+
.AddClass($"--bb-row-label-width: {LabelWidth}px;", LabelWidth.HasValue)
4343
.AddStyleFromAttributes(AdditionalAttributes)
4444
.Build();
4545

src/BootstrapBlazor/Components/Logout/Logout.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public partial class Logout
8181
.Build();
8282

8383
private string? AvatarStyleString => CssBuilder.Default()
84-
.AddStyle("--bb-logout-user-avatar-border-radius", AvatarRadius, !string.IsNullOrEmpty(AvatarRadius))
84+
.AddClass($"--bb-logout-user-avatar-border-radius: {AvatarRadius};", !string.IsNullOrEmpty(AvatarRadius))
8585
.Build();
8686

8787
/// <summary>

src/BootstrapBlazor/Components/Navbar/NavbarGroup.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public partial class NavbarGroup
3434
.Build();
3535

3636
private string? StyleString => CssBuilder.Default()
37-
.AddStyle("--bs-scroll-height", Height, IsScrolling)
37+
.AddClass($"--bs-scroll-height: {Height};", !string.IsNullOrEmpty(Height))
3838
.AddStyleFromAttributes(AdditionalAttributes)
3939
.Build();
4040

src/BootstrapBlazor/Components/Toolbar/Toolbar.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public partial class Toolbar
2727
.Build();
2828

2929
private string? StyleString => CssBuilder.Default()
30-
.AddStyle("flex-wrap", "wrap", IsWrap)
30+
.AddClass("flex-wrap: wrap;", IsWrap)
3131
.AddStyleFromAttributes(AdditionalAttributes)
3232
.Build();
3333
}

0 commit comments

Comments
 (0)