Source

atoms/story-elements/title/title.tsx

  1. import React from "react";
  2. import { StoryElementProps } from "../types";
  3. import styled from "styled-components";
  4. import { withStoryAndConfig } from "../../../context";
  5. import get from "lodash.get";
  6. export const StyledTitle = styled.h3`
  7. font-family: ${(props) => props.theme.font.family.primary};
  8. font-size: ${(props) => props.theme.font.size.l};
  9. font-weight: ${(props) => props.theme.font.weight.bold};
  10. line-height: ${(props) => props.theme.font.lineHeight.level6};
  11. margin: 0;
  12. text-transform: uppercase;
  13. `;
  14. export const TitleBase = ({ element, story, config }: StoryElementProps) => {
  15. const titleElementRender = get(config, ["opts", "render", "storyElementRender", "titleElementRender"], null);
  16. return titleElementRender ? (
  17. titleElementRender({ story, config, element })
  18. ) : (
  19. <StyledTitle>{element.text}</StyledTitle>
  20. );
  21. };
  22. /**
  23. * Summary is a story element.
  24. * The look of the Title can be changed using the render prop titleElementRender. In case titleElementRender is passed in the config, it is rendered. Otherwise a default Title is rendered.
  25. *
  26. * @param {Object} params object containing parameters passed to the render prop
  27. * @param {Object} params.story story object
  28. * @param {Object} params.config config object
  29. * @param {Object} params.element the story element. This is same as the story element found in the story API response
  30. *
  31. * ```javascript
  32. * ...
  33. * ampRoutes(app, {
  34. * render: {
  35. * storyElementRender: {
  36. * titleElementRender: ({ story, config, element }) => <MyCustomTitle story={story} config={config} storyElement={element} />
  37. * }
  38. * }
  39. * })
  40. * ...
  41. * ```
  42. *
  43. * @category StoryElements
  44. * @module Title
  45. * @component
  46. */
  47. export const Title = withStoryAndConfig(TitleBase);