-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Neo Nie
committed
Mar 22, 2022
1 parent
50d1578
commit 1f92b1f
Showing
52 changed files
with
372 additions
and
86 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
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,175 @@ | ||
import * as React from "react" | ||
import * as HookForm from "react-hook-form-v7" | ||
import { useRunner } from "react-runner" | ||
import Editor from "react-simple-code-editor" | ||
import Prism from "prismjs" | ||
|
||
import { ShadowRoot } from "./ShadowRoot" | ||
import * as styles from "./CodeArea.module.css" | ||
|
||
const scope = { | ||
import: { | ||
react: React, | ||
"react-hook-form": HookForm, | ||
}, | ||
} | ||
|
||
const style = ` | ||
body, :host { | ||
background: #0e101c; | ||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", | ||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", | ||
sans-serif; | ||
padding: 14px; | ||
} | ||
form { | ||
max-width: 500px; | ||
margin: 0 auto; | ||
} | ||
h1 { | ||
font-weight: 100; | ||
color: white; | ||
text-align: center; | ||
padding-bottom: 10px; | ||
border-bottom: 1px solid rgb(79, 98, 148); | ||
} | ||
.form { | ||
background: #0e101c; | ||
max-width: 400px; | ||
margin: 0 auto; | ||
} | ||
p { | ||
color: #bf1650; | ||
} | ||
p::before { | ||
display: inline; | ||
content: "⚠ "; | ||
} | ||
input { | ||
display: block; | ||
box-sizing: border-box; | ||
width: 100%; | ||
border-radius: 4px; | ||
border: 1px solid white; | ||
padding: 10px 15px; | ||
margin-bottom: 10px; | ||
font-size: 14px; | ||
} | ||
label { | ||
line-height: 2; | ||
text-align: left; | ||
display: block; | ||
margin-bottom: 13px; | ||
margin-top: 20px; | ||
color: white; | ||
font-size: 14px; | ||
font-weight: 200; | ||
} | ||
button[type="submit"], | ||
input[type="submit"] { | ||
background: #ec5990; | ||
color: white; | ||
text-transform: uppercase; | ||
border: none; | ||
margin-top: 40px; | ||
padding: 20px; | ||
font-size: 16px; | ||
font-weight: 100; | ||
letter-spacing: 10px; | ||
} | ||
button[type="submit"]:hover, | ||
input[type="submit"]:hover { | ||
background: #bf1650; | ||
} | ||
button[type="submit"]:active, | ||
input[type="button"]:active, | ||
input[type="submit"]:active { | ||
transition: 0.3s all; | ||
transform: translateY(3px); | ||
border: 1px solid transparent; | ||
opacity: 0.8; | ||
} | ||
input:disabled { | ||
opacity: 0.4; | ||
} | ||
input[type="button"]:hover { | ||
transition: 0.3s all; | ||
} | ||
button[type="submit"], | ||
input[type="button"], | ||
input[type="submit"] { | ||
-webkit-appearance: none; | ||
} | ||
.App { | ||
max-width: 600px; | ||
margin: 0 auto; | ||
} | ||
button[type="button"] { | ||
display: block; | ||
appearance: none; | ||
background: #333; | ||
color: white; | ||
border: none; | ||
text-transform: uppercase; | ||
padding: 10px 20px; | ||
border-radius: 4px; | ||
} | ||
hr { | ||
margin-top: 30px; | ||
} | ||
button { | ||
display: block; | ||
appearance: none; | ||
margin-top: 40px; | ||
border: 1px solid #333; | ||
margin-bottom: 20px; | ||
text-transform: uppercase; | ||
padding: 10px 20px; | ||
border-radius: 4px; | ||
}` | ||
|
||
export const LiveRunner = ({ initialCode }: { initialCode: string }) => { | ||
const [code, setCode] = React.useState(initialCode) | ||
const { element, error } = useRunner({ code, scope }) | ||
React.useEffect(() => { | ||
setCode(initialCode) | ||
}, [initialCode]) | ||
|
||
return ( | ||
<> | ||
<div className={styles.liveRunnerWrapper}> | ||
<Editor | ||
value={code} | ||
onValueChange={setCode} | ||
padding={15} | ||
className={styles.liveRunner} | ||
highlight={(code) => | ||
Prism.highlight(code, Prism.languages.javascript) | ||
} | ||
/> | ||
</div> | ||
<ShadowRoot> | ||
{element} | ||
{error && <div>{error}</div>} | ||
<style>{style}</style> | ||
</ShadowRoot> | ||
</> | ||
) | ||
} |
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,20 @@ | ||
import * as React from "react" | ||
import { createPortal } from "react-dom" | ||
|
||
export const ShadowRoot: React.FC<JSX.IntrinsicElements["div"]> = ({ | ||
children, | ||
...rest | ||
}) => { | ||
const ref = React.useRef<HTMLDivElement>(null) | ||
const [shadowRoot, setShadowRoot] = React.useState<ShadowRoot>() | ||
|
||
React.useEffect(() => { | ||
setShadowRoot(ref.current?.attachShadow({ mode: "open" })) | ||
}, []) | ||
|
||
return ( | ||
<div ref={ref} {...rest}> | ||
{shadowRoot && createPortal(children, shadowRoot as any)} | ||
</div> | ||
) | ||
} |
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
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
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
Oops, something went wrong.