redbadger/website-honestly

View on GitHub
site/components/raw-html/index.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
/* eslint-disable react/no-danger */
// @no-flow
import React from 'react';
import classnames from 'classnames/bind';
import textStyles from '../component-renderer/styles.css';
import styles from './style.css';

const cx = classnames.bind(styles);

function htmlDecode(input) {
  if (typeof window !== 'undefined' && typeof DOMParser !== 'undefined') {
    const doc = new DOMParser().parseFromString(input, 'text/html');
    return doc.documentElement.textContent;
  }
}

const RawHtml = ({ children, escaped }) => {
  return (
    <div
      className={cx(textStyles.typography, styles.rawHtml)}
      dangerouslySetInnerHTML={{ __html: escaped ? htmlDecode(children) : children }}
    />
  );
};

export default RawHtml;