-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ThemeProvider): add ThemeProvider component (#3430)
* feat: 增加 ThemeProvider 服务 * feat: 增加 ThemeProvider 组件 * doc: 更新示例 * feat: 增加 ThemeProvider 组件 * doc: 精简代码 * feat: 增加 auto 模式样式值 * refactor: 增加 ActiveModeIcon 图标 * chore: 格式化代码 * style: 微调样式 * test: 增加单元测试 * doc: 增加本地化 * chore: bump version 8.5.3 * refactor: 重构脚本逻辑 * doc: 更新初始化主题逻辑 * feat: 增加 SetTheme 方法 * reafactor: 重构脚本提高代码复用率 * feat: 增加 SetTheme/GetTheme 扩展方法 * feat: 增加 IThemeProvider 服务 * test: 更新单元测试
- Loading branch information
Showing
22 changed files
with
408 additions
and
47 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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { getPreferredTheme, setTheme } from "../../_content/BootstrapBlazor/modules/theme.js" | ||
import { getPreferredTheme, setTheme } from "../../_content/BootstrapBlazor/modules/utility.js" | ||
|
||
setTheme(getPreferredTheme()) | ||
setTheme(getPreferredTheme(), false) |
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
32 changes: 32 additions & 0 deletions
32
src/BootstrapBlazor/Components/ThemeProvider/ThemeProvider.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,32 @@ | ||
@namespace BootstrapBlazor.Components | ||
@inherits BootstrapModuleComponentBase | ||
@attribute [BootstrapModuleAutoLoader] | ||
|
||
<div @attributes="AdditionalAttributes" id="@Id" class="@ClassString"> | ||
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> | ||
<i class="bb-theme-mode-active invisible"></i> | ||
</button> | ||
<ul class="dropdown-menu dropdown-menu-end"> | ||
<li> | ||
<button type="button" class="dropdown-item d-flex align-items-center" data-bb-theme-value="light"> | ||
<i class="@LightModeIcon bb-theme-mode-light" data-bb-theme-icon="@LightModeIcon"></i> | ||
<span class="mx-2">@LightModeText</span> | ||
<i class="@ActiveIcon bb-theme-mode-check"></i> | ||
</button> | ||
</li> | ||
<li> | ||
<button type="button" class="dropdown-item d-flex align-items-center" data-bb-theme-value="dark"> | ||
<i class="@DarkModeIcon bb-theme-mode-dark" data-bb-theme-icon="@DarkModeIcon"></i> | ||
<span class="mx-2">@DarkModeText</span> | ||
<i class="@ActiveIcon bb-theme-mode-check"></i> | ||
</button> | ||
</li> | ||
<li> | ||
<button type="button" class="dropdown-item d-flex align-items-center" data-bb-theme-value="auto"> | ||
<i class="@AutoModeIcon bb-theme-mode-auto" data-bb-theme-icon="@AutoModeIcon"></i> | ||
<span class="mx-2">@AutoModeText</span> | ||
<i class="@ActiveIcon bb-theme-mode-check"></i> | ||
</button> | ||
</li> | ||
</ul> | ||
</div> |
82 changes: 82 additions & 0 deletions
82
src/BootstrapBlazor/Components/ThemeProvider/ThemeProvider.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,82 @@ | ||
// Copyright (c) Argo Zhang (argo@163.com). 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/ | ||
|
||
using Microsoft.Extensions.Localization; | ||
|
||
namespace BootstrapBlazor.Components; | ||
|
||
/// <summary> | ||
/// ThemeProvider 组件 | ||
/// </summary> | ||
public partial class ThemeProvider | ||
{ | ||
/// <summary> | ||
/// 获得/设置 自动模式图标 默认 null | ||
/// </summary> | ||
[Parameter] | ||
public string? AutoModeIcon { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 自动模式文本 默认 null 未设置使用本地化资源 | ||
/// </summary> | ||
[Parameter] | ||
public string? AutoModeText { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 暗黑模式图标 默认 null | ||
/// </summary> | ||
[Parameter] | ||
public string? DarkModeIcon { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 暗黑模式文本 默认 null 未设置使用本地化资源 | ||
/// </summary> | ||
[Parameter] | ||
public string? DarkModeText { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 明亮模式图标 默认 null | ||
/// </summary> | ||
[Parameter] | ||
public string? LightModeIcon { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 明亮模式文本 默认 null 未设置使用本地化资源 | ||
/// </summary> | ||
[Parameter] | ||
public string? LightModeText { get; set; } | ||
|
||
/// <summary> | ||
/// 获得/设置 当前选中模式图标 默认 null | ||
/// </summary> | ||
[Parameter] | ||
public string? ActiveIcon { get; set; } | ||
|
||
[Inject, NotNull] | ||
private IIconTheme? IconTheme { get; set; } | ||
|
||
[Inject, NotNull] | ||
private IStringLocalizer<ThemeProvider>? Localizer { get; set; } | ||
|
||
private string? ClassString => CssBuilder.Default("dropdown bb-theme-mode") | ||
.AddClassFromAttributes(AdditionalAttributes) | ||
.Build(); | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
protected override void OnParametersSet() | ||
{ | ||
base.OnParametersSet(); | ||
|
||
AutoModeIcon ??= IconTheme.GetIconByKey(ComponentIcons.ThemeProviderAutoModeIcon); | ||
DarkModeIcon ??= IconTheme.GetIconByKey(ComponentIcons.ThemeProviderDarkModeIcon); | ||
LightModeIcon ??= IconTheme.GetIconByKey(ComponentIcons.ThemeProviderLightModeIcon); | ||
ActiveIcon ??= IconTheme.GetIconByKey(ComponentIcons.ThemeProviderActiveModeIcon); | ||
|
||
AutoModeText ??= Localizer[nameof(AutoModeText)]; | ||
DarkModeText ??= Localizer[nameof(DarkModeText)]; | ||
LightModeText ??= Localizer[nameof(LightModeText)]; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/BootstrapBlazor/Components/ThemeProvider/ThemeProvider.razor.js
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,35 @@ | ||
import { getAutoThemeValue, getPreferredTheme, setActiveTheme, setTheme, saveTheme } from "../../modules/utility.js" | ||
import EventHandler from "../../modules/event-handler.js" | ||
|
||
export function init(id) { | ||
const el = document.getElementById(id); | ||
if (el) { | ||
const currentTheme = getPreferredTheme(); | ||
const activeItem = el.querySelector(`.dropdown-item[data-bb-theme-value="${currentTheme}"]`); | ||
if (activeItem) { | ||
setActiveTheme(el, activeItem); | ||
} | ||
|
||
const items = el.querySelectorAll('.dropdown-item'); | ||
items.forEach(item => { | ||
EventHandler.on(item, 'click', () => { | ||
setActiveTheme(el, item); | ||
|
||
let theme = item.getAttribute('data-bb-theme-value'); | ||
if (theme === 'auto') { | ||
theme = getAutoThemeValue(); | ||
} | ||
setTheme(theme, false); | ||
saveTheme(theme); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export function dispose(id) { | ||
const el = document.getElementById(id); | ||
const items = el.querySelectorAll('.dropdown-item'); | ||
items.forEach(item => { | ||
EventHandler.off(item, 'click'); | ||
}); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/BootstrapBlazor/Components/ThemeProvider/ThemeProvider.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,33 @@ | ||
.bb-theme-mode { | ||
--bb-theme-mode-width: 128px; | ||
--bb-theme-mode-label-width: 40px; | ||
--bb-theme-mode-icon-width: 14px; | ||
--bb-theme-mode-item-margin-top: 2px; | ||
|
||
.bb-theme-mode-active { | ||
width: var(--bb-theme-mode-icon-width); | ||
display: inline-block; | ||
} | ||
|
||
.dropdown-menu { | ||
--bs-dropdown-min-width: var(--bb-theme-mode-width); | ||
|
||
li:not(:first-child) { | ||
margin-top: var(--bb-theme-mode-item-margin-top); | ||
} | ||
|
||
.dropdown-item { | ||
span { | ||
width: var(--bb-theme-mode-label-width); | ||
} | ||
|
||
i { | ||
width: var(--bb-theme-mode-icon-width); | ||
} | ||
|
||
&:not(.active) .bb-theme-mode-check { | ||
display: none; | ||
} | ||
} | ||
} | ||
} |
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
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,28 @@ | ||
// Copyright (c) Argo Zhang (argo@163.com). 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; | ||
|
||
class DefaultThemeProvider(IJSRuntime jsRuntime) : IThemeProvider | ||
{ | ||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
/// <param name="themeName"></param> | ||
public async ValueTask SetThemeAsync(string themeName) | ||
{ | ||
var module = await jsRuntime.LoadUtility(); | ||
await module.SetTheme(themeName); | ||
} | ||
|
||
/// <summary> | ||
/// <inheritdoc/> | ||
/// </summary> | ||
public async ValueTask<string?> GetThemeAsync() | ||
{ | ||
var module = await jsRuntime.LoadUtility(); | ||
return await module.GetTheme(); | ||
} | ||
} |
Oops, something went wrong.