ant-design/.dumi/theme/builtins/TokenTable/index.tsx
a9d6422722
docs: improve display of color block in docs (#44790)
* chore: update blog style

* Revert "chore: update blog style"

This reverts commit afc0a1d927.

* Revert "site: update docs styles (#39348)"

This reverts commit 4a803962eb.

# Conflicts:
#	.dumi/theme/builtins/ColorChunk/index.tsx
#	.dumi/theme/builtins/TokenTable/index.tsx
#	docs/react/customize-theme.en-US.md
#	docs/react/customize-theme.zh-CN.md

* chore: revert

* chore:revert

* chore: rename props

* chore: update

* chore(deps): add `dumi-plugin-color-chunk` plugin

* chore: update docs

* chore: update

* Update package.json

Co-authored-by: afc163 <afc163@gmail.com>
Signed-off-by: 红 <wxh16144@qq.com>

* docs: pick #44945

Co-authored-by: RedJue <RedJue@users.noreply.github.com>

---------

Signed-off-by: 红 <wxh16144@qq.com>
Signed-off-by: 红 <wxh1220@gmail.com>
Co-authored-by: MadCcc <1075746765@qq.com>
Co-authored-by: afc163 <afc163@gmail.com>
Co-authored-by: RedJue <RedJue@users.noreply.github.com>
2023-09-20 16:59:56 +08:00

110 lines
2.7 KiB
TypeScript

import type { FC } from 'react';
import * as React from 'react';
import { createStyles } from 'antd-style';
import { getDesignToken } from 'antd-token-previewer';
import tokenMeta from 'antd/es/version/token-meta.json';
import type { TableProps } from 'antd';
import { Table } from 'antd';
import useLocale from '../../../hooks/useLocale';
import ColorChunk from '../ColorChunk';
type TokenTableProps = {
type: 'seed' | 'map' | 'alias';
lang: 'zh' | 'en';
};
type TokenData = {
name: string;
desc: string;
type: string;
value: any;
};
const defaultToken = getDesignToken();
const locales = {
cn: {
token: 'Token 名称',
description: '描述',
type: '类型',
value: '默认值',
},
en: {
token: 'Token Name',
description: 'Description',
type: 'Type',
value: 'Default Value',
},
};
const useStyle = createStyles(({ token, css }) => ({
codeSpan: css`
margin: 0 1px;
padding: 0.2em 0.4em;
font-size: 0.9em;
background: ${token.siteMarkdownCodeBg};
border: 1px solid ${token.colorSplit};
border-radius: 3px;
font-family: monospace;
`,
}));
export function useColumns(): Exclude<TableProps<TokenData>['columns'], undefined> {
const [locale] = useLocale(locales);
const { styles } = useStyle();
return [
{
title: locale.token,
key: 'name',
dataIndex: 'name',
},
{
title: locale.description,
key: 'desc',
dataIndex: 'desc',
},
{
title: locale.type,
key: 'type',
dataIndex: 'type',
render: (_, record) => <span className={styles.codeSpan}>{record.type}</span>,
},
{
title: locale.value,
key: 'value',
render: (_, record) => {
const isColor =
typeof record.value === 'string' &&
(record.value.startsWith('#') || record.value.startsWith('rgb'));
if (isColor) {
return <ColorChunk value={record.value}>{record.value}</ColorChunk>;
}
return typeof record.value !== 'string' ? JSON.stringify(record.value) : record.value;
},
},
];
}
const TokenTable: FC<TokenTableProps> = ({ type }) => {
const [, lang] = useLocale(locales);
const columns = useColumns();
const data = React.useMemo<TokenData[]>(
() =>
Object.entries(tokenMeta.global)
.filter(([, meta]) => meta.source === type)
.map(([token, meta]) => ({
name: token,
desc: lang === 'cn' ? meta.desc : meta.descEn,
type: meta.type,
value: defaultToken[token],
})),
[type, lang],
);
return <Table dataSource={data} columns={columns} pagination={false} bordered />;
};
export default TokenTable;