contextLine.tsx 882 B

1234567891011121314151617181920212223242526272829303132333435
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import classNames from 'classnames';
  4. const Context = styled('div')`
  5. display: inline;
  6. `;
  7. interface Props {
  8. isActive: boolean;
  9. line: [number, string];
  10. children?: React.ReactNode;
  11. className?: string;
  12. }
  13. const ContextLine = function (props: Props) {
  14. const {line, isActive, className} = props;
  15. let lineWs = '';
  16. let lineCode = '';
  17. if (typeof line[1] === 'string') {
  18. [, lineWs, lineCode] = line[1].match(/^(\s*)(.*?)$/m)!;
  19. }
  20. const Component = !props.children ? Fragment : Context;
  21. return (
  22. <li className={classNames(className, 'expandable', {active: isActive})} key={line[0]}>
  23. <Component>
  24. <span className="ws">{lineWs}</span>
  25. <span className="contextline">{lineCode}</span>
  26. </Component>
  27. {props.children}
  28. </li>
  29. );
  30. };
  31. export default ContextLine;