From 34c5238f47787afd2214554501bae350dc1dfd35 Mon Sep 17 00:00:00 2001 From: Jhonatan Lopes Date: Wed, 21 Feb 2024 12:21:59 -0300 Subject: [PATCH] Setup chart colours dynamically --- wagtail_ab_testing/static_src/main.tsx | 38 ++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/wagtail_ab_testing/static_src/main.tsx b/wagtail_ab_testing/static_src/main.tsx index 5780d4c..d0c82e3 100644 --- a/wagtail_ab_testing/static_src/main.tsx +++ b/wagtail_ab_testing/static_src/main.tsx @@ -10,6 +10,20 @@ document.addEventListener('DOMContentLoaded', () => { // Goal selector on create new A/B test initGoalSelector(); + const colorControl = '#0C0073'; // CSS $color-control + const colorControlDark = '#00B0B1'; // Wagtail --w-color-secondary-100 + const colorVariant = '#EF746F'; // CSS $color-variant + + // Match chart pattern colors to dark/light mode + let pattern = [colorControl, colorVariant]; + if ( + window.matchMedia && + window.matchMedia('(prefers-color-scheme: dark)').matches + ) { + // dark mode + pattern = [colorControlDark, colorVariant]; + } + // Charts on A/B test progress document.querySelectorAll('[component="chart"]').forEach((chartElement) => { if ( @@ -19,7 +33,7 @@ document.addEventListener('DOMContentLoaded', () => { return; } - c3.generate({ + const chart = c3.generate({ bindto: chartElement, data: JSON.parse(chartElement.getAttribute('data')!), padding: { @@ -34,12 +48,30 @@ document.addEventListener('DOMContentLoaded', () => { }, }, color: { - pattern: ['#0C0073', '#EF746F'], + pattern: pattern, }, }); + + // Add an event listener to update chart colors when the color scheme changes + window + .matchMedia('(prefers-color-scheme: dark)') + .addEventListener('change', (event) => { + const newColorScheme = event.matches ? 'dark' : 'light'; + if (newColorScheme === 'dark') { + chart.data.colors({ + Control: colorControlDark, + Variant: colorVariant, + }); + } else { + chart.data.colors({ + Control: colorControl, + Variant: colorVariant, + }); + } + }); }); - // A/B testing tab on page edito + // A/B testing tab on page editor if (abTestingTabProps) { $('ul.tab-nav').append(`
  • ${gettext('A/B testing')}