File size: 4,731 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
import React, { use, useRef } from 'react';
import {
BgColorsOutlined,
LinkOutlined,
SmileOutlined,
SunOutlined,
SyncOutlined,
} from '@ant-design/icons';
import { Badge, Button, Dropdown } from 'antd';
import type { MenuProps } from 'antd';
import { CompactTheme, DarkTheme } from 'antd-token-previewer/es/icons';
import { FormattedMessage, useLocation } from 'dumi';
import useThemeAnimation from '../../../hooks/useThemeAnimation';
import type { SiteContextProps } from '../../slots/SiteContext';
import SiteContext from '../../slots/SiteContext';
import { getLocalizedPathname, isZhCN, isLocalStorageNameSupported } from '../../utils';
import Link from '../Link';
import ThemeIcon from './ThemeIcon';
export type ThemeName = 'light' | 'dark' | 'auto' | 'compact' | 'motion-off' | 'happy-work';
// 主题持久化存储键名
const ANT_DESIGN_SITE_THEME = 'ant-design-site-theme';
export interface ThemeSwitchProps {
value?: ThemeName[];
}
const ThemeSwitch: React.FC<ThemeSwitchProps> = () => {
const { pathname, search } = useLocation();
const { theme, updateSiteConfig } = use<SiteContextProps>(SiteContext);
const toggleAnimationTheme = useThemeAnimation();
const lastThemeKey = useRef<string>(theme.includes('dark') ? 'dark' : 'light');
const badge = <Badge color="blue" style={{ marginTop: -1 }} />;
// 主题选项配置
const themeOptions = [
{
id: 'app.theme.switch.auto',
icon: <SyncOutlined />,
key: 'auto',
showBadge: () => theme.includes('auto'),
},
{
id: 'app.theme.switch.light',
icon: <SunOutlined />,
key: 'light',
showBadge: () => theme.includes('light'),
},
{
id: 'app.theme.switch.dark',
icon: <DarkTheme />,
key: 'dark',
showBadge: () => theme.includes('dark'),
},
{
type: 'divider',
},
{
id: 'app.theme.switch.compact',
icon: <CompactTheme />,
key: 'compact',
showBadge: () => theme.includes('compact'),
},
{
type: 'divider',
},
{
id: 'app.theme.switch.happy-work',
icon: <SmileOutlined />,
key: 'happy-work',
showBadge: () => theme.includes('happy-work'),
},
{
type: 'divider',
},
{
id: 'app.footer.theme',
icon: <BgColorsOutlined />,
key: 'theme-editor',
extra: <LinkOutlined />,
isLink: true,
linkPath: '/theme-editor',
},
];
// 构建下拉菜单项
const items = themeOptions.map((option, i) => {
if (option.type === 'divider') {
return { type: 'divider' as const, key: `divider-${i}` };
}
const { id, icon, key, showBadge, extra, isLink, linkPath } = option;
return {
label: isLink ? (
<Link to={getLocalizedPathname(linkPath!, isZhCN(pathname), search)}>
<FormattedMessage id={id} />
</Link>
) : (
<FormattedMessage id={id} />
),
icon,
key: key || i,
extra: showBadge ? (showBadge() ? badge : null) : extra,
};
});
// 处理主题切换
const handleThemeChange = (key: string, domEvent: React.MouseEvent<HTMLElement, MouseEvent>) => {
// 主题编辑器特殊处理
if (key === 'theme-editor') {
return;
}
const themeKey = key as ThemeName;
// 亮色/暗色/自动模式是互斥的
if (['light', 'dark', 'auto'].includes(key)) {
// 校验当前主题是否包含要切换的主题(避免 timeout in DOM update)
if (theme.includes(themeKey)) {
return;
}
// 亮色/暗色模式切换时应用动画效果
if (['light', 'dark'].includes(key)) {
lastThemeKey.current = key;
toggleAnimationTheme(domEvent, theme.includes('dark'));
}
const filteredTheme = theme.filter((t) => !['light', 'dark', 'auto'].includes(t));
const newTheme = [...filteredTheme, themeKey];
updateSiteConfig({
theme: newTheme,
});
// 持久化到 localStorage
if (isLocalStorageNameSupported()) {
localStorage.setItem(ANT_DESIGN_SITE_THEME, themeKey);
}
} else {
// 其他主题选项是开关式的
const hasTheme = theme.includes(themeKey);
updateSiteConfig({
theme: hasTheme ? theme.filter((t) => t !== themeKey) : [...theme, themeKey],
});
}
};
const onClick: MenuProps['onClick'] = ({ key, domEvent }) => {
handleThemeChange(key, domEvent as React.MouseEvent<HTMLElement, MouseEvent>);
};
return (
<Dropdown menu={{ items, onClick }} arrow={{ pointAtCenter: true }} placement="bottomRight">
<Button type="text" icon={<ThemeIcon />} style={{ fontSize: 16 }} />
</Dropdown>
);
};
export default ThemeSwitch;
|