-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add copy button * Use Docusaurus copy button and icons * Tweak styles to work in new context * Other places where inline copy would be helpful * Mess with CSS to make it look similar on different browsers
- Loading branch information
1 parent
70ecbe8
commit 9656a34
Showing
8 changed files
with
124 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,46 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {useCallback, useState, useRef, useEffect} from 'react'; | ||
import clsx from 'clsx'; | ||
import copy from 'copy-text-to-clipboard'; | ||
import IconCopy from '@theme/Icon/Copy'; | ||
import IconSuccess from '@theme/Icon/Success'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
export default function CopyButton({code, className}) { | ||
const [isCopied, setIsCopied] = useState(false); | ||
const copyTimeout = useRef<number | undefined>(undefined); | ||
const handleCopyCode = useCallback(() => { | ||
copy(code); | ||
setIsCopied(true); | ||
copyTimeout.current = window.setTimeout(() => { | ||
setIsCopied(false); | ||
}, 1000); | ||
}, [code]); | ||
|
||
useEffect(() => () => window.clearTimeout(copyTimeout.current), []); | ||
|
||
return ( | ||
<button | ||
type="button" | ||
aria-label="Copied" | ||
title="Copy" | ||
className={clsx( | ||
className, | ||
styles.copyButton, | ||
isCopied && styles.copyButtonCopied, | ||
)} | ||
onClick={handleCopyCode}> | ||
<span className={styles.copyButtonIcons} aria-hidden="true"> | ||
<IconCopy className={styles.copyButtonIcon} /> | ||
<IconSuccess className={styles.copyButtonSuccessIcon} /> | ||
</span> | ||
</button> | ||
); | ||
} |
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,46 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
:global(.theme-code-block:hover) .copyButtonCopied { | ||
opacity: 1 !important; | ||
} | ||
|
||
.copyButtonIcons { | ||
position: relative; | ||
display: block; | ||
width: 1.125rem; | ||
height: 1.125rem; | ||
} | ||
|
||
.copyButtonIcon, | ||
.copyButtonSuccessIcon { | ||
fill: currentColor; | ||
opacity: inherit; | ||
width: inherit; | ||
height: inherit; | ||
transition: all var(--ifm-transition-fast) ease; | ||
} | ||
|
||
.copyButtonSuccessIcon { | ||
top: -12px; | ||
left: 10px; | ||
transform: translate(-50%, -50%) scale(0.33); | ||
opacity: 0; | ||
color: #00d600; | ||
position: relative; | ||
} | ||
|
||
.copyButtonCopied .copyButtonIcon { | ||
transform: scale(0.33); | ||
opacity: 0; | ||
} | ||
|
||
.copyButtonCopied .copyButtonSuccessIcon { | ||
transform: translate(-50%, -50%) scale(1); | ||
opacity: 1; | ||
transition-delay: 0.075s; | ||
} |
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,15 @@ | ||
/* eslint-disable react/prop-types,import/no-unresolved */ | ||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
import styles from './styles.module.css'; | ||
|
||
import CopyButton from '@site/src/components/CopyButton' | ||
|
||
export default function InlineCopy({ code }) { | ||
return ( | ||
<div className={clsx(styles.container)}> | ||
<code>{code}</code> | ||
<CopyButton code={code} /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.container { | ||
display: flex; | ||
column-gap: 0.2rem; | ||
position: unset; | ||
background-color: var(--ifm-code-background); | ||
} |