|
| 1 | +import CloseButton from '../../common/close-button'; |
| 2 | +import copyTextToClipboard from '../../../utils/copy-to-clipboard'; |
| 3 | +import useShareData from './use-share-data'; |
| 4 | +import {ReactComponent as BlueSkyIcon} from '../../../images/icons/share-bluesky.svg'; |
| 5 | +import {ReactComponent as CheckmarkIcon} from '../../../images/icons/checkmark.svg'; |
| 6 | +import {ReactComponent as EnvelopeIcon} from '../../../images/icons/envelope.svg'; |
| 7 | +import {ReactComponent as EllipsisIcon} from '../../../images/icons/ellipsis.svg'; |
| 8 | +import {ReactComponent as FacebookIcon} from '../../../images/icons/share-facebook.svg'; |
| 9 | +import {ReactComponent as LinkIcon} from '../../../images/icons/share-link.svg'; |
| 10 | +import {ReactComponent as LinkedinIcon} from '../../../images/icons/share-linkedin.svg'; |
| 11 | +import {ReactComponent as ThreadsIcon} from '../../../images/icons/share-threads.svg'; |
| 12 | +import {ReactComponent as XIcon} from '../../../images/icons/share-x.svg'; |
| 13 | +import {useEffect, useRef, useState} from 'react'; |
| 14 | +import {t} from '../../../utils/i18n'; |
| 15 | + |
| 16 | +const ShareModal = () => { |
| 17 | + const [copied, setCopied] = useState(false); |
| 18 | + const [isMoreMenuOpen, setIsMoreMenuOpen] = useState(false); |
| 19 | + const copyTimeoutRef = useRef(); |
| 20 | + const moreMenuRef = useRef(null); |
| 21 | + |
| 22 | + const {shareUrl, shareTitle, shareExcerpt, shareImage, shareFavicon, shareSiteName, shareAuthor, socialLinks} = useShareData(); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + return () => { |
| 26 | + clearTimeout(copyTimeoutRef.current); |
| 27 | + }; |
| 28 | + }, []); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + if (!isMoreMenuOpen) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const onDocumentMouseDown = (event) => { |
| 36 | + if (moreMenuRef.current && !moreMenuRef.current.contains(event.target)) { |
| 37 | + setIsMoreMenuOpen(false); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + const onDocumentKeyDown = (event) => { |
| 42 | + if (event.key === 'Escape') { |
| 43 | + setIsMoreMenuOpen(false); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + document.addEventListener('mousedown', onDocumentMouseDown); |
| 48 | + document.addEventListener('keydown', onDocumentKeyDown); |
| 49 | + |
| 50 | + return () => { |
| 51 | + document.removeEventListener('mousedown', onDocumentMouseDown); |
| 52 | + document.removeEventListener('keydown', onDocumentKeyDown); |
| 53 | + }; |
| 54 | + }, [isMoreMenuOpen]); |
| 55 | + |
| 56 | + const onCopy = async () => { |
| 57 | + const copySuccess = await copyTextToClipboard(shareUrl); |
| 58 | + if (!copySuccess) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + setCopied(true); |
| 63 | + clearTimeout(copyTimeoutRef.current); |
| 64 | + copyTimeoutRef.current = setTimeout(() => { |
| 65 | + setCopied(false); |
| 66 | + }, 2000); |
| 67 | + }; |
| 68 | + |
| 69 | + const onToggleMoreMenu = () => { |
| 70 | + setIsMoreMenuOpen(isOpen => !isOpen); |
| 71 | + }; |
| 72 | + |
| 73 | + const onClickMoreItem = () => { |
| 74 | + setIsMoreMenuOpen(false); |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <div className='gh-portal-content gh-portal-share'> |
| 79 | + <CloseButton /> |
| 80 | + <div className='gh-portal-share-header'> |
| 81 | + <h1 className='gh-portal-main-title'>{t('Share')}</h1> |
| 82 | + </div> |
| 83 | + |
| 84 | + <div className='gh-portal-share-preview'> |
| 85 | + {shareImage && <img className='gh-portal-share-preview-image' src={shareImage} alt='' data-testid='share-preview-image' />} |
| 86 | + <div className='gh-portal-share-preview-content'> |
| 87 | + {shareTitle && <h2 className='gh-portal-share-preview-title'>{shareTitle}</h2>} |
| 88 | + {shareExcerpt && <p className='gh-portal-share-preview-excerpt'>{shareExcerpt}</p>} |
| 89 | + {(shareFavicon || shareSiteName || shareAuthor) && ( |
| 90 | + <div className='gh-portal-share-preview-footer'> |
| 91 | + {shareFavicon && ( |
| 92 | + <img |
| 93 | + className='gh-portal-share-preview-favicon' |
| 94 | + src={shareFavicon} |
| 95 | + alt='' |
| 96 | + data-testid='share-preview-favicon' |
| 97 | + /> |
| 98 | + )} |
| 99 | + <div className='gh-portal-share-preview-meta'> |
| 100 | + {shareSiteName && <span className='gh-portal-share-preview-site'>{shareSiteName}</span>} |
| 101 | + {shareAuthor && ( |
| 102 | + <span className='gh-portal-share-preview-author'> |
| 103 | + {shareSiteName ? `| ${shareAuthor}` : shareAuthor} |
| 104 | + </span> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + |
| 112 | + <div className='gh-portal-share-actions'> |
| 113 | + <button |
| 114 | + className='gh-portal-btn gh-portal-share-action copy' |
| 115 | + type='button' |
| 116 | + onClick={onCopy} |
| 117 | + aria-label={copied ? t('Copied') : t('Copy link')} |
| 118 | + title={copied ? t('Copied') : t('Copy link')} |
| 119 | + > |
| 120 | + {copied ? ( |
| 121 | + <span className='gh-portal-share-icon copied' aria-hidden='true'> |
| 122 | + <CheckmarkIcon /> |
| 123 | + </span> |
| 124 | + ) : ( |
| 125 | + <span className='gh-portal-share-icon' aria-hidden='true'> |
| 126 | + <LinkIcon /> |
| 127 | + </span> |
| 128 | + )} |
| 129 | + <span className='gh-portal-share-label'>{copied ? t('Copied') : t('Copy link')}</span> |
| 130 | + </button> |
| 131 | + |
| 132 | + <a |
| 133 | + className='gh-portal-btn gh-portal-share-action twitter' |
| 134 | + href={socialLinks.twitter} |
| 135 | + target='_blank' |
| 136 | + rel='noopener noreferrer' |
| 137 | + aria-label={t('X (Twitter)')} |
| 138 | + title={t('X (Twitter)')} |
| 139 | + > |
| 140 | + <span className='gh-portal-share-icon x' aria-hidden='true'> |
| 141 | + <XIcon /> |
| 142 | + </span> |
| 143 | + </a> |
| 144 | + |
| 145 | + <a |
| 146 | + className='gh-portal-btn gh-portal-share-action linkedin' |
| 147 | + href={socialLinks.linkedin} |
| 148 | + target='_blank' |
| 149 | + rel='noopener noreferrer' |
| 150 | + aria-label={t('LinkedIn')} |
| 151 | + title={t('LinkedIn')} |
| 152 | + > |
| 153 | + <span className='gh-portal-share-icon' aria-hidden='true'> |
| 154 | + <LinkedinIcon /> |
| 155 | + </span> |
| 156 | + </a> |
| 157 | + |
| 158 | + <a |
| 159 | + className='gh-portal-btn gh-portal-share-action email' |
| 160 | + href={socialLinks.email} |
| 161 | + target='_blank' |
| 162 | + rel='noopener noreferrer' |
| 163 | + aria-label={t('Email')} |
| 164 | + title={t('Email')} |
| 165 | + > |
| 166 | + <span className='gh-portal-share-icon' aria-hidden='true'> |
| 167 | + <EnvelopeIcon /> |
| 168 | + </span> |
| 169 | + </a> |
| 170 | + |
| 171 | + <div className='gh-portal-share-more' ref={moreMenuRef}> |
| 172 | + <button |
| 173 | + className='gh-portal-btn gh-portal-share-action more' |
| 174 | + type='button' |
| 175 | + onClick={onToggleMoreMenu} |
| 176 | + aria-label={t('More options')} |
| 177 | + title={t('More options')} |
| 178 | + aria-haspopup='menu' |
| 179 | + aria-expanded={isMoreMenuOpen} |
| 180 | + > |
| 181 | + <span className='gh-portal-share-icon' aria-hidden='true'> |
| 182 | + <EllipsisIcon /> |
| 183 | + </span> |
| 184 | + </button> |
| 185 | + {isMoreMenuOpen && ( |
| 186 | + <div className='gh-portal-share-more-menu' role='menu' aria-label={t('More options')}> |
| 187 | + <a |
| 188 | + className='gh-portal-share-more-item' |
| 189 | + href={socialLinks.facebook} |
| 190 | + target='_blank' |
| 191 | + rel='noopener noreferrer' |
| 192 | + role='menuitem' |
| 193 | + onClick={onClickMoreItem} |
| 194 | + > |
| 195 | + <span className='gh-portal-share-more-item-icon' aria-hidden='true'> |
| 196 | + <FacebookIcon /> |
| 197 | + </span> |
| 198 | + <span>{t('Facebook')}</span> |
| 199 | + </a> |
| 200 | + <a |
| 201 | + className='gh-portal-share-more-item' |
| 202 | + href={socialLinks.threads} |
| 203 | + target='_blank' |
| 204 | + rel='noopener noreferrer' |
| 205 | + role='menuitem' |
| 206 | + onClick={onClickMoreItem} |
| 207 | + > |
| 208 | + <span className='gh-portal-share-more-item-icon' aria-hidden='true'> |
| 209 | + <ThreadsIcon /> |
| 210 | + </span> |
| 211 | + <span>{t('Threads')}</span> |
| 212 | + </a> |
| 213 | + <a |
| 214 | + className='gh-portal-share-more-item' |
| 215 | + href={socialLinks.bluesky} |
| 216 | + target='_blank' |
| 217 | + rel='noopener noreferrer' |
| 218 | + role='menuitem' |
| 219 | + onClick={onClickMoreItem} |
| 220 | + > |
| 221 | + <span className='gh-portal-share-more-item-icon' aria-hidden='true'> |
| 222 | + <BlueSkyIcon /> |
| 223 | + </span> |
| 224 | + <span>{t('Bluesky')}</span> |
| 225 | + </a> |
| 226 | + </div> |
| 227 | + )} |
| 228 | + </div> |
| 229 | + </div> |
| 230 | + </div> |
| 231 | + ); |
| 232 | +}; |
| 233 | + |
| 234 | +export default ShareModal; |
0 commit comments