-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: "Copy link" button in Export dialog
- Loading branch information
Danko Kozar
committed
Apr 2, 2020
1 parent
7fd256f
commit 90f118c
Showing
6 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, { useState, ReactNode } from 'react' | ||
import Popper from 'popper.js' | ||
import { Button, Popover, PopoverHeader, PopoverBody } from 'reactstrap' | ||
import { copyToClipboard } from '../../helpers/copyToClipboard' | ||
|
||
interface ClipboardButtonProps { | ||
disabled: boolean | ||
children: ReactNode | ||
textToCopy?: string | ||
popoverBody: ReactNode | ||
popoverHeader?: string | ||
popoverPlacement?: Popper.Placement | ||
popoverTarget: string | HTMLElement | React.RefObject<HTMLElement> | ||
} | ||
|
||
/** | ||
* When clicked, copies textToCopy to clipboard | ||
* The user could then paste this text somewhere else | ||
*/ | ||
export default function ClipboardButton({ | ||
disabled, | ||
children, | ||
textToCopy, | ||
popoverBody = '', | ||
popoverHeader = '', | ||
popoverPlacement = 'bottom', | ||
popoverTarget, | ||
}: ClipboardButtonProps) { | ||
const [popoverOpen, setPopoverOpen] = useState(false) | ||
const closePopover = () => setPopoverOpen(false) | ||
const onClick = () => { | ||
if (textToCopy) { | ||
copyToClipboard(String(textToCopy)) | ||
setPopoverOpen(true) | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<Button disabled={disabled} onClick={onClick} color="primary" size="sm"> | ||
{children} | ||
</Button> | ||
<Popover placement={popoverPlacement} open={popoverOpen} target={popoverTarget} ontoggle={closePopover}> | ||
<PopoverHeader>{popoverHeader}</PopoverHeader> | ||
<PopoverBody>{popoverBody}</PopoverBody> | ||
</Popover> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const copyToClipboard = (str: string) => { | ||
const el = document.createElement('textarea') | ||
el.value = str | ||
document.body.appendChild(el) | ||
el.select() | ||
document.execCommand('copy') | ||
document.body.removeChild(el) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const createNonExistingElement = (id: string, type = 'div') => { | ||
let element = document.getElementById(id) | ||
|
||
if (!element) { | ||
element = document.createElement(type) | ||
element.setAttribute('id', id) | ||
document.body.append(element) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters