Copied.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use client';
  2. import './style.scss';
  3. import { useCallback } from 'react';
  4. import { Copy } from 'lucide-react';
  5. import { Button } from '@/components/ui/button';
  6. import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog';
  7. import { Input } from '@/components/ui/input';
  8. import { Label } from '@/components/ui/label';
  9. type Props = {
  10. isEnable: boolean;
  11. open: boolean;
  12. onChange: (open: boolean) => void;
  13. }
  14. export default function Copied({ isEnable, open, onChange }: Props)
  15. {
  16. const handleCopy = useCallback(async () => {
  17. try {
  18. await navigator.clipboard.writeText(window.location.href);
  19. alert("주소가 복사되었습니다.");
  20. onChange(false);
  21. } catch (err) {
  22. console.error("복사 실패:", err);
  23. }
  24. }, [onChange]);
  25. if (!isEnable) {
  26. return null;
  27. }
  28. return (
  29. <Dialog open={open} onOpenChange={onChange}>
  30. <DialogContent className='sm:max-w-md'>
  31. <DialogHeader>
  32. <DialogTitle>주소 복사</DialogTitle>
  33. <DialogDescription>
  34. 이 주소를 복사하여 다른 곳에 붙여넣기 하세요.
  35. </DialogDescription>
  36. </DialogHeader>
  37. <div className='flex items-center space-x-2'>
  38. <div className='grid flex-1 gap-2'>
  39. <Label htmlFor='link' className='sr-only'>
  40. Link
  41. </Label>
  42. <Input id='link' defaultValue={window.location.href} readOnly />
  43. </div>
  44. <Button type='submit' size='sm' className='px-3' onClick={handleCopy}>
  45. <span className='sr-only'>Copy</span>
  46. <Copy />
  47. </Button>
  48. </div>
  49. </DialogContent>
  50. </Dialog>
  51. );
  52. }