code.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import styled from '@emotion/styled';
  2. import {CodeSnippet} from 'sentry/components/codeSnippet';
  3. import space from 'sentry/styles/space';
  4. type Props = {
  5. /**
  6. * Main code content gets passed as the children prop
  7. */
  8. children: string;
  9. /**
  10. * Auto-generated class name for <pre> and <code> element,
  11. * with a 'language-' prefix, e.g. language-css
  12. */
  13. className?: string;
  14. /**
  15. * Meta props from the markdown syntax,
  16. * for example, in
  17. *
  18. * ```jsx label=hello
  19. * [some code]
  20. * ```
  21. *
  22. * the label prop is set to 'hello'
  23. */
  24. label?: string;
  25. };
  26. const Code = ({children, className, label}: Props) => {
  27. return (
  28. <CodeWrap className={className}>
  29. <LabelWrap>{label && <Label>{label.replaceAll('_', ' ')}</Label>}</LabelWrap>
  30. <CodeSnippet language={className?.split('language-')[1] ?? ''}>
  31. {children}
  32. </CodeSnippet>
  33. </CodeWrap>
  34. );
  35. };
  36. export default Code;
  37. const CodeWrap = styled('div')`
  38. position: relative;
  39. padding: ${space(2)} 0;
  40. pre {
  41. font-size: ${p => p.theme.fontSizeMedium};
  42. border: solid 1px ${p => p.theme.border};
  43. padding-top: ${space(2)};
  44. }
  45. `;
  46. const LabelWrap = styled('div')`
  47. display: flex;
  48. align-items: center;
  49. position: absolute;
  50. top: ${space(2)};
  51. left: calc(${space(2)} - ${space(1)});
  52. transform: translateY(-50%);
  53. z-index: 1;
  54. padding: 0 ${space(0.75)};
  55. background: ${p => p.theme.docsBackground};
  56. border: solid 1px ${p => p.theme.innerBorder};
  57. border-radius: ${p => p.theme.borderRadius};
  58. `;
  59. const Label = styled('p')`
  60. color: ${p => p.theme.subText};
  61. font-size: ${p => p.theme.fontSizeMedium};
  62. font-weight: 600;
  63. text-transform: uppercase;
  64. margin-bottom: 0;
  65. `;