|
| 1 | +import styled, { css } from 'styled-components'; |
| 2 | +import { getThemeVariantValue, ThemeVariantValueOptions } from '@uiw/utils'; |
| 3 | +import { TooltipProps } from '../'; |
| 4 | + |
| 5 | +export interface TooltipStyleProps extends TooltipProps, ThemeVariantValueOptions {} |
| 6 | + |
| 7 | +interface TooltipContainerProps { |
| 8 | + visibleArrow: boolean; |
| 9 | + placement: TooltipStyleProps['placement']; |
| 10 | +} |
| 11 | + |
| 12 | +interface TooltipArrowProps extends ThemeVariantValueOptions { |
| 13 | + placement: TooltipStyleProps['placement']; |
| 14 | +} |
| 15 | + |
| 16 | +function tooltipHandle(placement: TooltipStyleProps['placement']) { |
| 17 | + const obj = { |
| 18 | + right: 'right', |
| 19 | + rightTop: 'right', |
| 20 | + rightBottom: 'right', |
| 21 | + |
| 22 | + left: 'left', |
| 23 | + leftTop: 'left', |
| 24 | + leftBottom: 'left', |
| 25 | + |
| 26 | + top: 'top', |
| 27 | + topLeft: 'top', |
| 28 | + topRight: 'top', |
| 29 | + |
| 30 | + bottom: 'bottom', |
| 31 | + bottomLeft: 'bottom', |
| 32 | + bottomRight: 'bottom', |
| 33 | + }; |
| 34 | + switch (obj[placement!]) { |
| 35 | + case 'bottom': |
| 36 | + return css` |
| 37 | + padding-top: 5px; |
| 38 | + `; |
| 39 | + case 'top': |
| 40 | + return css` |
| 41 | + padding-bottom: 5px; |
| 42 | + `; |
| 43 | + case 'right': |
| 44 | + return css` |
| 45 | + padding-left: 5px; |
| 46 | + `; |
| 47 | + case 'left': |
| 48 | + return css` |
| 49 | + padding-right: 5px; |
| 50 | + `; |
| 51 | + default: |
| 52 | + return css``; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function tooltipArrowHandle(placement: TooltipStyleProps['placement']) { |
| 57 | + const obj = { |
| 58 | + right: 'right', |
| 59 | + rightTop: 'right', |
| 60 | + rightBottom: 'right', |
| 61 | + |
| 62 | + left: 'left', |
| 63 | + leftTop: 'left', |
| 64 | + leftBottom: 'left', |
| 65 | + |
| 66 | + top: 'top', |
| 67 | + topLeft: 'top', |
| 68 | + topRight: 'top', |
| 69 | + |
| 70 | + bottom: 'bottom', |
| 71 | + bottomLeft: 'bottom', |
| 72 | + bottomRight: 'bottom', |
| 73 | + }; |
| 74 | + switch (obj[placement!]) { |
| 75 | + case 'right': |
| 76 | + return css` |
| 77 | + border-right-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')}; |
| 78 | + border-width: 5px 5px 5px 0; |
| 79 | + left: 0; |
| 80 | + margin-top: -5px; |
| 81 | + top: 50%; |
| 82 | + `; |
| 83 | + case 'left': |
| 84 | + return css` |
| 85 | + border-left-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')}; |
| 86 | + border-width: 5px 0 5px 5px; |
| 87 | + margin-top: -5px; |
| 88 | + right: 0; |
| 89 | + top: 50%; |
| 90 | + `; |
| 91 | + case 'top': |
| 92 | + return css` |
| 93 | + border-top-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')}; |
| 94 | + border-width: 5px 5px 0; |
| 95 | + bottom: 0; |
| 96 | + left: 50%; |
| 97 | + margin-left: -5px; |
| 98 | + `; |
| 99 | + case 'bottom': |
| 100 | + return css` |
| 101 | + border-bottom-color: ${(props) => getThemeVariantValue(props, 'borderColorDefault')}; |
| 102 | + border-width: 0 5px 5px; |
| 103 | + left: 50%; |
| 104 | + margin-left: -5px; |
| 105 | + top: 0; |
| 106 | + `; |
| 107 | + default: |
| 108 | + return css``; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export const TooltipWarp = styled.div<TooltipStyleProps>``; |
| 113 | +export const TooltipContainerWarp = styled.div<TooltipContainerProps>` |
| 114 | + position: relative; |
| 115 | + display: inline-block; |
| 116 | + ${(props) => |
| 117 | + !props.visibleArrow && |
| 118 | + css` |
| 119 | + padding: 0 !important; |
| 120 | + `} |
| 121 | + ${(props) => tooltipHandle(props.placement)} |
| 122 | +`; |
| 123 | +export const TooltipArrowWarp = styled.div<TooltipArrowProps>` |
| 124 | + position: absolute; |
| 125 | + width: 0; |
| 126 | + height: 0; |
| 127 | + border-color: transparent; |
| 128 | + border-style: solid; |
| 129 | + ${(props) => tooltipArrowHandle(props.placement)} |
| 130 | + ${({ placement }) => { |
| 131 | + if (['leftTop', 'rightTop'].includes(placement!)) { |
| 132 | + return css` |
| 133 | + top: 15px; |
| 134 | + `; |
| 135 | + } |
| 136 | + if (['leftBottom', 'rightBottom'].includes(placement!)) { |
| 137 | + return css` |
| 138 | + bottom: 10px; |
| 139 | + top: auto; |
| 140 | + `; |
| 141 | + } |
| 142 | + if (['bottomLeft', 'topLeft'].includes(placement!)) { |
| 143 | + return css` |
| 144 | + left: 15px; |
| 145 | + `; |
| 146 | + } |
| 147 | + if (['bottomRight', 'topRight'].includes(placement!)) { |
| 148 | + return css` |
| 149 | + right: 10px; |
| 150 | + left: auto; |
| 151 | + `; |
| 152 | + } |
| 153 | + return css``; |
| 154 | + }} |
| 155 | +`; |
| 156 | +export const TooltipInnerWarp = styled.div<ThemeVariantValueOptions>` |
| 157 | + font-size: 12px; |
| 158 | + max-width: 250px; |
| 159 | + padding: 5px 10px; |
| 160 | + display: block; |
| 161 | + color: #fff; |
| 162 | + text-align: left; |
| 163 | + text-decoration: none; |
| 164 | + border-radius: 4px; |
| 165 | + box-shadow: 0 1px 6px rgba(0, 0, 0, 0.2); |
| 166 | + word-break: break-all; |
| 167 | + background-color: ${(props) => getThemeVariantValue(props, 'backgroundColorDefault')}; |
| 168 | +`; |
| 169 | + |
| 170 | +TooltipInnerWarp.defaultProps = { |
| 171 | + defaultTheme: { |
| 172 | + backgroundColorDefault: 'rgba(0, 0, 0, 0.75)', |
| 173 | + borderColorDefault: 'rgba(0, 0, 0, 0.75)', |
| 174 | + }, |
| 175 | +}; |
| 176 | +TooltipArrowWarp.defaultProps = { |
| 177 | + defaultTheme: { |
| 178 | + borderColorDefault: 'rgba(0, 0, 0, 0.75)', |
| 179 | + }, |
| 180 | +}; |
0 commit comments