import React from 'react'; import { InfoCircleOutlined, PushpinOutlined } from '@ant-design/icons'; import { Button, Col, ConfigProvider, Flex, Popover, Row, Tag, theme, Typography } from 'antd'; import { createStyles, css } from 'antd-style'; import classnames from 'classnames'; import Prism from 'prismjs'; import get from 'rc-util/lib/utils/get'; import set from 'rc-util/lib/utils/set'; import Markers from './Markers'; export interface SemanticPreviewInjectionProps { classNames?: Record; } const useStyle = createStyles(({ token }) => ({ container: css` position: relative; `, colWrap: css` border-inline-end: 1px solid ${token.colorBorderSecondary}; display: flex; justify-content: center; align-items: center; padding: ${token.paddingMD}px; overflow: hidden; `, colWrapPaddingLess: css` padding: 0; `, listWrap: css` display: flex; flex-direction: column; list-style: none; margin: 0; padding: 0; overflow: hidden; `, listItem: css` cursor: pointer; padding: ${token.paddingSM}px; transition: background-color ${token.motionDurationFast} ease; &:hover { background-color: ${token.controlItemBgHover}; } &:not(:first-of-type) { border-top: 1px solid ${token.colorBorderSecondary}; } `, })); function getSemanticCells(semanticPath: string) { return semanticPath.split('.'); } function HighlightExample(props: { componentName: string; semanticName: string; itemsAPI?: string; }) { const { componentName, semanticName, itemsAPI } = props; const highlightCode = React.useMemo(() => { const classNames = set({}, getSemanticCells(semanticName), `my-classname`); const styles = set({}, getSemanticCells(semanticName), { color: 'red' }); function format(obj: object, offset = 1) { const str = JSON.stringify(obj, null, 2); return ( str // Add space .split('\n') .map((line) => `${' '.repeat(offset)}${line}`) .join('\n') .trim() // Replace quotes .replace(/"/g, "'") // Remove key quotes .replace(/'([^']+)':/g, '$1:') ); } let code: string; if (itemsAPI) { // itemsAPI with array code = ` <${componentName} ${itemsAPI}={[{ classNames: ${format(classNames, 2)}, styles: ${format(styles, 2)}, }]} />`.trim(); } else { // itemsAPI is not provided code = ` <${componentName} classNames={${format(classNames)}} styles={${format(styles)}} />`.trim(); } return Prism.highlight(code, Prism.languages.javascript, 'jsx'); }, [componentName, semanticName]); return ( // biome-ignore lint: lint/security/noDangerouslySetInnerHtml
); } const getMarkClassName = (semanticKey: string) => `semantic-mark-${semanticKey}`.replace(/\./g, '-'); export interface SemanticPreviewProps { componentName: string; semantics: { name: string; desc: string; version?: string }[]; itemsAPI?: string; children: React.ReactElement; height?: number; padding?: false; } const SemanticPreview: React.FC = (props) => { const { semantics = [], children, height, padding, componentName = 'Component', itemsAPI, } = props; const { token } = theme.useToken(); const semanticClassNames = React.useMemo>(() => { let classNames: Record = {}; semantics.forEach((semantic) => { const pathCell = getSemanticCells(semantic.name); classNames = set(classNames, pathCell, getMarkClassName(semantic.name)); }); return classNames; }, [semantics]); // ======================== Hover ========================= const containerRef = React.useRef(null); const [pinSemantic, setPinSemantic] = React.useState(null); const [hoverSemantic, setHoverSemantic] = React.useState(null); const mergedSemantic = pinSemantic || hoverSemantic; const { styles } = useStyle(); const hoveredSemanticClassNames = React.useMemo(() => { if (!mergedSemantic) { return semanticClassNames; } const hoverCell = getSemanticCells(mergedSemantic); const clone = set( semanticClassNames, hoverCell, classnames(get(semanticClassNames, hoverCell), getMarkClassName('active')), ); return clone; }, [semanticClassNames, mergedSemantic]); // ======================== Render ======================== const cloneNode = React.cloneElement(children, { classNames: hoveredSemanticClassNames, }); return (
{cloneNode}
    {semantics.map((semantic) => (
  • setHoverSemantic(semantic.name)} onMouseLeave={() => setHoverSemantic(null)} > {/* Title + Version */} {semantic.name} {semantic.version && {semantic.version}} {/* Pin + Sample */}
  • ))}
); }; export default SemanticPreview;