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

Leverage CopyToClipboard module #942

Open
llxia opened this issue Nov 25, 2024 · 0 comments
Open

Leverage CopyToClipboard module #942

llxia opened this issue Nov 25, 2024 · 0 comments

Comments

@llxia
Copy link
Contributor

llxia commented Nov 25, 2024

CopyToClipboard should be used instead of using document.getElementById

const copyCodeToClipboard = () => {
const markdownText = document.getElementById('markdown-text');
let range, selection;
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(markdownText);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(markdownText);
selection.removeAllRanges();
selection.addRange(range);
}
let alert;
if (document.execCommand('Copy')) {
alert = (
<Alert
message="Successfully copied to clipboard"
type="success"
showIcon
/>
);
} else {
alert = (
<Alert
message="Failed to copy to clipboard"
type="error"
showIcon
/>
);
}
ReactDOM.render(alert, document.getElementById('copy-status'));
};

Example of using CopyToClipboard module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant