Skip to content

Commit f75a5a2

Browse files
committed
fix: unify mobile lang toggle and right-align ossheroes mobile nav
- www: lang toggle shows only the target language (EN/中文) with whitespace-nowrap to stop mobile wrapping - ossheroes: replace 中/EN dual buttons with a single icon+label toggle matching the www style; update i18n.ts apply() accordingly - ossheroes: right-align mobile navbar items (incl. past-rankings dropdown); desktop layout unchanged
1 parent 31ea013 commit f75a5a2

4 files changed

Lines changed: 39 additions & 22 deletions

File tree

apps/ossheroes/src/components/Navbar.astro

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,21 @@ const pastYears = getPastYears();
4242
<li class="nav-item">
4343
<a href="https://opensource.win/" class="nav-link" data-i18n-zh="回到官网" data-i18n-en="Main Site">回到官网</a>
4444
</li>
45-
<!-- 语言切换:中 / EN,与官网共用 localStorage key osw-language -->
46-
<li class="nav-item lang-switch" aria-label="Language / 语言">
47-
<button type="button" class="lang-btn" data-lang-switch="zh" onclick="window.OSW_I18N && OSW_I18N.set('zh')">中</button><span class="lang-sep" aria-hidden="true">/</span><button type="button" class="lang-btn" data-lang-switch="en" onclick="window.OSW_I18N && OSW_I18N.set('en')">EN</button>
45+
<!-- 语言切换:单按钮显示目标语言,与官网共用 localStorage key osw-language -->
46+
<li class="nav-item lang-switch">
47+
<button
48+
type="button"
49+
class="lang-btn"
50+
data-lang-en="中文"
51+
data-lang-zh="EN"
52+
data-aria-en="切换到中文"
53+
data-aria-zh="Switch to English"
54+
aria-label="Switch to English"
55+
onclick="window.OSW_I18N && OSW_I18N.set(document.documentElement.lang === 'zh-CN' ? 'en' : 'zh')"
56+
>
57+
<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m5 8 6 6" /><path d="m4 14 6-6 2-3" /><path d="M2 5h12" /><path d="M7 2h1" /><path d="m22 22-5-10-5 10" /><path d="M14 18h6" /></svg>
58+
<span data-lang-label>EN</span>
59+
</button>
4860
</li>
4961
</ul>
5062
</div>

apps/ossheroes/src/scripts/i18n.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,14 @@ function apply(lang: Lang) {
6464
for (let i = 0; i < nodes.length; i++) {
6565
nodes[i].innerHTML = nodes[i].getAttribute(attr) ?? '';
6666
}
67-
const btns = document.querySelectorAll('[data-lang-switch]');
68-
for (let j = 0; j < btns.length; j++) {
69-
const active = btns[j].getAttribute('data-lang-switch') === lang;
70-
btns[j].classList.toggle('active', active);
71-
btns[j].setAttribute('aria-pressed', active ? 'true' : 'false');
67+
/* 单按钮语言切换:按钮文案为目标语言(中文页显示 EN,英文页显示 中文) */
68+
const toggle = document.querySelector<HTMLButtonElement>('.lang-btn[data-lang-en]');
69+
if (toggle) {
70+
const label = toggle.querySelector('[data-lang-label]');
71+
const text = toggle.getAttribute(`data-lang-${lang}`);
72+
if (label && text) label.textContent = text;
73+
const ariaLabel = toggle.getAttribute(`data-aria-${lang}`);
74+
if (ariaLabel) toggle.setAttribute('aria-label', ariaLabel);
7275
}
7376
}
7477

apps/ossheroes/src/styles/global.css

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ a:hover {
136136
.navbar-nav {
137137
display: flex;
138138
flex-direction: column;
139+
align-items: flex-end;
139140
list-style: none;
140141
margin: 0;
141142
padding: 0.5rem 0;
@@ -185,6 +186,7 @@ a:hover {
185186
border: 1px solid var(--osw-border);
186187
border-radius: 0;
187188
min-width: 10rem;
189+
text-align: right;
188190
}
189191
.nav-dropdown.open .dropdown-menu {
190192
display: block;
@@ -221,6 +223,7 @@ a:hover {
221223
right: 0;
222224
top: 100%;
223225
z-index: 1000;
226+
text-align: left;
224227
}
225228
}
226229

@@ -639,33 +642,32 @@ a.user-item:hover .nickname {
639642
line-height: 1.8;
640643
}
641644

642-
/* 导航语言切换:中 / EN */
645+
/* 导航语言切换:单按钮显示目标语言,与官网首页 #lang-toggle 同款观感 */
643646
.lang-switch {
644-
display: flex;
645-
align-items: center;
646647
padding: 0.5rem;
647-
gap: 2px;
648648
}
649649
.lang-btn {
650+
display: inline-flex;
651+
align-items: center;
652+
gap: 0.5rem;
650653
background: none;
651-
border: 1px solid transparent;
654+
border: none;
652655
border-radius: 0;
653656
color: var(--osw-muted);
654657
font-family: var(--osw-font);
655658
font-size: 14px;
656-
padding: 0 4px;
659+
padding: 0;
657660
cursor: pointer;
661+
white-space: nowrap;
662+
transition: color 0.2s;
658663
}
659664
.lang-btn:hover,
660665
.lang-btn:focus {
661666
color: var(--osw-primary);
662667
}
663-
.lang-btn.active {
664-
color: var(--osw-primary);
665-
border-color: var(--osw-border);
666-
}
667-
.lang-sep {
668-
color: var(--osw-border);
668+
.lang-btn svg {
669+
width: 1rem;
670+
height: 1rem;
669671
}
670672

671673
/* 详情页顶部终端风页面头部:grid 背景 + 径向辉光 + 扫描线 + 描边大字,零图片素材 */

apps/www/src/pages/index.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ const jsonLd = {
243243
</div>
244244

245245
<div class="flex items-center gap-4">
246-
<button id="lang-toggle" type="button" class="inline-flex items-center font-mono text-muted-foreground hover:text-primary transition-colors text-sm px-3 py-2" data-lang-en="EN / 中文" data-lang-zh="中文 / EN" data-aria-en="Switch to Chinese" data-aria-zh="切换到英文" aria-label="Switch to Chinese">
246+
<button id="lang-toggle" type="button" class="inline-flex items-center whitespace-nowrap font-mono text-muted-foreground hover:text-primary transition-colors text-sm px-3 py-2" data-lang-en="中文" data-lang-zh="EN" data-aria-en="Switch to Chinese" data-aria-zh="切换到英文" aria-label="Switch to Chinese">
247247
<svg aria-hidden="true" class="mr-2 h-4 w-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m5 8 6 6" /><path d="m4 14 6-6 2-3" /><path d="M2 5h12" /><path d="M7 2h1" /><path d="m22 22-5-10-5 10" /><path d="M14 18h6" /></svg>
248-
<span data-lang-label>EN / 中文</span>
248+
<span data-lang-label>中文</span>
249249
</button>
250250

251251
<a href={`${SITE}heroes/`} class="inline-flex items-center justify-center border bg-background text-sm px-4 py-2 font-mono text-foreground hover:bg-accent hover:text-accent-foreground transition-colors hidden sm:inline-flex">

0 commit comments

Comments
 (0)