Skip to content

Commit

Permalink
Merge pull request #453 from softeerbootcamp-2nd/hotfix/clipboard-error
Browse files Browse the repository at this point in the history
Hotfix/clipboard error
  • Loading branch information
jijiseong authored Aug 24, 2023
2 parents 38ca280 + 695b87b commit 551d1c7
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions frontend/src/components/modal/ShareModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HTMLAttributes, useContext, useState } from 'react';
import { HTMLAttributes, useContext, useRef, useState } from 'react';
import { DimmedBackground } from './DimmedBackground';
import WhiteModal from './WhiteModal';
import { styled } from 'styled-components';
Expand All @@ -18,10 +18,24 @@ export default function ShareModal({ ...props }: IShareModal) {
const { visible, setVisible } = useContext(ShareModalContext);
const { selectedItem } = useContext(ItemContext);
const [copyAlertVisible, setCopyAlertVisible] = useState(false);
const copyTarget = useRef<HTMLParagraphElement>(null);

function unsecuredCopyToClipboard(text: string) {
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
} catch (err) {
console.error('Unable to copy to clipboard', err);
}
document.body.removeChild(textArea);
}

const handleCopyClick = async () => {
try {
await navigator.clipboard.writeText(shareUrl);
unsecuredCopyToClipboard(shareUrl);
setCopyAlertVisible(true);
setTimeout(() => {
setCopyAlertVisible(false);
Expand Down Expand Up @@ -109,7 +123,7 @@ export default function ShareModal({ ...props }: IShareModal) {
<Desc>내 견적서 링크를 복사해 견적을 다시 확인해보세요.</Desc>
</div>
<LinkWrapper>
<UrlText>{shareUrl}</UrlText>
<UrlText ref={copyTarget}>{shareUrl}</UrlText>
<ButtonContainer>
<CopyButton onClick={handleCopyClick}>
<Alert $visible={copyAlertVisible}>copied!</Alert>
Expand Down

0 comments on commit 551d1c7

Please sign in to comment.