From e6d00866c37d7b6f3f44250ff5236f40d5dd7925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B0=E9=91=AB?= <1192065030@qq.com> Date: Sat, 16 Jul 2022 17:21:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(Tooltip)=20=E9=87=8D=E6=9E=84=E6=96=87?= =?UTF-8?q?=E5=AD=97=E6=8F=90=E7=A4=BA=20#845?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/react-tooltip/src/index.tsx | 16 +- packages/react-tooltip/src/style/index.ts | 180 ++++++++++++++++++++++ 2 files changed, 188 insertions(+), 8 deletions(-) create mode 100644 packages/react-tooltip/src/style/index.ts diff --git a/packages/react-tooltip/src/index.tsx b/packages/react-tooltip/src/index.tsx index 85a46200b5..44f4134e2a 100644 --- a/packages/react-tooltip/src/index.tsx +++ b/packages/react-tooltip/src/index.tsx @@ -1,8 +1,7 @@ import React from 'react'; import OverlayTrigger, { OverlayTriggerProps } from '@uiw/react-overlay-trigger'; import { IProps } from '@uiw/utils'; -import './style/index.less'; - +import { TooltipWarp, TooltipContainerWarp, TooltipArrowWarp, TooltipInnerWarp } from './style'; export interface TooltipProps extends IProps, OverlayTriggerProps { visibleArrow?: boolean; content?: React.ReactNode; @@ -23,7 +22,8 @@ export default (props: TooltipProps = {}) => { } = props; const cls = [prefixCls, className, !visibleArrow ? 'no-arrow' : null].filter(Boolean).join(' ').trim(); return ( - { placement={placement} {...other} overlay={ -
- {visibleArrow &&
} -
{props.content}
-
+ + {visibleArrow && } + {props.content} + } > {typeof props.children === 'object' ? props.children : {props.children}} - + ); }; diff --git a/packages/react-tooltip/src/style/index.ts b/packages/react-tooltip/src/style/index.ts new file mode 100644 index 0000000000..05530d19e6 --- /dev/null +++ b/packages/react-tooltip/src/style/index.ts @@ -0,0 +1,180 @@ +import styled, { css } from 'styled-components'; +import { getThemeVariantValue, ThemeVariantValueOptions } from '@uiw/utils'; +import { TooltipProps } from '../'; + +export interface TooltipStyleProps extends TooltipProps, ThemeVariantValueOptions {} + +interface TooltipContainerProps { + visibleArrow: boolean; + placement: TooltipStyleProps['placement']; +} + +interface TooltipArrowProps extends ThemeVariantValueOptions { + placement: TooltipStyleProps['placement']; +} + +function tooltipHandle(placement: TooltipStyleProps['placement']) { + const obj = { + right: 'right', + rightTop: 'right', + rightBottom: 'right', + + left: 'left', + leftTop: 'left', + leftBottom: 'left', + + top: 'top', + topLeft: 'top', + topRight: 'top', + + bottom: 'bottom', + bottomLeft: 'bottom', + bottomRight: 'bottom', + }; + switch (obj[placement!]) { + case 'bottom': + return css` + padding-top: 5px; + `; + case 'top': + return css` + padding-bottom: 5px; + `; + case 'right': + return css` + padding-left: 5px; + `; + case 'left': + return css` + padding-right: 5px; + `; + default: + return css``; + } +} + +function tooltipArrowHandle(placement: TooltipStyleProps['placement']) { + const obj = { + right: 'right', + rightTop: 'right', + rightBottom: 'right', + + left: 'left', + leftTop: 'left', + leftBottom: 'left', + + top: 'top', + topLeft: 'top', + topRight: 'top', + + bottom: 'bottom', + bottomLeft: 'bottom', + bottomRight: 'bottom', + }; + switch (obj[placement!]) { + case 'right': + return css` + border-right-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')}; + border-width: 5px 5px 5px 0; + left: 0; + margin-top: -5px; + top: 50%; + `; + case 'left': + return css` + border-left-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')}; + border-width: 5px 0 5px 5px; + margin-top: -5px; + right: 0; + top: 50%; + `; + case 'top': + return css` + border-top-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')}; + border-width: 5px 5px 0; + bottom: 0; + left: 50%; + margin-left: -5px; + `; + case 'bottom': + return css` + border-bottom-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')}; + border-width: 0 5px 5px; + left: 50%; + margin-left: -5px; + top: 0; + `; + default: + return css``; + } +} + +export const TooltipWarp = styled.div``; +export const TooltipContainerWarp = styled.div` + position: relative; + display: inline-block; + ${(props) => + !props.visibleArrow && + css` + padding: 0 !important; + `} + ${(props) => tooltipHandle(props.placement)} +`; +export const TooltipArrowWarp = styled.div` + position: absolute; + width: 0; + height: 0; + border-color: transparent; + border-style: solid; + ${(props) => tooltipArrowHandle(props.placement)} + ${({ placement }) => { + if (['leftTop', 'rightTop'].includes(placement!)) { + return css` + top: 15px; + `; + } + if (['leftBottom', 'rightBottom'].includes(placement!)) { + return css` + bottom: 10px; + top: auto; + `; + } + if (['bottomLeft', 'topLeft'].includes(placement!)) { + return css` + left: 15px; + `; + } + if (['bottomRight', 'topRight'].includes(placement!)) { + return css` + right: 10px; + left: auto; + `; + } + return css``; + }} +`; +export const TooltipInnerWarp = styled.div` + font-size: 12px; + max-width: 250px; + padding: 5px 10px; + display: block; + color: #fff; + text-align: left; + text-decoration: none; + border-radius: 4px; + box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2); + word-break: break-all; + background-color: ${(props) => getThemeVariantValue(props, 'backgroundColorDefault')}; +`; + +TooltipInnerWarp.defaultProps = { + defaultTheme: { + backgroundColorDefault: 'rgba(0, 0, 0, 0.75)', + borderColorDefault: 'rgba(0, 0, 0, 0.75)', + }, +}; +TooltipArrowWarp.defaultProps = { + defaultTheme: { + borderColorDefault: 'rgba(0, 0, 0, 0.75)', + }, +};