import React, { useEffect } from 'react'; import { enUS, zhCN, ThemeEditor } from 'antd-token-previewer'; import { Button, ConfigProvider, message, Modal, Typography } from 'antd'; import type { ThemeConfig } from 'antd/es/config-provider/context'; 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'; const CustomTheme = () => { const [messageApi, contextHolder] = message.useMessage(); const [modalApi, modalContextHolder] = Modal.useModal(); const [locale, lang] = useLocale(locales); const [theme, setTheme] = React.useState({}); 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: (
{locale.exportDesc}
            
              
), }); }; const handleReset = () => { setTheme({}); }; return (
{`${locale.title} - Ant Design`} {contextHolder} {modalContextHolder}
{locale.title}
{ setTheme(newTheme.config); }} locale={lang === 'cn' ? zhCN : enUS} />
); }; export default CustomTheme;