-
Notifications
You must be signed in to change notification settings - Fork 2
feat: added dark mode toggle and mobile collapsible menu. #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ayyyyano
wants to merge
2
commits into
TransHelper:main
Choose a base branch
from
ayyyyano:dark-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,31 +1,125 @@ | ||
| <template> | ||
| <div class="min-h-screen flex flex-col"> | ||
| <header class="bg-white border-b border-gray-200 sticky top-0 z-50"> | ||
| <header class="bg-white dark:bg-gray-900 border-b border-gray-200 dark:border-gray-800 sticky top-0 z-50"> | ||
| <div class="max-w-6xl mx-auto px-4 h-14 flex items-center justify-between"> | ||
| <NuxtLink to="/" class="text-lg font-bold text-primary-600 hover:text-primary-700"> | ||
| <NuxtLink to="/" class="text-lg font-bold text-primary-600 hover:text-primary-700 shrink-0"> | ||
| Trans‑Search | ||
| </NuxtLink> | ||
| <nav class="flex items-center gap-1"> | ||
| <NuxtLink to="/" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100" active-class="bg-primary-50 text-primary-700 font-medium"> | ||
|
|
||
| <!-- Desktop nav + dark toggle --> | ||
| <nav class="hidden md:flex items-center gap-1"> | ||
| <NuxtLink to="/" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium"> | ||
| 搜索 | ||
| </NuxtLink> | ||
| <NuxtLink to="/tree" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100" active-class="bg-primary-50 text-primary-700 font-medium"> | ||
| <NuxtLink to="/tree" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium"> | ||
| 分类 | ||
| </NuxtLink> | ||
| <NuxtLink to="/stats" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100" active-class="bg-primary-50 text-primary-700 font-medium"> | ||
| <NuxtLink to="/stats" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium"> | ||
| 统计 | ||
| </NuxtLink> | ||
| <NuxtLink to="/config" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100" active-class="bg-primary-50 text-primary-700 font-medium"> | ||
| <NuxtLink to="/config" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium"> | ||
| 管理 | ||
| </NuxtLink> | ||
| <NuxtLink to="/about" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100" active-class="bg-primary-50 text-primary-700 font-medium"> | ||
| <NuxtLink to="/about" class="px-3 py-1.5 rounded-md text-sm hover:bg-gray-100 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium"> | ||
| 关于 | ||
| </NuxtLink> | ||
|
|
||
| <!-- Dark mode toggle (desktop: inside nav) --> | ||
| <button | ||
| class="ml-1 p-1.5 rounded-md hover:bg-gray-100 dark:hover:bg-gray-800 text-gray-500 dark:text-gray-400" | ||
| aria-label="切换暗色模式" | ||
| @click="toggleDark" | ||
| > | ||
| <svg v-if="isDark" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"> | ||
| <path stroke-linecap="round" stroke-linejoin="round" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" /> | ||
| </svg> | ||
| <svg v-else class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"> | ||
| <path stroke-linecap="round" stroke-linejoin="round" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" /> | ||
| </svg> | ||
| </button> | ||
| </nav> | ||
|
|
||
| <!-- Mobile: dark toggle + hamburger (only) --> | ||
| <div class="flex md:hidden items-center gap-1"> | ||
| <button | ||
| class="p-2 rounded-md hover:bg-gray-100 dark:hover:bg-gray-800 text-gray-500 dark:text-gray-400" | ||
| aria-label="切换暗色模式" | ||
| @click="toggleDark" | ||
| > | ||
| <svg v-if="isDark" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"> | ||
| <path stroke-linecap="round" stroke-linejoin="round" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" /> | ||
| </svg> | ||
| <svg v-else class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"> | ||
| <path stroke-linecap="round" stroke-linejoin="round" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" /> | ||
| </svg> | ||
| </button> | ||
|
|
||
| <button | ||
| class="p-2 rounded-md hover:bg-gray-100 dark:hover:bg-gray-800 text-gray-600 dark:text-gray-300" | ||
| aria-label="菜单" | ||
| @click="mobileMenuOpen = !mobileMenuOpen" | ||
| > | ||
| <svg v-if="!mobileMenuOpen" class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"> | ||
| <path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16" /> | ||
| </svg> | ||
| <svg v-else class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"> | ||
| <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" /> | ||
| </svg> | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| <!-- Mobile slide-down menu --> | ||
| <Transition name="slide-down"> | ||
| <nav v-if="mobileMenuOpen" class="md:hidden border-t border-gray-100 dark:border-gray-800 bg-white dark:bg-gray-900 px-4 py-2 flex flex-col gap-0.5"> | ||
| <NuxtLink to="/" class="px-3 py-2.5 rounded-md text-sm hover:bg-gray-50 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium" @click="mobileMenuOpen = false"> | ||
| 搜索 | ||
| </NuxtLink> | ||
| <NuxtLink to="/tree" class="px-3 py-2.5 rounded-md text-sm hover:bg-gray-50 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium" @click="mobileMenuOpen = false"> | ||
| 分类 | ||
| </NuxtLink> | ||
| <NuxtLink to="/stats" class="px-3 py-2.5 rounded-md text-sm hover:bg-gray-50 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium" @click="mobileMenuOpen = false"> | ||
| 统计 | ||
| </NuxtLink> | ||
| <NuxtLink to="/config" class="px-3 py-2.5 rounded-md text-sm hover:bg-gray-50 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium" @click="mobileMenuOpen = false"> | ||
| 管理 | ||
| </NuxtLink> | ||
| <NuxtLink to="/about" class="px-3 py-2.5 rounded-md text-sm hover:bg-gray-50 dark:hover:bg-gray-800 dark:text-gray-300" active-class="bg-primary-50 dark:bg-primary-900/30 text-primary-700 dark:text-primary-400 font-medium" @click="mobileMenuOpen = false"> | ||
| 关于 | ||
| </NuxtLink> | ||
| </nav> | ||
| </Transition> | ||
| </header> | ||
| <main class="flex-1"> | ||
| <main class="flex-1 bg-gray-50 dark:bg-gray-950"> | ||
| <NuxtPage /> | ||
| </main> | ||
| </div> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| const mobileMenuOpen = ref(false) | ||
| const { isDark, toggle: toggleDark } = useDarkMode() | ||
|
|
||
| // Close menu on route change | ||
| const router = useRouter() | ||
| router.afterEach(() => { | ||
| mobileMenuOpen.value = false | ||
| }) | ||
| </script> | ||
|
|
||
| <style> | ||
| .slide-down-enter-active, | ||
| .slide-down-leave-active { | ||
| transition: all 0.2s ease; | ||
| } | ||
| .slide-down-enter-from, | ||
| .slide-down-leave-to { | ||
| opacity: 0; | ||
| max-height: 0; | ||
| } | ||
| .slide-down-enter-to, | ||
| .slide-down-leave-from { | ||
| opacity: 1; | ||
| max-height: 300px; | ||
| } | ||
| </style> |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -5,3 +5,7 @@ | |
| body { | ||
| @apply bg-gray-50 text-gray-900 min-h-screen; | ||
| } | ||
|
|
||
| html.dark body { | ||
| @apply bg-gray-950 text-gray-100; | ||
| } | ||
This file contains hidden or 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 @@ | ||
| export function useDarkMode() { | ||
| const colorMode = useCookie<string>('dark-mode', { | ||
| default: () => '', | ||
| watch: true, | ||
| maxAge: 60 * 60 * 24 * 365, // 1 year | ||
| }) | ||
|
|
||
| const isDark = computed({ | ||
| get: () => colorMode.value === 'dark', | ||
| set: (val: boolean) => { | ||
| colorMode.value = val ? 'dark' : 'light' | ||
| }, | ||
| }) | ||
|
|
||
| // Sync cookie <-> <html> class | ||
| if (import.meta.client) { | ||
| watch(isDark, (val) => { | ||
| document.documentElement.classList.toggle('dark', val) | ||
| }, { immediate: true }) | ||
| } | ||
|
|
||
| function toggle() { | ||
| isDark.value = !isDark.value | ||
| } | ||
|
|
||
| // If no cookie set yet, use system preference on client | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 先通过CSS读浏览器深色偏好,优先跟随浏览器偏好,手动设置了再往cookie写 |
||
| if (import.meta.client && !colorMode.value) { | ||
| const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches | ||
| isDark.value = prefersDark | ||
| } | ||
|
|
||
| return { isDark, toggle } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
只改了背景,没有改控件,会出现比如侧边滚动条还是亮色的情况