@gechiui/components
Version:
UI components for GeChiUI.
63 lines (57 loc) • 1.72 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement } from "@gechiui/element";
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* GeChiUI dependencies
*/
import { useRef, useEffect } from '@gechiui/element';
import { useCopyToClipboard } from '@gechiui/compose';
import deprecated from '@gechiui/deprecated';
/**
* Internal dependencies
*/
import Button from '../button';
const TIMEOUT = 4000;
export default function ClipboardButton(_ref) {
let {
className,
children,
onCopy,
onFinishCopy,
text,
...buttonProps
} = _ref;
deprecated('gc.components.ClipboardButton', {
since: '10.3',
plugin: 'Gutenberg',
alternative: 'gc.compose.useCopyToClipboard'
});
const timeoutId = useRef();
const ref = useCopyToClipboard(text, () => {
onCopy();
clearTimeout(timeoutId.current);
if (onFinishCopy) {
timeoutId.current = setTimeout(() => onFinishCopy(), TIMEOUT);
}
});
useEffect(() => {
clearTimeout(timeoutId.current);
}, []);
const classes = classnames('components-clipboard-button', className); // Workaround for inconsistent behavior in Safari, where <textarea> is not
// the document.activeElement at the moment when the copy event fires.
// This causes documentHasSelection() in the copy-handler component to
// mistakenly override the ClipboardButton, and copy a serialized string
// of the current block instead.
const focusOnCopyEventTarget = event => {
event.target.focus();
};
return createElement(Button, _extends({}, buttonProps, {
className: classes,
ref: ref,
onCopy: focusOnCopyEventTarget
}), children);
}
//# sourceMappingURL=index.js.map