On this page

Guides

Dark mode

Dark mode works without any code: tally-charts/theme.css defines the tokens for both themes, and the charts use the dark ones when the visitor's system prefers it (prefers-color-scheme: dark). To follow a toggle in your app instead, set data-ch-theme="light" or data-ch-theme="dark" on <html> or on any ancestor of a chart. The attribute wins over the system setting, and without it the system setting decides.

Forcing a theme #

The attribute works on any element, not only on <html>. Both panels below are pinned, so they look the same whatever the visitor's setting is:

Orders by month, light theme
View of Orders by month, light theme
Orders by month, light theme
MonthOrders
Jan420
Feb385
Mar460
Apr510
May475
Jun540
Orders by month, dark theme
MonthOrders
Jan420
Feb385
Mar460
Apr510
May475
Jun540

Class-based dark mode #

An app whose dark mode is a class (.dark, as with Tailwind and shadcn) has to set the attribute too. The class alone does not reach the tokens, because the tokens look at data-ch-theme and at the system setting, not at class names.

With next-themes #

Tell ThemeProvider to write both: the class for your own styles and the attribute for the charts. suppressHydrationWarning on <html> is needed because the provider sets the attribute before React hydrates.

app/layout.tsx
import { ThemeProvider } from 'next-themes'
import 'tally-charts/theme.css'

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body>
        <ThemeProvider attribute={['class', 'data-ch-theme']}>{children}</ThemeProvider>
      </body>
    </html>
  )
}

With your own provider #

Set document.documentElement.dataset.chTheme = resolvedTheme wherever your code toggles the class, and render the same data-ch-theme on <html> on the server, for example from a cookie, so the first paint is in the right theme and nothing flashes.

app/layout.tsx
import { cookies } from 'next/headers'
import 'tally-charts/theme.css'

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const stored = (await cookies()).get('theme')?.value
  const theme = stored === 'light' || stored === 'dark' ? stored : undefined

  return (
    // No cookie yet: no attribute, so the charts follow the system setting.
    <html lang="en" className={theme} data-ch-theme={theme}>
      <body>{children}</body>
    </html>
  )
}

// In the client code that toggles your class, set the attribute with it:
//   document.documentElement.classList.toggle('dark', resolvedTheme === 'dark')
//   document.documentElement.dataset.chTheme = resolvedTheme

See Theming for the tokens and Palettes for the vivid palette, which is light only.