UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

63 lines (62 loc) 2.12 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["children", "className", "disabled", "showCursor", "copyContent"]; import React, { useCallback, useEffect, useRef } from 'react'; import classnames from 'classnames'; import { runIOSSelectionFix, copyWithEffect } from '../number-format/NumberUtils'; import { hasSelectedText, IS_IOS, warn } from '../../shared/helpers'; import { convertJsxToString } from '../../shared/component-helper'; import { useTranslation } from '../../shared'; import { Span } from '../../elements'; const CopyOnClick = _ref => { let { children, className = null, disabled, showCursor = true, copyContent = null } = _ref, props = _objectWithoutProperties(_ref, _excluded); const ref = useRef(null); useEffect(() => { if (IS_IOS) { runIOSSelectionFix(); } }, []); const { CopyOnClick: { clipboard_copy } } = useTranslation(); const copy = useCallback((value, positionElement) => { copyWithEffect(value, clipboard_copy, positionElement); }, [clipboard_copy]); const onClickHandler = useCallback(() => { if (!hasSelectedText()) { try { const str = convertJsxToString(copyContent || children); if (str) { const selection = window.getSelection(); const range = document.createRange(); range.selectNodeContents(ref.current); selection.removeAllRanges(); selection.addRange(range); copy(str, ref.current); } } catch (e) { warn(e); } } }, [children, copyContent, copy]); const params = { onClick: disabled ? undefined : onClickHandler }; return React.createElement(Span, _extends({ className: classnames('dnb-copy-on-click', className, showCursor && !disabled && 'dnb-copy-on-click--cursor'), ref: ref }, props, params), children); }; CopyOnClick._supportsSpacingProps = true; export default CopyOnClick; //# sourceMappingURL=CopyOnClick.js.map