feat: 添加主题切换按钮并更新导航栏样式

This commit is contained in:
chuan
2026-05-16 19:21:13 +08:00
Unverified
parent f4f13b51bb
commit 01ece71945
3 changed files with 58 additions and 4 deletions
+21 -2
View File
@@ -10,6 +10,25 @@ export const navbarRight = `
.navbar-right { .navbar-right {
min-height: 48px; min-height: 48px;
} }
.navbar-right {
gap: 8px;
> .navbar-theme-toggle {
align-items: center;
justify-content: center;
border: 1px solid var(--color-light-border);
border-radius: var(--border-radius);
color: var(--color-text);
height: 32px;
min-height: 32px;
min-width: 32px;
padding: 0;
width: 32px;
&:hover,
&.active {
background-color: var(--color-nav-hoverBg);
}
}
}
.navbar-left { .navbar-left {
gap: 8px; gap: 8px;
> .item { > .item {
@@ -35,7 +54,7 @@ export const navbarRight = `
.navbar-right:has(.user-menu) { .navbar-right:has(.user-menu) {
gap: 8px; gap: 8px;
/* 右侧按钮, 但不包括头像 */ /* 右侧按钮, 但不包括头像 */
> .item:not(:last-child) { > .item:not(.navbar-user-menu):not(.navbar-theme-toggle) {
align-items: center; align-items: center;
justify-content: center; justify-content: center;
gap: 4px; gap: 4px;
@@ -75,7 +94,7 @@ export const navbarRight = `
} }
.item.ui.dropdown { .item.ui.dropdown {
/* 头像菜单 */ /* 头像菜单 */
&:last-child { &.navbar-user-menu {
padding: 0; padding: 0;
.text { .text {
/* 不显示头像右侧的小箭头下拉菜单标识 */ /* 不显示头像右侧的小箭头下拉菜单标识 */
+22 -2
View File
@@ -29,7 +29,8 @@
<!-- the full dropdown menus --> <!-- the full dropdown menus -->
<div class="navbar-right"> <div class="navbar-right">
{{if and .IsSigned .MustChangePassword}} {{if and .IsSigned .MustChangePassword}}
<div class="ui dropdown jump item" data-tooltip-content="{{ctx.Locale.Tr "user_profile_and_more"}}"> {{template "base/theme_toggle" .}}
<div class="ui dropdown jump item navbar-user-menu" data-tooltip-content="{{ctx.Locale.Tr "user_profile_and_more"}}">
<span class="text"> <span class="text">
{{ctx.AvatarUtils.Avatar .SignedUser 24 "tw-mr-1"}} {{ctx.AvatarUtils.Avatar .SignedUser 24 "tw-mr-1"}}
<span class="only-mobile">{{.SignedUser.Name}}</span> <span class="only-mobile">{{.SignedUser.Name}}</span>
@@ -72,7 +73,8 @@
</div><!-- end dropdown menu create new --> </div><!-- end dropdown menu create new -->
{{template "base/head_navbar_icons" dict "ItemExtraClass" "not-mobile" "PageGlobalData" .PageGlobalData "ShowMilestonesDashboardPage" .ShowMilestonesDashboardPage}} {{template "base/head_navbar_icons" dict "ItemExtraClass" "not-mobile" "PageGlobalData" .PageGlobalData "ShowMilestonesDashboardPage" .ShowMilestonesDashboardPage}}
<div class="ui dropdown jump item" data-tooltip-content="{{ctx.Locale.Tr "user_profile_and_more"}}"> {{template "base/theme_toggle" .}}
<div class="ui dropdown jump item navbar-user-menu" data-tooltip-content="{{ctx.Locale.Tr "user_profile_and_more"}}">
<span class="text tw-flex tw-items-center"> <span class="text tw-flex tw-items-center">
<span class="navbar-avatar"> <span class="navbar-avatar">
{{ctx.AvatarUtils.Avatar .SignedUser 24 "tw-mr-2"}} {{ctx.AvatarUtils.Avatar .SignedUser 24 "tw-mr-2"}}
@@ -134,6 +136,7 @@
{{svg "octicon-sign-in"}} {{svg "octicon-sign-in"}}
<span class="tw-ml-1">{{ctx.Locale.Tr "sign_in"}}</span> <span class="tw-ml-1">{{ctx.Locale.Tr "sign_in"}}</span>
</a> </a>
{{template "base/theme_toggle" .}}
{{end}} {{end}}
</div><!-- end full right menu --> </div><!-- end full right menu -->
@@ -165,4 +168,21 @@
</div> </div>
{{end}} {{end}}
</nav> </nav>
<script>
document.addEventListener("DOMContentLoaded", () => {
const toggle = document.querySelector(".navbar-theme-toggle");
if (!toggle) return;
toggle.addEventListener("click", async () => {
const current = toggle.getAttribute("data-current-theme");
const light = toggle.getAttribute("data-light-theme");
const dark = toggle.getAttribute("data-dark-theme");
const next = current === dark ? light : dark;
await fetch(`{{AppSubUrl}}/-/web-theme/apply?theme=${encodeURIComponent(next)}`, {
method: "POST",
headers: {"X-Csrf-Token": "{{ctx.CsrfToken}}"},
});
window.location.reload();
});
});
</script>
{{template "base/head_banner"}} {{template "base/head_banner"}}
+15
View File
@@ -0,0 +1,15 @@
<button
type="button"
class="item ui icon mini button tw-m-0 navbar-theme-toggle"
data-current-theme="{{ctx.CurrentWebTheme.InternalName}}"
data-light-theme="github-dev"
data-dark-theme="github-dev-dark"
data-tooltip-content="{{ctx.Locale.Tr "settings.manage_themes"}}"
aria-label="{{ctx.Locale.Tr "settings.manage_themes"}}"
>
{{if eq ctx.CurrentWebTheme.InternalName "github-dev-dark"}}
{{svg "octicon-sun" 16}}
{{else}}
{{svg "octicon-moon" 16}}
{{end}}
</button>