Skip to content

Commit 363b1e5

Browse files
ldezimfing
andauthored
feat(navbar): language switcher (#760)
* feat(navbar): language switcher * docs: add language-switch * chore: just for the demo * fix: drop conflicting CSS * fix: use constant * fix: pre-existing bug with rtl on mobile * docs: add comment to explain the algo * chore: more readable algo * review Co-authored-by: Xin <[email protected]> * feat: different icons * feat: icon as param * fix: inconsitency with rtl * fix: render inside the sidebar * chore: remove the demo --------- Co-authored-by: Xin <[email protected]>
1 parent b2ff662 commit 363b1e5

File tree

8 files changed

+72
-6
lines changed

8 files changed

+72
-6
lines changed

assets/js/lang.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,32 @@
77
const optionsElement = switcher.nextElementSibling;
88
optionsElement.classList.toggle('hx:hidden');
99

10-
// Calculate position of language options element
10+
// Calculate the position of a language options element.
1111
const switcherRect = switcher.getBoundingClientRect();
12-
const translateY = switcherRect.top - window.innerHeight - 15;
13-
optionsElement.style.transform = `translate3d(${switcherRect.left}px, ${translateY}px, 0)`;
12+
13+
// Must be called before optionsElement.clientWidth.
1414
optionsElement.style.minWidth = `${Math.max(switcherRect.width, 50)}px`;
15+
16+
const isOnTop = switcher.dataset.location === 'top';
17+
const isRTL = document.body.dir === 'rtl'
18+
19+
// Stuck on the left side of the switcher.
20+
let translateX = switcherRect.left;
21+
22+
if (isOnTop && !isRTL || !isOnTop && isRTL) {
23+
// Stuck on the right side of the switcher.
24+
translateX = switcherRect.right - optionsElement.clientWidth;
25+
}
26+
27+
// Stuck on the top of the switcher.
28+
let translateY = switcherRect.top - window.innerHeight - 15;
29+
30+
if (isOnTop) {
31+
// Stuck on the bottom of the switcher.
32+
translateY = switcherRect.top - window.innerHeight + 180;
33+
}
34+
35+
optionsElement.style.transform = `translate3d(${translateX}px, ${translateY}px, 0)`;
1536
});
1637
});
1738

exampleSite/content/docs/guide/configuration.fa.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ menu:
6969
- name: Theme Toggle
7070
params:
7171
type: theme-toggle
72+
label: true # optional, default is false
73+
```
74+
6. مُبدِّل اللغة
75+
```yaml
76+
- name: مُبدِّل اللغة
77+
params:
78+
type: language-switch
79+
label: true # optional, default is false
80+
icon: "globe-alt" # optional, default is "translate"
7281
```
7382

7483
این آیتم‌های منو را می‌توان با تنظیم پارامتر `weight` مرتب کرد.

exampleSite/content/docs/guide/configuration.ja.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ menu:
6969
- name: Theme Toggle
7070
params:
7171
type: theme-toggle
72+
label: true # optional, default is false
73+
```
74+
6. 言語スイッチャー
75+
```yaml
76+
- name: 言語スイッチャー
77+
params:
78+
type: language-switch
79+
label: true # optional, default is false
80+
icon: "globe-alt" # optional, default is "translate"
7281
```
7382

7483
これらのメニュー項目は `weight` パラメータを設定することで並べ替えられます。

exampleSite/content/docs/guide/configuration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ There are different types of menu items:
6969
- name: Theme Toggle
7070
params:
7171
type: theme-toggle
72+
label: true # optional, default is false
73+
```
74+
6. Language Switcher
75+
```yaml
76+
- name: Language Switcher
77+
params:
78+
type: language-switch
79+
label: true # optional, default is false
80+
icon: "globe-alt" # optional, default is "translate"
7281
```
7382

7483
These menu items can be sorted by setting the `weight` parameter.

exampleSite/content/docs/guide/configuration.zh-cn.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ menu:
6969
- name: Theme Toggle
7070
params:
7171
type: theme-toggle
72+
label: true # optional, default is false
73+
```
74+
6. 语言切换器
75+
```yaml
76+
- name: 语言切换器
77+
params:
78+
type: language-switch
79+
label: true # optional, default is false
80+
icon: "globe-alt" # optional, default is "translate"
7281
```
7382

7483
通过设置 `weight` 参数可以调整菜单项的排序。

layouts/_partials/language-switch.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{{- $page := .context -}}
2+
{{- $iconName := .iconName | default "globe-alt" -}}
3+
{{- $iconHeight := .iconHeight | default 12 -}}
4+
{{- $location := .location -}}
5+
6+
{{- $class := .class | default "hx:h-7 hx:px-2 hx:text-xs hx:text-gray-600 hx:transition-colors hx:dark:text-gray-400 hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50" -}}
27

38
{{- $grow := .grow -}}
49
{{- $hideLabel := .hideLabel | default false -}}
@@ -10,12 +15,13 @@
1015
<button
1116
title="{{ $changeLanguage }}"
1217
data-state="closed"
13-
class="hextra-language-switcher hx:cursor-pointer hx:h-7 hx:rounded-md hx:px-2 hx:text-left hx:text-xs hx:font-medium hx:text-gray-600 hx:transition-colors hx:dark:text-gray-400 hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:grow"
18+
data-location="{{ $location }}"
19+
class="hextra-language-switcher hx:cursor-pointer hx:rounded-md hx:text-left hx:font-medium {{ $class }} hx:grow"
1420
type="button"
1521
aria-label="{{ $changeLanguage }}"
1622
>
1723
<div class="hx:flex hx:items-center hx:gap-2 hx:capitalize">
18-
{{- partial "utils/icon" (dict "name" "globe-alt" "attributes" "height=12") -}}
24+
{{- partial "utils/icon" (dict "name" $iconName "attributes" (printf "height=%d" $iconHeight)) -}}
1925
{{- if not $hideLabel }}<span>{{ site.Language.LanguageName }}</span>{{ end -}}
2026
</div>
2127
</button>

layouts/_partials/navbar.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{{ end -}}
88
{{- end -}}
99

10+
{{- $page := . -}}
1011
{{- $iconHeight := 24 -}}
1112

1213
<div class="hextra-nav-container hx:sticky hx:top-0 hx:z-20 hx:w-full hx:bg-transparent hx:print:hidden">
@@ -39,6 +40,8 @@
3940
</a>
4041
{{- else if eq .Params.type "theme-toggle" -}}
4142
{{- partial "theme-toggle.html" (dict "iconHeight" $iconHeight "class" "hx:p-2" "hideLabel" (not .Params.label)) -}}
43+
{{- else if eq .Params.type "language-switch" -}}
44+
{{- partial "language-switch" (dict "context" $page "grow" false "hideLabel" (not .Params.label) "iconName" (.Params.icon | default "translate") "iconHeight" $iconHeight "location" "top" "class" "hx:p-2") -}}
4245
{{- else -}}
4346
{{- $active := or ($currentPage.HasMenuCurrent "main" .) ($currentPage.IsMenuCurrent "main" .) -}}
4447
{{- $activeClass := cond $active "hx:font-medium" "hx:text-gray-600 hx:hover:text-gray-800 hx:dark:text-gray-400 hx:dark:hover:text-gray-200" -}}

layouts/_partials/theme-toggle.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{- $hideLabel := .hideLabel -}}
22
{{- $iconHeight := .iconHeight | default 12 -}}
3-
{{- $class := .class | default "hx:px-2 hx:text-xs hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:font-medium hx:text-gray-600 hx:transition-colors hx:dark:text-gray-400" -}}
3+
{{- $class := .class | default "hx:h-7 hx:px-2 hx:text-xs hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:font-medium hx:text-gray-600 hx:transition-colors hx:dark:text-gray-400" -}}
44

55
{{- $changeTheme := (T "changeTheme") | default "Change theme" -}}
66
{{- $light := (T "light") | default "Light" -}}

0 commit comments

Comments
 (0)