-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): add ThemedImage component (#3730)
* feat(v2): add ThemedImage component * add themed image problematic example * refactor, SSR fix, openness about extending img tag, docs update * refactor themed-image * update themed image doc Co-authored-by: slorber <[email protected]> Co-authored-by: Sébastien Lorber <[email protected]>
- Loading branch information
1 parent
73f151d
commit 9d90e89
Showing
9 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/docusaurus-theme-bootstrap/src/theme/ThemeContext.ts
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,15 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import type {ThemeContextProps} from '@theme/hooks/useThemeContext'; | ||
|
||
const ThemeContext = React.createContext<ThemeContextProps | undefined>( | ||
undefined, | ||
); | ||
|
||
export default ThemeContext; |
53 changes: 53 additions & 0 deletions
53
packages/docusaurus-theme-bootstrap/src/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,53 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
|
||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import useThemeContext from '@theme/hooks/useThemeContext'; | ||
import type {Props} from '@theme/ThemedImage'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
const ThemedImage = (props: Props): JSX.Element => { | ||
const {isClient} = useDocusaurusContext(); | ||
const {isDarkTheme} = useThemeContext(); | ||
const {sources, className, alt = '', ...propsRest} = props; | ||
|
||
type SourceName = keyof Props['sources']; | ||
|
||
const renderedSourceNames: SourceName[] = isClient | ||
? isDarkTheme | ||
? ['dark'] | ||
: ['light'] | ||
: // 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) => { | ||
return ( | ||
<img | ||
key={sourceName} | ||
src={sources[sourceName]} | ||
alt={alt} | ||
className={clsx( | ||
styles.themedImage, | ||
styles[`themedImage--${sourceName}`], | ||
className, | ||
)} | ||
{...propsRest} | ||
/> | ||
); | ||
})} | ||
</> | ||
); | ||
}; | ||
|
||
export default ThemedImage; |
11 changes: 11 additions & 0 deletions
11
packages/docusaurus-theme-bootstrap/src/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,11 @@ | ||
.themedImage { | ||
display: none; | ||
} | ||
|
||
html[data-theme='light'] .themedImage--light { | ||
display: block; | ||
} | ||
|
||
html[data-theme='dark'] .themedImage--dark { | ||
display: block; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/docusaurus-theme-bootstrap/src/theme/hooks/useThemeContext.ts
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 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {useContext} from 'react'; | ||
|
||
import ThemeContext from '@theme/ThemeContext'; | ||
import type {ThemeContextProps} from '@theme/hooks/useThemeContext'; | ||
|
||
// TODO: Un-stub the theme context (#3730) | ||
function useThemeContext(): ThemeContextProps { | ||
const context = useContext<ThemeContextProps | undefined>(ThemeContext); | ||
return context == null ? {isDarkTheme: false} : context; | ||
} | ||
|
||
export default useThemeContext; |
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
53 changes: 53 additions & 0 deletions
53
packages/docusaurus-theme-classic/src/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,53 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
|
||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import useThemeContext from '@theme/hooks/useThemeContext'; | ||
import type {Props} from '@theme/ThemedImage'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
const ThemedImage = (props: Props): JSX.Element => { | ||
const {isClient} = useDocusaurusContext(); | ||
const {isDarkTheme} = useThemeContext(); | ||
const {sources, className, alt = '', ...propsRest} = props; | ||
|
||
type SourceName = keyof Props['sources']; | ||
|
||
const renderedSourceNames: SourceName[] = isClient | ||
? isDarkTheme | ||
? ['dark'] | ||
: ['light'] | ||
: // 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) => { | ||
return ( | ||
<img | ||
key={sourceName} | ||
src={sources[sourceName]} | ||
alt={alt} | ||
className={clsx( | ||
styles.themedImage, | ||
styles[`themedImage--${sourceName}`], | ||
className, | ||
)} | ||
{...propsRest} | ||
/> | ||
); | ||
})} | ||
</> | ||
); | ||
}; | ||
|
||
export default ThemedImage; |
11 changes: 11 additions & 0 deletions
11
packages/docusaurus-theme-classic/src/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,11 @@ | ||
.themedImage { | ||
display: none; | ||
} | ||
|
||
html[data-theme='light'] .themedImage--light { | ||
display: block; | ||
} | ||
|
||
html[data-theme='dark'] .themedImage--dark { | ||
display: block; | ||
} |
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