Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a copy button in the playground jsx output header #698

Merged
merged 1 commit into from
Mar 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions website/src/components/playground/Playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,30 @@ const FloatingAd = styled.div`
width: 400;
`

const EditorTitle = styled.h3`
const EditorTitleContainer = styled.div`
padding: 0 2;
font-size: 12;
height: 28;
display: flex;
align-items: center;
justify-content: space-between;
text-transform: uppercase;
font-weight: bold;
border-bottom: 1;
border-color: layout-border;
color: on-background-light;
`

const EditorTitle = styled.h3`
display: flex;
align-items: center;
font-weight: bold;
`

const EditorTitleButton = styled.button`
border-radius: 4;
height: 20;
`

const InnerDialog = styled.div`
background-color: background-light;
color: on-background;
Expand Down Expand Up @@ -236,6 +247,9 @@ const ClientOnly = ({ children }) => {
return visible ? children : null
}

const Copy = 'Copy'
const Copied = 'Copied!'

export function Playground() {
const [input, setInput] = useState(defaultSvg)
const [output, setOutput] = useState('')
Expand Down Expand Up @@ -279,6 +293,20 @@ export function Playground() {
transform()
}, [input, JSON.stringify(state)])

const handleButtonClick = (event) => {
navigator.clipboard.writeText(output)
!dialogDisplayedRef.current &&
setTimeout(() => {
dialog.show()
dialogDisplayedRef.current = true
}, 50)
const button = event.target
button.innerText = Copied
setTimeout(() => {
button.innerText = Copy
}, 2000)
}

return (
<>
<GlobalStyle />
Expand All @@ -292,7 +320,9 @@ export function Playground() {
<Suspense fallback={<Loading />}>
<Editors>
<EditorContainer>
<EditorTitle>SVG input</EditorTitle>
<EditorTitleContainer>
<EditorTitle>SVG input</EditorTitle>
</EditorTitleContainer>
<DropArea onChange={setInput}>
<Editor
name="input"
Expand All @@ -315,7 +345,12 @@ export function Playground() {
}
}}
>
<EditorTitle>JSX output</EditorTitle>
<EditorTitleContainer>
<EditorTitle>JSX output</EditorTitle>
<EditorTitleButton onClick={handleButtonClick}>
{Copy}
</EditorTitleButton>
</EditorTitleContainer>
<Editor name="output" mode="jsx" readOnly value={output} />
</EditorContainer>
</Editors>
Expand Down