Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 40 additions & 32 deletions src/components/layout/Header.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import { localizedDocsHome, localizedLandingPath } from '../../lib/locales';
import type { LanguageAlternates, Locale } from '../../lib/locales';
import LanguageSwitcher from './LanguageSwitcher.astro';

interface Props {
locale?: Locale;
Expand All @@ -11,9 +12,6 @@ const { locale = 'vi', languageAlternates } = Astro.props;
const english = locale === 'en';
const isBlog = Astro.url.pathname.startsWith('/blog');
const home = localizedLandingPath(locale);
const alternateLocale = english ? 'vi' : 'en';
const alternatePath = languageAlternates?.[alternateLocale];
const alternateLabel = english ? 'Tiếng Việt' : 'English';
const navigation = [
{ href: `${home}#experience`, label: english ? 'Explore' : 'Khám phá' },
{ href: `${home}#platforms`, label: english ? 'Platforms' : 'Nền tảng' },
Expand Down Expand Up @@ -42,10 +40,10 @@ const navigation = [
))}
</nav>
<div class="nav-actions">
{alternatePath && (
<a class="locale-link" href={alternatePath} lang={alternateLocale} hreflang={alternateLocale}>
{alternateLabel}
</a>
{languageAlternates && (
<div class="desktop-language">
<LanguageSwitcher locale={locale} paths={languageAlternates} />
</div>
)}
<a class="download-link" href={`${home}#platforms`}>
{english ? 'Download Funput' : 'Tải Funput'} <span aria-hidden="true">↓</span>
Expand All @@ -63,10 +61,10 @@ const navigation = [
<a href="/privacy/" hreflang={english ? 'vi' : undefined}>
{english ? 'Privacy policy (Vietnamese)' : 'Quyền riêng tư'}
</a>
{alternatePath && (
<a href={alternatePath} lang={alternateLocale} hreflang={alternateLocale}>
{alternateLabel}
</a>
{languageAlternates && (
<div class="mobile-language">
<LanguageSwitcher locale={locale} paths={languageAlternates} wide />
</div>
)}
</nav>
</details>
Expand Down Expand Up @@ -120,11 +118,14 @@ const navigation = [
margin-left: auto;
font-size: 12px;
}
nav a:hover,
nav a[aria-current='page'] {
.desktop-nav > a:hover,
.mobile-menu nav > a:hover,
.desktop-nav > a[aria-current='page'],
.mobile-menu nav > a[aria-current='page'] {
color: var(--orange);
}
nav a[aria-current='page'] {
.desktop-nav > a[aria-current='page'],
.mobile-menu nav > a[aria-current='page'] {
text-decoration: underline;
text-underline-offset: 7px;
}
Expand All @@ -133,18 +134,9 @@ const navigation = [
align-items: center;
gap: 12px;
}
.locale-link {
display: inline-flex;
min-height: 44px;
align-items: center;
padding-inline: 6px;
font-size: 12px;
white-space: nowrap;
}
.locale-link:hover {
color: var(--orange);
text-decoration: underline;
text-underline-offset: 4px;
.desktop-language {
display: flex;
flex: none;
}
.download-link {
font-size: 12px;
Expand Down Expand Up @@ -187,19 +179,35 @@ const navigation = [
border-radius: 10px;
box-shadow: 0 18px 40px #24251f18;
}
.mobile-menu nav a {
.mobile-menu nav > a {
display: block;
padding: 13px;
font-size: 14px;
border-radius: 5px;
}
.mobile-menu nav a:hover {
background: #e8eddb;
.mobile-menu nav > a:hover {
background: var(--sage);
}
.mobile-language {
margin-top: 6px;
padding-top: 10px;
border-top: 1px solid var(--line);
}
@media (width > 760px) and (width <= 900px) {
.site-header,
.site-header {
gap: 16px;
}
.desktop-nav {
gap: 18px;
gap: 14px;
}
.nav-actions {
gap: 8px;
}
.download-link {
padding-inline: 16px;
}
.download-link span {
margin-left: 12px;
}
}
@media (max-width: 760px) {
Expand All @@ -208,7 +216,7 @@ const navigation = [
gap: 12px;
}
.desktop-nav,
.locale-link {
.desktop-language {
display: none;
}
.mobile-menu {
Expand Down
98 changes: 98 additions & 0 deletions src/components/layout/LanguageSwitcher.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
import type { LanguageAlternates, Locale } from '../../lib/locales';

interface Props {
locale: Locale;
paths: LanguageAlternates;
wide?: boolean;
}

const { locale, paths, wide = false } = Astro.props;
const languages = [
['vi', 'Tiếng Việt'],
['en', 'English'],
] as const;
---

<div
class:list={['language-switcher', { 'language-switcher-wide': wide }]}
role="group"
aria-label={locale === 'vi' ? 'Ngôn ngữ' : 'Language'}
>
{languages.map(([language, name]) => {
const current = language === locale;
const Option = current ? 'span' : 'a';
return (
<Option
class="language-option"
lang={language}
href={current ? undefined : paths[language]}
hreflang={current ? undefined : language}
aria-current={current ? 'true' : undefined}
>
<span class="language-code" aria-hidden="true">
{language.toUpperCase()}
</span>
<span class="language-name">{name}</span>
</Option>
);
})}
</div>

<style>
.language-switcher {
display: inline-grid;
grid-template-columns: repeat(2, minmax(36px, 1fr));
gap: 3px;
padding: 3px;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--paper);
}
.language-option {
display: grid;
place-items: center;
position: relative;
min-height: 36px;
padding-inline: 10px;
border-radius: 5px;
color: var(--muted);
font-size: 12px;
font-weight: 600;
line-height: 1.25;
white-space: nowrap;
}
.language-option[aria-current='true'] {
background: var(--sage);
color: var(--ink);
}
a.language-option:hover {
background: var(--line);
color: var(--ink);
}
a.language-option:focus-visible {
outline-offset: -2px;
}
.language-code {
display: none;
}
.language-switcher-wide {
display: grid;
}
.language-switcher-wide .language-option {
min-height: 44px;
font-size: 13px;
}
@media (max-width: 1050px) {
.language-switcher:not(.language-switcher-wide) .language-code {
display: inline;
}
.language-switcher:not(.language-switcher-wide) .language-name {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}
}
</style>
Loading