Skip to content

Commit

Permalink
refact: removed unnecessary logic from code component
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Bolotsky authored and Pepe Cano committed Oct 21, 2020
1 parent 791cfee commit ebeaf11
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/components/shared/code/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ import classNames from 'classnames';
import { WithCopyButton } from 'components/shared/with-copy-button';
import Highlight, { defaultProps } from 'prism-react-renderer';
import PropTypes from 'prop-types';
import React, { useLayoutEffect, useRef, useState } from 'react';
import React from 'react';

import styles from './code.module.scss';

const Code = ({ children, showLineNumbers }) => {
if (!children) return null;
const codeContainerRef = useRef(null);
const [codeToCopy, setCodeToCopy] = useState('');

useLayoutEffect(() => {
if (codeContainerRef.current) {
setCodeToCopy(codeContainerRef.current.textContent);
}
}, [codeContainerRef]);

return codeToCopy ? (
<WithCopyButton dataToCopy={codeToCopy}>
return (
<WithCopyButton dataToCopy={children.props.children}>
<Highlight
{...defaultProps}
code={children.props.children}
Expand Down Expand Up @@ -52,13 +44,6 @@ const Code = ({ children, showLineNumbers }) => {
)}
</Highlight>
</WithCopyButton>
) : (
<div
style={{ position: 'absolute', visibility: 'hidden' }}
ref={codeContainerRef}
>
{children.props.children}
</div>
);
};

Expand Down

0 comments on commit ebeaf11

Please sign in to comment.