Skip to content

Commit 7fcceab

Browse files
vnbaaijdvoituron
andauthored
Add IconOnly parameter (#3432)
Co-authored-by: Denis Voituron <[email protected]>
1 parent 2afbf3c commit 7fcceab

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed

examples/Demo/FluentUI.Demo.Client/Documentation/Components/Button/Examples/ButtonIcon.razor

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@
1313
<FluentIcon Value="@(new Icons.Regular.Size20.Globe())" Color="Color.Error" Slot="start" />
1414
Button
1515
</FluentButton>
16+
17+
18+
<FluentButton IconOnly="true">
19+
<svg viewBox="0 0 20 20">
20+
<path fill="currentColor" d="M7.851 3.146a.5.5 0 0 1 0 .707L4.706 7H10c2.932 0 5.593 1.64 6.936 4.043a.5.5 0 1 1-.872.488C14.894 9.439 12.564 8 10 8H4.707l3.144 3.145a.5.5 0 0 1-.707.707L3.161 7.867a.5.5 0 0 1-.014-.721l3.997-4a.5.5 0 0 1 .707 0M8 15a2 2 0 1 1 4 0a2 2 0 0 1-4 0m2-1a1 1 0 1 0 0 2a1 1 0 0 0 0-2"></path>
21+
</svg>
22+
</FluentButton>
23+
24+
25+

examples/Demo/FluentUI.Demo.Client/Documentation/Components/Button/FluentButton.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ The button will be enabled again after 2 seconds.
3535

3636
You can add icons to a button to help identify the action it triggers.
3737
To do this, you can use an `IconStart` or `IconEnd` property to add an icon
38-
to the beginning or end of the button text.
38+
to the beginning or end of the button text. When using `IconStart` or `IconEnd`
39+
without supplying any content, the button will be displayed in a smaller form.
3940

40-
With icon in the content. By doing it this way, it is possible to specify
41-
a <code>Color</code> for the icon.
41+
By putting an icon in the content, it is possible to specify
42+
a `Color` for the icon.
43+
44+
By setting the `IconOnly` parameter to true, you can use an icon as the button's content but still have it display in a smaller form.
4245

4346
{{ ButtonIcon }}
4447

src/Core/Components/Button/FluentButton.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
size="@(Size == ButtonSize.Medium ? null : Size.ToAttributeValue())"
1919
id="@Id"
2020
value="@Value"
21-
icon-only="@(ChildContent is null)"
21+
icon-only="@(IconOnly || ChildContent is null)"
2222
disabled="@(Disabled || Loading)"
2323
disabled-focusable="@(DisabledFocusable || Loading)"
2424
name="@Name"

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ public partial class FluentButton : FluentComponentBase
154154
[Parameter]
155155
public bool Loading { get; set; } = false;
156156

157+
/// <summary>
158+
/// Gets or sets if the button only shows an icon
159+
/// Can be used when using <see cref="ChildContent"/> that renders as an icon
160+
/// </summary>
161+
[Parameter]
162+
public bool IconOnly { get; set; }
163+
157164
/// <summary>
158165
/// Gets or sets the <see cref="Icon"/> displayed at the start of button content.
159166
/// </summary>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
<fluent-button icon-only="" blazor:onclick="x">
3+
<svg viewBox="0 0 20 20">
4+
<path fill="currentColor" d="M7.851 3.146a.5.5 0 0 1 0 .707L4.706 7H10c2.932 0 5.593 1.64 6.936 4.043a.5.5 0 1 1-.872.488C14.894 9.439 12.564 8 10 8H4.707l3.144 3.145a.5.5 0 0 1-.707.707L3.161 7.867a.5.5 0 0 1-.014-.721l3.997-4a.5.5 0 0 1 .707 0M8 15a2 2 0 1 1 4 0a2 2 0 0 1-4 0m2-1a1 1 0 1 0 0 2a1 1 0 0 0 0-2"></path>
5+
</svg>
6+
</fluent-button>

tests/Core/Components/Button/FluentButtonTests.razor

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
bool clicked = false;
355355

356356
// Arrange
357-
var cut = Render(@<FluentButton OnClick="@(e => { clicked = true; } )">My button</FluentButton>);
357+
var cut = Render(@<FluentButton OnClick="@(e => { clicked = true; })">My button</FluentButton>);
358358

359359
// Act
360360
cut.Find("fluent-button").Click();
@@ -402,10 +402,10 @@
402402
// Arrange
403403
// Not adding StopPropagation here explicitly because it is false by default
404404
var cut = Render(@<div @onclick="@(e => {clickedondiv = true; })">
405-
<FluentButton OnClick="@(e => { clicked = true; } )">
406-
My button
407-
</FluentButton>
408-
</div>);
405+
<FluentButton OnClick="@(e => { clicked = true; })">
406+
My button
407+
</FluentButton>
408+
</div>);
409409

410410
// Act
411411
cut.Find("fluent-button").Click();
@@ -423,10 +423,10 @@
423423

424424
// Arrange
425425
var cut = Render(@<div @onclick="@(e => {clickedondiv = true; })">
426-
<FluentButton StopPropagation="true" OnClick="@(e => { clicked = true; } )">
427-
My button
428-
</FluentButton>
429-
</div>);
426+
<FluentButton StopPropagation="true" OnClick="@(e => { clicked = true; })">
427+
My button
428+
</FluentButton>
429+
</div>);
430430

431431
// Act
432432
cut.Find("fluent-button").Click();
@@ -435,5 +435,18 @@
435435
Assert.False(clickedondiv);
436436
Assert.True(clicked);
437437
}
438+
439+
[Fact]
440+
public void FluentButton_IconOnly()
441+
{
442+
// Arrange && Act
443+
var cut = Render(@<FluentButton IconOnly="true">
444+
<svg viewBox="0 0 20 20">
445+
<path fill="currentColor" d="M7.851 3.146a.5.5 0 0 1 0 .707L4.706 7H10c2.932 0 5.593 1.64 6.936 4.043a.5.5 0 1 1-.872.488C14.894 9.439 12.564 8 10 8H4.707l3.144 3.145a.5.5 0 0 1-.707.707L3.161 7.867a.5.5 0 0 1-.014-.721l3.997-4a.5.5 0 0 1 .707 0M8 15a2 2 0 1 1 4 0a2 2 0 0 1-4 0m2-1a1 1 0 1 0 0 2a1 1 0 0 0 0-2"></path>
446+
</svg>
447+
</FluentButton>);
448+
// Assert
449+
cut.Verify();
450+
}
438451
}
439-
}
452+

0 commit comments

Comments
 (0)