2022-11-30 11:05:41 +08:00
|
|
|
import type { FC } from 'react';
|
|
|
|
import React from 'react';
|
2022-11-22 17:00:28 +08:00
|
|
|
import { FloatButton, theme } from 'antd';
|
|
|
|
import { DarkTheme, Light, CompactTheme } from 'antd-token-previewer/es/icons';
|
2022-11-30 11:05:41 +08:00
|
|
|
import ThemeIcon from './ThemeIcon';
|
2022-11-22 17:00:28 +08:00
|
|
|
|
|
|
|
const { defaultAlgorithm, darkAlgorithm, compactAlgorithm } = theme;
|
|
|
|
|
|
|
|
export type ThemeSwitchProps = {
|
|
|
|
value: typeof defaultAlgorithm[];
|
|
|
|
onChange: (value: typeof defaultAlgorithm[]) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ThemeSwitch: FC<ThemeSwitchProps> = ({ value, onChange }) => {
|
|
|
|
const handleLightSwitch = () => {
|
|
|
|
let newValue = [...value];
|
|
|
|
if (value.includes(darkAlgorithm)) {
|
|
|
|
newValue = newValue.filter((item) => item !== darkAlgorithm);
|
|
|
|
}
|
|
|
|
if (!value.includes(defaultAlgorithm)) {
|
|
|
|
newValue.unshift(defaultAlgorithm);
|
|
|
|
}
|
|
|
|
onChange(newValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDarkSwitch = () => {
|
|
|
|
let newValue = [...value];
|
|
|
|
if (value.includes(defaultAlgorithm)) {
|
|
|
|
newValue = newValue.filter((item) => item !== defaultAlgorithm);
|
|
|
|
}
|
|
|
|
if (!value.includes(darkAlgorithm)) {
|
|
|
|
newValue.push(darkAlgorithm);
|
|
|
|
}
|
|
|
|
onChange(newValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleCompactSwitch = () => {
|
|
|
|
if (value.includes(compactAlgorithm)) {
|
|
|
|
onChange(value.filter((item) => item !== compactAlgorithm));
|
|
|
|
} else {
|
|
|
|
onChange([...value, compactAlgorithm]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FloatButton.Group trigger="click" icon={<ThemeIcon />}>
|
|
|
|
<FloatButton
|
|
|
|
icon={<Light />}
|
|
|
|
type={value.includes(defaultAlgorithm) ? 'primary' : 'default'}
|
|
|
|
onClick={handleLightSwitch}
|
|
|
|
tooltip="Light"
|
|
|
|
/>
|
|
|
|
<FloatButton
|
|
|
|
icon={<DarkTheme />}
|
|
|
|
type={value.includes(darkAlgorithm) ? 'primary' : 'default'}
|
|
|
|
onClick={handleDarkSwitch}
|
|
|
|
tooltip="Dark"
|
|
|
|
/>
|
|
|
|
<FloatButton
|
|
|
|
icon={<CompactTheme />}
|
|
|
|
type={value.includes(compactAlgorithm) ? 'primary' : 'default'}
|
|
|
|
onClick={handleCompactSwitch}
|
|
|
|
tooltip="Compact"
|
|
|
|
/>
|
|
|
|
</FloatButton.Group>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ThemeSwitch;
|