Skip to content

Commit 3b4ff2a

Browse files
committed
fix language switcher remaining open on change
1 parent 2884236 commit 3b4ff2a

4 files changed

Lines changed: 134 additions & 109 deletions

File tree

‎src/lib/components/ContactSection.svelte‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
}
187187
188188
.email-card strong {
189-
font-size: clamp(1.6rem, 7vw, 2.3rem);
189+
font-size: clamp(1.6rem, 6vw, 2.3rem);
190190
}
191191
192192
.meta-grid {

‎src/lib/components/HeroSection.svelte‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@
116116
.title-accent {
117117
color: var(--color-primary-container);
118118
font-style: italic;
119-
margin-left: 0.18ch;
120119
}
121120
122121
p {
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<script lang="ts">
2+
import { ChevronDown } from "@lucide/svelte";
3+
import { page } from "$app/state";
4+
import {
5+
defaultLocale,
6+
localeMeta,
7+
localizePath,
8+
type AppLocale,
9+
} from "$lib/i18n/locales";
10+
import { locale as localeStore } from "$lib/translations";
11+
12+
let { pathname }: { pathname: string } = $props();
13+
let isOpen = $state(false);
14+
15+
const copy = {
16+
en: {
17+
languageLabel: "Language",
18+
languageName: "English",
19+
},
20+
ro: {
21+
languageLabel: "Limba",
22+
languageName: "Romana",
23+
},
24+
} as const;
25+
26+
const activeLocale = $derived(
27+
($localeStore as AppLocale | undefined) ?? defaultLocale,
28+
);
29+
const strings = $derived(copy[activeLocale]);
30+
const switcherOptions = $derived([
31+
{
32+
code: "en" as const,
33+
flag: localeMeta.en.flag,
34+
label: copy.en.languageName,
35+
href: `${localizePath(pathname, "en")}${page.url.hash}`,
36+
},
37+
{
38+
code: "ro" as const,
39+
flag: localeMeta.ro.flag,
40+
label: copy.ro.languageName,
41+
href: `${localizePath(pathname, "ro")}${page.url.hash}`,
42+
},
43+
]);
44+
const currentOption = $derived(
45+
switcherOptions.find((option) => option.code === activeLocale) ??
46+
switcherOptions[0],
47+
);
48+
const alternateOptions = $derived(
49+
switcherOptions.filter((option) => option.code !== activeLocale),
50+
);
51+
</script>
52+
53+
<details bind:open={isOpen} class="locale-switcher">
54+
<summary aria-label={strings.languageLabel}>
55+
<span class="flag" aria-hidden="true">{currentOption.flag}</span>
56+
<ChevronDown size={18} strokeWidth={2.25} />
57+
</summary>
58+
<div class="locale-menu">
59+
{#each alternateOptions as option}
60+
<a
61+
href={option.href}
62+
data-sveltekit-preload-data="off"
63+
data-sveltekit-preload-code="off"
64+
lang={option.code}
65+
aria-label={option.label}
66+
onclick={() => {
67+
isOpen = false;
68+
}}
69+
>
70+
<span class="flag" aria-hidden="true">{option.flag}</span>
71+
</a>
72+
{/each}
73+
</div>
74+
</details>
75+
76+
<style>
77+
.locale-switcher {
78+
position: relative;
79+
}
80+
81+
.locale-switcher summary {
82+
list-style: none;
83+
display: inline-flex;
84+
align-items: center;
85+
gap: 0.55rem;
86+
color: var(--color-ink);
87+
cursor: pointer;
88+
}
89+
90+
.locale-switcher summary::-webkit-details-marker {
91+
display: none;
92+
}
93+
94+
.locale-menu {
95+
position: absolute;
96+
top: calc(100% + 0.9rem);
97+
right: 0;
98+
display: grid;
99+
grid-template-columns: minmax(0, 1fr);
100+
gap: 0.35rem;
101+
min-width: 3.75rem;
102+
padding: 0.45rem;
103+
background: color-mix(in srgb, var(--color-surface) 94%, white);
104+
border: 1px solid rgba(138, 112, 118, 0.12);
105+
border-radius: 1rem;
106+
box-shadow: var(--shadow-ambient);
107+
}
108+
109+
.locale-menu a {
110+
display: flex;
111+
align-items: center;
112+
justify-content: center;
113+
padding: 0.7rem;
114+
border-radius: 0.75rem;
115+
}
116+
117+
.locale-menu a:hover {
118+
background: rgba(159, 24, 83, 0.08);
119+
}
120+
121+
.flag {
122+
font-size: 1.1rem;
123+
line-height: 1;
124+
}
125+
</style>

‎src/lib/components/TopNav.svelte‎

Lines changed: 8 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
<script lang="ts">
2-
import { ChevronDown, Menu, MoonStar, X } from "@lucide/svelte";
3-
import { page } from "$app/state";
4-
import {
5-
defaultLocale,
6-
localeMeta,
7-
localizePath,
8-
type AppLocale,
9-
} from "$lib/i18n/locales";
10-
import { locale as localeStore } from "$lib/translations";
2+
import { Menu, X } from "@lucide/svelte";
3+
import { defaultLocale, localizePath, type AppLocale } from "$lib/i18n/locales";
114
import Logo from "./Logo.svelte";
5+
import LanguageSwitcher from "./LanguageSwitcher.svelte";
6+
import { locale as localeStore } from "$lib/translations";
127
138
let { pathname }: { pathname: string } = $props();
149
let isMobileMenuOpen = $state(false);
@@ -17,28 +12,22 @@
1712
en: {
1813
homeLabel: "SAMIZDATA home",
1914
primaryLabel: "Primary navigation",
20-
languageLabel: "Language",
2115
mobileMenuOpen: "Open menu",
2216
mobileMenuClose: "Close menu",
2317
themeToggle: "Toggle theme",
2418
work: "What we do",
2519
about: "Who we are",
2620
contact: "Contact",
27-
languageName: "English",
28-
otherLanguageName: "Romanian",
2921
},
3022
ro: {
3123
homeLabel: "Pagina principala SAMIZDATA",
3224
primaryLabel: "Navigatie principala",
33-
languageLabel: "Limba",
3425
mobileMenuOpen: "Deschide meniul",
3526
mobileMenuClose: "Inchide meniul",
3627
themeToggle: "Schimba tema",
3728
work: "Ce facem",
3829
about: "Cine suntem",
3930
contact: "Contact",
40-
languageName: "Romana",
41-
otherLanguageName: "English",
4231
},
4332
} as const;
4433
@@ -54,27 +43,6 @@
5443
href: `${localizePath("/", activeLocale)}#contact`,
5544
},
5645
]);
57-
const switcherOptions = $derived([
58-
{
59-
code: "en" as const,
60-
flag: localeMeta.en.flag,
61-
label: copy.en.languageName,
62-
href: `${localizePath(pathname, "en")}${page.url.hash}`,
63-
},
64-
{
65-
code: "ro" as const,
66-
flag: localeMeta.ro.flag,
67-
label: copy.ro.languageName,
68-
href: `${localizePath(pathname, "ro")}${page.url.hash}`,
69-
},
70-
]);
71-
const currentOption = $derived(
72-
switcherOptions.find((option) => option.code === activeLocale) ??
73-
switcherOptions[0],
74-
);
75-
const alternateOptions = $derived(
76-
switcherOptions.filter((option) => option.code !== activeLocale),
77-
);
7846
</script>
7947

8048
<nav class="nav">
@@ -90,25 +58,7 @@
9058
</div>
9159

9260
<div class="actions">
93-
<details class="locale-switcher">
94-
<summary aria-label={strings.languageLabel}>
95-
<span class="flag" aria-hidden="true">{currentOption.flag}</span>
96-
<ChevronDown size={18} strokeWidth={2.25} />
97-
</summary>
98-
<div class="locale-menu">
99-
{#each alternateOptions as option}
100-
<a
101-
href={option.href}
102-
data-sveltekit-preload-data="off"
103-
data-sveltekit-preload-code="off"
104-
lang={option.code}
105-
aria-label={option.label}
106-
>
107-
<span class="flag" aria-hidden="true">{option.flag}</span>
108-
</a>
109-
{/each}
110-
</div>
111-
</details>
61+
<LanguageSwitcher {pathname} />
11262
<!--
11363
<button
11464
type="button"
@@ -190,14 +140,14 @@
190140
191141
.links a,
192142
.actions button,
193-
.locale-switcher summary {
143+
.actions :global(summary) {
194144
color: rgba(27, 28, 25, 0.7);
195145
transition: color 180ms ease;
196146
}
197147
198148
.links a:hover,
199149
.actions button:hover,
200-
.locale-switcher summary:hover {
150+
.actions :global(summary:hover) {
201151
color: var(--color-primary-container);
202152
}
203153
@@ -208,8 +158,7 @@
208158
margin-left: auto;
209159
}
210160
211-
button,
212-
summary {
161+
button {
213162
display: inline-flex;
214163
align-items: center;
215164
gap: 0.45rem;
@@ -219,54 +168,6 @@
219168
cursor: pointer;
220169
}
221170
222-
.locale-switcher {
223-
position: relative;
224-
}
225-
226-
.locale-switcher summary {
227-
list-style: none;
228-
display: inline-flex;
229-
align-items: center;
230-
gap: 0.55rem;
231-
color: var(--color-ink);
232-
}
233-
234-
.locale-switcher summary::-webkit-details-marker {
235-
display: none;
236-
}
237-
238-
.locale-menu {
239-
position: absolute;
240-
top: calc(100% + 0.9rem);
241-
right: 0;
242-
display: grid;
243-
grid-template-columns: minmax(0, 1fr);
244-
gap: 0.35rem;
245-
min-width: 3.75rem;
246-
padding: 0.45rem;
247-
background: color-mix(in srgb, var(--color-surface) 94%, white);
248-
border: 1px solid rgba(138, 112, 118, 0.12);
249-
border-radius: 1rem;
250-
box-shadow: var(--shadow-ambient);
251-
}
252-
253-
.locale-menu a {
254-
display: flex;
255-
align-items: center;
256-
justify-content: center;
257-
padding: 0.7rem;
258-
border-radius: 0.75rem;
259-
}
260-
261-
.locale-menu a:hover {
262-
background: rgba(159, 24, 83, 0.08);
263-
}
264-
265-
.flag {
266-
font-size: 1.1rem;
267-
line-height: 1;
268-
}
269-
270171
.menu-toggle {
271172
display: inline-flex;
272173
}

0 commit comments

Comments
 (0)