From 8eb9e26d389ec4176ac47c22c19d04fb91040b96 Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Tue, 11 Apr 2023 17:53:03 +0800 Subject: [PATCH] docs: add demo token inspection --- .dumi/theme/builtins/DemoWrapper/index.tsx | 32 +++++++++++++-- .../builtins/Previewer/CodePreviewer.tsx | 41 ++++++++++++++++++- .dumi/theme/builtins/Previewer/index.tsx | 2 + .dumi/theme/locales/en-US.json | 2 + .dumi/theme/locales/zh-CN.json | 2 + components/button/demo/basic.tsx | 3 ++ 6 files changed, 77 insertions(+), 5 deletions(-) diff --git a/.dumi/theme/builtins/DemoWrapper/index.tsx b/.dumi/theme/builtins/DemoWrapper/index.tsx index 262e437a17..e603127a01 100644 --- a/.dumi/theme/builtins/DemoWrapper/index.tsx +++ b/.dumi/theme/builtins/DemoWrapper/index.tsx @@ -1,7 +1,14 @@ import React, { useContext, useState } from 'react'; import { DumiDemoGrid, FormattedMessage } from 'dumi'; import { Tooltip } from 'antd'; -import { BugFilled, BugOutlined, CodeFilled, CodeOutlined } from '@ant-design/icons'; +import { + BugFilled, + BugOutlined, + CodeFilled, + CodeOutlined, + ExperimentFilled, + ExperimentOutlined, +} from '@ant-design/icons'; import classNames from 'classnames'; import DemoContext from '../../slots/DemoContext'; @@ -9,6 +16,7 @@ const DemoWrapper: typeof DumiDemoGrid = ({ items }) => { const { showDebug, setShowDebug } = useContext(DemoContext); const [expandAll, setExpandAll] = useState(false); + const [showToken, setShowToken] = useState(process.env.NODE_ENV === 'development'); const expandTriggerClass = classNames('code-box-expand-trigger', { 'code-box-expand-trigger-active': expandAll, @@ -22,6 +30,10 @@ const DemoWrapper: typeof DumiDemoGrid = ({ items }) => { setExpandAll(!expandAll); }; + const handleTokenToggle = () => { + setShowToken(!showToken); + }; + const demos = React.useMemo( () => items.reduce((acc, item) => { @@ -42,10 +54,11 @@ const DemoWrapper: typeof DumiDemoGrid = ({ items }) => { * @see https://github.com/ant-design/ant-design/pull/40130#issuecomment-1380208762 */ originDebug: debug, + showToken, }, }); }, [] as typeof items), - [expandAll, showDebug], + [expandAll, showDebug, showToken], ); return ( @@ -73,9 +86,22 @@ const DemoWrapper: typeof DumiDemoGrid = ({ items }) => { )} + + } + > + {showToken ? ( + + ) : ( + + )} + {/* FIXME: find a new way instead of `key` to trigger re-render */} - + ); }; diff --git a/.dumi/theme/builtins/Previewer/CodePreviewer.tsx b/.dumi/theme/builtins/Previewer/CodePreviewer.tsx index e119f2fc3e..7b5e501f0a 100644 --- a/.dumi/theme/builtins/Previewer/CodePreviewer.tsx +++ b/.dumi/theme/builtins/Previewer/CodePreviewer.tsx @@ -6,7 +6,7 @@ import { } from '@ant-design/icons'; import type { Project } from '@stackblitz/sdk'; import stackblitzSdk from '@stackblitz/sdk'; -import { Alert, Badge, Space, Tooltip } from 'antd'; +import { Alert, Badge, ConfigProvider, Divider, Space, Tag, Tooltip } from 'antd'; import classNames from 'classnames'; import { FormattedMessage, useSiteData } from 'dumi'; import toReactElement from 'jsonml-to-react-element'; @@ -28,6 +28,7 @@ import type { SiteContextProps } from '../../slots/SiteContext'; import SiteContext from '../../slots/SiteContext'; import { ping } from '../../utils'; import type { AntdPreviewerProps } from '.'; +import useSiteToken from '../../../hooks/useSiteToken'; const { ErrorBoundary } = Alert; @@ -105,10 +106,13 @@ const CodePreviewer: React.FC = (props) => { filename, version, clientOnly, + tokens, + showToken, } = props; const { pkg } = useSiteData(); const location = useLocation(); + const { token } = useSiteToken(); const entryCode = asset.dependencies['index.tsx'].value; const showRiddleButton = useShowRiddleButton(); @@ -129,6 +133,8 @@ const CodePreviewer: React.FC = (props) => { const [showOnlineUrl, setShowOnlineUrl] = useState(false); + const hasToken = !!tokens?.split(',').length; + const highlightedCodes = { jsx: Prism.highlight(jsx, Prism.languages.javascript, 'jsx'), tsx: Prism.highlight(entryCode, Prism.languages.javascript, 'jsx'), @@ -384,11 +390,42 @@ createRoot(document.getElementById('container')).render(); backgroundColor: background === 'grey' ? backgroundGrey : undefined, }; + const [activeToken, setActiveToken] = useState(); + const onMouseEnterToken = (target: string) => { + setActiveToken(target); + }; + + const onMouseLeaveToken = () => { + setActiveToken(undefined); + }; + const codeBox: React.ReactNode = (
- {liveDemo.current} + + + {liveDemo.current} + + {showToken && hasToken && ( + <> + +
+ Hover token to see what changed. +
+ {tokens?.split(',').map((item) => ( + onMouseEnterToken(item.trim())} + onMouseLeave={onMouseLeaveToken} + > + {item.trim()} + + ))} + + )} +
{style ?