import React from 'react'; import {Button} from 'coral-ui'; import styles from './styles.css'; import t from 'coral-framework/services/i18n'; import ClickOutside from 'coral-framework/components/ClickOutside'; import cn from 'classnames'; const name = 'talk-plugin-permalink'; export default class PermalinkButton extends React.Component { constructor (props) { super(props); this.state = { popoverOpen: false, copySuccessful: null, copyFailure: null }; } toggle = () => { this.popover.style.top = `${this.linkButton.offsetTop - 80}px`; this.setState({ popoverOpen: !this.state.popoverOpen }); } handleClickOutside = () => { this.setState({ popoverOpen: false }); } copyPermalink = () => { this.permalinkInput.select(); try { document.execCommand('copy'); this.setState({ copySuccessful: true, copyFailure: null }); } catch (err) { this.setState({ copyFailure: true, copySuccessful: null }); } this.timeout = window.setTimeout(() => { this.setState({ copyFailure: null, copySuccessful: null }); }, 3000); } componentWillUnmount() { window.clearTimeout(this.timeout); } render () { const {copySuccessful, copyFailure, popoverOpen} = this.state; const {asset} = this.props; return (
this.popover = ref} className={cn([`${name}-popover`, styles.popover, {[styles.active]: popoverOpen}])}> this.permalinkInput = input} defaultValue={`${asset.url}#${this.props.commentId}`} />
); } }