/
PreRenderer.tsx
56 lines (49 loc) · 1.53 KB
/
PreRenderer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React from 'react';
import PropTypes from 'prop-types';
import cx from 'clsx';
import Styled, { JssInjectedProps } from 'rsg-components/Styled';
import prismTheme from '../../../styles/prismTheme';
import * as Rsg from '../../../../typings';
const styles = ({ space, color, fontSize, fontFamily, borderRadius }: Rsg.Theme) => ({
pre: {
fontFamily: fontFamily.monospace,
fontSize: fontSize.small,
lineHeight: 1.5,
color: color.base,
whiteSpace: 'pre-wrap',
wordWrap: 'normal',
tabSize: 2,
hyphens: 'none',
backgroundColor: color.codeBackground,
padding: [[space[1], space[2]]],
border: [[1, color.codeBackground, 'solid']],
borderRadius,
marginTop: 0,
marginBottom: space[2],
overflow: 'auto',
...prismTheme({ color }),
},
});
export interface PreProps {
className?: string;
children: React.ReactNode;
}
type PrePropsWithClasses = JssInjectedProps & PreProps;
export const PreRenderer: React.FunctionComponent<PrePropsWithClasses> = ({
classes,
className,
children,
}) => {
const classNames = cx(className, classes.pre);
const isHighlighted = className && className.indexOf('lang-') !== -1;
if (isHighlighted && children) {
return <pre className={classNames} dangerouslySetInnerHTML={{ __html: children.toString() }} />;
}
return <pre className={classNames}>{children}</pre>;
};
PreRenderer.propTypes = {
classes: PropTypes.objectOf(PropTypes.string.isRequired).isRequired,
className: PropTypes.string,
children: PropTypes.any.isRequired,
};
export default Styled<PrePropsWithClasses>(styles)(PreRenderer);