Theming & Fonts System
Relevant source files
The following files were used as context for generating this wiki page:
The Theming & Fonts System in OpenChat provides a robust infrastructure for managing the visual appearance of the application, including color schemes, typography, and layout properties. It is designed to support dynamic switching between light and dark modes, custom font selections, and user-defined HSL adjustments. The system integrates closely with Tailwind CSS v4 and uses CSS variables to drive styles across the entire UI.
This system ensures that accessibility standards, such as contrast ratios and keyboard visibility, are maintained through a set of core design guidelines while allowing users to personalize their experience via the Customization Settings.
Core Architecture
The system is built around a centralized state management approach using Zustand for the editor and Zod for schema validation. The architecture separates the definition of styles (what the colors are) from the application of styles (how they are injected into the DOM).
Data Structures & Schemas
The foundation of the system is the ThemeStyleProps, which defines all CSS variables used for colors, shadows, and spacing.
export interface ThemeEditorState {
styles: ThemeStyles;
currentMode: "light" | "dark";
preset?: string;
hslAdjustments: {
hueShift: number;
saturationScale: number;
lightnessScale: number;
};
}
Sources: src/lib/types/theme.ts:92-101
The themeStylePropsSchema ensures that every theme includes mandatory properties, such as background, primary, sidebar, and chart colors.
Sources: src/lib/types/theme.ts:3-56
Theme Flow Diagram
This diagram illustrates how theme state is transformed and applied to the application root.
The system utilizes a top-down approach where the ThemeEditorState drives the applyThemeToElement function to update the DOM.
Sources: src/lib/theme/apply-theme.ts:100-116
Theme Application Logic
The application of themes is handled by the applyThemeToElement utility. This function performs three primary tasks:
- Class Management: Toggles the
.darkclass on the root element based on thecurrentMode. - Color Injection: Iterates through the style properties and sets CSS custom properties (variables) on the target element.
- Font Normalization: Maps font family names to specific internal CSS variables (e.g.,
--font-inter) to ensure compatibility with Next.js font loading.
Style Application Rules
The system distinguishes between "common" styles (non-color properties like radius and spacing) and "mode-specific" styles (colors like background and primary).
| Property Type | Source | Application Behavior |
|---|---|---|
| Common Styles | Light Theme Definition | Applied globally regardless of mode (e.g., --radius). |
| Colors | Mode-specific Definition | Values pulled from styles.light or styles.dark based on active mode. |
| Fonts | Light Theme Definition | Driven via --active-font-sans and --active-font-mono. |
Sources: src/lib/theme/apply-theme.ts:79-98, src/lib/config/tests/theme.test.ts:104-107
Font Management System
OpenChat supports a variety of sans-serif and monospace fonts. The system uses a mapping strategy to convert human-readable labels into valid CSS font stacks and Next.js font variables.
Font Categories
Fonts are categorized into sans and mono. Each font is represented as a FontOption.
- Sans Fonts: System Default, Geist, Inter, Space Grotesk, Open Sans, etc.
- Mono Fonts: System Mono, Geist Mono, Fira Mono, JetBrains Mono, etc.
Sources: src/lib/theme/theme-fonts.ts:24-81
Font Variable Mapping
To avoid overriding Tailwind v4 tokens directly, the system uses "active" variables. When a font is selected, buildActiveFontValue looks up the corresponding Next.js font variable.
Sources: src/lib/theme/apply-theme.ts:47-64
Customization Logic
The customization system allows users to modify their visual preferences and personal information. It includes safeguards to prevent accidental data loss.
State Snapshotting
When the customization page loads, it creates a "snapshot" of the user's current settings. This snapshot is compared against a local "draft" state to determine if there are unsaved changes.
Sources: src/routes/settings/customization.tsx:32-41, src/routes/settings/customization.tsx:64-70
Unsaved Changes Safeguards
The system implements multiple levels of protection:
- BeforeUnload: Prevents browser-level navigation or tab closing if changes are pending.
- Navigation Interception: Uses a custom interceptor for application-level routing to show an
AlertDialog.
const handleNavigationAttempt = useCallback(
(url: string, e: MouseEvent) => {
e.preventDefault();
setPendingUrl(url);
setShowUnsavedChangesDialog(true);
},
[setPendingUrl, setShowUnsavedChangesDialog],
);
Sources: src/routes/settings/customization.tsx:96-106
Design & Accessibility Guidelines
The theming system must adhere to strict design rules defined in the project's guidelines to ensure high performance and accessibility.
Key Guidelines
- Contrast: MUST meet contrast requirements, preferring APCA over WCAG 2.
- Motion: MUST honor
prefers-reduced-motionand animate only compositor-friendly properties liketransformandopacity. - Interactions: MUST provide visible focus rings using
:focus-visibleand manage focus traps for modals. - Typography: MUST use tabular numbers (
font-variant-numeric: tabular-nums) for comparisons and ensure mobile<input>font-size is ≥16px to prevent auto-zoom.
Sources: agent_rules/design_guidelines.md:5-15, agent_rules/design_guidelines.md:51-58, agent_rules/design_guidelines.md:104-106
Summary
The Theming & Fonts System provides a highly configurable and type-safe environment for visual customization. By leveraging Zod schemas for validation and a centralized application logic in apply-theme.ts, the project maintains consistency across diverse UI components. The integration of a draft-based customization flow and strict accessibility guidelines ensures that the system is both user-friendly and inclusive.