UNPKG

@carbon/react

Version:

React components for the Carbon Design System

136 lines (124 loc) 4.84 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var PropTypes = require('prop-types'); var React = require('react'); var cx = require('classnames'); var events = require('../../tools/events.js'); var usePrefix = require('../../internal/usePrefix.js'); var index = require('../IconButton/index.js'); var noopFn = require('../../internal/noopFn.js'); var deprecateValuesWithin = require('../../prop-types/deprecateValuesWithin.js'); var mapPopoverAlign = require('../../tools/mapPopoverAlign.js'); var debounce = require('../../node_modules/es-toolkit/dist/compat/function/debounce.mjs.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes); var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx); function Copy({ align = 'bottom', autoAlign = false, children, className, feedback = 'Copied!', feedbackTimeout = 2000, onAnimationEnd, onClick = noopFn.noopFn, ...other }) { const [animation, setAnimation] = React.useState(''); const prefix = usePrefix.usePrefix(); const classNames = cx__default["default"](className, `${prefix}--copy`, { [`${prefix}--copy-btn--animating`]: animation, [`${prefix}--copy-btn--${animation}`]: animation }); // eslint-disable-next-line react-hooks/exhaustive-deps const handleFadeOut = React.useCallback(debounce.debounce(() => { setAnimation('fade-out'); }, feedbackTimeout), [feedbackTimeout]); const handleClick = React.useCallback(() => { setAnimation('fade-in'); handleFadeOut(); }, [handleFadeOut]); const handleAnimationEnd = event => { if (event.animationName === `${prefix}--hide-feedback`) { setAnimation(''); } }; React.useEffect(() => () => { handleFadeOut.cancel(); }, [handleFadeOut]); const initialLabel = other['aria-label'] ?? ''; return /*#__PURE__*/React__default["default"].createElement(index.IconButton, _rollupPluginBabelHelpers["extends"]({ closeOnActivation: false, align: align, autoAlign: autoAlign, className: classNames, label: animation ? feedback : initialLabel, onClick: events.composeEventHandlers([onClick, handleClick]), onAnimationEnd: events.composeEventHandlers([onAnimationEnd, handleAnimationEnd]) }, other, { "aria-label": !children && (animation ? feedback : other['aria-label']) || undefined }), children); } Copy.propTypes = { /** * Specify how the trigger should align with the tooltip */ align: deprecateValuesWithin["default"](PropTypes__default["default"].oneOf(['top', 'top-left', // deprecated use top-start instead 'top-right', // deprecated use top-end instead 'bottom', 'bottom-left', // deprecated use bottom-start instead 'bottom-right', // deprecated use bottom-end instead 'left', 'left-bottom', // deprecated use left-end instead 'left-top', // deprecated use left-start instead 'right', 'right-bottom', // deprecated use right-end instead 'right-top', // deprecated use right-start instead // new values to match floating-ui 'top-start', 'top-end', 'bottom-start', 'bottom-end', 'left-end', 'left-start', 'right-end', 'right-start']), ['top', 'top-start', 'top-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end'], mapPopoverAlign.mapPopoverAlign), /** * **Experimental**: Will attempt to automatically align the tooltip */ autoAlign: PropTypes__default["default"].bool, /** * Pass in content to be rendered in the underlying `<button>` */ children: PropTypes__default["default"].node, /** * Specify an optional className to be applied to the underlying `<button>` */ className: PropTypes__default["default"].string, /** * Specify the string that is displayed when the button is clicked and the * content is copied */ feedback: PropTypes__default["default"].string, /** * Specify the time it takes for the feedback message to timeout */ feedbackTimeout: PropTypes__default["default"].number, /** * Specify an optional `onAnimationEnd` handler that is called when the underlying * animation ends */ onAnimationEnd: PropTypes__default["default"].func, /** * Specify an optional `onClick` handler that is called when the underlying * `<button>` is clicked */ onClick: PropTypes__default["default"].func }; exports["default"] = Copy;