ExampleIframe.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use client';
  2. import React, { useState } from 'react';
  3. import clsx from 'clsx';
  4. import { uiPackageName, uiVersion } from '@/config/site';
  5. import { generateIframeContent } from '@/lib/components';
  6. export default function ExampleIframe({
  7. html,
  8. height = 15,
  9. wrapper,
  10. backgroundColor = '',
  11. plugins,
  12. resizabe = false,
  13. className
  14. }) {
  15. const iframeRef = React.useRef<HTMLIFrameElement>(null);
  16. const [contentHeight, setContentHeight] = useState(0);
  17. const iframeLoaded = e => {
  18. setContentHeight(e.target.contentWindow.document.body.scrollHeight);
  19. };
  20. function resizeIframe(obj) {
  21. obj.style.height = obj.contentWindow.document.documentElement.scrollHeight + 'px';
  22. }
  23. const srcDoc = generateIframeContent({
  24. html,
  25. wrapper,
  26. plugins,
  27. backgroundColor
  28. });
  29. // React.useEffect(() => {
  30. // const iframe = iframeRef.current
  31. // // const listener = e => console.log(e)
  32. // // iframe && iframe.addEventListener("load", listener)
  33. // // return () => {
  34. // // iframe && iframe.removeEventListener("load", listener)
  35. // // }
  36. // })
  37. return (
  38. <iframe
  39. ref={iframeRef}
  40. className={clsx('example-frame', resizabe && 'example-frame-resizable', className)}
  41. srcDoc={srcDoc}
  42. style={{ minHeight: `${height}rem`, height: `${contentHeight}px` }}
  43. onLoad={iframeLoaded}
  44. />
  45. );
  46. }