Source

molecules/hero-image/hero-image.tsx

  1. import React from "react";
  2. import { HeroImageBaseTypes } from "./types";
  3. import { HeroImageMetadata } from "../../types/story";
  4. import styled from "styled-components";
  5. import { withStoryAndConfig } from "../../context";
  6. import { Image } from "../../atoms";
  7. import get from "lodash.get";
  8. const StyledCaption = styled.span`
  9. margin-right: 8px;
  10. color: ${(props) => props.theme.color.mono6};
  11. line-height: ${(props) => props.theme.font.lineHeight.level3};
  12. font-size: ${(props) => props.theme.font.size.xxs};
  13. word-wrap: break-word;
  14. `;
  15. const StyledAttribution = styled.span`
  16. color: ${(props) => props.theme.color.mono4};
  17. font-size: ${(props) => props.theme.font.size.xxs};
  18. line-height: ${(props) => props.theme.font.lineHeight.level3};
  19. word-wrap: break-word;
  20. `;
  21. const StyledDiv = styled.div`
  22. background-color: ${(props) => props.theme.color.mono2};
  23. `;
  24. const StyledFigcaption = styled.div`
  25. text-align: left;
  26. padding-top: 8px;
  27. `;
  28. export const HeroImageBase = ({ story, config }: HeroImageBaseTypes) => {
  29. const metadata: HeroImageMetadata = get(story, "hero-image-metadata", null);
  30. const slug: string | null = get(story, "hero-image-s3-key", null);
  31. if (!slug || !metadata) return null;
  32. const attribution: string | null = get(story, "hero-image-attribution", null);
  33. const caption: string | null = get(story, "hero-image-caption", null);
  34. const altText: string | null = get(story, "hero-image-alt-text", null);
  35. const disableImgPreload: boolean = get(config, ["opts", "optimizeAmpHtml"], true);
  36. return (
  37. <>
  38. <StyledDiv>
  39. <Image
  40. data-hero={"true"}
  41. metadata={metadata}
  42. slug={slug}
  43. alt={altText || caption || attribution || ""}
  44. disableImgPreload={disableImgPreload}></Image>
  45. </StyledDiv>
  46. <StyledFigcaption>
  47. {caption && <StyledCaption dangerouslySetInnerHTML={{ __html: `${caption}` + "&nbsp;" }} />}
  48. {attribution && <StyledAttribution dangerouslySetInnerHTML={{ __html: `${attribution}` }} />}
  49. </StyledFigcaption>
  50. </>
  51. );
  52. };
  53. /**
  54. * HeroImage Component is a wrapper around the <amp-img> amp component. It even serves figure caption text which picks up "hero-image-attribution" and/or "hero-image-caption" depending which one is available.
  55. *
  56. * @category Molecules
  57. * @component
  58. */
  59. export const HeroImage = withStoryAndConfig(HeroImageBase);
  60. export function getFigcaptionText(caption, attribution) {
  61. if (caption && attribution) return `${caption} | ${attribution}`;
  62. else if (caption || attribution) return `${caption || attribution}`;
  63. else return false;
  64. }