2023-04-23 15:42:36 +08:00
|
|
|
import { BgColorsOutlined } from '@ant-design/icons';
|
2022-12-08 17:44:49 +08:00
|
|
|
import { FloatButton } from 'antd';
|
2023-04-23 15:42:36 +08:00
|
|
|
import { CompactTheme, DarkTheme, Motion } from 'antd-token-previewer/es/icons';
|
2022-12-15 12:07:06 +08:00
|
|
|
import { FormattedMessage, Link, useLocation } from 'dumi';
|
2023-04-23 15:42:36 +08:00
|
|
|
import React from 'react';
|
2022-12-15 12:07:06 +08:00
|
|
|
import useSiteToken from '../../../hooks/useSiteToken';
|
|
|
|
import { getLocalizedPathname, isZhCN } from '../../utils';
|
2022-11-30 11:05:41 +08:00
|
|
|
import ThemeIcon from './ThemeIcon';
|
2022-11-22 17:00:28 +08:00
|
|
|
|
2023-04-23 15:42:36 +08:00
|
|
|
export type ThemeName = 'light' | 'dark' | 'compact' | 'motion-off';
|
2022-11-22 17:00:28 +08:00
|
|
|
|
|
|
|
export type ThemeSwitchProps = {
|
2022-12-08 17:44:49 +08:00
|
|
|
value?: ThemeName[];
|
|
|
|
onChange: (value: ThemeName[]) => void;
|
2022-11-22 17:00:28 +08:00
|
|
|
};
|
|
|
|
|
2022-12-19 11:42:41 +08:00
|
|
|
const ThemeSwitch: React.FC<ThemeSwitchProps> = (props: ThemeSwitchProps) => {
|
|
|
|
const { value = ['light'], onChange } = props;
|
2022-12-15 12:07:06 +08:00
|
|
|
const { token } = useSiteToken();
|
|
|
|
const { pathname, search } = useLocation();
|
2023-04-23 15:42:36 +08:00
|
|
|
|
|
|
|
const isMotionOff = value.includes('motion-off');
|
|
|
|
|
2022-12-15 12:07:06 +08:00
|
|
|
return (
|
|
|
|
<FloatButton.Group trigger="click" icon={<ThemeIcon />}>
|
|
|
|
<Link
|
|
|
|
to={getLocalizedPathname('/theme-editor', isZhCN(pathname), search)}
|
|
|
|
style={{ display: 'block', marginBottom: token.margin }}
|
|
|
|
>
|
|
|
|
<FloatButton
|
|
|
|
icon={<BgColorsOutlined />}
|
|
|
|
tooltip={<FormattedMessage id="app.footer.theme" />}
|
|
|
|
/>
|
|
|
|
</Link>
|
|
|
|
<FloatButton
|
|
|
|
icon={<DarkTheme />}
|
|
|
|
type={value.includes('dark') ? 'primary' : 'default'}
|
|
|
|
onClick={() => {
|
|
|
|
if (value.includes('dark')) {
|
|
|
|
onChange(value.filter((theme) => theme !== 'dark'));
|
|
|
|
} else {
|
|
|
|
onChange([...value, 'dark']);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
tooltip={<FormattedMessage id="app.theme.switch.dark" />}
|
|
|
|
/>
|
|
|
|
<FloatButton
|
|
|
|
icon={<CompactTheme />}
|
|
|
|
type={value.includes('compact') ? 'primary' : 'default'}
|
|
|
|
onClick={() => {
|
|
|
|
if (value.includes('compact')) {
|
|
|
|
onChange(value.filter((theme) => theme !== 'compact'));
|
|
|
|
} else {
|
|
|
|
onChange([...value, 'compact']);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
tooltip={<FormattedMessage id="app.theme.switch.compact" />}
|
|
|
|
/>
|
2023-04-23 15:42:36 +08:00
|
|
|
<FloatButton
|
|
|
|
icon={<Motion />}
|
|
|
|
type={!isMotionOff ? 'primary' : 'default'}
|
|
|
|
onClick={() => {
|
|
|
|
if (isMotionOff) {
|
|
|
|
onChange(value.filter((theme) => theme !== 'motion-off'));
|
|
|
|
} else {
|
|
|
|
onChange([...value, 'motion-off']);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
tooltip={
|
|
|
|
<FormattedMessage
|
|
|
|
id={isMotionOff ? 'app.theme.switch.motion.off' : 'app.theme.switch.motion.on'}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2022-12-15 12:07:06 +08:00
|
|
|
</FloatButton.Group>
|
|
|
|
);
|
|
|
|
};
|
2022-11-22 17:00:28 +08:00
|
|
|
|
|
|
|
export default ThemeSwitch;
|