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:
| Month | Orders |
|---|---|
| Jan | 420 |
| Feb | 385 |
| Mar | 460 |
| Apr | 510 |
| May | 475 |
| Jun | 540 |
| Month | Orders |
|---|---|
| Jan | 420 |
| Feb | 385 |
| Mar | 460 |
| Apr | 510 |
| May | 475 |
| Jun | 540 |
import type { CSSProperties } from 'react'
import { BarChart } from 'tally-charts'
import type { ExampleMeta } from '../types'
export const meta = {
id: 'dark-mode/side-by-side',
title: 'Orders by month, light theme',
rscSafe: true,
} satisfies ExampleMeta
const months = [
{ month: 'Jan', orders: 420 },
{ month: 'Feb', orders: 385 },
{ month: 'Mar', orders: 460 },
{ month: 'Apr', orders: 510 },
{ month: 'May', orders: 475 },
{ month: 'Jun', orders: 540 },
]
const grid = {
display: 'grid',
gridTemplateColumns: 'repeat(auto-fit, minmax(min(100%, 16rem), 1fr))',
gap: '1.5rem',
} satisfies CSSProperties
const panel = {
background: 'var(--ch-surface)',
color: 'var(--ch-ink)',
padding: '1rem',
borderRadius: 'var(--ch-radius)',
} satisfies CSSProperties
// The attribute wins over the system preference, so each panel is in its own theme whatever
// the visitor's setting is. Props stay serializable (no functions): the page is a server component.
export default function SideBySide() {
return (
<div style={grid}>
<div data-ch-theme="light" style={panel}>
<BarChart
data={months}
x="month"
y="orders"
height={220}
title={meta.title}
description="Orders rose from 420 in January to 540 in June."
/>
</div>
<div data-ch-theme="dark" style={panel}>
<BarChart
data={months}
x="month"
y="orders"
height={220}
title="Orders by month, dark theme"
description="The same orders in the dark theme."
/>
</div>
</div>
)
}
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.
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.
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