-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Icon): add BootstrapBlazorIcon component (#3724)
* feat: 增加 Icon 组件库 * feat: 增加 Icon 图标 * doc: 增加示例 * refactor: 增加图标配置 * doc: 更新样式逻辑 * test: 增加单元测试 * chore: bump version 8.6.4
- Loading branch information
Showing
14 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@page "/icon" | ||
@inject IStringLocalizer<Icons> Localizer | ||
|
||
<h3>@Localizer["IconsTitle"]</h3> | ||
|
||
<h4>@Localizer["IconsDescription"]</h4> | ||
|
||
<DemoBlock Title="@Localizer["FATitle"]" Introduction="@Localizer["FAIntro"]" Name="Font"> | ||
<div class="bb-icon-list"> | ||
<BootstrapBlazorIcon Name="fa-solid fa-home"></BootstrapBlazorIcon> | ||
</div> | ||
</DemoBlock> | ||
|
||
<DemoBlock Title="@Localizer["SvgTitle"]" Introduction="@Localizer["SvgIntro"]" Name="Svg"> | ||
<div class="bb-icon-list"> | ||
<BootstrapBlazorIcon Name="config" IsSvgSprites="true" Url="./_content/BootstrapBlazor.IconPark/icon-park.svg"></BootstrapBlazorIcon> | ||
</div> | ||
</DemoBlock> | ||
|
||
<DemoBlock Title="@Localizer["ImageTitle"]" Introduction="@Localizer["ImageIntro"]" Name="Image"> | ||
<div class="bb-icon-list"> | ||
<BootstrapBlazorIcon> | ||
<img src="./favicon.png" alt="img" /> | ||
</BootstrapBlazorIcon> | ||
</div> | ||
</DemoBlock> |
13 changes: 13 additions & 0 deletions
13
src/BootstrapBlazor.Server/Components/Samples/Icons.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
namespace BootstrapBlazor.Server.Components.Samples; | ||
|
||
/// <summary> | ||
/// Icon 组件 | ||
/// </summary> | ||
public partial class Icons : ComponentBase | ||
{ | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/BootstrapBlazor.Server/Components/Samples/Icons.razor.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.bb-icon-list { | ||
display: flex; | ||
} | ||
|
||
::deep .bb-icon { | ||
--bb-icon-width: 24px; | ||
--bb-icon-color: var(--bs-body-color); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/BootstrapBlazor/Components/Icon/BootstrapBlazorIcon.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@namespace BootstrapBlazor.Components | ||
@inherits BootstrapComponentBase | ||
|
||
<div @attributes="@AdditionalAttributes" class="@ClassString"> | ||
@if (ChildContent != null) | ||
{ | ||
@ChildContent | ||
} | ||
else if (IsSvgSprites) | ||
{ | ||
<svg xmlns="http://www.w3.org/2000/svg"> | ||
<use href="@Href"></use> | ||
</svg> | ||
} | ||
else | ||
{ | ||
<i class="@Name"></i> | ||
} | ||
</div> |
43 changes: 43 additions & 0 deletions
43
src/BootstrapBlazor/Components/Icon/BootstrapBlazorIcon.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
namespace BootstrapBlazor.Components; | ||
|
||
/// <summary> | ||
/// Icon 组件 | ||
/// </summary> | ||
public partial class BootstrapBlazorIcon | ||
{ | ||
/// <summary> | ||
/// 获得/设置 图标名称 | ||
/// </summary> | ||
/// <remarks>如果是字库图标应该是样式名称如 fa-solid fa-home 如果是 svg sprites 应该为 Id</remarks> | ||
[Parameter] | ||
[NotNull] | ||
public string? Name { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 是否为 svg sprites 默认 false | ||
/// </summary> | ||
[Parameter] | ||
public bool IsSvgSprites { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 Svg Sprites 路径 | ||
/// </summary> | ||
[Parameter] | ||
public string? Url { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 子组件 | ||
/// </summary> | ||
[Parameter] | ||
public RenderFragment? ChildContent { get; set; } | ||
|
||
private string? Href => $"{Url}#{Name}"; | ||
|
||
private string? ClassString => CssBuilder.Default("bb-icon") | ||
.AddClass($"bb-icon-{Name}", !string.IsNullOrEmpty(Name) && IsSvgSprites) | ||
.Build(); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/BootstrapBlazor/Components/Icon/BootstrapBlazorIcon.razor.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.bb-icon { | ||
--bb-icon-width: 12px; | ||
--bb-icon-color: var(--bs-body-color); | ||
display: inline-flex; | ||
|
||
> svg { | ||
width: var(--bb-icon-width); | ||
height: var(--bb-icon-width); | ||
|
||
> * { | ||
stroke: var(--bb-icon-color); | ||
} | ||
} | ||
|
||
> i { | ||
font-size: var(--bb-icon-width); | ||
} | ||
|
||
> img { | ||
width: var(--bb-icon-width); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Argo Zhang ([email protected]). All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
// Website: https://www.blazor.zone or https://argozhang.github.io/ | ||
|
||
namespace UnitTest.Components; | ||
|
||
public class IconTest : BootstrapBlazorTestBase | ||
{ | ||
[Fact] | ||
public void FontIcon_Ok() | ||
{ | ||
var cut = Context.RenderComponent<BootstrapBlazorIcon>(pb => | ||
{ | ||
pb.Add(a => a.Name, "fa-solid fa-home"); | ||
}); | ||
cut.Contains("i class=\"fa-solid fa-home\""); | ||
} | ||
|
||
[Fact] | ||
public void SvgIcon_Ok() | ||
{ | ||
var cut = Context.RenderComponent<BootstrapBlazorIcon>(pb => | ||
{ | ||
pb.Add(a => a.Name, "home"); | ||
pb.Add(a => a.IsSvgSprites, true); | ||
pb.Add(a => a.Url, "./_content/svg.svg"); | ||
}); | ||
cut.Contains("<use href=\"./_content/svg.svg#home\"></use>"); | ||
} | ||
|
||
[Fact] | ||
public void ImageIcon_Ok() | ||
{ | ||
var cut = Context.RenderComponent<BootstrapBlazorIcon>(pb => | ||
{ | ||
pb.Add(a => a.ChildContent, builder => builder.AddMarkupContent(0, "<img src=\"test.png\" />")); | ||
}); | ||
cut.Contains("<img src=\"test.png\" />"); | ||
} | ||
} |