Why Your Chat Widget Ignores Your Dark Mode Toggle

Engineering

You shipped dark mode. A visitor clicks the moon icon, the whole page goes dark in one smooth transition — and in the bottom-right corner a bright white rectangle stays exactly where it was. The support chat.

It is a small thing, and it is the kind of small thing people notice, because it is the one part of the page that clearly was not built by you. Here is why embedded widgets get this wrong so often, why the obvious fixes do not work, and what a widget needs to offer so that yours does not.

There are two dark modes, not one

"Dark mode" on a website means one of two different things.

  • The operating system's preference. The visitor set their laptop to dark. The browser exposes it through the prefers-color-scheme media query.

  • Your site's setting. A toggle in your header, stored in localStorage or a cookie, and usually applied as a dark class or a data-theme attribute on <html>.

Most sites with a toggle offer three choices — light, dark and system — precisely because the two disagree. Plenty of people run a light OS and still want a dark docs site, a dark dashboard, a dark anything at night.

A third-party widget can see the first one. It cannot see the second one unless you tell it.

Why "auto" is not enough

Almost every embeddable widget has a theme option, and almost every one of them offers auto:

HTML
<script src="https://widget.example.com/chat.js" data-theme="auto"></script>

auto means "follow prefers-color-scheme". That is exactly right for a site with no toggle, which is why it is a sensible default. It is exactly wrong the moment your site has its own switch: a visitor with a light OS who picked dark on your site gets a dark page and a light widget. The media query cannot see the class on your <html> element, because it does not look at your page at all.

Hard-coding data-theme="dark" only moves the problem to the visitors who chose light.

Why you cannot fix it with CSS

The next instinct is to reach in and restyle it:

CSS
.dark .chat-panel { background: #111; color: #eee; }

On a well-built widget this does nothing, and that is on purpose. Widgets that live on other people's pages render inside a shadow root, so the host page's CSS cannot break them and theirs cannot break the host page. Your selector never crosses that boundary.

Some widgets do let a few CSS custom properties through, and it is tempting to set those from your dark-mode class. Resist it: those names are the widget's internals, not its API. They change without a changelog entry, and your dark mode quietly breaks on a release you never saw.

Why reloading it is worse

Some widget docs suggest the blunt instrument: destroy the widget and create it again with the new theme.

JavaScript
Widget.destroy();
Widget.init({ ...config, theme: 'dark' });

It works, in the sense that the colors change. It also unmounts everything the visitor was in the middle of. The panel they had open closes. The answer that was still streaming is cut off halfway through a sentence. And the toggle is often clicked because the visitor is reading something late at night — the worst possible moment to throw their conversation away.

What a widget actually needs to offer

The theme belongs to the host page, so the host page has to be able to tell the widget when it changes. That is one method:

JavaScript
KookeeChat.setTheme('dark'); // 'light', 'dark' or 'auto'

The method is the easy part. What makes it usable is how it behaves:

  1. It applies in place. No remount: an open panel stays open and a streaming answer keeps streaming. Only the colors change.

  2. It works before the widget has loaded. Widget scripts load async, and your theme code often runs first. A call made early should be queued, not dropped or turned into an error.

  3. It does not care about order. Whether your call ran before or after the widget read its own config, the explicit call wins.

  4. auto is still an option. "System" is a real choice in your toggle, and the widget should be able to follow it live.

If you are evaluating a chat or support widget and your product has a theme toggle, those four are worth checking before you commit. The first one is where most implementations quietly fall short.

Wiring it to your toggle

The simplest version lives inside the function your toggle already calls:

JavaScript
function applyTheme(choice) { // 'light' | 'dark' | 'system'
  const dark = choice === 'dark' || (choice === 'system' && matchMedia('(prefers-color-scheme: dark)').matches);
  document.documentElement.classList.toggle('dark', dark);
  localStorage.setItem('theme', choice);

  KookeeChat.setTheme(choice === 'system' ? 'auto' : choice);
}

Notice that the last line maps system to 'auto', not to whatever system resolves to right now. Pass the resolved value and the widget freezes at that moment: when the visitor's OS switches to dark at sunset, your page follows and the widget does not. Pass 'auto' and the widget watches the OS itself.

Setting the starting theme without a flash

setTheme() handles changes. For the first paint, hand the widget the choice you already know, as early as your own theme script runs:

JavaScript
const stored = localStorage.getItem('theme'); // 'light' | 'dark' | 'system' | null
KookeeChat.setTheme(stored === 'light' || stored === 'dark' ? stored : 'auto');

If that runs before the widget script has loaded, the small queueing stub from the install snippet holds the call until the script arrives — as long as 'setTheme' is in the stub's list of methods. If you render the page on the server and know the theme there, a data-theme attribute on the script tag does the same job with no JavaScript at all.

When you do not own the toggle

On a Shopify theme, a WordPress theme, or a design system that manages dark mode for you, there may be no toggle function to add a line to. Watch the result instead:

JavaScript
const root = document.documentElement;
const sync = () => KookeeChat.setTheme(root.classList.contains('dark') ? 'dark' : 'light');

new MutationObserver(sync).observe(root, { attributes: true, attributeFilter: ['class', 'data-theme'] });
sync();

Adjust the check to however your theme marks dark mode — a data-theme="dark" attribute is as common as a class. This passes the resolved value on purpose: when you only see the result, following the page exactly is the right call, and the theme resolves "system" for you.

In React

When the widget is a React component, the theme is just a prop, and passing your current setting is the whole job. With next-themes:

tsx
const { theme } = useTheme(); // 'light' | 'dark' | 'system' | undefined

<KookeeChatWidget
  clientOptions={{ apiKey }}
  theme={theme === 'light' || theme === 'dark' ? theme : 'auto'}
/>;

Use theme, not resolvedTheme. resolvedTheme is unknown during server rendering and on the first client render, so a widget fed from it starts light and flips a moment later for every dark-mode visitor. theme mapped this way falls back to 'auto', which is already correct for most of them.

The takeaway

auto follows the operating system, and a toggle on your site is by definition something the operating system does not know about. A widget that sits on your page should take its theme from your page — at runtime, in place, and without losing what the visitor was doing.


Kookee's AI chat widget supports this today. From the script tag, call KookeeChat.setTheme(); in React, the theme prop of KookeeChatWidget updates live. The React widget is the one exception to the shadow-root rule above: it renders in your page, and the --kookee-* variables it reads are documented, so setting them is safe. See Customize the chat widget's appearance for the theme options, and Control the chat widget from JavaScript for the full API.