Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

ShareDialog.tsx

113 lines3.6 KB

1import { useCallback, useState } from "react"
2import { Dialog } from "@base-ui/react/dialog"
3import "./ShareDialog.css"
4​
5export interface ShareDialogProps {
6 open: boolean
7 onOpenChange: (open: boolean) => void
8}
9​
10/**
11 * Check if the Web Share API is available.
12 */
13function canUseNativeShare(): boolean {
14 return typeof navigator !== `undefined` && `share` in navigator
16​
17export function ShareDialog({ open, onOpenChange }: ShareDialogProps) {
18 const [copied, setCopied] = useState(false)
19​
20 const shareUrl = typeof window !== `undefined` ? window.location.href : ``
21 const supportsNativeShare = canUseNativeShare()
22​
23 const handleCopy = useCallback(async () => {
24 try {
25 await navigator.clipboard.writeText(shareUrl)
26 setCopied(true)
27 setTimeout(() => setCopied(false), 2000)
28 } catch {
29 // Fallback for older browsers
30 const input = document.querySelector<HTMLInputElement>(`.share-dialog-input`)
31 input?.select()
32 document.execCommand(`copy`)
33 setCopied(true)
34 setTimeout(() => setCopied(false), 2000)
35 }
36 }, [shareUrl])
37​
38 const handleNativeShare = useCallback(async () => {
39 try {
40 await navigator.share({
41 title: `1 Million Boxes`,
42 text: `Join me in this massive multiplayer Dots & Boxes game!`,
43 url: shareUrl,
44 })
45 // Close dialog after successful share
46 onOpenChange(false)
47 } catch (err) {
48 // User cancelled or share failed - ignore
49 if (err instanceof Error && err.name !== `AbortError`) {
50 console.error(`Share failed:`, err)
51 }
52 }
53 }, [shareUrl, onOpenChange])
54​
55 return (
56 <Dialog.Root open={open} onOpenChange={onOpenChange}>
57 <Dialog.Portal>
58 <Dialog.Backdrop className="share-dialog-backdrop" />
59 <Dialog.Popup className="share-dialog-popup">
60 <Dialog.Title className="share-dialog-title">Share this game</Dialog.Title>
61 <Dialog.Description className="share-dialog-description">
62 Invite friends to join the game
63 </Dialog.Description>
64​
65 {supportsNativeShare && (
66 <button className="share-dialog-native-button" onClick={handleNativeShare}>
67 <svg
68 width="20"
69 height="20"
70 viewBox="0 0 24 24"
71 fill="none"
72 stroke="currentColor"
73 strokeWidth="2"
74 strokeLinecap="round"
75 strokeLinejoin="round"
76 >
77 <circle cx="18" cy="5" r="3" />
78 <circle cx="6" cy="12" r="3" />
79 <circle cx="18" cy="19" r="3" />
80 <line x1="8.59" y1="13.51" x2="15.42" y2="17.49" />
81 <line x1="15.41" y1="6.51" x2="8.59" y2="10.49" />
82 </svg>
83 Share via...
84 </button>
85 )}
86​
87 <div className="share-dialog-divider">
88 <span>or copy link</span>
89 </div>
90​
91 <div className="share-dialog-content">
92 <input
93 type="text"
94 className="share-dialog-input"
95 aria-label="Game link"
96 value={shareUrl}
97 readOnly
98 onClick={(e) => (e.target as HTMLInputElement).select()}
99 />
100 <button
101 className={`share-dialog-copy-button ${copied ? `copied` : ``}`}
102 onClick={handleCopy}
103 >
104 {copied ? `Copied!` : `Copy`}
105 </button>
106 </div>
107​
108 <Dialog.Close className="share-dialog-close-button">Close</Dialog.Close>
109 </Dialog.Popup>
110 </Dialog.Portal>
111 </Dialog.Root>
112 )