Add share button

This commit is contained in:
Sebastien Castiel
2023-12-13 12:55:31 -05:00
parent 6bd99d9a34
commit 9e2834abc3
3 changed files with 54 additions and 4 deletions

View File

@@ -0,0 +1,44 @@
'use client'
import { Button } from '@/components/ui/button'
import { Share } from 'lucide-react'
import { useEffect, useState } from 'react'
interface Props {
text: string
url: string
}
export function ShareUrlButton({ url, text }: Props) {
const canShare = useCanShare(url, text)
if (!canShare) return null
return (
<Button
size="icon"
variant="secondary"
type="button"
onClick={() => {
if (navigator.share) {
navigator.share({ text, url })
} else {
console.log('Sharing is not available', { text, url })
}
}}
>
<Share className="w-4 h-4" />
</Button>
)
}
function useCanShare(url: string, text: string) {
const [canShare, setCanShare] = useState<boolean | null>(null)
useEffect(() => {
setCanShare(
navigator.share !== undefined && navigator.canShare({ url, text }),
)
}, [text, url])
return canShare
}