Skip to content

Commit 2f48af2

Browse files
authored
Merge pull request #76 from wagtail-nest/bugfix/62-support-dark-mode
Support dark mode
2 parents 67945f9 + 01be244 commit 2f48af2

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

wagtail_ab_testing/static_src/main.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ document.addEventListener('DOMContentLoaded', () => {
1010
// Goal selector on create new A/B test
1111
initGoalSelector();
1212

13+
const colorControl = '#0C0073'; // CSS $color-control
14+
const colorControlDark = '#00B0B1'; // Wagtail --w-color-secondary-100
15+
const colorVariant = '#EF746F'; // CSS $color-variant
16+
17+
// Match chart pattern colors to dark/light mode
18+
let pattern = [colorControl, colorVariant];
19+
if (
20+
window.matchMedia &&
21+
window.matchMedia('(prefers-color-scheme: dark)').matches
22+
) {
23+
// dark mode
24+
pattern = [colorControlDark, colorVariant];
25+
}
26+
1327
// Charts on A/B test progress
1428
document.querySelectorAll('[component="chart"]').forEach((chartElement) => {
1529
if (
@@ -19,7 +33,7 @@ document.addEventListener('DOMContentLoaded', () => {
1933
return;
2034
}
2135

22-
c3.generate({
36+
const chart = c3.generate({
2337
bindto: chartElement,
2438
data: JSON.parse(chartElement.getAttribute('data')!),
2539
padding: {
@@ -34,12 +48,30 @@ document.addEventListener('DOMContentLoaded', () => {
3448
},
3549
},
3650
color: {
37-
pattern: ['#0C0073', '#EF746F'],
51+
pattern: pattern,
3852
},
3953
});
54+
55+
// Add an event listener to update chart colors when the color scheme changes
56+
window
57+
.matchMedia('(prefers-color-scheme: dark)')
58+
.addEventListener('change', (event) => {
59+
const newColorScheme = event.matches ? 'dark' : 'light';
60+
if (newColorScheme === 'dark') {
61+
chart.data.colors({
62+
Control: colorControlDark,
63+
Variant: colorVariant,
64+
});
65+
} else {
66+
chart.data.colors({
67+
Control: colorControl,
68+
Variant: colorVariant,
69+
});
70+
}
71+
});
4072
});
4173

42-
// A/B testing tab on page edito
74+
// A/B testing tab on page editor
4375
if (abTestingTabProps) {
4476
$('ul.tab-nav').append(`<li role="tab" aria-controls="tab-abtesting">
4577
<a href="#tab-abtesting" class="">${gettext('A/B testing')}</a>

wagtail_ab_testing/static_src/style/progress.scss

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,37 @@
33
$color-control: #0c0073;
44
$color-variant: #ef746f;
55

6-
$light-teal: #e1f0f0;
7-
$dark-teal: #007273;
8-
9-
$charcoal-grey: #333;
10-
116
.abtest-progressbar {
127
position: relative;
138

149
&__sample-size {
1510
width: 100%;
1611
height: 160px;
12+
fill: var(--w-color-text-placeholder);
1713
}
1814

1915
&__sample-size line {
20-
stroke: black;
16+
stroke: var(--w-color-border-field-default);
2117
stroke-width: 2px;
2218
}
2319

2420
&__sample-size-bg {
25-
fill: $light-teal;
21+
fill: var(--w-color-surface-field-inactive);
2622
}
2723

2824
&__sample-size-bar {
29-
fill: $dark-teal;
25+
fill: var(--w-color-surface-button-default);
3026
}
3127

3228
&__sample-size-percentage {
3329
font-weight: bold;
3430
font-size: 40px;
35-
fill: white;
31+
fill: var(--w-color-text-button);
3632
}
3733

3834
&__sample-size-complete {
3935
font-size: 17px;
40-
fill: white;
36+
fill: var(--w-color-text-button);
4137
}
4238
}
4339

@@ -53,14 +49,23 @@ $charcoal-grey: #333;
5349
&--control {
5450
padding-right: 10px;
5551
color: $color-control;
56-
5752
a {
5853
color: $color-control !important;
5954

6055
&:hover {
6156
color: darken($color-control, 10%) !important;
6257
}
6358
}
59+
@media (prefers-color-scheme: dark) {
60+
color: var(--w-color-secondary-100);
61+
62+
a {
63+
color: var(--w-color-secondary-100) !important;
64+
&:hover {
65+
color: var(--w-color-secondary-400) !important;
66+
}
67+
}
68+
}
6469
}
6570

6671
&--variant {
@@ -87,7 +92,7 @@ $charcoal-grey: #333;
8792
height: 30px;
8893
border-bottom-width: 5px;
8994
border-bottom-style: solid;
90-
color: white;
95+
color: var(--w-color-text-button);
9196
text-transform: uppercase;
9297
text-align: center;
9398
font-size: 15px;
@@ -104,20 +109,26 @@ $charcoal-grey: #333;
104109
}
105110
&__version--control &__version-heading {
106111
border-bottom-color: $color-control;
112+
@media (prefers-color-scheme: dark) {
113+
border-bottom-color: var(--w-color-secondary-100);
114+
}
107115
}
108116
&__version--variant &__version-heading {
109117
border-bottom-color: $color-variant;
110118
}
111119

112120
&__version--control &__version-heading--winner {
113121
background-color: $color-control;
122+
@media (prefers-color-scheme: dark) {
123+
background-color: var(--w-color-secondary-100);
124+
}
114125
}
115126
&__version--variant &__version-heading--winner {
116127
background-color: $color-variant;
117128
}
118129

119130
&__version-inner {
120-
border: 1px solid #eeeeee;
131+
border: 1px solid var(--w-color-border-furniture);
121132
border-top: none;
122133
box-sizing: border-box;
123134
padding: 20px;
@@ -134,6 +145,17 @@ $charcoal-grey: #333;
134145
}
135146
}
136147

148+
&__version-title--control {
149+
color: $color-control;
150+
@media (prefers-color-scheme: dark) {
151+
color: var(--w-color-secondary-100);
152+
}
153+
}
154+
155+
&__version-title--variant {
156+
color: $color-variant;
157+
}
158+
137159
&__version-stats {
138160
list-style-type: none;
139161
margin: 0;
@@ -161,7 +183,7 @@ $charcoal-grey: #333;
161183
&__version-stat-name {
162184
text-transform: uppercase;
163185
font-size: 20px;
164-
color: $charcoal-grey;
186+
color: var(--w-color-text-placeholder);
165187
margin-top: 20px;
166188

167189
span {
@@ -172,4 +194,16 @@ $charcoal-grey: #333;
172194

173195
.abtest-chart {
174196
padding-top: 20px;
197+
198+
// Chart lines
199+
.c3-axis .tick line,
200+
.c3-axis .domain {
201+
stroke: var(--w-color-text-label);
202+
}
203+
204+
// Axes text
205+
.c3-axis .tick text tspan,
206+
.c3-legend-item text {
207+
fill: var(--w-color-text-label);
208+
}
175209
}

wagtail_ab_testing/templates/wagtail_ab_testing/results.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ <h2>{{ ab_test.name }}</h2>
4545
{% if control_is_winner %}{% icon "crown" %} {% trans "Winner!" %}{% elif unclear_winner %}{% trans "No clear winner" %}{% endif %}
4646
</div>
4747
<div class="abtest-results__version-inner">
48-
<h3>{% trans "Control" %} <a href="{% pageurl page %}" target="_blank">{% icon name="link-external" %}</a></h3>
48+
<h3 class="abtest-results__version-title--control">{% trans "Control" %} <a href="{% pageurl page %}" target="_blank">{% icon name="link-external" %}</a></h3>
4949

5050
<ul class="abtest-results__version-stats">
5151
<li>
@@ -79,7 +79,7 @@ <h3>{% trans "Control" %} <a href="{% pageurl page %}" target="_blank">{% icon n
7979
{% if variant_is_winner %}{% icon "crown" %} {% trans "Winner!" %}{% elif unclear_winner %}{% trans "No clear winner" %}{% endif %}
8080
</div>
8181
<div class="abtest-results__version-inner">
82-
<h3>{% trans "Variant" %} <a href="{% url 'wagtailadmin_pages:view_draft' page.id %}" target="_blank">{% icon name="link-external" %}</a></h3>
82+
<h3 class="abtest-results__version-title--variant">{% trans "Variant" %} <a href="{% url 'wagtailadmin_pages:view_draft' page.id %}" target="_blank">{% icon name="link-external" %}</a></h3>
8383

8484
<ul class="abtest-results__version-stats">
8585
<li>

0 commit comments

Comments
 (0)