Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: fix theme flashing #27385

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions client/web/src/theme.ts
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import memoizeOne from 'memoize-one'
import { useCallback, useMemo, useState } from 'react'

import { observeSystemIsLightTheme, ThemeProps } from '@sourcegraph/shared/src/theme'
import { useObservable } from '@sourcegraph/shared/src/util/useObservable'
Expand Down Expand Up @@ -39,6 +40,13 @@ const readStoredThemePreference = (localStorage: Pick<Storage, 'getItem' | 'setI
}
}

const updateDocumentThemeClassname = memoizeOne(
(isLightTheme: boolean, documentElement: Pick<HTMLElement, 'classList'>) => {
documentElement.classList.toggle('theme-light', isLightTheme)
documentElement.classList.toggle('theme-dark', !isLightTheme)
}
)

/**
* A React hook for getting and setting the theme.
*
Expand Down Expand Up @@ -68,10 +76,10 @@ export const useTheme = (
)

const isLightTheme = themePreference === 'system' ? systemIsLightTheme : themePreference === 'light'
useEffect(() => {
documentElement.classList.toggle('theme-light', isLightTheme)
documentElement.classList.toggle('theme-dark', !isLightTheme)
}, [documentElement.classList, isLightTheme])

// Memoized function executed synchronously to avoid theme flashing:
// https://github.com/sourcegraph/sourcegraph/issues/26435
updateDocumentThemeClassname(isLightTheme, documentElement)

return {
isLightTheme,
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -384,6 +384,7 @@
"lodash": "^4.17.20",
"marked": "^4.0.0",
"mdi-react": "^8.1.0",
"memoize-one": "^6.0.0",
"minimatch": "^3.0.4",
"monaco-editor": "^0.24.0",
"monaco-yaml": "^3.2.1",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -16464,6 +16464,11 @@ memoize-one@^5.0.0:
resolved "https://registry.npmjs.org/memoize-one/-/memoize-one-5.0.4.tgz#005928aced5c43d890a4dfab18ca908b0ec92cbc"
integrity sha512-P0z5IeAH6qHHGkJIXWw0xC2HNEgkx/9uWWBQw64FJj3/ol14VYdfVGWWr0fXfjhhv3TKVIqUq65os6O4GUNksA==

memoize-one@^6.0.0:
version "6.0.0"
resolved "https://registry.npmjs.org/memoize-one/-/memoize-one-6.0.0.tgz#b2591b871ed82948aee4727dc6abceeeac8c1045"
integrity sha512-rkpe71W0N0c0Xz6QD0eJETuWAJGnJ9afsl1srmwPrI+yBCkge5EycXXbYRyvL29zZVUWQCY7InPRCv3GDXuZNw==

memoizee@^0.4.14:
version "0.4.14"
resolved "https://registry.npmjs.org/memoizee/-/memoizee-0.4.14.tgz#07a00f204699f9a95c2d9e77218271c7cd610d57"
Expand Down