UNPKG

@elastic/eui

Version:

Elastic UI Component Library

88 lines (87 loc) 4.13 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import React, { Component, createRef } from 'react'; import { copyToClipboard } from '../../services'; import { EuiScreenReaderOnly } from '../accessibility'; import { EuiToolTip } from '../tool_tip'; import { jsx as ___EmotionJSX } from "@emotion/react"; export var EuiCopy = /*#__PURE__*/function (_Component) { function EuiCopy(props) { var _this; _classCallCheck(this, EuiCopy); _this = _callSuper(this, EuiCopy, [props]); _defineProperty(_this, "tooltipRef", /*#__PURE__*/createRef()); _defineProperty(_this, "copy", function () { var isCopied = copyToClipboard(_this.props.textToCopy); if (isCopied) { _this.setState({ tooltipText: _this.props.afterMessage, isCopied: true }, // `EuiToolTip` suppresses showing when content is empty, so `EuiCopy` // imperatively shows the tooltip after the post-copy state update. function () { var _this$tooltipRef$curr; (_this$tooltipRef$curr = _this.tooltipRef.current) === null || _this$tooltipRef$curr === void 0 || _this$tooltipRef$curr.showToolTip(); }); } }); _defineProperty(_this, "resetTooltipText", function () { _this.setState({ tooltipText: _this.props.beforeMessage, isCopied: false }); }); _this.state = { tooltipText: _this.props.beforeMessage, isCopied: false }; return _this; } _inherits(EuiCopy, _Component); return _createClass(EuiCopy, [{ key: "render", value: function render() { var _this2 = this; var _this$props = this.props, children = _this$props.children, tooltipProps = _this$props.tooltipProps, afterMessage = _this$props.afterMessage; var _this$state = this.state, tooltipText = _this$state.tooltipText, isCopied = _this$state.isCopied; return ___EmotionJSX(React.Fragment, null, ___EmotionJSX(EuiToolTip, _extends({ ref: this.tooltipRef, content: tooltipText, onMouseOut: this.resetTooltipText }, tooltipProps, { onBlur: function onBlur() { var _tooltipProps$onBlur; tooltipProps === null || tooltipProps === void 0 || (_tooltipProps$onBlur = tooltipProps.onBlur) === null || _tooltipProps$onBlur === void 0 || _tooltipProps$onBlur.call(tooltipProps); if (isCopied) _this2.resetTooltipText(); }, disableScreenReaderOutput: isCopied || !!(tooltipProps !== null && tooltipProps !== void 0 && tooltipProps.disableScreenReaderOutput) }), children(this.copy)), ___EmotionJSX(EuiScreenReaderOnly, null, ___EmotionJSX("div", { "aria-live": "assertive", "aria-atomic": "true" }, isCopied ? afterMessage : ''))); } }]); }(Component); _defineProperty(EuiCopy, "defaultProps", { afterMessage: 'Copied' });