Browse Source
* docs: theme should be in searchParams * chore: use locale text * chore: fix searchParams usage * fix: compact can use with dark/light theme * chore: fix lintpull/39404/head
afc163
2 years ago
committed by
GitHub
4 changed files with 71 additions and 126 deletions
@ -1,68 +1,51 @@ |
|||
import React from 'react'; |
|||
import { FloatButton, theme } from 'antd'; |
|||
import { FloatButton } from 'antd'; |
|||
import { FormattedMessage } from 'dumi'; |
|||
import { DarkTheme, Light, CompactTheme } from 'antd-token-previewer/es/icons'; |
|||
import ThemeIcon from './ThemeIcon'; |
|||
|
|||
const { defaultAlgorithm, darkAlgorithm, compactAlgorithm } = theme; |
|||
export type ThemeName = 'light' | 'dark' | 'compact'; |
|||
|
|||
export type ThemeSwitchProps = { |
|||
value: typeof defaultAlgorithm[]; |
|||
onChange: (value: typeof defaultAlgorithm[]) => void; |
|||
value?: ThemeName[]; |
|||
onChange: (value: ThemeName[]) => void; |
|||
}; |
|||
|
|||
const ThemeSwitch: React.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> |
|||
); |
|||
}; |
|||
const ThemeSwitch: React.FC<ThemeSwitchProps> = ({ value, onChange }) => ( |
|||
<FloatButton.Group trigger="click" icon={<ThemeIcon />}> |
|||
<FloatButton |
|||
icon={<Light />} |
|||
type={!value.includes('dark') ? 'primary' : 'default'} |
|||
onClick={() => { |
|||
if (value.includes('dark')) { |
|||
onChange(value.filter((theme) => theme !== 'dark')); |
|||
} |
|||
}} |
|||
tooltip={<FormattedMessage id="app.theme.switch.default" />} |
|||
/> |
|||
<FloatButton |
|||
icon={<DarkTheme />} |
|||
type={value.includes('dark') ? 'primary' : 'default'} |
|||
onClick={() => { |
|||
if (!value.includes('dark')) { |
|||
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" />} |
|||
/> |
|||
</FloatButton.Group> |
|||
); |
|||
|
|||
export default ThemeSwitch; |
|||
|
Loading…
Reference in new issue