Enhance share dialog (#860)

This commit is contained in:
Erik Michelson 2020-12-22 21:17:40 +01:00 committed by GitHub
parent 1c6e6e10fb
commit 721c8c0e5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 120 additions and 25 deletions

View file

@ -4,31 +4,52 @@ SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
SPDX-License-Identifier: AGPL-3.0-only
*/
import React, { Fragment, useRef } from 'react'
import React, { Fragment, useCallback, useRef } from 'react'
import { Button, FormControl, InputGroup } from 'react-bootstrap'
import { useTranslation } from 'react-i18next'
import { ForkAwesomeIcon } from '../../fork-awesome/fork-awesome-icon'
import { ShowIf } from '../../show-if/show-if'
import { CopyOverlay } from '../copy-overlay'
export interface CopyableFieldProps {
content: string
nativeShareButton?: boolean
url?: string
}
export const CopyableField: React.FC<CopyableFieldProps> = ({ content }) => {
export const CopyableField: React.FC<CopyableFieldProps> = ({ content, nativeShareButton, url }) => {
useTranslation()
const button = useRef<HTMLButtonElement>(null)
const copyButton = useRef<HTMLButtonElement>(null)
const doShareAction = useCallback(() => {
navigator.share({
text: content,
url: url
}).catch(err => {
console.error('Native sharing failed: ', err)
})
}, [content, url])
const sharingSupported = typeof navigator.share === 'function'
return (
<Fragment>
<InputGroup className="my-3">
<FormControl readOnly={true} className={'text-center'} value={content} />
<InputGroup.Append>
<Button variant="outline-secondary" ref={button} title={'Copy'}>
<Button variant="outline-secondary" ref={copyButton} title={'Copy'}>
<ForkAwesomeIcon icon='files-o'/>
</Button>
</InputGroup.Append>
<ShowIf condition={!!nativeShareButton && sharingSupported}>
<InputGroup.Append>
<Button variant="outline-secondary" title={'Share'} onClick={doShareAction}>
<ForkAwesomeIcon icon='share-alt'/>
</Button>
</InputGroup.Append>
</ShowIf>
</InputGroup>
<CopyOverlay content={content} clickComponent={button}/>
<CopyOverlay content={content} clickComponent={copyButton}/>
</Fragment>
)
}