|
| 1 | +import React from 'react'; |
| 2 | +import { Col, ConfigProvider, Flex, Row, Tag, theme, Typography } from 'antd'; |
| 3 | +import { createStyles, css } from 'antd-style'; |
| 4 | +import classnames from 'classnames'; |
| 5 | + |
| 6 | +const MARK_BORDER_SIZE = 2; |
| 7 | + |
| 8 | +const useStyle = createStyles(({ token }, markPos: [number, number, number, number]) => ({ |
| 9 | + container: css` |
| 10 | + position: relative; |
| 11 | + `, |
| 12 | + colWrap: css` |
| 13 | + border-right: 1px solid ${token.colorBorderSecondary}; |
| 14 | + display: flex; |
| 15 | + justify-content: center; |
| 16 | + align-items: center; |
| 17 | + padding: ${token.paddingMD}px; |
| 18 | + overflow: hidden; |
| 19 | + `, |
| 20 | + listWrap: css` |
| 21 | + display: flex; |
| 22 | + flex-direction: column; |
| 23 | + list-style: none; |
| 24 | + margin: 0; |
| 25 | + padding: 0; |
| 26 | + overflow: hidden; |
| 27 | + `, |
| 28 | + listItem: css` |
| 29 | + cursor: pointer; |
| 30 | + padding: ${token.paddingSM}px; |
| 31 | + transition: background-color ${token.motionDurationFast} ease; |
| 32 | + &:hover { |
| 33 | + background-color: ${token.controlItemBgHover}; |
| 34 | + } |
| 35 | + &:not(:first-of-type) { |
| 36 | + border-top: 1px solid ${token.colorBorderSecondary}; |
| 37 | + } |
| 38 | + `, |
| 39 | + marker: css` |
| 40 | + position: absolute; |
| 41 | + border: ${MARK_BORDER_SIZE}px solid ${token.colorWarning}; |
| 42 | + box-sizing: border-box; |
| 43 | + z-index: 999999; |
| 44 | + box-shadow: 0 0 0 1px #fff; |
| 45 | + pointer-events: none; |
| 46 | + left: ${markPos[0] - MARK_BORDER_SIZE}px; |
| 47 | + top: ${markPos[1] - MARK_BORDER_SIZE}px; |
| 48 | + width: ${markPos[2] + MARK_BORDER_SIZE * 2}px; |
| 49 | + height: ${markPos[3] + MARK_BORDER_SIZE * 2}px; |
| 50 | + `, |
| 51 | + markerActive: css` |
| 52 | + opacity: 1; |
| 53 | + `, |
| 54 | + markerNotActive: css` |
| 55 | + opacity: 0; |
| 56 | + `, |
| 57 | + markerMotion: css` |
| 58 | + transition: |
| 59 | + opacity ${token.motionDurationSlow} ease, |
| 60 | + all ${token.motionDurationSlow} ease; |
| 61 | + `, |
| 62 | + markerNotMotion: css` |
| 63 | + transition: opacity ${token.motionDurationSlow} ease; |
| 64 | + `, |
| 65 | +})); |
| 66 | + |
| 67 | +export interface SemanticPreviewProps { |
| 68 | + semantics: { name: string; desc: string; version?: string }[]; |
| 69 | + children: React.ReactElement; |
| 70 | + height?: number; |
| 71 | +} |
| 72 | + |
| 73 | +const SemanticPreview: React.FC<SemanticPreviewProps> = (props) => { |
| 74 | + const { semantics = [], children, height } = props; |
| 75 | + const { token } = theme.useToken(); |
| 76 | + |
| 77 | + // ======================= Semantic ======================= |
| 78 | + const getMarkClassName = React.useCallback( |
| 79 | + (semanticKey: string) => `semantic-mark-${semanticKey}`, |
| 80 | + [], |
| 81 | + ); |
| 82 | + |
| 83 | + const semanticClassNames = React.useMemo<Record<string, string>>(() => { |
| 84 | + const classNames: Record<string, string> = {}; |
| 85 | + |
| 86 | + semantics.forEach((semantic) => { |
| 87 | + classNames[semantic.name] = getMarkClassName(semantic.name); |
| 88 | + }); |
| 89 | + |
| 90 | + return classNames; |
| 91 | + }, [semantics]); |
| 92 | + |
| 93 | + const cloneNode = React.cloneElement(children, { |
| 94 | + classNames: semanticClassNames, |
| 95 | + }); |
| 96 | + |
| 97 | + // ======================== Hover ========================= |
| 98 | + const containerRef = React.useRef<HTMLDivElement>(null); |
| 99 | + |
| 100 | + const timerRef = React.useRef<ReturnType<typeof setTimeout>>(); |
| 101 | + |
| 102 | + const [positionMotion, setPositionMotion] = React.useState<boolean>(false); |
| 103 | + const [hoverSemantic, setHoverSemantic] = React.useState<string | null>(null); |
| 104 | + const [markPos, setMarkPos] = React.useState<[number, number, number, number]>([0, 0, 0, 0]); |
| 105 | + |
| 106 | + const { styles } = useStyle(markPos); |
| 107 | + |
| 108 | + React.useEffect(() => { |
| 109 | + if (hoverSemantic) { |
| 110 | + const targetClassName = getMarkClassName(hoverSemantic); |
| 111 | + const targetElement = containerRef.current?.querySelector<HTMLElement>(`.${targetClassName}`); |
| 112 | + const containerRect = containerRef.current?.getBoundingClientRect(); |
| 113 | + const targetRect = targetElement?.getBoundingClientRect(); |
| 114 | + setMarkPos([ |
| 115 | + (targetRect?.left || 0) - (containerRect?.left || 0), |
| 116 | + (targetRect?.top || 0) - (containerRect?.top || 0), |
| 117 | + targetRect?.width || 0, |
| 118 | + targetRect?.height || 0, |
| 119 | + ]); |
| 120 | + timerRef.current = setTimeout(() => { |
| 121 | + setPositionMotion(true); |
| 122 | + }, 10); |
| 123 | + } else { |
| 124 | + timerRef.current = setTimeout(() => { |
| 125 | + setPositionMotion(false); |
| 126 | + }, 500); |
| 127 | + } |
| 128 | + return () => { |
| 129 | + if (timerRef.current) { |
| 130 | + clearTimeout(timerRef.current); |
| 131 | + } |
| 132 | + }; |
| 133 | + }, [hoverSemantic]); |
| 134 | + |
| 135 | + // ======================== Render ======================== |
| 136 | + return ( |
| 137 | + <div className={classnames(styles.container)} ref={containerRef}> |
| 138 | + <Row style={{ minHeight: height }}> |
| 139 | + <Col span={16} className={classnames(styles.colWrap)}> |
| 140 | + <ConfigProvider theme={{ token: { motion: false } }}>{cloneNode}</ConfigProvider> |
| 141 | + </Col> |
| 142 | + <Col span={8}> |
| 143 | + <ul className={classnames(styles.listWrap)}> |
| 144 | + {semantics.map<React.ReactNode>((semantic) => ( |
| 145 | + <li |
| 146 | + key={semantic.name} |
| 147 | + className={classnames(styles.listItem)} |
| 148 | + onMouseEnter={() => setHoverSemantic(semantic.name)} |
| 149 | + onMouseLeave={() => setHoverSemantic(null)} |
| 150 | + > |
| 151 | + <Flex vertical gap="small"> |
| 152 | + <Flex gap="small" align="center"> |
| 153 | + <Typography.Title level={5} style={{ margin: 0 }}> |
| 154 | + {semantic.name} |
| 155 | + </Typography.Title> |
| 156 | + {semantic.version && <Tag color="blue">{semantic.version}</Tag>} |
| 157 | + </Flex> |
| 158 | + <Typography.Paragraph style={{ margin: 0, fontSize: token.fontSizeSM }}> |
| 159 | + {semantic.desc} |
| 160 | + </Typography.Paragraph> |
| 161 | + </Flex> |
| 162 | + </li> |
| 163 | + ))} |
| 164 | + </ul> |
| 165 | + </Col> |
| 166 | + </Row> |
| 167 | + <div |
| 168 | + className={classnames( |
| 169 | + styles.marker, |
| 170 | + hoverSemantic ? styles.markerActive : styles.markerNotActive, |
| 171 | + positionMotion ? styles.markerMotion : styles.markerNotMotion, |
| 172 | + )} |
| 173 | + /> |
| 174 | + </div> |
| 175 | + ); |
| 176 | +}; |
| 177 | + |
| 178 | +export default SemanticPreview; |
0 commit comments