|
| 1 | +import {ImageSizes} from './concerns/constants'; |
| 2 | +import imageService from './concerns/image-service'; |
| 3 | +import {Byline, Headshot} from './props'; |
| 4 | +import Link from './link'; |
| 5 | + |
| 6 | +interface BylineProps extends Byline, Headshot { |
| 7 | + showBylineHeadshot?: boolean; |
| 8 | +} |
| 9 | + |
| 10 | +const BylineLink = ({text, url, headshot, showBylineHeadshot}: {text: string, url: string, headshot?: string, showBylineHeadshot?: boolean}) => ( |
| 11 | + <Link |
| 12 | + url={url} |
| 13 | + attrs={{ |
| 14 | + 'data-trackable': 'byline-link', |
| 15 | + className: "o-teaser__byline-link" |
| 16 | + }} |
| 17 | + > |
| 18 | + <> |
| 19 | + {showBylineHeadshot && headshot ? |
| 20 | + <img |
| 21 | + className="o-teaser__byline-headshot" |
| 22 | + width={ImageSizes.BylineHeadshot} |
| 23 | + height={ImageSizes.BylineHeadshot} |
| 24 | + alt="" |
| 25 | + aria-hidden="true" |
| 26 | + src={imageService(headshot, ImageSizes.BylineHeadshot, {})} |
| 27 | + srcSet={ |
| 28 | + `${imageService(headshot, ImageSizes.BylineHeadshot*2, {})} 2x` |
| 29 | + } |
| 30 | + /> |
| 31 | + : null} |
| 32 | + <span className="o-teaser__byline-link-label">{text}</span> |
| 33 | + </> |
| 34 | + </Link> |
| 35 | +); |
| 36 | + |
| 37 | +const BylineText = ({text}: {text: string}) => ( |
| 38 | + text ? <span className="o-teaser__byline-text">{text}</span> : null |
| 39 | +); |
| 40 | + |
| 41 | +export default ({byline, showBylineHeadshot}: BylineProps) => ( |
| 42 | + Array.isArray(byline) && byline.length > 0 ? ( |
| 43 | + <div className="o-teaser__byline"> |
| 44 | + {byline.map(([text, url, headshot], index) => ( |
| 45 | + text && url ? |
| 46 | + <BylineLink |
| 47 | + key={`byline-link-${index}-${text}`} |
| 48 | + text={text} |
| 49 | + url={url} |
| 50 | + headshot={headshot} |
| 51 | + showBylineHeadshot={showBylineHeadshot} |
| 52 | + /> |
| 53 | + : <BylineText key={`byline-text-${index}-${text}`} text={text} /> |
| 54 | + ))} |
| 55 | + </div> |
| 56 | + ) : null |
| 57 | +); |
0 commit comments