2023-09-08 17:42:12 +08:00
|
|
|
import React from 'react';
|
2023-08-07 10:44:04 +08:00
|
|
|
import { BgColorsOutlined, SmileOutlined } from '@ant-design/icons';
|
2023-09-08 17:42:12 +08:00
|
|
|
import { FloatButton } from 'antd';
|
|
|
|
import { useTheme } from 'antd-style';
|
2023-08-07 10:44:04 +08:00
|
|
|
import { CompactTheme, DarkTheme } from 'antd-token-previewer/es/icons';
|
|
|
|
// import { Motion } from 'antd-token-previewer/es/icons';
|
2022-12-15 12:07:06 +08:00
|
|
|
import { FormattedMessage, Link, useLocation } from 'dumi';
|
2023-09-08 17:42:12 +08:00
|
|
|
|
|
|
|
import useThemeAnimation from '../../../hooks/useThemeAnimation';
|
2022-12-15 12:07:06 +08:00
|
|
|
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-08-07 10:44:04 +08:00
|
|
|
export type ThemeName = 'light' | 'dark' | 'compact' | 'motion-off' | 'happy-work';
|
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
|
|
|
};
|
|
|
|
|
2023-07-07 18:28:13 +08:00
|
|
|
const ThemeSwitch: React.FC<ThemeSwitchProps> = (props) => {
|
2022-12-19 11:42:41 +08:00
|
|
|
const { value = ['light'], onChange } = props;
|
2023-07-20 19:27:33 +08:00
|
|
|
const token = useTheme();
|
2022-12-15 12:07:06 +08:00
|
|
|
const { pathname, search } = useLocation();
|
2023-04-23 15:42:36 +08:00
|
|
|
|
2023-08-07 10:44:04 +08:00
|
|
|
// const isMotionOff = value.includes('motion-off');
|
|
|
|
const isHappyWork = value.includes('happy-work');
|
2023-09-08 17:42:12 +08:00
|
|
|
const isDark = value.includes('dark');
|
|
|
|
|
|
|
|
const toggleAnimationTheme = useThemeAnimation();
|
2023-04-23 15:42:36 +08:00
|
|
|
|
2022-12-15 12:07:06 +08:00
|
|
|
return (
|
2023-08-08 19:48:41 +08:00
|
|
|
<FloatButton.Group
|
|
|
|
trigger="click"
|
|
|
|
icon={<ThemeIcon />}
|
|
|
|
aria-label="Theme Switcher"
|
|
|
|
badge={{ dot: true }}
|
|
|
|
style={{ zIndex: 1010 }}
|
|
|
|
>
|
2022-12-15 12:07:06 +08:00
|
|
|
<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 />}
|
2023-09-08 17:42:12 +08:00
|
|
|
type={isDark ? 'primary' : 'default'}
|
|
|
|
onClick={(e) => {
|
|
|
|
// Toggle animation when switch theme
|
|
|
|
toggleAnimationTheme(e, isDark);
|
|
|
|
|
|
|
|
if (isDark) {
|
2022-12-15 12:07:06 +08:00
|
|
|
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-08-07 10:44:04 +08:00
|
|
|
{/* Too many float button. Hide motion one */}
|
|
|
|
{/* <FloatButton
|
2023-04-23 15:42:36 +08:00
|
|
|
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'}
|
|
|
|
/>
|
|
|
|
}
|
2023-08-07 10:44:04 +08:00
|
|
|
/> */}
|
|
|
|
<FloatButton
|
2023-08-08 19:48:41 +08:00
|
|
|
badge={{ dot: true }}
|
2023-08-07 10:44:04 +08:00
|
|
|
icon={<SmileOutlined />}
|
|
|
|
type={isHappyWork ? 'primary' : 'default'}
|
|
|
|
onClick={() => {
|
|
|
|
if (isHappyWork) {
|
|
|
|
onChange(value.filter((theme) => theme !== 'happy-work'));
|
|
|
|
} else {
|
|
|
|
onChange([...value, 'happy-work']);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
tooltip={
|
|
|
|
<FormattedMessage
|
|
|
|
id={isHappyWork ? 'app.theme.switch.happy-work.off' : 'app.theme.switch.happy-work.on'}
|
|
|
|
/>
|
|
|
|
}
|
2023-04-23 15:42:36 +08:00
|
|
|
/>
|
2022-12-15 12:07:06 +08:00
|
|
|
</FloatButton.Group>
|
|
|
|
);
|
|
|
|
};
|
2022-11-22 17:00:28 +08:00
|
|
|
|
|
|
|
export default ThemeSwitch;
|