-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ArticleDetail): refactor CopyToClipboard Component
- Loading branch information
Showing
19 changed files
with
226 additions
and
246 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
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
30 changes: 10 additions & 20 deletions
30
src/components/CommentBeta/DropdownActions/CopyCommentButton.tsx
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 |
---|---|---|
@@ -1,41 +1,43 @@ | ||
import C2C from 'react-copy-to-clipboard' | ||
import * as clipboard from 'clipboard-polyfill' | ||
import { FormattedMessage } from 'react-intl' | ||
|
||
import { toast } from '~/components' | ||
|
||
interface CopyToClipboardProps { | ||
text: string | ||
successMessage?: React.ReactNode | ||
type?: 'plain' | 'html' | ||
children: ({ | ||
copyToClipboard, | ||
}: { | ||
copyToClipboard: () => void | ||
}) => React.ReactNode | ||
} | ||
|
||
export const CopyToClipboard: React.FC< | ||
React.PropsWithChildren<CopyToClipboardProps> | ||
> = ({ text, successMessage, children }) => { | ||
return ( | ||
<C2C | ||
text={text} | ||
onCopy={(_, copied) => { | ||
if (!copied) { | ||
toast.error({ | ||
message: ( | ||
<FormattedMessage | ||
defaultMessage="Failed to copy, please try again." | ||
id="JRkgKV" | ||
/> | ||
), | ||
}) | ||
export const CopyToClipboard: React.FC<CopyToClipboardProps> = ({ | ||
text, | ||
successMessage, | ||
type = 'plain', | ||
children, | ||
}) => { | ||
const copyToClipboard = async function () { | ||
let item = new clipboard.ClipboardItem({ | ||
'text/plain': new Blob([text], { type: 'text/plain' }), | ||
}) | ||
if (type === 'html') { | ||
item = new clipboard.ClipboardItem({ | ||
'text/html': new Blob([text], { type: 'text/html' }), | ||
}) | ||
} | ||
|
||
return | ||
} | ||
await clipboard.write([item]) | ||
|
||
toast.success({ | ||
message: successMessage || ( | ||
<FormattedMessage defaultMessage="Copied successful" id="SYyBFF" /> | ||
), | ||
}) | ||
}} | ||
> | ||
{children} | ||
</C2C> | ||
) | ||
toast.success({ | ||
message: successMessage || ( | ||
<FormattedMessage defaultMessage="Copied successful" id="SYyBFF" /> | ||
), | ||
}) | ||
} | ||
|
||
return <>{children({ copyToClipboard })}</> | ||
} |
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.