2022-03-17 14:06:21 +08:00
|
|
|
import { BugOutlined, EyeOutlined } from '@ant-design/icons';
|
2022-06-24 11:11:42 +08:00
|
|
|
import { TinyColor } from '@ctrl/tinycolor';
|
|
|
|
import { Button, Checkbox, Drawer, Form, Input, InputNumber, Space } from 'antd';
|
|
|
|
import * as React from 'react';
|
2022-03-28 23:46:30 +08:00
|
|
|
import { useState } from 'react';
|
2022-06-24 11:11:42 +08:00
|
|
|
import { useIntl } from 'react-intl';
|
2022-05-09 22:20:07 +08:00
|
|
|
import type { SeedToken } from '../../../../../components/_util/theme';
|
2022-03-24 18:44:42 +08:00
|
|
|
import { PresetColors } from '../../../../../components/_util/theme/interface';
|
2022-06-24 11:11:42 +08:00
|
|
|
import defaultSeedToken from '../../../../../components/_util/theme/themes/seed';
|
2022-03-28 23:46:30 +08:00
|
|
|
import Diff from './Diff';
|
2022-06-24 11:11:42 +08:00
|
|
|
import Preview from './Preview';
|
2022-02-18 14:17:32 +08:00
|
|
|
|
|
|
|
export interface ThemeConfigProps {
|
2022-03-17 14:06:21 +08:00
|
|
|
componentName: string;
|
2022-03-21 16:48:25 +08:00
|
|
|
defaultToken: SeedToken;
|
|
|
|
onChangeTheme: (theme: SeedToken) => void;
|
2022-02-18 14:17:32 +08:00
|
|
|
}
|
|
|
|
|
2022-06-07 23:29:12 +08:00
|
|
|
export default function DynamicTheme({
|
|
|
|
onChangeTheme,
|
|
|
|
defaultToken,
|
|
|
|
componentName,
|
|
|
|
}: ThemeConfigProps) {
|
2022-02-18 14:17:32 +08:00
|
|
|
const { formatMessage } = useIntl();
|
|
|
|
const [visible, setVisible] = React.useState(false);
|
2022-03-17 14:06:21 +08:00
|
|
|
const [previewVisible, setPreviewVisible] = React.useState(false);
|
2022-02-18 14:17:32 +08:00
|
|
|
const [form] = Form.useForm();
|
2022-03-28 23:46:30 +08:00
|
|
|
const [showDiff, setShowDiff] = useState(false);
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-03-21 16:48:25 +08:00
|
|
|
const keys = Object.keys(defaultSeedToken);
|
2022-02-18 14:17:32 +08:00
|
|
|
|
2022-03-21 16:48:25 +08:00
|
|
|
const onFinish = (nextToken: SeedToken) => {
|
2022-02-18 14:17:32 +08:00
|
|
|
onChangeTheme(nextToken);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-03-28 23:46:30 +08:00
|
|
|
{/* FIXME: need to be removed before published */}
|
|
|
|
<Diff show={showDiff} />
|
2022-02-18 14:17:32 +08:00
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
position: 'fixed',
|
|
|
|
right: 0,
|
|
|
|
bottom: 32,
|
|
|
|
fontSize: 16,
|
|
|
|
borderRadius: '4px 0 0 4px',
|
|
|
|
background: '#FFF',
|
|
|
|
boxShadow: '0 0 4px rgba(0, 0, 0, 0.3)',
|
|
|
|
padding: '8px 16px 8px 12px',
|
|
|
|
cursor: 'pointer',
|
2022-04-19 11:28:06 +08:00
|
|
|
zIndex: 1,
|
2022-02-18 14:17:32 +08:00
|
|
|
}}
|
|
|
|
onClick={() => setVisible(true)}
|
|
|
|
>
|
|
|
|
<BugOutlined /> Dynamic Theme
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Drawer
|
2022-03-17 14:06:21 +08:00
|
|
|
mask={false}
|
2022-02-18 14:17:32 +08:00
|
|
|
zIndex={10001}
|
|
|
|
visible={visible}
|
|
|
|
onClose={() => {
|
|
|
|
setVisible(false);
|
|
|
|
}}
|
|
|
|
title={formatMessage({ id: 'app.theme.switch.dynamic' })}
|
|
|
|
extra={
|
2022-03-17 14:06:21 +08:00
|
|
|
<Space>
|
2022-03-28 23:46:30 +08:00
|
|
|
<Checkbox checked={showDiff} onChange={e => setShowDiff(e.target.checked)}>
|
|
|
|
Diff
|
|
|
|
</Checkbox>
|
2022-03-17 14:06:21 +08:00
|
|
|
<Button icon={<EyeOutlined />} onClick={() => setPreviewVisible(true)} />
|
|
|
|
<Button onClick={form.submit} type="primary">
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</Space>
|
2022-02-18 14:17:32 +08:00
|
|
|
}
|
|
|
|
destroyOnClose
|
|
|
|
>
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
initialValues={defaultToken}
|
|
|
|
layout="vertical"
|
|
|
|
onFinish={onFinish}
|
|
|
|
autoComplete="off"
|
|
|
|
>
|
|
|
|
{keys.map((key: keyof typeof defaultToken) => {
|
2022-03-24 18:44:42 +08:00
|
|
|
if (PresetColors.includes(key as any)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-02-18 14:17:32 +08:00
|
|
|
const originValue = defaultToken[key];
|
|
|
|
const originValueType = typeof originValue;
|
|
|
|
|
|
|
|
let node: React.ReactElement;
|
|
|
|
|
|
|
|
switch (originValueType) {
|
|
|
|
case 'number':
|
|
|
|
node = <InputNumber />;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
node = <Input />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rules: any[] = [{ required: true }];
|
|
|
|
const originColor = new TinyColor(originValue);
|
|
|
|
if (originValueType === 'string' && originColor.isValid) {
|
|
|
|
rules.push({
|
|
|
|
validator: async (_: any, value: string) => {
|
|
|
|
if (!new TinyColor(value).isValid) {
|
|
|
|
throw new Error('Invalidate color type');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form.Item key={key} label={key} name={key} rules={rules} validateFirst>
|
|
|
|
{node}
|
|
|
|
</Form.Item>
|
|
|
|
);
|
|
|
|
})}
|
2022-03-03 14:55:19 +08:00
|
|
|
|
|
|
|
<Form.Item name="hashed" valuePropName="checked">
|
|
|
|
<Checkbox>Bind Style on hash className</Checkbox>
|
|
|
|
</Form.Item>
|
2022-02-18 14:17:32 +08:00
|
|
|
</Form>
|
|
|
|
</Drawer>
|
2022-03-17 14:06:21 +08:00
|
|
|
|
|
|
|
<Preview
|
|
|
|
visible={previewVisible}
|
|
|
|
componentName={componentName}
|
|
|
|
onClose={() => setPreviewVisible(false)}
|
|
|
|
/>
|
2022-02-18 14:17:32 +08:00
|
|
|
</>
|
|
|
|
);
|
2022-06-07 23:29:12 +08:00
|
|
|
}
|