Skip to content

Commit 2dfebaf

Browse files
authored
Pass the resolved PRC color scheme to the brand provider (#62782)
1 parent a89d92f commit 2dfebaf

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { PropsWithChildren } from 'react'
2+
import { useTheme as usePrimerTheme } from '@primer/react'
3+
import { ThemeProvider } from '@primer/react-brand'
4+
5+
import { getBrandColorMode } from '@/color-schemes/lib/get-brand-color-mode'
6+
7+
export const BrandThemeProvider = ({ children }: PropsWithChildren) => {
8+
// We need to resolve the color scheme through PRC first, because there are
9+
// otherwise many unhandled edge cases.
10+
// E.g. auto mode + dark mode + light scheme.
11+
const { resolvedColorScheme } = usePrimerTheme()
12+
const colorMode = getBrandColorMode(resolvedColorScheme)
13+
14+
return <ThemeProvider colorMode={colorMode}>{children}</ThemeProvider>
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getBrandColorMode(resolvedColorScheme?: string) {
2+
return resolvedColorScheme?.startsWith('dark') ? 'dark' : 'light'
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { describe, expect, test } from 'vitest'
2+
3+
import { getBrandColorMode } from '@/color-schemes/lib/get-brand-color-mode'
4+
5+
describe('getBrandColorMode', () => {
6+
test.each([
7+
['light', 'light'],
8+
['dark', 'dark'],
9+
['dark_dimmed', 'dark'],
10+
['dark_high_contrast', 'dark'],
11+
])('maps the %s scheme to %s mode', (scheme, mode) => {
12+
expect(getBrandColorMode(scheme)).toBe(mode)
13+
})
14+
})

src/frame/pages/app.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import App from 'next/app'
33
import type { AppProps, AppContext } from 'next/app'
44
import Head from 'next/head'
55
import { ThemeProvider } from '@primer/react'
6-
import { ThemeProvider as BrandThemeProvider } from '@primer/react-brand'
76
import { useRouter } from 'next/router'
87

8+
import { BrandThemeProvider } from '@/color-schemes/components/BrandThemeProvider'
99
import { initializeEvents } from '@/events/components/events'
1010
import {
1111
initializeExperiments,
@@ -117,10 +117,10 @@ const MyApp = ({ Component, pageProps, languagesContext, stagingName }: MyAppPro
117117
components receive brand theme context during the Docs 2026 migration
118118
(github/docs-engineering#5879). Runs alongside the @primer/react
119119
ThemeProvider above while the component-by-component swap is in progress.
120-
Brand expects a CSS color mode ('auto' | 'light' | 'dark'), so pass
121-
theme.css.colorMode rather than the component ('auto' | 'day' | 'night') mode.
120+
Resolve Brand's color mode from Primer React's active color scheme so
121+
opposite-mode day/night schemes stay in sync.
122122
*/}
123-
<BrandThemeProvider colorMode={theme.css.colorMode}>
123+
<BrandThemeProvider>
124124
<LanguagesContext.Provider value={languagesContext}>
125125
<SharedUIContextProvider>
126126
<ClientSideHashFocus />

0 commit comments

Comments
 (0)