Skip to content

Commit f5a8e45

Browse files
committed
Make contrasting colours work in dark mode and Storybook
1 parent 2304c35 commit f5a8e45

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

dotcom-rendering/src/components/FootballMatchStat.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ export const TeamColourContrastCorrection = {
184184
home={{
185185
teamName: match.home.name,
186186
teamColour: match.home.colour,
187-
value: args.home.value,
187+
value: 3,
188188
}}
189189
away={{
190190
teamName: match.away.name,
191191
teamColour: match.away.colour,
192-
value: args.away.value,
192+
value: 3,
193193
}}
194194
key={index}
195195
/>

dotcom-rendering/src/components/FootballMatchStat.tsx

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { css } from '@emotion/react';
22
import {
33
from,
4+
palette as sourcePalette,
45
space,
56
textSansBold14,
67
textSansBold15,
@@ -63,8 +64,22 @@ const largeNumberCss = css`
6364
}
6465
`;
6566

66-
const numberFixContrastCss = css`
67-
color: ${palette('--football-match-stat-text')};
67+
const numberLightContrastCss = css`
68+
@media (prefers-color-scheme: light) {
69+
color: ${palette('--football-match-stat-contrast')};
70+
}
71+
[data-color-scheme='light'] & {
72+
color: ${palette('--football-match-stat-contrast')};
73+
}
74+
`;
75+
76+
const numberDarkContrastCss = css`
77+
@media (prefers-color-scheme: dark) {
78+
color: ${palette('--football-match-stat-contrast')};
79+
}
80+
[data-color-scheme='dark'] & {
81+
color: ${palette('--football-match-stat-contrast')};
82+
}
6883
`;
6984

7085
const awayStatCss = css`
@@ -85,8 +100,22 @@ const barCss = css`
85100
border-radius: 8px;
86101
`;
87102

88-
const barFixContrastCss = css`
89-
border: 1px solid ${palette('--football-match-stat-text')};
103+
const barLightContrastCss = css`
104+
@media (prefers-color-scheme: light) {
105+
border: 1px solid ${palette('--football-match-stat-border-contrast')};
106+
}
107+
[data-color-scheme='light'] & {
108+
border: 1px solid ${palette('--football-match-stat-border-contrast')};
109+
}
110+
`;
111+
112+
const barDarkContrastCss = css`
113+
@media (prefers-color-scheme: dark) {
114+
border: 1px solid ${palette('--football-match-stat-border-contrast')};
115+
}
116+
[data-color-scheme='dark'] & {
117+
border: 1px solid ${palette('--football-match-stat-border-contrast')};
118+
}
90119
`;
91120

92121
type MatchStatistic = {
@@ -120,19 +149,27 @@ export const FootballMatchStat = ({
120149

121150
const minimumContrast = 3.1; // https://www.w3.org/TR/WCAG21/#contrast-minimum
122151

123-
const backgroundColour = '#f6f6f6'; // TODO: fetch from palette
152+
const backgroundLight = sourcePalette.neutral[97];
153+
const backgroundDark = sourcePalette.neutral[10];
124154

125-
const homeNeedsContrast =
126-
getContrast(home.teamColour, backgroundColour) < minimumContrast;
127-
const awayNeedsContrast =
128-
getContrast(away.teamColour, backgroundColour) < minimumContrast;
155+
const homeNeedsContrastWhenLight =
156+
getContrast(home.teamColour, backgroundLight) < minimumContrast;
157+
const awayNeedsContrastWhenLight =
158+
getContrast(away.teamColour, backgroundLight) < minimumContrast;
159+
const homeNeedsContrastWhenDark =
160+
getContrast(home.teamColour, backgroundDark) < minimumContrast;
161+
const awayNeedsContrastWhenDark =
162+
getContrast(away.teamColour, backgroundDark) < minimumContrast;
129163

130164
/**
131165
* If either team colour lacks sufficient contrast we adjust both numbers
132166
* so we don't appear to be favouring one team over the other. For the chart
133167
* we keep the team colour and apply a contrasting border colour.
134168
*/
135-
const numbersNeedContrast = homeNeedsContrast || awayNeedsContrast;
169+
const numbersNeedContrastWhenLight =
170+
homeNeedsContrastWhenLight || awayNeedsContrastWhenLight;
171+
const numbersNeedContrastWhenDark =
172+
homeNeedsContrastWhenDark || awayNeedsContrastWhenDark;
136173

137174
return (
138175
<div css={containerCss}>
@@ -142,7 +179,8 @@ export const FootballMatchStat = ({
142179
css={[
143180
numberCss,
144181
largeNumbersOnDesktop && largeNumberCss,
145-
numbersNeedContrast && numberFixContrastCss,
182+
numbersNeedContrastWhenLight && numberLightContrastCss,
183+
numbersNeedContrastWhenDark && numberDarkContrastCss,
146184
]}
147185
style={{ '--match-stat-team-colour': home.teamColour }}
148186
>
@@ -160,7 +198,8 @@ export const FootballMatchStat = ({
160198
numberCss,
161199
awayStatCss,
162200
largeNumbersOnDesktop && largeNumberCss,
163-
numbersNeedContrast && numberFixContrastCss,
201+
numbersNeedContrastWhenLight && numberLightContrastCss,
202+
numbersNeedContrastWhenDark && numberDarkContrastCss,
164203
]}
165204
style={{ '--match-stat-team-colour': away.teamColour }}
166205
>
@@ -176,14 +215,22 @@ export const FootballMatchStat = ({
176215
</div>
177216
<div aria-hidden="true" css={chartCss}>
178217
<div
179-
css={[barCss, homeNeedsContrast && barFixContrastCss]}
218+
css={[
219+
barCss,
220+
homeNeedsContrastWhenLight && barLightContrastCss,
221+
homeNeedsContrastWhenDark && barDarkContrastCss,
222+
]}
180223
style={{
181224
'--match-stat-percentage': `${homePercentage}%`,
182225
'--match-stat-team-colour': home.teamColour,
183226
}}
184227
></div>
185228
<div
186-
css={[barCss, awayNeedsContrast && barFixContrastCss]}
229+
css={[
230+
barCss,
231+
awayNeedsContrastWhenLight && barLightContrastCss,
232+
awayNeedsContrastWhenDark && barDarkContrastCss,
233+
]}
187234
style={{
188235
'--match-stat-percentage': `${awayPercentage}%`,
189236
'--match-stat-team-colour': away.teamColour,

dotcom-rendering/src/paletteDeclarations.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7124,6 +7124,14 @@ const paletteColours = {
71247124
light: () => '#00679E', // replace with Source's `calculateHoverColour` when available
71257125
dark: () => '#00A1E6',
71267126
},
7127+
'--football-match-stat-contrast': {
7128+
light: () => sourcePalette.neutral[7],
7129+
dark: () => sourcePalette.neutral[86],
7130+
},
7131+
'--football-match-stat-border-contrast': {
7132+
light: () => sourcePalette.neutral[86],
7133+
dark: () => sourcePalette.neutral[38],
7134+
},
71277135
'--football-match-stat-text': {
71287136
light: () => sourcePalette.neutral[7],
71297137
dark: () => sourcePalette.neutral[86],

0 commit comments

Comments
 (0)