|
| 1 | +import React from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import Link from '@docusaurus/Link'; |
| 4 | +import AuthorSocials from '@theme/Blog/Components/Author/Socials'; |
| 5 | +import Heading from '@theme/Heading'; |
| 6 | +import styles from './styles.module.css'; |
| 7 | + |
| 8 | +function MaybeLink(props) { |
| 9 | + if (props.href) { |
| 10 | + return <Link {...props} />; |
| 11 | + } |
| 12 | + return <>{props.children}</>; |
| 13 | +} |
| 14 | +function AuthorTitle({title}) { |
| 15 | + return ( |
| 16 | + <small className={styles.authorTitle} title={title}> |
| 17 | + {title} |
| 18 | + </small> |
| 19 | + ); |
| 20 | +} |
| 21 | +function AuthorName({name, as}) { |
| 22 | + if (!as) { |
| 23 | + return <span className={styles.authorName}>{name}</span>; |
| 24 | + } else { |
| 25 | + return ( |
| 26 | + <Heading as={as} className={styles.authorName}> |
| 27 | + {name} |
| 28 | + </Heading> |
| 29 | + ); |
| 30 | + } |
| 31 | +} |
| 32 | +function AuthorBlogPostCount({count}) { |
| 33 | + return <span className={clsx(styles.authorBlogPostCount)}>{count}</span>; |
| 34 | +} |
| 35 | +// Note: in the future we might want to have multiple "BlogAuthor" components |
| 36 | +// Creating different display modes with the "as" prop may not be the best idea |
| 37 | +// Explainer: https://kyleshevlin.com/prefer-multiple-compositions/ |
| 38 | +// For now, we almost use the same design for all cases, so it's good enough |
| 39 | +export default function BlogAuthor({as, author, className, count}) { |
| 40 | + const {name, title, url, imageURL, email, page} = author; |
| 41 | + const link = |
| 42 | + page?.permalink || url || (email && `mailto:${email}`) || undefined; |
| 43 | + return ( |
| 44 | + <div |
| 45 | + className={clsx( |
| 46 | + 'avatar margin-bottom--md', |
| 47 | + className, |
| 48 | + styles[`author-as-${as}`] |
| 49 | + )}> |
| 50 | + {imageURL && ( |
| 51 | + <MaybeLink href={link} className="avatar__photo-link"> |
| 52 | + <img |
| 53 | + className={clsx('avatar__photo', styles.authorImage)} |
| 54 | + src={imageURL} |
| 55 | + alt={name} |
| 56 | + /> |
| 57 | + </MaybeLink> |
| 58 | + )} |
| 59 | + |
| 60 | + {(name || title) && ( |
| 61 | + <div className={clsx('avatar__intro', styles.authorDetails)}> |
| 62 | + <div className="avatar__name"> |
| 63 | + {name && ( |
| 64 | + <MaybeLink href={link}> |
| 65 | + <AuthorName name={name} as={as} /> |
| 66 | + </MaybeLink> |
| 67 | + )} |
| 68 | + {count && <AuthorBlogPostCount count={count} />} |
| 69 | + </div> |
| 70 | + {!!title && <AuthorTitle title={title} />} |
| 71 | + |
| 72 | + {/* |
| 73 | + We always render AuthorSocials even if there's none |
| 74 | + This keeps other things aligned with flexbox layout |
| 75 | + */} |
| 76 | + <AuthorSocials author={author} /> |
| 77 | + </div> |
| 78 | + )} |
| 79 | + </div> |
| 80 | + ); |
| 81 | +} |
0 commit comments