-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from neilime/feat/fix-doc-usage-frame
docs: fix usage frame auto resize
- Loading branch information
Showing
4 changed files
with
1,017 additions
and
974 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { useRef, useState } from "react"; | ||
import { FunctionComponent } from "react"; | ||
|
||
const AutoResizeFrame: FunctionComponent<{ srcDoc: string }> = ({ srcDoc }) => { | ||
const ref = useRef<HTMLIFrameElement>(); | ||
const [height, setHeight] = useState("100px"); | ||
const onLoad = () => { | ||
setHeight(ref?.current?.contentWindow.document.body.scrollHeight + "px"); | ||
}; | ||
|
||
const id = "iframe_" + Math.random().toString(36).slice(2, 9); | ||
|
||
return ( | ||
<iframe | ||
ref={ref} | ||
onLoad={onLoad} | ||
id={id} | ||
srcDoc={srcDoc} | ||
width="100%" | ||
height={height} | ||
scrolling="no" | ||
frameBorder="0" | ||
style={{ | ||
maxWidth: "100%", | ||
width: "100%", | ||
overflow: "auto", | ||
}} | ||
></iframe> | ||
); | ||
}; | ||
|
||
export default AutoResizeFrame; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,17 @@ | ||
import React, { Children, FunctionComponent, ReactNode } from "react"; | ||
import { createRef, RefObject, useEffect, useState } from "react"; | ||
import Frame from "react-frame-component"; | ||
import React, { FunctionComponent, ReactNode } from "react"; | ||
import { renderToStaticMarkup } from "react-dom/server"; | ||
import AutoResizeFrame from "./AutoResizeFrame"; | ||
|
||
interface IProps { | ||
children: ReactNode; | ||
bootstrapVersion: string; | ||
} | ||
|
||
interface IFrame { | ||
node: HTMLIFrameElement; | ||
} | ||
|
||
const HtmlCode: FunctionComponent<IProps> = ({ | ||
children, | ||
bootstrapVersion, | ||
}) => { | ||
const [height, setHeight] = useState(100); | ||
const iframeRef: RefObject<IFrame> = createRef(); | ||
const handleResize = (iframe: RefObject<IFrame>) => { | ||
const doc = iframe?.current?.node?.contentDocument; | ||
const body_ = doc?.body; | ||
const html_ = doc?.documentElement; | ||
|
||
const height = Math.max( | ||
body_?.scrollHeight, | ||
body_?.offsetHeight, | ||
html_?.clientHeight, | ||
html_?.scrollHeight, | ||
html_?.offsetHeight | ||
); | ||
|
||
if (height !== 0) { | ||
setHeight(height); | ||
} | ||
}; | ||
useEffect(() => handleResize(iframeRef), [children]); | ||
|
||
const htmlString = renderToStaticMarkup(children); | ||
const initialContent = `<!DOCTYPE html> | ||
<html> | ||
<head> | ||
|
@@ -45,9 +21,14 @@ const HtmlCode: FunctionComponent<IProps> = ({ | |
crossorigin="anonymous" | ||
/> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"> | ||
<link | ||
href="https://getbootstrap.com/docs/${bootstrapVersion}/assets/css/docs.min.css" | ||
rel="stylesheet" | ||
crossorigin="anonymous" | ||
/> | ||
</head> | ||
<body> | ||
<div></div> | ||
<div class="bd-example">${htmlString}</div> | ||
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" crossorigin="anonymous"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@${bootstrapVersion}/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script> | ||
<script type="text/javascript"> | ||
|
@@ -58,19 +39,6 @@ const HtmlCode: FunctionComponent<IProps> = ({ | |
</body> | ||
</html>`; | ||
|
||
return ( | ||
<Frame | ||
style={{ | ||
height, | ||
width: "100%", | ||
}} | ||
ref={iframeRef} | ||
onLoad={() => handleResize(iframeRef)} | ||
initialContent={initialContent} | ||
> | ||
{children} | ||
</Frame> | ||
); | ||
return <AutoResizeFrame srcDoc={initialContent} />; | ||
}; | ||
|
||
export default HtmlCode; |
Oops, something went wrong.