-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use light theme when js is disabled
Fix a bug where the LSD css vars and themed images were not applied correctly when JS was disabled. This was caused by Docusaurus not setting a data-theme attribute to the HTML element. The commit adds a fallback to the light theme and the LSD theme variables in the CSS.
- Loading branch information
Showing
4 changed files
with
74 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/logos-docusaurus-theme/src/client/theme/ThemedImage/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react' | ||
import clsx from 'clsx' | ||
import useIsBrowser from '@docusaurus/useIsBrowser' | ||
import { useColorMode } from '@docusaurus/theme-common' | ||
import type { Props } from '@theme/ThemedImage' | ||
|
||
import styles from './styles.module.css' | ||
|
||
export default function ThemedImage(props: Props): JSX.Element { | ||
const isBrowser = useIsBrowser() | ||
const { colorMode } = useColorMode() | ||
const { sources, className, alt, ...propsRest } = props | ||
|
||
type SourceName = keyof Props['sources'] | ||
|
||
const clientThemes: SourceName[] = colorMode === 'dark' ? ['dark'] : ['light'] | ||
|
||
const renderedSourceNames: SourceName[] = isBrowser | ||
? clientThemes | ||
: // We need to render both images on the server to avoid flash | ||
// See https://github.com/facebook/docusaurus/pull/3730 | ||
['light', 'dark'] | ||
|
||
return ( | ||
<> | ||
{renderedSourceNames.map((sourceName) => ( | ||
<img | ||
key={sourceName} | ||
src={sources[sourceName]} | ||
alt={alt} | ||
className={clsx( | ||
styles.themedImage, | ||
styles[`themedImage--${sourceName}`], | ||
className, | ||
)} | ||
{...propsRest} | ||
/> | ||
))} | ||
</> | ||
) | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/logos-docusaurus-theme/src/client/theme/ThemedImage/styles.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.themedImage { | ||
display: none; | ||
} | ||
|
||
[data-theme='light'] .themedImage--light { | ||
display: initial; | ||
} | ||
|
||
[data-theme='dark'] .themedImage--dark { | ||
display: initial; | ||
} | ||
|
||
/* | ||
JS disabled??? Show light version by default => better than showing nothing | ||
TODO bad, but we currently always show light mode when there's no data-theme | ||
*/ | ||
html:not([data-theme]) .themedImage--light { | ||
display: initial; | ||
} |