Skip to content

Commit e8ffba7

Browse files
committed
[@mantine/core] Add autoContrast prop to Tooltip for automatic text color adjustment
- Introduced `autoContrast` prop to adjust tooltip text color based on background luminosity. - Updated `varsResolver` to handle `autoContrast` logic and generate appropriate theme variables. - Added a new story (`AutoContrast`) to demonstrate the `autoContrast` feature in Tooltip.
1 parent b616f96 commit e8ffba7

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

packages/@mantine/core/src/components/Tooltip/Tooltip.story.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,40 @@ export function Usage() {
2121
);
2222
}
2323

24+
export function AutoContrast() {
25+
return (
26+
<>
27+
<div style={{ padding: 40 }}>
28+
<Tooltip
29+
position="right"
30+
label="Tooltip label"
31+
withArrow
32+
transitionProps={{ duration: 0 }}
33+
opened
34+
color="cyan"
35+
radius="md"
36+
>
37+
<button type="button">target</button>
38+
</Tooltip>
39+
</div>
40+
<div style={{ padding: 40 }}>
41+
<Tooltip
42+
position="right"
43+
label="Tooltip label"
44+
withArrow
45+
transitionProps={{ duration: 0 }}
46+
opened
47+
color="cyan"
48+
radius="md"
49+
autoContrast
50+
>
51+
<button type="button">target</button>
52+
</Tooltip>
53+
</div>
54+
</>
55+
);
56+
}
57+
2458
export function Unstyled() {
2559
return (
2660
<div style={{ padding: 40 }}>

packages/@mantine/core/src/components/Tooltip/Tooltip.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
getDefaultZIndex,
1010
getRadius,
1111
getRefProp,
12-
getThemeColor,
1312
isElement,
1413
useDirection,
1514
useProps,
@@ -82,6 +81,9 @@ export interface TooltipProps extends TooltipBaseProps {
8281

8382
/** Changes floating ui [position strategy](https://floating-ui.com/docs/usefloating#strategy), `'absolute'` by default */
8483
floatingStrategy?: FloatingStrategy;
84+
85+
/** Determines whether tooltip text color should depend on `background-color`. If luminosity of the `color` prop is less than `theme.luminosityThreshold`, then `theme.white` will be used for text color, otherwise `theme.black`. Overrides `theme.autoContrast`. */
86+
autoContrast?: boolean;
8587
}
8688

8789
export type TooltipFactory = Factory<{
@@ -112,14 +114,24 @@ const defaultProps: Partial<TooltipProps> = {
112114
positionDependencies: [],
113115
middlewares: { flip: true, shift: true, inline: false },
114116
};
117+
const varsResolver = createVarsResolver<TooltipFactory>(
118+
(theme, { radius, color, variant, autoContrast }) => {
119+
const colors = theme.variantColorResolver({
120+
theme,
121+
color: color || theme.primaryColor,
122+
autoContrast,
123+
variant: variant || 'filled',
124+
});
115125

116-
const varsResolver = createVarsResolver<TooltipFactory>((theme, { radius, color }) => ({
117-
tooltip: {
118-
'--tooltip-radius': radius === undefined ? undefined : getRadius(radius),
119-
'--tooltip-bg': color ? getThemeColor(color, theme) : undefined,
120-
'--tooltip-color': color ? 'var(--mantine-color-white)' : undefined,
121-
},
122-
}));
126+
return {
127+
tooltip: {
128+
'--tooltip-radius': radius === undefined ? undefined : getRadius(radius),
129+
'--tooltip-bg': color ? colors.background : undefined,
130+
'--tooltip-color': color ? colors.color : undefined,
131+
},
132+
};
133+
}
134+
);
123135

124136
export const Tooltip = factory<TooltipFactory>((_props, ref) => {
125137
const props = useProps('Tooltip', defaultProps, _props);
@@ -164,6 +176,7 @@ export const Tooltip = factory<TooltipFactory>((_props, ref) => {
164176
mod,
165177
floatingStrategy,
166178
middlewares,
179+
autoContrast,
167180
...others
168181
} = useProps('Tooltip', defaultProps, props);
169182

0 commit comments

Comments
 (0)