mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-31 01:47:17 +02:00
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 <lorber.sebastien@gmail.com> Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
This commit is contained in:
parent
73f151d04c
commit
9d90e896f0
9 changed files with 229 additions and 4 deletions
|
@ -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;
|
Loading…
Add table
Add a link
Reference in a new issue