diff --git a/.dumi/theme/builtins/Previewer/index.jsx b/.dumi/theme/builtins/Previewer/index.tsx similarity index 88% rename from .dumi/theme/builtins/Previewer/index.jsx rename to .dumi/theme/builtins/Previewer/index.tsx index 7f25b4cb55..c4de1d61d4 100644 --- a/.dumi/theme/builtins/Previewer/index.jsx +++ b/.dumi/theme/builtins/Previewer/index.tsx @@ -1,6 +1,7 @@ /* eslint jsx-a11y/no-noninteractive-element-interactions: 0 */ import { CheckOutlined, SnippetsOutlined, ThunderboltOutlined } from '@ant-design/icons'; import stackblitzSdk from '@stackblitz/sdk'; +import type { Project } from '@stackblitz/sdk'; import { Alert, Badge, Tooltip, Space } from 'antd'; import classNames from 'classnames'; import LZString from 'lz-string'; @@ -16,26 +17,55 @@ import CodeSandboxIcon from '../../common/CodeSandboxIcon'; import RiddleIcon from '../../common/RiddleIcon'; import ExternalLinkIcon from '../../common/ExternalLinkIcon'; import fromDumiProps from './fromDumiProps'; +import { version } from '../../../../package.json'; const { ErrorBoundary } = Alert; -function compress(string) { +function compress(string: string): string { return LZString.compressToBase64(string) .replace(/\+/g, '-') // Convert '+' to '-' .replace(/\//g, '_') // Convert '/' to '_' .replace(/=+$/, ''); // Remove ending '=' } -class Demo extends React.Component { - iframeRef = React.createRef(); +interface DemoProps { + meta: any; + src: string; + content: string; + preview: (react: typeof React, reactDOM: typeof ReactDOM) => React.ReactNode; + highlightedCodes: Record; + style: string; + highlightedStyle: string; + expand: boolean; + intl: any; + theme: string; + sourceCodes: Record<'jsx' | 'tsx', string>; + location: Location; + showRiddleButton: boolean; + utils?: any; +} - codeSandboxIconRef = React.createRef(); +interface DemoState { + codeType?: string; + copied?: boolean; + codeExpand?: boolean; + copyTooltipOpen?: boolean | string; +} - riddleIconRef = React.createRef(); +class Demo extends React.Component { + liveDemo: any; - codepenIconRef = React.createRef(); + iframeRef = React.createRef(); - state = { + anchorRef = React.createRef(); + + codeSandboxIconRef = React.createRef(); + + riddleIconRef = React.createRef(); + + codepenIconRef = React.createRef(); + + state: DemoState = { codeExpand: false, copied: false, copyTooltipOpen: false, @@ -45,11 +75,11 @@ class Demo extends React.Component { componentDidMount() { const { meta, location } = this.props; if (meta.id === location.hash.slice(1)) { - this.anchor.click(); + this.anchorRef.current?.click(); } } - shouldComponentUpdate(nextProps, nextState) { + shouldComponentUpdate(nextProps: DemoProps, nextState: DemoState) { const { codeExpand, copied, copyTooltipOpen, codeType } = this.state; const { expand, theme, showRiddleButton } = this.props; return ( @@ -62,47 +92,31 @@ class Demo extends React.Component { ); } - getSourceCode() { + getSourceCode = (): [string, string] => { const { sourceCodes } = this.props; return [sourceCodes.jsx, sourceCodes.tsx]; - } + }; - handleCodeExpand = (demo) => { + handleCodeExpand = (demo: string) => { const { codeExpand } = this.state; this.setState({ codeExpand: !codeExpand }); - this.track({ - type: 'expand', - demo, - }); + this.track({ type: 'expand', demo }); }; - saveAnchor = (anchor) => { - this.anchor = anchor; - }; - - handleCodeCopied = (demo) => { + handleCodeCopied = (demo: string) => { this.setState({ copied: true }); - this.track({ - type: 'copy', - demo, - }); + this.track({ type: 'copy', demo }); }; - onCopyTooltipOpenChange = (open) => { + onCopyTooltipOpenChange = (open: boolean) => { if (open) { - this.setState({ - copyTooltipOpen: open, - copied: false, - }); + this.setState({ copyTooltipOpen: open, copied: false }); return; } - this.setState({ - copyTooltipOpen: open, - }); + this.setState({ copyTooltipOpen: open }); }; - // eslint-disable-next-line class-methods-use-this - track({ type, demo }) { + track = ({ type, demo }: { type: string; demo: string }) => { if (!window.gtag) { return; } @@ -110,7 +124,7 @@ class Demo extends React.Component { event_category: type, event_label: demo, }); - } + }; render() { const { state } = this; @@ -134,7 +148,6 @@ class Demo extends React.Component {