Skip to content

Commit 5632722

Browse files
authored
Update src/color-schemes/README.md (#58836)
1 parent e23f667 commit 5632722

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

src/color-schemes/README.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
1-
# Color schemes
1+
# Color Schemes
22

3-
Color schemes are user preferences regarding which type of colors they would like the site to use. Currently we support a number of mode, including light, dark, and several accessibility options.
3+
This module manages the application of color themes (light, dark, high contrast, etc.) to the GitHub Docs site. It ensures that the documentation matches the user's preferred color scheme as configured on GitHub.com.
44

5-
We chose the name "schemes" instead of modes or themes because that is what the HTML specification calls them.
5+
## Purpose & Scope
6+
7+
The primary goal is to read the user's color preference from a cookie and apply the correct theme context to the React application. This supports:
8+
- **Modes**: Light, Dark, Auto (system preference).
9+
- **Themes**: Specific variations like "Dark Dimmed" or "Dark High Contrast".
10+
- **Compatibility**: Bridging the gap between raw CSS class names and Primer React component props.
11+
12+
## Architecture
13+
14+
The core logic is contained within `src/color-schemes/components/useTheme.ts`.
15+
16+
### The `color_mode` Cookie
17+
18+
The site relies on a cookie named `color_mode` to determine the user's preference. This cookie is typically set by the main GitHub application and shared with the docs subdomains. The cookie value is a JSON string containing:
19+
- `color_mode`: The overall mode (`light`, `dark`, `auto`).
20+
- `light_theme`: The specific theme to use when in light mode.
21+
- `dark_theme`: The specific theme to use when in dark mode.
22+
23+
### `useTheme` Hook
24+
25+
The `useTheme` hook is the main entry point. It performs the following steps:
26+
1. **Reads the Cookie**: Parses the `color_mode` cookie safely.
27+
2. **Normalizes Data**: Validates the values against supported enums (`SupportedTheme`, `CssColorMode`).
28+
3. **Formats for Consumers**: Returns two distinct theme objects:
29+
- `css`: For applying global CSS classes (uses `light`/`dark`).
30+
- `component`: For passing to Primer React's `ThemeProvider` (uses `day`/`night`).
31+
32+
### Mapping Logic
33+
34+
Primer React uses slightly different terminology than the underlying CSS or the cookie schema. The module handles this translation:
35+
- CSS `light` -> Component `day`
36+
- CSS `dark` -> Component `night`
37+
38+
## Setup & Usage
39+
40+
To access the current theme in a component:
41+
42+
```typescript
43+
import { useTheme } from '@/color-schemes/components/useTheme'
44+
45+
const MyComponent = () => {
46+
const { theme } = useTheme()
47+
48+
// Access CSS-friendly values
49+
console.log(theme.css.colorMode)
50+
51+
// Access Primer-friendly values
52+
console.log(theme.component.dayScheme)
53+
}
54+
```
55+
56+
This hook is primarily used at the root of the application (e.g., in `src/frame/components/Page.tsx` or `_app.tsx`) to wrap the content in a `ThemeProvider`.
57+
58+
## Dependencies
59+
60+
- **`js-cookie`**: Used via `src/frame/components/lib/cookies` to read the browser cookie.
61+
- **Primer React**: The output format is specifically designed to satisfy Primer React's theming requirements.
62+
63+
## Ownership
64+
65+
- **Team**: `@github/docs-engineering`
66+
67+
## Current State & Known Issues
68+
69+
- **Hydration Mismatch / Flash of Unstyled Content**: Since the theme is read from a cookie on the client side (in `useEffect`), there can be a brief moment where the default theme is applied before the user's preference loads.
70+
- **Race Condition Workaround**: There is a `setTimeout` hack in `useTheme.ts` to delay the theme application. This is necessary to prevent Primer React's internal logic from overriding the user's preference with `auto` on initial load.
71+
- *Reference*: [Primer React Issue #2229](https://github.com/primer/react/issues/2229)
72+
- **Future**: The long-term goal is to rely entirely on CSS variables, removing the need for complex JavaScript state management for theming.

0 commit comments

Comments
 (0)