My approach of copy to clipboard works fine on pc but has not worked on android. I tried on chrome(latest version) on my Samsung galaxy s20fe android version 12
<button onClick={() => handleCopy()}>Copy</button>
const clipboardData = `Name: ${modalData.name}
Department: ${modalData.department}
Batch: ${modalData.batch}
BG: ${modalData.bg}
Phone: ${modalData.phone}`;
const handleCopy = () => {
navigator.clipboard
.writeText(clipboardData)
.then(() => {
alert('successfully copied');
})
.catch(() => {
alert('something went wrong');
});
alert('📋 Coppied to clipboard!');
};
Accroding to mdn documentation
The asynchronous clipboard API is a relatively recent addition, and the process of implementing it in browsers is not yet complete. Due to both potential security concerns and technical complexities, the process of integrating this API is happening gradually in most browsers.
So you can try this:
const handleCopy= () => {
const element = document.createElement("textarea");
element.value = clipboardData;
document.body.appendChild(element)
element.select();
document.execCommand("copy");
document.body.removeChild(element);
alert('📋 Coppied to clipboard!');
}