2023-08-31 09:48:54 +08:00
|
|
|
import { RightOutlined, LinkOutlined, QuestionCircleOutlined } from '@ant-design/icons';
|
2023-07-20 19:27:33 +08:00
|
|
|
import { createStyles, css, useTheme } from 'antd-style';
|
2023-03-12 14:13:03 +08:00
|
|
|
import { getDesignToken } from 'antd-token-previewer';
|
2023-07-28 16:17:43 +08:00
|
|
|
import React, { useMemo, useState } from 'react';
|
2023-03-12 14:13:03 +08:00
|
|
|
import tokenMeta from 'antd/es/version/token-meta.json';
|
|
|
|
import tokenData from 'antd/es/version/token.json';
|
2023-08-31 09:48:54 +08:00
|
|
|
import { ConfigProvider, Table, Popover, Typography } from 'antd';
|
2023-03-12 14:13:03 +08:00
|
|
|
import useLocale from '../../../hooks/useLocale';
|
|
|
|
import { useColumns } from '../TokenTable';
|
|
|
|
|
|
|
|
const defaultToken = getDesignToken();
|
|
|
|
|
|
|
|
const locales = {
|
|
|
|
cn: {
|
|
|
|
token: 'Token 名称',
|
|
|
|
description: '描述',
|
|
|
|
type: '类型',
|
|
|
|
value: '默认值',
|
2023-07-05 17:49:59 +08:00
|
|
|
componentToken: '组件 Token',
|
|
|
|
globalToken: '全局 Token',
|
2023-08-30 23:02:35 +08:00
|
|
|
help: '如何定制?',
|
|
|
|
customizeTokenLink: '/docs/react/customize-theme-cn#修改主题变量',
|
2023-08-31 09:48:54 +08:00
|
|
|
customizeComponentTokenLink: '/docs/react/customize-theme-cn#修改组件变量',
|
2023-03-12 14:13:03 +08:00
|
|
|
},
|
|
|
|
en: {
|
|
|
|
token: 'Token Name',
|
|
|
|
description: 'Description',
|
|
|
|
type: 'Type',
|
|
|
|
value: 'Default Value',
|
2023-07-05 17:49:59 +08:00
|
|
|
componentToken: 'Component Token',
|
|
|
|
globalToken: 'Global Token',
|
2023-08-31 09:48:54 +08:00
|
|
|
help: 'How to use?',
|
2023-08-30 23:02:35 +08:00
|
|
|
customizeTokenLink: '/docs/react/customize-theme#customize-design-token',
|
|
|
|
customizeComponentTokenLink: 'docs/react/customize-theme#customize-component-token',
|
2023-03-12 14:13:03 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const useStyle = createStyles(() => ({
|
2023-05-25 21:02:49 +08:00
|
|
|
tableTitle: css`
|
|
|
|
cursor: pointer;
|
|
|
|
position: relative;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: flex-start;
|
|
|
|
line-height: 40px;
|
|
|
|
`,
|
|
|
|
arrowIcon: css`
|
|
|
|
font-size: 16px;
|
|
|
|
margin-right: 8px;
|
|
|
|
& svg {
|
|
|
|
transition: all 0.3s;
|
|
|
|
}
|
|
|
|
`,
|
2023-08-30 23:02:35 +08:00
|
|
|
help: css`
|
2023-08-31 09:48:54 +08:00
|
|
|
margin-left: 8px;
|
|
|
|
font-size: 12px;
|
2023-08-30 23:02:35 +08:00
|
|
|
font-weight: normal;
|
|
|
|
color: #999;
|
|
|
|
a {
|
|
|
|
color: #999;
|
|
|
|
}
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
}));
|
2023-05-25 21:02:49 +08:00
|
|
|
|
2023-03-12 14:13:03 +08:00
|
|
|
interface SubTokenTableProps {
|
|
|
|
defaultOpen?: boolean;
|
|
|
|
title: string;
|
2023-08-31 09:48:54 +08:00
|
|
|
helpText: React.ReactNode;
|
|
|
|
helpLink: string;
|
2023-03-12 14:13:03 +08:00
|
|
|
tokens: string[];
|
2023-07-05 17:49:59 +08:00
|
|
|
component?: string;
|
2023-03-12 14:13:03 +08:00
|
|
|
}
|
|
|
|
|
2023-08-30 23:02:35 +08:00
|
|
|
const SubTokenTable: React.FC<SubTokenTableProps> = ({
|
|
|
|
defaultOpen,
|
|
|
|
tokens,
|
|
|
|
title,
|
2023-08-31 09:48:54 +08:00
|
|
|
helpText,
|
|
|
|
helpLink,
|
2023-08-30 23:02:35 +08:00
|
|
|
component,
|
|
|
|
}) => {
|
2023-03-12 14:13:03 +08:00
|
|
|
const [, lang] = useLocale(locales);
|
2023-07-20 19:27:33 +08:00
|
|
|
const token = useTheme();
|
2023-03-12 14:13:03 +08:00
|
|
|
const columns = useColumns();
|
|
|
|
|
2023-05-25 21:02:49 +08:00
|
|
|
const [open, setOpen] = useState<boolean>(defaultOpen || process.env.NODE_ENV !== 'production');
|
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const { styles } = useStyle();
|
2023-05-25 21:02:49 +08:00
|
|
|
|
2023-03-12 14:13:03 +08:00
|
|
|
if (!tokens.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:17:28 +08:00
|
|
|
const data = tokens
|
2023-07-05 17:49:59 +08:00
|
|
|
.sort(
|
|
|
|
component
|
|
|
|
? undefined
|
|
|
|
: (token1, token2) => {
|
|
|
|
const hasColor1 = token1.toLowerCase().includes('color');
|
|
|
|
const hasColor2 = token2.toLowerCase().includes('color');
|
|
|
|
|
|
|
|
if (hasColor1 && !hasColor2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasColor1 && hasColor2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return token1 < token2 ? -1 : 1;
|
|
|
|
},
|
|
|
|
)
|
2023-03-23 17:17:28 +08:00
|
|
|
.map((name) => {
|
2023-07-05 17:49:59 +08:00
|
|
|
const meta = component
|
|
|
|
? tokenMeta.components[component].find((item) => item.token === name)
|
|
|
|
: tokenMeta.global[name];
|
2023-03-23 17:17:28 +08:00
|
|
|
|
2023-03-27 15:33:22 +08:00
|
|
|
if (!meta) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:17:28 +08:00
|
|
|
return {
|
|
|
|
name,
|
|
|
|
desc: lang === 'cn' ? meta.desc : meta.descEn,
|
|
|
|
type: meta.type,
|
2023-07-05 17:49:59 +08:00
|
|
|
value: component ? tokenData[component].component[name] : defaultToken[name],
|
2023-03-23 17:17:28 +08:00
|
|
|
};
|
2023-03-27 15:33:22 +08:00
|
|
|
})
|
2023-05-25 21:02:49 +08:00
|
|
|
.filter(Boolean);
|
2023-03-12 14:13:03 +08:00
|
|
|
|
2023-08-31 09:48:54 +08:00
|
|
|
const code = component
|
|
|
|
? `<ConfigProvider
|
|
|
|
theme={{
|
|
|
|
components: {
|
|
|
|
${component}: {
|
|
|
|
/* here is your component tokens */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
...
|
|
|
|
</ConfigProvider>`
|
|
|
|
: `<ConfigProvider
|
|
|
|
theme={{
|
|
|
|
token: {
|
|
|
|
/* here is your global tokens */
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
...
|
|
|
|
</ConfigProvider>`;
|
|
|
|
|
2023-03-12 14:13:03 +08:00
|
|
|
return (
|
2023-08-30 23:02:35 +08:00
|
|
|
<>
|
2023-07-20 19:27:33 +08:00
|
|
|
<div className={styles.tableTitle} onClick={() => setOpen(!open)}>
|
|
|
|
<RightOutlined className={styles.arrowIcon} rotate={open ? 90 : 0} />
|
2023-08-30 23:02:35 +08:00
|
|
|
<h3>
|
|
|
|
{title}
|
2023-08-31 09:48:54 +08:00
|
|
|
<Popover
|
|
|
|
title={null}
|
|
|
|
popupStyle={{ width: 400 }}
|
|
|
|
content={
|
|
|
|
<Typography>
|
|
|
|
<pre style={{ fontSize: 12 }}>{code}</pre>
|
|
|
|
<a href={helpLink} target="_blank" rel="noreferrer">
|
|
|
|
<LinkOutlined style={{ marginRight: 4 }} />
|
|
|
|
{helpText}
|
|
|
|
</a>
|
|
|
|
</Typography>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<span className={styles.help}>
|
|
|
|
<QuestionCircleOutlined style={{ marginRight: 3 }} />
|
|
|
|
{helpText}
|
|
|
|
</span>
|
|
|
|
</Popover>
|
2023-08-30 23:02:35 +08:00
|
|
|
</h3>
|
2023-05-25 21:02:49 +08:00
|
|
|
</div>
|
|
|
|
{open && (
|
|
|
|
<ConfigProvider theme={{ token: { borderRadius: 0 } }}>
|
|
|
|
<Table
|
|
|
|
size="middle"
|
|
|
|
columns={columns}
|
|
|
|
bordered
|
|
|
|
dataSource={data}
|
|
|
|
style={{ marginBottom: token.margin }}
|
|
|
|
pagination={false}
|
|
|
|
rowKey={(record) => record.name}
|
|
|
|
/>
|
|
|
|
</ConfigProvider>
|
|
|
|
)}
|
2023-08-30 23:02:35 +08:00
|
|
|
</>
|
2023-03-12 14:13:03 +08:00
|
|
|
);
|
2023-05-25 21:02:49 +08:00
|
|
|
};
|
2023-03-12 14:13:03 +08:00
|
|
|
|
|
|
|
export interface ComponentTokenTableProps {
|
|
|
|
component: string;
|
|
|
|
}
|
|
|
|
|
2023-05-25 21:02:49 +08:00
|
|
|
const ComponentTokenTable: React.FC<ComponentTokenTableProps> = ({ component }) => {
|
2023-07-05 17:49:59 +08:00
|
|
|
const [locale] = useLocale(locales);
|
2023-05-25 21:02:49 +08:00
|
|
|
const [mergedGlobalTokens] = useMemo(() => {
|
2023-03-27 15:33:22 +08:00
|
|
|
const globalTokenSet = new Set<string>();
|
|
|
|
|
|
|
|
component.split(',').forEach((comp) => {
|
2023-07-05 17:49:59 +08:00
|
|
|
const { global: globalTokens = [] } = tokenData[comp] || {};
|
2023-03-27 15:33:22 +08:00
|
|
|
|
|
|
|
globalTokens.forEach((token: string) => {
|
|
|
|
globalTokenSet.add(token);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-07-05 17:49:59 +08:00
|
|
|
return [Array.from(globalTokenSet)] as const;
|
2023-03-27 15:33:22 +08:00
|
|
|
}, [component]);
|
2023-03-12 14:13:03 +08:00
|
|
|
|
2023-07-05 17:49:59 +08:00
|
|
|
return (
|
|
|
|
<>
|
2023-07-06 13:45:40 +08:00
|
|
|
{tokenMeta.components[component] && (
|
|
|
|
<SubTokenTable
|
|
|
|
title={locale.componentToken}
|
2023-08-31 09:48:54 +08:00
|
|
|
helpText={locale.help}
|
|
|
|
helpLink={locale.customizeTokenLink}
|
2023-07-06 13:45:40 +08:00
|
|
|
tokens={tokenMeta.components[component].map((item) => item.token)}
|
|
|
|
component={component}
|
2023-08-30 23:02:35 +08:00
|
|
|
defaultOpen
|
2023-07-06 13:45:40 +08:00
|
|
|
/>
|
|
|
|
)}
|
2023-08-30 23:02:35 +08:00
|
|
|
<SubTokenTable
|
|
|
|
title={locale.globalToken}
|
2023-08-31 09:48:54 +08:00
|
|
|
helpText={locale.help}
|
|
|
|
helpLink={locale.customizeComponentTokenLink}
|
2023-08-30 23:02:35 +08:00
|
|
|
tokens={mergedGlobalTokens}
|
|
|
|
/>
|
2023-07-05 17:49:59 +08:00
|
|
|
</>
|
|
|
|
);
|
2023-05-25 21:02:49 +08:00
|
|
|
};
|
2023-03-12 14:13:03 +08:00
|
|
|
|
|
|
|
export default React.memo(ComponentTokenTable);
|