UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

65 lines (64 loc) 2.32 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", "tooltipContent"]; 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 = false, showCursor = true, copyContent = null, tooltipContent = 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) => { const message = tooltipContent !== null && tooltipContent !== void 0 ? tooltipContent : clipboard_copy; copyWithEffect(value, message, positionElement); }, [clipboard_copy, tooltipContent]); const onClickHandler = useCallback(() => { if (!hasSelectedText()) { try { const str = convertJsxToString(copyContent || children) || ref.current.textContent; 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