Skip to content

Commit 97da8fc

Browse files
committed
fix: language switch icon
1 parent ee0d326 commit 97da8fc

File tree

2 files changed

+22
-95
lines changed

2 files changed

+22
-95
lines changed

src/slots/Header/Navigation.tsx

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ import { MenuFoldOutlined } from '@ant-design/icons';
22
import { css } from '@emotion/react';
33
import type { MenuProps } from 'antd';
44
import { Menu } from 'antd';
5-
import { useLocale, useLocation, useNavData, useSiteData } from 'dumi';
6-
import { ILocale, ILocalesConfig } from 'dumi/dist/client/theme-api/types';
7-
import { useCallback } from 'react';
5+
import { useLocation, useNavData } from 'dumi';
86
import ExternalLink from '../../common/ExternalLink';
97
import useAdditionalThemeConfig from '../../hooks/useAdditionalThemeConfig';
108
import useSiteToken from '../../hooks/useSiteToken';
11-
import { ILocaleEnhance } from '../../types';
12-
import { getTargetLocalePath } from '../../utils';
139
import { getMoreLinksGroup } from './More';
1410
import { type IResponsive } from './index';
1511

@@ -86,49 +82,10 @@ const useStyle = () => {
8682
};
8783
};
8884

89-
function getNextLang(locale: ILocale, locales: ILocalesConfig, localesEnhance?: ILocaleEnhance[]) {
90-
const changeLangByHostname = localesEnhance && localesEnhance.every((item) => item.hostname);
91-
if (
92-
typeof window !== 'undefined' &&
93-
changeLangByHostname &&
94-
window.location.hostname !== 'localhost'
95-
) {
96-
const nextLocaleEnhance = localesEnhance.find(
97-
(item) => item.hostname !== window.location.hostname
98-
);
99-
if (nextLocaleEnhance) {
100-
const nextLang = locales.find((item) => item.id === nextLocaleEnhance.id);
101-
if (nextLang) {
102-
return {
103-
...nextLang,
104-
nextPath: window.location.href.replace(
105-
window.location.hostname,
106-
nextLocaleEnhance.hostname!
107-
)
108-
};
109-
}
110-
}
111-
}
112-
113-
const nextLang = locales.filter((item) => item.id !== locale.id)[0];
114-
const nextPath = getTargetLocalePath({
115-
current: locale,
116-
target: nextLang
117-
});
118-
return {
119-
...nextLang,
120-
nextPath
121-
};
122-
}
123-
12485
export default function Navigation({ isMobile, responsive }: NavigationProps) {
125-
// 统一使用 themeConfig.nav,使用 useNavData,当存在自定义 pages 时,会导致 nav 混乱
12686
const navList = useNavData();
127-
12887
const { pathname, search } = useLocation();
129-
const { locales } = useSiteData();
130-
const locale = useLocale();
131-
const { github, moreLinks = [], localesEnhance } = useAdditionalThemeConfig();
88+
const { github, moreLinks = [] } = useAdditionalThemeConfig();
13289
const activeMenuItem = pathname.split('/').slice(0, 2).join('/');
13390
// @ts-ignore
13491
const menuItems: MenuProps['items'] = (navList ?? []).map((navItem) => {
@@ -139,45 +96,6 @@ export default function Navigation({ isMobile, responsive }: NavigationProps) {
13996
};
14097
});
14198

142-
// 获取小屏幕下多语言导航栏节点
143-
const getLangNode = useCallback(() => {
144-
if (locales.length < 2) {
145-
return null;
146-
}
147-
if (locales.length === 2) {
148-
const nextLang = getNextLang(locale, locales, localesEnhance);
149-
return {
150-
label: (
151-
<a rel="noopener noreferrer" href={nextLang.nextPath}>
152-
{nextLang.name}
153-
</a>
154-
),
155-
key: nextLang.id
156-
};
157-
}
158-
return {
159-
label: <span>{locale.name}</span>,
160-
key: 'multi-lang',
161-
children: locales
162-
.filter((item) => item.id !== locale.id)
163-
.map((item) => {
164-
const nextPath = getTargetLocalePath({
165-
current: locale,
166-
target: item
167-
});
168-
return {
169-
label: (
170-
<a rel="noopener noreferrer" href={nextPath}>
171-
{item.name}
172-
</a>
173-
),
174-
key: item.id
175-
};
176-
})
177-
};
178-
}, [locale, locales, localesEnhance]);
179-
180-
let additional: MenuProps['items'];
18199
const additionalItems: MenuProps['items'] = [
182100
github
183101
? {
@@ -189,13 +107,13 @@ export default function Navigation({ isMobile, responsive }: NavigationProps) {
189107
key: 'github'
190108
}
191109
: null,
192-
getLangNode(),
193110
...(getMoreLinksGroup(moreLinks) || [])
194111
];
195112

113+
let additional: MenuProps['items'];
196114
if (isMobile) {
197115
additional = additionalItems;
198-
} else if (responsive === 'crowded') {
116+
} else if (responsive === 'crowded' || responsive === 'narrow') {
199117
additional = [
200118
{
201119
label: <MenuFoldOutlined />,
@@ -204,9 +122,11 @@ export default function Navigation({ isMobile, responsive }: NavigationProps) {
204122
}
205123
];
206124
}
125+
207126
const items: MenuProps['items'] = [...(menuItems ?? []), ...(additional ?? [])];
208127
const menuMode = isMobile ? 'inline' : 'horizontal';
209128
const style = useStyle();
129+
210130
return (
211131
<Menu
212132
items={items}

src/slots/Header/index.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,23 @@ const Header: FC = () => {
277277
<Col
278278
flex="none"
279279
style={{
280-
width: 100,
281-
height: 50,
282280
display: 'flex',
283-
justifyContent: 'center',
284-
gap: '8px' // 使用 gap 来设置子元素之间的间距
281+
alignItems: 'center',
282+
...(isMobile
283+
? {
284+
width: 120,
285+
height: 50
286+
}
287+
: {
288+
// 桌面端布局,不设置固定宽度
289+
css: style.menuRow
290+
})
285291
}}
286292
>
287293
{isMobile ? (
288294
<>
289-
{/* 菜单图标 */}
290-
<div style={{ display: 'flex', width: 50, height: 1 }}>
295+
<div style={{ width: 80 }}>
296+
{/* 菜单图标 */}
291297
<ClassNames>
292298
{({ css: cssFn }) => (
293299
<Popover
@@ -308,13 +314,14 @@ const Header: FC = () => {
308314
</ClassNames>
309315
</div>
310316
{/* LangSwitch */}
311-
<div style={{ display: 'flex', width: 50, marginLeft: 8 }}>
317+
<div style={{ display: 'flex' }}>
312318
<LangSwitch key="lang-switch" />
313319
</div>
314320
</>
315321
) : (
316-
// 桌面端布局
317-
<div css={style.menuRow}>{menu}</div>
322+
// 桌面端布局,保持原有菜单
323+
// eslint-disable-next-line react/jsx-no-useless-fragment
324+
<>{menu}</>
318325
)}
319326
</Col>
320327
</Row>

0 commit comments

Comments
 (0)