Skip to content

Commit 91f3967

Browse files
committed
fix: arrow border behavior and backface
1 parent 296223f commit 91f3967

7 files changed

Lines changed: 45 additions & 85 deletions

File tree

src/components/Tooltip/Tooltip.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {
99
computeTooltipPosition,
1010
cssTimeToMs,
1111
clearTimeoutRef,
12-
getBorderWidth,
13-
getBorderColor,
1412
} from 'utils'
1513
import type { IComputedPosition } from 'utils'
1614
import { useTooltip } from 'components/TooltipProvider'
@@ -98,10 +96,6 @@ const Tooltip = ({
9896
const hoveringTooltip = useRef(false)
9997
const [anchorsBySelect, setAnchorsBySelect] = useState<HTMLElement[]>([])
10098
const mounted = useRef(false)
101-
const arrowBorderWidth = getBorderWidth(border)
102-
const arrowBorderColor = getBorderColor(border)
103-
const arrowBackground =
104-
border && arrowBorderColor ? arrowBorderColor : (arrowColor ?? 'var(--rt-tooltip-background)')
10599

106100
/**
107101
* @todo Update when deprecated stuff gets removed.
@@ -932,7 +926,15 @@ const Tooltip = ({
932926
}}
933927
ref={tooltipRef}
934928
>
935-
{actualContent}
929+
<WrapperElement
930+
className={classNames(
931+
'react-tooltip-content-wrapper',
932+
coreStyles['content'],
933+
styles['content'],
934+
)}
935+
>
936+
{actualContent}
937+
</WrapperElement>
936938
<WrapperElement
937939
className={classNames(
938940
'react-tooltip-arrow',
@@ -943,15 +945,13 @@ const Tooltip = ({
943945
)}
944946
style={{
945947
...computedPosition.tooltipArrowStyles,
948+
background: arrowColor
949+
? `linear-gradient(to right bottom, transparent 50%, ${arrowColor} 50%)`
950+
: undefined,
946951
'--rt-arrow-size': `${arrowSize}px`,
947-
'--rt-arrow-background': arrowBackground,
948-
'--rt-arrow-border-width': arrowBorderColor ? arrowBorderWidth : '0px',
949-
'--rt-arrow-border-color': arrowBorderColor,
950952
}}
951953
ref={tooltipArrowRef}
952-
>
953-
<WrapperElement className={coreStyles['arrowInner']} />
954-
</WrapperElement>
954+
/>
955955
</WrapperElement>
956956
) : null
957957
}

src/components/Tooltip/core-styles.module.css

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313

1414
.arrow {
1515
position: absolute;
16+
background: inherit;
1617
z-index: -1;
17-
clip-path: polygon(0% 100%, 100% 0%, 100% 100%);
18-
background: var(--rt-arrow-border-color, var(--rt-arrow-background));
19-
overflow: hidden;
18+
-webkit-backface-visibility: hidden;
19+
backface-visibility: hidden;
2020
}
2121

22-
.arrowInner {
23-
position: absolute;
24-
inset: var(--rt-arrow-border-width, 0px);
25-
background: var(--rt-arrow-background);
22+
.content {
23+
position: relative;
24+
z-index: 1;
2625
}
2726

2827
.noArrow {

src/components/Tooltip/styles.module.css

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
.tooltip {
2-
padding: 8px 16px;
32
border-radius: 3px;
43
font-size: 90%;
54
width: max-content;
65
}
76

7+
.content {
8+
background: inherit;
9+
border-radius: inherit;
10+
padding: 8px 16px;
11+
}
12+
813
.arrow {
914
width: var(--rt-arrow-size);
1015
height: var(--rt-arrow-size);
@@ -28,37 +33,31 @@
2833

2934
/** Types variant **/
3035
.dark {
31-
--rt-tooltip-background: var(--rt-color-dark);
32-
background: var(--rt-tooltip-background);
36+
background: var(--rt-color-dark);
3337
color: var(--rt-color-white);
3438
}
3539

3640
.light {
37-
--rt-tooltip-background: var(--rt-color-white);
38-
background-color: var(--rt-tooltip-background);
41+
background-color: var(--rt-color-white);
3942
color: var(--rt-color-dark);
4043
}
4144

4245
.success {
43-
--rt-tooltip-background: var(--rt-color-success);
44-
background-color: var(--rt-tooltip-background);
46+
background-color: var(--rt-color-success);
4547
color: var(--rt-color-white);
4648
}
4749

4850
.warning {
49-
--rt-tooltip-background: var(--rt-color-warning);
50-
background-color: var(--rt-tooltip-background);
51+
background-color: var(--rt-color-warning);
5152
color: var(--rt-color-white);
5253
}
5354

5455
.error {
55-
--rt-tooltip-background: var(--rt-color-error);
56-
background-color: var(--rt-tooltip-background);
56+
background-color: var(--rt-color-error);
5757
color: var(--rt-color-white);
5858
}
5959

6060
.info {
61-
--rt-tooltip-background: var(--rt-color-info);
62-
background-color: var(--rt-tooltip-background);
61+
background-color: var(--rt-color-info);
6362
color: var(--rt-color-white);
6463
}

src/components/TooltipController/TooltipController.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,13 @@ const TooltipController = React.forwardRef<TooltipRefProps, ITooltipController>(
8989
const { anchorRefs, activeAnchor: providerActiveAnchor } = useTooltip(id)
9090

9191
const getDataAttributesFromAnchorElement = (elementReference: HTMLElement) => {
92-
const dataAttributes = elementReference?.getAttributeNames().reduce(
93-
(acc, name) => {
94-
if (name.startsWith('data-tooltip-')) {
95-
const parsedAttribute = name.replace(/^data-tooltip-/, '') as DataAttribute
96-
acc[parsedAttribute] = elementReference?.getAttribute(name) ?? null
97-
}
98-
return acc
99-
},
100-
{} as Record<DataAttribute, string | null>,
101-
)
92+
const dataAttributes = elementReference?.getAttributeNames().reduce((acc, name) => {
93+
if (name.startsWith('data-tooltip-')) {
94+
const parsedAttribute = name.replace(/^data-tooltip-/, '') as DataAttribute
95+
acc[parsedAttribute] = elementReference?.getAttribute(name) ?? null
96+
}
97+
return acc
98+
}, {} as Record<DataAttribute, string | null>)
10299

103100
return dataAttributes
104101
}

src/utils/border-style.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/utils/compute-tooltip-position.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ const computeTooltipPosition = async ({
5353
}[placement.split('-')[0]] ?? 'bottom'
5454
/* c8 ignore end */
5555

56+
const borderSide = border && {
57+
borderBottom: border,
58+
borderRight: border,
59+
}
60+
5661
let borderWidth = 0
5762
if (border) {
5863
const match = `${border}`.match(/(\d+)px/)
@@ -73,10 +78,8 @@ const computeTooltipPosition = async ({
7378
top: arrowY != null ? `${arrowY}px` : '',
7479
right: '',
7580
bottom: '',
76-
// Keep the arrow tucked under the tooltip body. When the tooltip has a
77-
// border, offset by that width, but do not draw a separate arrow border:
78-
// the rotated/clipped element cannot render that seam cleanly.
79-
[staticSide]: `-${arrowSize * 0.5 + borderWidth}px`,
81+
...borderSide,
82+
[staticSide]: `-${arrowSize / 2 + borderWidth - 1}px`,
8083
}
8184
/* c8 ignore end */
8285

src/utils/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import deepEqual from './deep-equal'
77
import getScrollParent from './get-scroll-parent'
88
import useIsomorphicLayoutEffect from './use-isomorphic-layout-effect'
99
import clearTimeoutRef from './clear-timeout-ref'
10-
import { getBorderWidth, getBorderColor } from './border-style'
1110

1211
export type { IComputedPosition }
1312
export {
@@ -19,6 +18,4 @@ export {
1918
getScrollParent,
2019
useIsomorphicLayoutEffect,
2120
clearTimeoutRef,
22-
getBorderWidth,
23-
getBorderColor,
2421
}

0 commit comments

Comments
 (0)