|
| 1 | +import React, { useContext, useEffect, useState } from 'react'; |
| 2 | +import { toClipboard } from 'copee'; |
| 3 | + |
| 4 | +import { ShareContext } from '../../services/share'; |
| 5 | +import { WhatsappIcon, FacebookIcon, MessangerIcon, TwitterIcon, CopyIcon } from '../Icons/Share'; |
| 6 | + |
| 7 | +export function FallbackShare() { |
| 8 | + const { closeShareDialog, isOpen } = useContext(ShareContext); |
| 9 | + const [isMounted, setIsMounted] = useState(false); |
| 10 | + useEffect(() => { |
| 11 | + setIsMounted(true); |
| 12 | + }, []); |
| 13 | + |
| 14 | + if (!isMounted) { |
| 15 | + return null; |
| 16 | + } |
| 17 | + |
| 18 | + const title = document.title; |
| 19 | + const url = window.location.href; |
| 20 | + |
| 21 | + const whatsappText = `${title}\n\n${url}`; |
| 22 | + function copy() { |
| 23 | + const success = toClipboard(url); |
| 24 | + if (success) { |
| 25 | + alert(`Successfully copied link to your clipboard`); |
| 26 | + } else { |
| 27 | + alert(`Failed to copied link to your clipboard`); |
| 28 | + } |
| 29 | + } |
| 30 | + return ( |
| 31 | + <div className="container" role="button" onClick={closeShareDialog}> |
| 32 | + <div className="popup shadow-2xl bg-gray-100 border border-gray-200"> |
| 33 | + <h3>Share via</h3> |
| 34 | + <ul> |
| 35 | + <li> |
| 36 | + <a href={`whatsapp://send?text=${encodeURIComponent(whatsappText)}`}> |
| 37 | + <WhatsappIcon style={{ width: 50, height: 50 }} /> |
| 38 | + <div className="icon-name">WhatsApp</div> |
| 39 | + </a> |
| 40 | + </li> |
| 41 | + <li> |
| 42 | + <a |
| 43 | + href={`http://www.facebook.com/sharer.php?u=${encodeURIComponent(url)}&p[title]=${encodeURIComponent( |
| 44 | + title |
| 45 | + )}`}> |
| 46 | + <FacebookIcon style={{ width: 40, height: 40 }} /> |
| 47 | + <div style={{ position: 'relative', top: 5 }} className="icon-name"> |
| 48 | + Facebook |
| 49 | + </div> |
| 50 | + </a> |
| 51 | + </li> |
| 52 | + <li> |
| 53 | + <a href={`fb-messenger://share/?link=${encodeURIComponent(url)}`}> |
| 54 | + <MessangerIcon style={{ width: 45, height: 45 }} /> |
| 55 | + <div style={{ position: 'relative', top: 2 }} className="icon-name"> |
| 56 | + Messanger |
| 57 | + </div> |
| 58 | + </a> |
| 59 | + </li> |
| 60 | + <li> |
| 61 | + <a |
| 62 | + href={`https://twitter.com/intent/tweet?text=${encodeURIComponent(title)}&url=${encodeURIComponent( |
| 63 | + url |
| 64 | + )}`}> |
| 65 | + <TwitterIcon style={{ width: 40, height: 40 }} /> |
| 66 | + <div style={{ position: 'relative', top: 0 }} className="icon-name"> |
| 67 | + Twitter |
| 68 | + </div> |
| 69 | + </a> |
| 70 | + </li> |
| 71 | + <li> |
| 72 | + <button type="button" onClick={copy} className="social-link btn"> |
| 73 | + <CopyIcon style={{ width: 36, height: 36 }} /> |
| 74 | + <div className="icon-name">Copy to clipboard</div> |
| 75 | + </button> |
| 76 | + </li> |
| 77 | + </ul> |
| 78 | + </div> |
| 79 | + <style jsx> |
| 80 | + {` |
| 81 | + .container { |
| 82 | + width: 100%; |
| 83 | + height: 100vh; |
| 84 | + position: absolute; |
| 85 | + top: 0; |
| 86 | + left: 0; |
| 87 | + right: 0; |
| 88 | + bottom: 0; |
| 89 | + z-index: ${isOpen ? 1000 : -1}; |
| 90 | + } |
| 91 | + .popup { |
| 92 | + min-height: 240px; |
| 93 | + bottom: 80px; |
| 94 | + position: absolute; |
| 95 | + z-index: 2; |
| 96 | + padding: 12px 4px; |
| 97 | + transition: all 0.25s cubic-bezier(0.4, 0, 0.6, 1); |
| 98 | + transform: ${isOpen ? `translateY(0)` : `translateY(200%)`}; |
| 99 | + opacity: ${isOpen ? 1 : 0}; |
| 100 | + margin: 4px; |
| 101 | + left: 10px; |
| 102 | + right: 10px; |
| 103 | + border-radius: 16px; |
| 104 | + z-index: ${isOpen ? 'auto' : -1}; |
| 105 | + } |
| 106 | + h3 { |
| 107 | + text-align: center; |
| 108 | + } |
| 109 | + ul { |
| 110 | + display: flex; |
| 111 | + flex-wrap: wrap; |
| 112 | + padding: 0; |
| 113 | + margin: 0; |
| 114 | + list-style: none; |
| 115 | + align-items: center; |
| 116 | + } |
| 117 | + ul li { |
| 118 | + margin: 24px; |
| 119 | + } |
| 120 | + ul li :global(.social-link) { |
| 121 | + display: block; |
| 122 | + } |
| 123 | + ul li :global(.social-link.btn) { |
| 124 | + -webkit-tap-highlight-color: transparent; |
| 125 | + appearence: none; |
| 126 | + outline: none; |
| 127 | + border: none; |
| 128 | + background: transparent; |
| 129 | + } |
| 130 | + .icon-name { |
| 131 | + width: 40px; |
| 132 | + font-size: 10px; |
| 133 | + display: flex; |
| 134 | + align-items: center; |
| 135 | + text-align: center; |
| 136 | + justify-content: center; |
| 137 | + margin: 0 auto; |
| 138 | + color: #888; |
| 139 | + } |
| 140 | + `} |
| 141 | + </style> |
| 142 | + </div> |
| 143 | + ); |
| 144 | +} |
0 commit comments