2022-12-07 20:22:15 +08:00
|
|
|
import React, { useEffect } from 'react';
|
2022-12-12 11:40:18 +08:00
|
|
|
import { enUS, zhCN, ThemeEditor } from 'antd-token-previewer';
|
2022-12-07 20:22:15 +08:00
|
|
|
import { Button, ConfigProvider, message, Modal, Typography } from 'antd';
|
2022-11-30 11:05:41 +08:00
|
|
|
import type { ThemeConfig } from 'antd/es/config-provider/context';
|
2022-12-07 20:22:15 +08:00
|
|
|
import { Helmet } from 'dumi';
|
|
|
|
import { css } from '@emotion/react';
|
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
|
|
|
import { CopyOutlined } from '@ant-design/icons';
|
|
|
|
import useLocale from '../../hooks/useLocale';
|
|
|
|
|
|
|
|
const locales = {
|
|
|
|
cn: {
|
|
|
|
title: '主题编辑器',
|
|
|
|
save: '保存',
|
|
|
|
reset: '重置',
|
|
|
|
export: '导出',
|
|
|
|
exportDesc: '将下面的 JSON 对象复制到 ConfigProvider 的 theme 属性中即可。',
|
|
|
|
saveSuccessfully: '保存成功',
|
|
|
|
},
|
|
|
|
en: {
|
|
|
|
title: 'Theme Editor',
|
|
|
|
save: 'Save',
|
|
|
|
reset: 'Reset',
|
|
|
|
export: 'Export',
|
|
|
|
exportDesc: 'Copy the following JSON object to the theme prop of ConfigProvider.',
|
|
|
|
saveSuccessfully: 'Saved successfully',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const useStyle = () => ({
|
|
|
|
header: css({
|
|
|
|
display: 'flex',
|
|
|
|
height: 56,
|
|
|
|
alignItems: 'center',
|
|
|
|
padding: '0 24px',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
borderBottom: '1px solid #F0F0F0',
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
const ANT_DESIGN_V5_THEME_EDITOR_THEME = 'ant-design-v5-theme-editor-theme';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
const CustomTheme = () => {
|
2022-12-07 20:22:15 +08:00
|
|
|
const [messageApi, contextHolder] = message.useMessage();
|
|
|
|
const [modalApi, modalContextHolder] = Modal.useModal();
|
|
|
|
const [locale, lang] = useLocale(locales);
|
|
|
|
|
|
|
|
const [theme, setTheme] = React.useState<ThemeConfig>({});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const storedConfig = localStorage.getItem(ANT_DESIGN_V5_THEME_EDITOR_THEME);
|
|
|
|
if (storedConfig) {
|
|
|
|
setTheme(() => JSON.parse(storedConfig));
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const styles = useStyle();
|
|
|
|
|
|
|
|
const handleSave = () => {
|
|
|
|
localStorage.setItem(ANT_DESIGN_V5_THEME_EDITOR_THEME, JSON.stringify(theme));
|
|
|
|
messageApi.success(locale.saveSuccessfully);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCopy = (text: string, result: boolean) => {
|
|
|
|
if (result) {
|
|
|
|
messageApi.success('Copy theme config successfully!');
|
|
|
|
} else {
|
|
|
|
messageApi.error('Copy failed, please try again.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleOutput = () => {
|
|
|
|
modalApi.info({
|
|
|
|
title: locale.export,
|
|
|
|
width: 600,
|
|
|
|
content: (
|
|
|
|
<div>
|
|
|
|
<div style={{ color: 'rgba(0,0,0,0.65)' }}>{locale.exportDesc}</div>
|
|
|
|
<pre
|
|
|
|
style={{
|
|
|
|
padding: 12,
|
|
|
|
background: '#f5f5f5',
|
|
|
|
borderRadius: 4,
|
|
|
|
marginTop: 12,
|
|
|
|
position: 'relative',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<CopyToClipboard text={JSON.stringify(theme, null, 2)} onCopy={onCopy}>
|
|
|
|
<Button
|
|
|
|
type="text"
|
|
|
|
icon={<CopyOutlined />}
|
|
|
|
style={{ position: 'absolute', right: 8, top: 8 }}
|
|
|
|
/>
|
|
|
|
</CopyToClipboard>
|
|
|
|
{JSON.stringify(theme, null, 2)}
|
|
|
|
</pre>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleReset = () => {
|
|
|
|
setTheme({});
|
|
|
|
};
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2022-12-07 20:22:15 +08:00
|
|
|
<Helmet>
|
|
|
|
<title>{`${locale.title} - Ant Design`}</title>
|
|
|
|
<meta property="og:title" content={`${locale.title} - Ant Design`} />
|
|
|
|
</Helmet>
|
|
|
|
{contextHolder}
|
|
|
|
{modalContextHolder}
|
2022-11-22 17:00:28 +08:00
|
|
|
<ConfigProvider theme={{ inherit: false }}>
|
2022-12-07 20:22:15 +08:00
|
|
|
<div css={styles.header}>
|
|
|
|
<Typography.Title level={5} style={{ margin: 0 }}>
|
|
|
|
{locale.title}
|
|
|
|
</Typography.Title>
|
|
|
|
<div>
|
|
|
|
<Button onClick={handleOutput} style={{ marginRight: 8 }}>
|
|
|
|
{locale.export}
|
|
|
|
</Button>
|
|
|
|
<Button onClick={handleReset} style={{ marginRight: 8 }}>
|
|
|
|
{locale.reset}
|
|
|
|
</Button>
|
|
|
|
<Button type="primary" onClick={handleSave}>
|
|
|
|
{locale.save}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-09 12:28:04 +08:00
|
|
|
<ThemeEditor
|
2022-11-22 17:00:28 +08:00
|
|
|
theme={{ name: 'Custom Theme', key: 'test', config: theme }}
|
2022-12-07 20:22:15 +08:00
|
|
|
style={{ height: 'calc(100vh - 64px - 56px)' }}
|
2022-11-19 13:47:33 +08:00
|
|
|
onThemeChange={(newTheme) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
setTheme(newTheme.config);
|
|
|
|
}}
|
2022-12-07 20:22:15 +08:00
|
|
|
locale={lang === 'cn' ? zhCN : enUS}
|
2022-11-09 12:28:04 +08:00
|
|
|
/>
|
|
|
|
</ConfigProvider>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CustomTheme;
|