Customization: TUI Theming
Relevant source files
The following files were used as context for generating this wiki page:
- packages/opencode/src/cli/cmd/tui/context/theme.tsx
- packages/opencode/src/cli/cmd/tui/context/theme/opencode.json
- packages/opencode/src/cli/cmd/tui/context/theme/dracula.json
- packages/opencode/src/cli/cmd/tui/context/theme/kanagawa.json
- packages/opencode/src/cli/cmd/tui/context/theme/rosepine.json
- CONTRIBUTING.md
The OpenCode Terminal User Interface (TUI) is designed to be fully themeable, allowing for extensive visual customization. This system enables users to change the color scheme of the entire interface to match their preferences or terminal setup. The theming is managed through a system of JSON configuration files and a React Context provider that dynamically generates and injects CSS variables.
The TUI itself is built using SolidJS and the opentui library. The theming system leverages this stack to apply styles across all components consistently. This document outlines the architecture of the theming system, the structure of theme files, and the process of loading and applying a theme.
Sources: CONTRIBUTING.md:19, packages/opencode/src/cli/cmd/tui/context/theme.tsx
Theme Architecture
The theming system is centered around the ThemeProvider component and the ThemeContext. The ThemeProvider is responsible for loading a specified theme, processing its color definitions, and injecting the resulting CSS custom properties into the application's DOM. This allows all TUI components to use these variables for styling.
Sources: packages/opencode/src/cli/cmd/tui/context/theme.tsx:10-14, 52-73
Core Components
| Component / Function | Description | File |
|---|---|---|
ThemeContext | A SolidJS context that stores the current theme's properties. | theme.tsx:12 |
ThemeProvider | A component that wraps the application, loads a theme, and provides it to all children via ThemeContext. | theme.tsx:52 |
useTheme | A hook to access the current theme context from any component. | theme.tsx:14 |
loadTheme | An asynchronous function that fetches and parses a theme's JSON file, resolving color definitions into a flat map of CSS variables. | theme.tsx:16 |
Sources: packages/opencode/src/cli/cmd/tui/context/theme.tsx
Theme Loading and Application Flow
The diagram below illustrates the process from selecting a theme to applying it to the TUI.
This diagram shows how the ThemeProvider initiates theme loading, processes the JSON file, and injects the final CSS variables into a style tag for the UI to consume.
Sources: packages/opencode/src/cli/cmd/tui/context/theme.tsx:16-50, 52-73
The loadTheme function is central to this flow. It takes a theme name, fetches the corresponding JSON file, and processes it. The function first resolves the color palette defined in the defs section. It then iterates through the theme section, which maps semantic names (e.g., primary, background) to color names from defs. Finally, it generates a string of CSS custom properties (e.g., --color-primary: #RRGGBB;) that the ThemeProvider injects into a <style> element in the document head.
Sources: packages/opencode/src/cli/cmd/tui/context/theme.tsx:16-50
Theme File Structure
Each theme is defined in a single JSON file. These files adhere to a specific schema, identified by https://opencode.ai/theme.json. A theme file consists of two main sections: defs and theme.
Sources: packages/opencode/src/cli/cmd/tui/context/theme/opencode.json:2, packages/opencode/src/cli/cmd/tui/context/theme/dracula.json:2, packages/opencode/src/cli/cmd/tui/context/theme/kanagawa.json:2
defs Section
The defs (definitions) section is a key-value map where keys are color names and values are their corresponding hex color codes. This acts as the theme's color palette.
Example from dracula.json:
// packages/opencode/src/cli/cmd/tui/context/theme/dracula.json:3-17
"defs": {
"background": "#282a36",
"currentLine": "#44475a",
"foreground": "#f8f8f2",
"comment": "#6272a4",
"cyan": "#8be9fd",
"green": "#50fa7b",
"orange": "#ffb86c",
"pink": "#ff79c6",
"purple": "#bd93f9",
"red": "#ff5555",
"yellow": "#f1fa8c"
}
Sources: packages/opencode/src/cli/cmd/tui/context/theme/dracula.json:3-17
theme Section
The theme section maps semantic UI element roles to the color names defined in defs. Each property in this section can have dark and light variants, allowing a single theme file to support both modes. The value for each variant is a key from the defs section.
The structure of the theme object is shown below.
This diagram shows that a theme file is composed of color definitions (defs) and semantic mappings (theme), with each mapping supporting dark and light modes.
Sources: packages/opencode/src/cli/cmd/tui/context/theme/opencode.json:10-101, packages/opencode/src/cli/cmd/tui/context/theme/rosepine.json:30-197
Key Theme Properties
The following table lists some of the core semantic properties defined in the theme object. These properties govern the main look and feel of the TUI.
| Property | Description | Example opencode.json Dark Value |
|---|---|---|
primary | Main interactive color for buttons, active borders, etc. | cyan (#00FFFF) |
secondary | Secondary interactive color. | magenta (#FF00FF) |
accent | Accent color for special highlights. | yellow (#FFFF00) |
error | Color for error messages and states. | red (#FF0000) |
text | Default text color. | white (#FFFFFF) |
textMuted | Color for less important text. | gray (#808080) |
background | The main background color of the TUI. | black (#000000) |
backgroundPanel | Background color for panels and distinct sections. | brightBlack (#555555) |
border | Default border color. | brightBlack (#555555) |
borderActive | Border color for focused or active elements. | cyan (#00FFFF) |
*Sources: packages/opencode/src/cli/cmd/tui/context/theme/opencode.json:10-101, packages/opencode/src/cli/cmd/tui/context/theme/kanagawa.json:20-108, packages/opencode/src/cli/cmd/tui/context/theme/rosepine.json:30-197 *
The theme also defines more specific properties for UI elements like diff views and markdown rendering, such as diffAdded, diffRemoved, markdownHeading, and syntaxKeyword.
Available Themes
The repository includes several pre-defined themes:
- opencode: The default theme.
- dracula: A popular dark theme.
- kanagawa: A theme inspired by the Kanagawa wave painting.
- rosepine: A soft, elegant theme.
Each of these is defined in its own JSON file within the packages/opencode/src/cli/cmd/tui/context/theme/ directory.
Sources: packages/opencode/src/cli/cmd/tui/context/theme/opencode.json, packages/opencode/src/cli/cmd/tui/context/theme/dracula.json, packages/opencode/src/cli/cmd/tui/context/theme/kanagawa.json, packages/opencode/src/cli/cmd/tui/context/theme/rosepine.json
Summary
The OpenCode TUI theming system is a flexible and powerful feature that allows for deep customization of the user interface. By leveraging JSON configuration files for color definitions and a SolidJS context provider for dynamic style injection, it provides a straightforward way for users to create and apply new themes. The separation of color definitions (defs) from semantic application (theme) makes themes easy to create and maintain.