Guides

Theming

Design tokens, dark mode, pinned colours and Chart.js options.

Styling runs on CSS variables that start with --em-, and you can override any of them in the provider’s theme.styles. The default look is an orange and blue palette on a white card with a hairline border. Semantic tokens, such as the background, the text and the ten chart colours, restyle every component, and component tokens adjust one.

BrandCharts.tsx
import type { ReactNode } from 'react';
import { ChartsProvider } from 'em-kit';

const theme = {
  styles: {
    '--em-sem-chart-color--1': '#0b6e99', // first series colour
    '--em-sem-chart-color--2': '#23a6d5', // second series colour
    '--em-sem-background': '#ffffff', // card background
    '--em-card-border-radius': '0.5rem', // one component's token
  },
};

export function BrandCharts({ children }: { children: ReactNode }) {
  return <ChartsProvider theme={theme}>{children}</ChartsProvider>;
}

The tokens are set on the page root, so one theme is active per page. To see every chart restyle together, use the theme switcher (Default, Cool, Mono or Uluru) above the preview on any component page.

Dark mode

A dark theme is a handful of semantic overrides.

DarkModeCharts.tsx
import type { ReactNode } from 'react';
import { ChartsProvider } from 'em-kit';

const dark = {
  styles: {
    '--em-sem-background': '#18181b',
    '--em-sem-background--light': '#27272a',
    '--em-sem-text': '#fafafa',
    '--em-sem-text--muted': '#a1a1aa',
    '--em-sem-text--subtle': '#52525b',
  },
};

const light = {}; // no overrides: the default theme

export function DarkModeCharts({ isDark, children }: { isDark: boolean; children: ReactNode }) {
  return <ChartsProvider theme={isDark ? dark : light}>{children}</ChartsProvider>;
}

Pinned colours

Charts give each value a colour and keep it the same in every chart. To choose the colour yourself, map it in theme.charts.backgroundColorMap: the key is dimensionName.value for a dimension value, or the measure’s name.

PinnedColours.tsx
import type { ReactNode } from 'react';
import { ChartsProvider } from 'em-kit';

const theme = {
  charts: {
    backgroundColorMap: {
      // dimensionName.value
      dimensionValue: {
        'plan.Starter': '#fbbf24',
        'plan.Growth': '#d97706',
        'plan.Enterprise': '#92400e',
      },
      // measure name
      measure: { revenue: '#0b6e99' },
    },
  },
};

export function PinnedColours({ children }: { children: ReactNode }) {
  return <ChartsProvider theme={theme}>{children}</ChartsProvider>;
}

Chart.js options

Each chart type takes Chart.js options under theme.charts, merged over the defaults. The keys are pieChartPro, donutChartPro, donutLabelChartPro, barChartDefaultPro, barChartDefaultHorizontalPro, barChartGroupedPro, barChartGroupedHorizontalPro, barChartStackedPro, barChartStackedHorizontalPro, lineChartDefaultPro, lineChartGroupedPro, lineChartComparisonDefaultPro, barLineChartPro, areaChartPro, scatterChartPro, bubbleChartPro and funnelChartPro. This example turns animation off for readers who ask for reduced motion.

StillCharts.tsx
import type { ReactNode } from 'react';
import { ChartsProvider } from 'em-kit';

// Chart.js options for one chart type, merged over the defaults.
const noAnimation = { options: { animation: false as const } };

const still = {
  charts: {
    barChartDefaultPro: noAnimation,
    lineChartDefaultPro: noAnimation,
    pieChartPro: noAnimation,
  },
};
const animated = {}; // no overrides: the default theme

export function StillCharts({ children }: { children: ReactNode }) {
  const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  return <ChartsProvider theme={reduceMotion ? still : animated}>{children}</ChartsProvider>;
}