Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Apply Settings theme toggle optimistically on the changing client (#1978)

Andrew Mikofalvyee5e75604515
diff --git a/.changeset/optimistic-theme-toggle.md b/.changeset/optimistic-theme-toggle.md
new file mode 100644
index 000000000..b8ea1ee98
--- /dev/null
+++ b/.changeset/optimistic-theme-toggle.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Make the appearance theme toggle in Settings → Preferences flip instantly. Clicking Light, Dark, or System now applies the new appearance optimistically on the client you clicked in, instead of waiting for the change to round-trip through the config write and re-render. The setting still saves to your user config and still propagates to other open projects and windows, and "System" keeps tracking your OS appearance live.
diff --git a/packages/app/src/components/settings/SettingsDialogBody.dom.test.tsx b/packages/app/src/components/settings/SettingsDialogBody.dom.test.tsx
index febd71eae..450df8027 100644
--- a/packages/app/src/components/settings/SettingsDialogBody.dom.test.tsx
+++ b/packages/app/src/components/settings/SettingsDialogBody.dom.test.tsx
@@ -6,8 +6,9 @@ import {
type ConfigPatch,
ConfigSchema,
} from '@inkeep/open-knowledge-core';
-import { act, cleanup, render, screen, waitFor } from '@testing-library/react';
+import { act, cleanup, render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import { ThemeProvider, useTheme } from 'next-themes';
import { TooltipProvider } from '@/components/ui/tooltip';
import { emitConfigValidationRejected } from '@/lib/config-validation-events';
import { expectVisualClassTokens } from '@/test-utils/visual-contract';
@@ -178,3 +179,90 @@ describe('SettingsDialogBody preferences runtime', () => {
expectVisualClassTokens(wordWrapField?.className, ['animate-settings-flash']);
});
});
+
+let themeStorageKeySeq = 0;
+
+function ThemeProbe() {
+ const { theme } = useTheme();
+ return <span data-testid="theme-probe">{theme ?? ''}</span>;
+}
+
+function renderPreferencesWithTheme(binding: ConfigBinding) {
+ themeStorageKeySeq += 1;
+ return render(
+ <ThemeProvider
+ attribute="class"
+ defaultTheme="system"
+ enableSystem
+ storageKey={`ok-theme-v1-test-${themeStorageKeySeq}`}
+ >
+ <TooltipProvider>
+ <SettingsDialogBody
+ activeId="preferences"
+ userBinding={binding}
+ okignoreBinding={null}
+ okignoreSynced={false}
+ />
+ <ThemeProbe />
+ </TooltipProvider>
+ </ThemeProvider>,
+ );
+}
+
+function themeToggleItem(container: HTMLElement, option: string): HTMLElement {
+ const field = container.querySelector('[data-field="appearance.theme"]');
+ if (!field) throw new Error('appearance.theme field not rendered');
+ return within(field as HTMLElement).getByText(option);
+}
+
+describe('SettingsDialogBody theme toggle — optimistic apply', () => {
+ afterEach(() => {
+ cleanup();
+ });
+
+ test('clicking Dark flips next-themes immediately (no ConfigProvider) and still persists via binding.patch', async () => {
+ const user = userEvent.setup();
+ const { binding, patches } = makeBinding();
+ const { container } = renderPreferencesWithTheme(binding);
+
+ expect(screen.getByTestId('theme-probe').textContent).toBe('system');
+
+ await user.click(themeToggleItem(container, 'dark'));
+
+ await waitFor(() => {
+ expect(screen.getByTestId('theme-probe').textContent).toBe('dark');
+ });
+ expect(patches).toEqual([{ appearance: { theme: 'dark' } }]);
+ });
+
+ test("clicking System forwards 'system' verbatim to next-themes (does not resolve to light/dark)", async () => {
+ const user = userEvent.setup();
+ const { binding, patches } = makeBinding();
+ const { container } = renderPreferencesWithTheme(binding);
+
+ await user.click(themeToggleItem(container, 'dark'));
+ await waitFor(() => {
+ expect(screen.getByTestId('theme-probe').textContent).toBe('dark');
+ });
+
+ await user.click(themeToggleItem(container, 'system'));
+
+ await waitFor(() => {
+ expect(screen.getByTestId('theme-probe').textContent).toBe('system');
+ });
+ expect(patches.at(-1)).toEqual({ appearance: { theme: 'system' } });
+ });
+
+ test('clicking Light flips to light and records the patch', async () => {
+ const user = userEvent.setup();
+ const { binding, patches } = makeBinding();
+ const { container } = renderPreferencesWithTheme(binding);
+
+ await user.click(themeToggleItem(container, 'light'));
+
+ await waitFor(() => {
+ expect(screen.getByTestId('theme-probe').textContent).toBe('light');
+ });
+ expect(patches).toEqual([{ appearance: { theme: 'light' } }]);
+ });
+});
diff --git a/packages/app/src/components/settings/SettingsDialogBody.tsx b/packages/app/src/components/settings/SettingsDialogBody.tsx
index 82a171b14..eed9e3070 100644
--- a/packages/app/src/components/settings/SettingsDialogBody.tsx
+++ b/packages/app/src/components/settings/SettingsDialogBody.tsx
@@ -17,6 +17,7 @@ import type { MessageDescriptor } from '@lingui/core';
import { msg } from '@lingui/core/macro';
import { Trans, useLingui } from '@lingui/react/macro';
import { ArrowUpRight, Check, ChevronRight, RotateCcw } from 'lucide-react';
+import { useTheme } from 'next-themes';
import { useEffect, useRef, useState } from 'react';
import { type ControllerRenderProps, type FieldPath, useFormContext } from 'react-hook-form';
import { toast } from 'sonner';
@@ -728,6 +729,7 @@ function FieldControlBody({
}: FieldControlBodyProps & SlotForwardedProps) {
'use no memo';
const { t } = useLingui();
+ const { setTheme } = useTheme();
if (typeTag === 'boolean') {
return (
<Switch
@@ -745,6 +747,7 @@ function FieldControlBody({
if (typeTag === 'enum' && enumOptions && enumOptions.length > 0) {
if (field.control === 'enum-toggle' || enumOptions.length <= 4) {
const { id: forwardedId, ...wrapperSlotProps } = slotForwarded;
+ const isThemeField = field.path[0] === 'appearance' && field.path[1] === 'theme';
return (
<ToggleGroup
{...wrapperSlotProps}
@@ -753,6 +756,7 @@ function FieldControlBody({
ref={ctl.ref}
onValueChange={(next) => {
if (!next) return;
+ if (isThemeField) setTheme(next);
ctl.onChange(next);
onCommit();
}}