2024-12-06 13:40:58 +08:00
|
|
|
import React, { Suspense, useContext } from 'react';
|
2024-10-10 13:38:45 +08:00
|
|
|
import { BugOutlined, CodeOutlined, ExperimentOutlined } from '@ant-design/icons';
|
|
|
|
import { ConfigProvider, Tooltip, Button } from 'antd';
|
2024-04-06 16:11:17 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import { DumiDemoGrid, FormattedMessage } from 'dumi';
|
2024-10-23 11:25:33 +08:00
|
|
|
import { css, Global } from '@emotion/react';
|
2024-04-06 16:11:17 +08:00
|
|
|
|
2023-04-13 18:13:13 +08:00
|
|
|
import useLayoutState from '../../../hooks/useLayoutState';
|
2023-11-10 10:02:46 +08:00
|
|
|
import useLocale from '../../../hooks/useLocale';
|
2024-04-06 16:11:17 +08:00
|
|
|
import DemoContext from '../../slots/DemoContext';
|
2023-11-10 10:02:46 +08:00
|
|
|
|
|
|
|
const locales = {
|
|
|
|
cn: {
|
|
|
|
enableCssVar: '启用 CSS 变量',
|
|
|
|
disableCssVar: '禁用 CSS 变量',
|
|
|
|
},
|
|
|
|
en: {
|
|
|
|
enableCssVar: 'Enable CSS Var',
|
|
|
|
disableCssVar: 'Disable CSS Var',
|
|
|
|
},
|
|
|
|
};
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
const DemoWrapper: typeof DumiDemoGrid = ({ items }) => {
|
2022-11-17 01:22:09 +08:00
|
|
|
const { showDebug, setShowDebug } = useContext(DemoContext);
|
2023-11-10 10:02:46 +08:00
|
|
|
const [locale] = useLocale(locales);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-04-13 18:13:13 +08:00
|
|
|
const [expandAll, setExpandAll] = useLayoutState(false);
|
2023-11-10 10:02:46 +08:00
|
|
|
const [enableCssVar, setEnableCssVar] = useLayoutState(true);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
const handleVisibleToggle = () => {
|
|
|
|
setShowDebug?.(!showDebug);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleExpandToggle = () => {
|
|
|
|
setExpandAll(!expandAll);
|
|
|
|
};
|
|
|
|
|
2023-11-10 10:02:46 +08:00
|
|
|
const handleCssVarToggle = () => {
|
|
|
|
setEnableCssVar((v) => !v);
|
|
|
|
};
|
|
|
|
|
2023-01-13 10:41:54 +08:00
|
|
|
const demos = React.useMemo(
|
|
|
|
() =>
|
2024-09-24 22:37:38 +08:00
|
|
|
items.map((item: any) => {
|
2024-06-22 23:57:39 +08:00
|
|
|
const { previewerProps } = item;
|
|
|
|
const { debug } = previewerProps;
|
2024-09-24 22:37:38 +08:00
|
|
|
return {
|
2024-06-22 23:57:39 +08:00
|
|
|
...item,
|
|
|
|
previewerProps: {
|
2024-09-24 22:37:38 +08:00
|
|
|
...item.previewerProps,
|
2024-06-22 23:57:39 +08:00
|
|
|
expand: expandAll,
|
|
|
|
// always override debug property, because dumi will hide debug demo in production
|
|
|
|
debug: false,
|
|
|
|
/**
|
|
|
|
* antd extra marker for the original debug
|
|
|
|
* @see https://github.com/ant-design/ant-design/pull/40130#issuecomment-1380208762
|
|
|
|
*/
|
|
|
|
originDebug: debug,
|
|
|
|
},
|
2024-09-24 22:37:38 +08:00
|
|
|
};
|
|
|
|
}),
|
2023-01-13 10:41:54 +08:00
|
|
|
[expandAll, showDebug],
|
|
|
|
);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
return (
|
2024-09-24 22:37:38 +08:00
|
|
|
<div
|
|
|
|
className={classNames('demo-wrapper', {
|
|
|
|
'demo-wrapper-show-debug': showDebug,
|
|
|
|
})}
|
|
|
|
>
|
2024-10-23 11:25:33 +08:00
|
|
|
<Global
|
|
|
|
styles={css`
|
|
|
|
:root {
|
|
|
|
--antd-site-api-deprecated-display: ${showDebug ? 'table-row' : 'none'};
|
|
|
|
}
|
|
|
|
`}
|
|
|
|
/>
|
2022-11-09 12:28:04 +08:00
|
|
|
<span className="all-code-box-controls">
|
|
|
|
<Tooltip
|
|
|
|
title={
|
|
|
|
<FormattedMessage id={`app.component.examples.${expandAll ? 'collapse' : 'expand'}`} />
|
|
|
|
}
|
|
|
|
>
|
2024-10-10 13:38:45 +08:00
|
|
|
<Button
|
|
|
|
type="text"
|
|
|
|
size="small"
|
|
|
|
icon={<CodeOutlined />}
|
|
|
|
onClick={handleExpandToggle}
|
|
|
|
className={expandAll ? 'icon-enabled' : ''}
|
|
|
|
/>
|
2022-11-09 12:28:04 +08:00
|
|
|
</Tooltip>
|
|
|
|
<Tooltip
|
|
|
|
title={
|
|
|
|
<FormattedMessage id={`app.component.examples.${showDebug ? 'hide' : 'visible'}`} />
|
|
|
|
}
|
|
|
|
>
|
2024-10-10 13:38:45 +08:00
|
|
|
<Button
|
|
|
|
type="text"
|
|
|
|
size="small"
|
|
|
|
icon={<BugOutlined />}
|
|
|
|
onClick={handleVisibleToggle}
|
|
|
|
className={showDebug ? 'icon-enabled' : ''}
|
|
|
|
/>
|
2022-11-09 12:28:04 +08:00
|
|
|
</Tooltip>
|
2023-11-10 10:02:46 +08:00
|
|
|
<Tooltip title={enableCssVar ? locale.disableCssVar : locale.enableCssVar}>
|
2024-10-10 13:38:45 +08:00
|
|
|
<Button
|
|
|
|
type="text"
|
|
|
|
size="small"
|
|
|
|
icon={<ExperimentOutlined />}
|
|
|
|
onClick={handleCssVarToggle}
|
|
|
|
className={enableCssVar ? 'icon-enabled' : ''}
|
|
|
|
/>
|
2023-11-10 10:02:46 +08:00
|
|
|
</Tooltip>
|
2022-11-09 12:28:04 +08:00
|
|
|
</span>
|
2024-04-15 11:40:58 +08:00
|
|
|
<ConfigProvider theme={{ cssVar: enableCssVar, hashed: !enableCssVar }}>
|
2024-12-06 13:40:58 +08:00
|
|
|
<Suspense>
|
|
|
|
<DumiDemoGrid items={demos} />
|
|
|
|
</Suspense>
|
2023-11-10 10:02:46 +08:00
|
|
|
</ConfigProvider>
|
2022-11-09 12:28:04 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DemoWrapper;
|