Skip to content

Commit 180db76

Browse files
authored
doc(Theme): add switch theme button (#2704)
* feat: 增加 ThemeMode 组件 * chore: 更新主题样式 * chore: 更新脚本配置 * doc: 更新图标文件路径 * refactor: 更新样式 * refactor: 更新切换样式 * chore: 更新样式 * refactor: 更新切换主题脚本 * refactor: 更新组件
1 parent 4a6923d commit 180db76

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

src/BootstrapBlazor.Server/Components/Components/Header.razor.css

-16
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,6 @@
4343
white-space: nowrap;
4444
}
4545

46-
.icon-theme img {
47-
cursor: pointer;
48-
}
49-
50-
.icon-theme .icon-dark {
51-
display: none;
52-
}
53-
54-
[data-bs-theme='dark'] .icon-dark {
55-
display: block;
56-
}
57-
58-
[data-bs-theme='dark'] .icon-light {
59-
display: none;
60-
}
61-
6246
@media (min-width: 768px) {
6347
.navbar-header {
6448
position: sticky;

src/BootstrapBlazor.Server/Components/Components/Header.razor.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ export function init() {
1515
prevScrollTop = currentScrollTop
1616
})
1717

18-
const themeElement = document.querySelector('.icon-theme');
19-
if (themeElement) {
20-
EventHandler.on(themeElement, 'click', e => {
21-
let theme = getPreferredTheme();
22-
if (theme === 'dark') {
23-
theme = 'light';
24-
}
25-
else {
26-
theme = 'dark';
27-
}
28-
setTheme(theme);
18+
const themeElements = document.querySelectorAll('.icon-theme');
19+
if (themeElements) {
20+
themeElements.forEach(el => {
21+
EventHandler.on(el, 'click', e => {
22+
let theme = getPreferredTheme();
23+
if (theme === 'dark') {
24+
theme = 'light';
25+
}
26+
else {
27+
theme = 'dark';
28+
}
29+
setTheme(theme);
30+
});
2931
});
3032
}
3133
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@inherits ComponentBase
2+
3+
<div class="btn btn-circle btn-fade btn-theme-mode icon-theme d-flex d-sm-none">
4+
<img src="./images/theme-light.svg" class="icon-light" />
5+
<img src="./images/theme-night.svg" class="icon-dark" />
6+
</div>

src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<main>
77
@Body
88

9-
<LinkButton TooltipPlacement="Placement.Left" ButtonStyle="ButtonStyle.Circle" Color="Color.None" TooltipText="@ChatTooltip" class="btn-fade btn-theme-mode" Url="./ai-chat" Icon="fa-solid fa-comments"></LinkButton>
9+
<ThemeMode />
1010

1111
<LinkButton TooltipPlacement="Placement.Left" ButtonStyle="ButtonStyle.Circle" Color="Color.None" TooltipText="@ChatTooltip" class="btn-fade btn-chat" Url="./ai-chat" Icon="fa-solid fa-comments"></LinkButton>
1212

src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor.css

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@
4343
}
4444

4545
::deep .btn-theme-mode {
46-
--bs-btn-bg: #8759ff;
47-
--bs-btn-hover-bg: var(--bs-btn-bg);
48-
--bs-btn-active-bg: var(--bs-btn-bg);
46+
--bs-btn-bg: #562fff;
47+
--bs-btn-hover-bg: #562fff;
48+
--bs-btn-active-bg: #562fff;
49+
box-shadow: 0 0 8px #6143e2;
4950
bottom: 17rem;
5051
right: 1rem;
5152
overflow: hidden;

src/BootstrapBlazor.Server/wwwroot/css/site.css

+16
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ code {
200200
background-color: rgba(var(--bs-emphasis-color-rgb),.1);
201201
}
202202

203+
.icon-theme img {
204+
cursor: pointer;
205+
}
206+
207+
.icon-theme .icon-light {
208+
display: none;
209+
}
210+
211+
[data-bs-theme='dark'] .icon-dark {
212+
display: none;
213+
}
214+
215+
[data-bs-theme='dark'] .icon-light {
216+
display: block;
217+
}
218+
203219
@media (min-width: 768px) {
204220
:root {
205221
--bs-header-height: 50px;

0 commit comments

Comments
 (0)