Skip to content

Commit fdcf547

Browse files
authored
Update the FluentButton custom style for BackgroundColor and Color properties (#1603)
1 parent e51a322 commit fdcf547

7 files changed

+78
-23
lines changed

examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,17 @@
861861
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentButton.OnAfterRenderAsync(System.Boolean)">
862862
<summary />
863863
</member>
864+
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentButton.CustomStyle">
865+
<summary />
866+
</member>
864867
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentButton.#ctor">
865868
<summary>
866869
Constructs an instance of <see cref="T:Microsoft.FluentUI.AspNetCore.Components.FluentButton"/>.
867870
</summary>
868871
</member>
872+
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentButton.OnInitialized">
873+
<summary />
874+
</member>
869875
<member name="M:Microsoft.FluentUI.AspNetCore.Components.FluentButton.OnClickHandlerAsync(Microsoft.AspNetCore.Components.Web.MouseEventArgs)">
870876
<summary />
871877
</member>

src/Core/Components/Button/FluentButton.razor

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
@namespace Microsoft.FluentUI.AspNetCore.Components
1+
@namespace Microsoft.FluentUI.AspNetCore.Components
22
@using Microsoft.AspNetCore.Components.Rendering;
33
@inherits FluentComponentBase
44

5-
@if (!String.IsNullOrEmpty(CustomId))
6-
{
7-
<style>@CustomStyle</style>
8-
}
5+
@CustomStyle
96

107
@if (LoadingOverlay)
118
{
@@ -42,7 +39,6 @@ else
4239
aria-label=@Title
4340
title=@Title
4441
appearance=@Appearance.ToAttributeValue()
45-
custom-id="@CustomId"
4642
@onclick="@OnClickHandlerAsync"
4743
@attributes="AdditionalAttributes">
4844
@if (IconStart != null)
@@ -72,4 +68,4 @@ else
7268
}
7369
</fluent-button>
7470
}
75-
}
71+
}

src/Core/Components/Button/FluentButton.razor.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Components;
22
using Microsoft.AspNetCore.Components.Web;
3+
using Microsoft.FluentUI.AspNetCore.Components.Utilities;
34
using Microsoft.JSInterop;
45

56
namespace Microsoft.FluentUI.AspNetCore.Components;
@@ -8,7 +9,6 @@ public partial class FluentButton : FluentComponentBase, IAsyncDisposable
89

910
private const string JAVASCRIPT_FILE = "./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Button/FluentButton.razor.js";
1011

11-
private readonly string _customId = Identifier.NewId();
1212
private readonly RenderFragment _renderButton;
1313

1414
/// <summary />
@@ -185,19 +185,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
185185
}
186186
}
187187

188-
private string? CustomId =>
189-
string.IsNullOrEmpty(BackgroundColor) && string.IsNullOrEmpty(Color) ? null : _customId;
190-
191-
private string? CustomStyle =>
192-
$@" fluent-button[custom-id='{_customId}']::part(control) {{
193-
background: padding-box linear-gradient({BackgroundColor}, {BackgroundColor}), border-box {BackgroundColor};
194-
color: {Color};
195-
}}
196-
197-
fluent-button[custom-id='{_customId}']::part(control):hover {{
198-
opacity: 0.8;
199-
}}
200-
";
188+
/// <summary />
189+
protected virtual MarkupString CustomStyle => new InlineStyleBuilder()
190+
.AddStyle($"#{Id}::part(control)", "background", $"padding-box linear-gradient({BackgroundColor}, {BackgroundColor}), border-box {BackgroundColor}", when: !string.IsNullOrEmpty(BackgroundColor))
191+
.AddStyle($"#{Id}::part(control)", "color", $"{Color}", when: !string.IsNullOrEmpty(Color))
192+
.AddStyle($"#{Id}::part(control):hover", "opacity", "0.8", when: !string.IsNullOrEmpty(Color) || !string.IsNullOrEmpty(BackgroundColor))
193+
.BuildMarkupString();
201194

202195
/// <summary>
203196
/// Constructs an instance of <see cref="FluentButton"/>.
@@ -207,6 +200,15 @@ public FluentButton()
207200
_renderButton = RenderButton;
208201
}
209202

203+
/// <summary />
204+
protected override void OnInitialized()
205+
{
206+
if (string.IsNullOrEmpty(Id) && (!string.IsNullOrEmpty(BackgroundColor) || !string.IsNullOrEmpty(Color)))
207+
{
208+
Id = Identifier.NewId();
209+
}
210+
}
211+
210212
/// <summary />
211213
protected async Task OnClickHandlerAsync(MouseEventArgs e)
212214
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
<style>
3+
#MyButton::part(control) { background: padding-box linear-gradient(#ff0000, #ff0000), border-box #ff0000; }
4+
#MyButton::part(control):hover { opacity: 0.8; }
5+
6+
</style>
7+
<fluent-button type="button" id="xxx" appearance="neutral" blazor:onclick="1" b-x1200685t0="" blazor:elementreference="xxx">My button</fluent-button>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
<style>
3+
#MyButton::part(control) { background: padding-box linear-gradient(#ff0000, #ff0000), border-box #ff0000; color: #00ff00; }
4+
#MyButton::part(control):hover { opacity: 0.8; }
5+
6+
</style>
7+
<fluent-button type="button" id="xxx" appearance="neutral" blazor:onclick="1" b-x1200685t0="" blazor:elementreference="xxx">My button</fluent-button>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
<style>
3+
#MyButton::part(control) { color: #00ff00; }
4+
#MyButton::part(control):hover { opacity: 0.8; }
5+
6+
</style>
7+
<fluent-button type="button" id="xxx" appearance="neutral" blazor:onclick="1" b-x1200685t0="" blazor:elementreference="xxx">My button</fluent-button>

tests/Core/Button/FluentButtonTests.razor

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@using Xunit;
1+
@using Xunit;
22
@inherits TestContext
33
@code
44
{
@@ -12,6 +12,36 @@
1212
cut.Verify();
1313
}
1414

15+
[Fact]
16+
public void FluentButton_BackgroundColor()
17+
{
18+
// Arrange && Act
19+
var cut = Render(@<FluentButton Id="MyButton" BackgroundColor="#ff0000">My button</FluentButton>);
20+
21+
// Assert
22+
cut.Verify();
23+
}
24+
25+
[Fact]
26+
public void FluentButton_Color()
27+
{
28+
// Arrange && Act
29+
var cut = Render(@<FluentButton Id="MyButton" Color="#00ff00">My button</FluentButton>);
30+
31+
// Assert
32+
cut.Verify();
33+
}
34+
35+
[Fact]
36+
public void FluentButton_BackgroundColorColor()
37+
{
38+
// Arrange && Act
39+
var cut = Render(@<FluentButton Id="MyButton" BackgroundColor="#ff0000" Color="#00ff00">My button</FluentButton>);
40+
41+
// Assert
42+
cut.Verify();
43+
}
44+
1545
[Fact]
1646
public void FluentButton_OnClick()
1747
{
@@ -28,4 +58,4 @@
2858
// Assert
2959
Assert.True(clicked);
3060
}
31-
}
61+
}

0 commit comments

Comments
 (0)