Skip to content

Commit

Permalink
[@mantine/core] Fix autoContrast defined on theme not working in so…
Browse files Browse the repository at this point in the history
…me components (#5655)
  • Loading branch information
rtivital committed Jan 27, 2024
1 parent dc732bb commit 41e282e
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function AutoContrast() {
value="hello"
defaultChecked
color="lime.4"
autoContrast
// autoContrast
/>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/@mantine/core/src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
extractStyleProps,
factory,
Factory,
getAutoContrastValue,
getContrastColor,
getRadius,
getSize,
Expand Down Expand Up @@ -110,7 +111,7 @@ const varsResolver = createVarsResolver<CheckboxFactory>(
'--checkbox-color': variant === 'outline' ? outlineColor : getThemeColor(color, theme),
'--checkbox-icon-color': iconColor
? getThemeColor(iconColor, theme)
: autoContrast
: getAutoContrastValue(autoContrast, theme)
? getContrastColor({ color, theme })
: undefined,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ElementProps,
factory,
Factory,
getAutoContrastValue,
getContrastColor,
getRadius,
getThemeColor,
Expand Down Expand Up @@ -100,7 +101,9 @@ const varsResolver = createVarsResolver<IndicatorFactory>(
(theme, { color, position, offset, size, radius, zIndex, autoContrast }) => ({
root: {
'--indicator-color': color ? getThemeColor(color, theme) : undefined,
'--indicator-text-color': autoContrast ? getContrastColor({ color, theme }) : undefined,
'--indicator-text-color': getAutoContrastValue(autoContrast, theme)
? getContrastColor({ color, theme })
: undefined,
'--indicator-size': rem(size),
'--indicator-radius': radius === undefined ? undefined : getRadius(radius),
'--indicator-z-index': zIndex?.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function DynamicTotal() {
}

export function AutoContrast() {
return <Pagination total={45} color="lime.3" autoContrast />;
return <Pagination total={45} color="lime.3" />;
}

export function Controlled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ElementProps,
factory,
Factory,
getAutoContrastValue,
getContrastColor,
getFontSize,
getRadius,
Expand Down Expand Up @@ -105,7 +106,9 @@ const varsResolver = createVarsResolver<PaginationRootFactory>(
'--pagination-control-size': getSize(size, 'pagination-control-size'),
'--pagination-control-fz': getFontSize(size),
'--pagination-active-bg': color ? getThemeColor(color, theme) : undefined,
'--pagination-active-color': autoContrast ? getContrastColor({ color, theme }) : undefined,
'--pagination-active-color': getAutoContrastValue(autoContrast, theme)
? getContrastColor({ color, theme })
: undefined,
},
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ElementProps,
factory,
Factory,
getAutoContrastValue,
getContrastColor,
getThemeColor,
MantineColor,
Expand Down Expand Up @@ -87,7 +88,9 @@ export const ProgressSection = factory<ProgressSectionFactory>((props, ref) => {
__vars={{
'--progress-section-width': `${value}%`,
'--progress-section-color': getThemeColor(color, theme),
'--progress-label-color': ctx.autoContrast ? getContrastColor({ color, theme }) : undefined,
'--progress-label-color': getAutoContrastValue(ctx.autoContrast, theme)
? getContrastColor({ color, theme })
: undefined,
}}
/>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/@mantine/core/src/components/Radio/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
extractStyleProps,
factory,
Factory,
getAutoContrastValue,
getContrastColor,
getRadius,
getSize,
Expand Down Expand Up @@ -108,7 +109,7 @@ const varsResolver = createVarsResolver<RadioFactory>(
'--radio-color': variant === 'outline' ? outlineColor : getThemeColor(color, theme),
'--radio-icon-color': iconColor
? getThemeColor(iconColor, theme)
: autoContrast
: getAutoContrastValue(autoContrast, theme)
? getContrastColor({ color, theme })
: undefined,
'--radio-icon-size': getSize(size, 'radio-icon-size'),
Expand Down
5 changes: 4 additions & 1 deletion packages/@mantine/core/src/components/Stepper/Stepper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ElementProps,
factory,
Factory,
getAutoContrastValue,
getContrastColor,
getFontSize,
getRadius,
Expand Down Expand Up @@ -129,7 +130,9 @@ const varsResolver = createVarsResolver<StepperFactory>(
(theme, { color, iconSize, size, contentPadding, radius, autoContrast }) => ({
root: {
'--stepper-color': color ? getThemeColor(color, theme) : undefined,
'--stepper-icon-color': autoContrast ? getContrastColor({ color, theme }) : undefined,
'--stepper-icon-color': getAutoContrastValue(autoContrast, theme)
? getContrastColor({ color, theme })
: undefined,
'--stepper-icon-size':
iconSize === undefined ? getSize(size, 'stepper-icon-size') : rem(iconSize),
'--stepper-content-padding': getSpacing(contentPadding),
Expand Down
5 changes: 4 additions & 1 deletion packages/@mantine/core/src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ElementProps,
factory,
Factory,
getAutoContrastValue,
getContrastColor,
getRadius,
getSafeId,
Expand Down Expand Up @@ -116,7 +117,9 @@ const varsResolver = createVarsResolver<TabsFactory>((theme, { radius, color, au
root: {
'--tabs-radius': getRadius(radius),
'--tabs-color': getThemeColor(color, theme),
'--tabs-text-color': autoContrast ? getContrastColor({ color, theme }) : undefined,
'--tabs-text-color': getAutoContrastValue(autoContrast, theme)
? getContrastColor({ color, theme })
: undefined,
},
}));

Expand Down
5 changes: 4 additions & 1 deletion packages/@mantine/core/src/components/Timeline/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ElementProps,
factory,
Factory,
getAutoContrastValue,
getContrastColor,
getRadius,
getThemeColor,
Expand Down Expand Up @@ -80,7 +81,9 @@ const varsResolver = createVarsResolver<TimelineFactory>(
'--tl-line-width': rem(lineWidth),
'--tl-radius': radius === undefined ? undefined : getRadius(radius),
'--tl-color': color ? getThemeColor(color, theme) : undefined,
'--tl-icon-color': autoContrast ? getContrastColor({ color, theme }) : undefined,
'--tl-icon-color': getAutoContrastValue(autoContrast, theme)
? getContrastColor({ color, theme })
: undefined,
},
})
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { MantineTheme } from '../../theme.types';

export function getAutoContrastValue(autoContrast: boolean | undefined, theme: MantineTheme) {
return typeof autoContrast === 'boolean' ? autoContrast : theme.autoContrast;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { darken } from './darken/darken';
export { lighten } from './lighten/lighten';
export { luminance, isLightColor } from './luminance/luminance';
export { getContrastColor, getPrimaryContrastColor } from './get-contrast-color/get-contrast-color';
export { getAutoContrastValue } from './get-auto-contrast-value/get-auto-contrast-value';

export type { RGBA } from './to-rgba/to-rgba';
export type {
Expand Down

0 comments on commit 41e282e

Please sign in to comment.