UNPKG

@carbon/react

Version:

React components for the Carbon Design System

129 lines (121 loc) 4.32 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. */ import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js'; import PropTypes from 'prop-types'; import React, { useState, useCallback, useEffect } from 'react'; import cx from 'classnames'; import { composeEventHandlers } from '../../tools/events.js'; import { usePrefix } from '../../internal/usePrefix.js'; import { IconButton } from '../IconButton/index.js'; import { noopFn } from '../../internal/noopFn.js'; import deprecateValuesWithin from '../../prop-types/deprecateValuesWithin.js'; import { mapPopoverAlign } from '../../tools/mapPopoverAlign.js'; import { debounce } from '../../node_modules/es-toolkit/dist/compat/function/debounce.js'; function Copy({ align = 'bottom', autoAlign = false, children, className, feedback = 'Copied!', feedbackTimeout = 2000, onAnimationEnd, onClick = noopFn, ...other }) { const [animation, setAnimation] = useState(''); const prefix = usePrefix(); const classNames = cx(className, `${prefix}--copy`, { [`${prefix}--copy-btn--animating`]: animation, [`${prefix}--copy-btn--${animation}`]: animation }); // eslint-disable-next-line react-hooks/exhaustive-deps const handleFadeOut = useCallback(debounce(() => { setAnimation('fade-out'); }, feedbackTimeout), [feedbackTimeout]); const handleClick = useCallback(() => { setAnimation('fade-in'); handleFadeOut(); }, [handleFadeOut]); const handleAnimationEnd = event => { if (event.animationName === `${prefix}--hide-feedback`) { setAnimation(''); } }; useEffect(() => () => { handleFadeOut.cancel(); }, [handleFadeOut]); const initialLabel = other['aria-label'] ?? ''; return /*#__PURE__*/React.createElement(IconButton, _extends({ closeOnActivation: false, align: align, autoAlign: autoAlign, className: classNames, label: animation ? feedback : initialLabel, leaveDelayMs: animation ? feedbackTimeout : undefined, onClick: composeEventHandlers([onClick, handleClick]), onAnimationEnd: 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(PropTypes.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), /** * **Experimental**: Will attempt to automatically align the tooltip. Requires * React v17+ * @see https://github.com/carbon-design-system/carbon/issues/18714 */ autoAlign: PropTypes.bool, /** * Pass in content to be rendered in the underlying `<button>` */ children: PropTypes.node, /** * Specify an optional className to be applied to the underlying `<button>` */ className: PropTypes.string, /** * Specify the string that is displayed when the button is clicked and the * content is copied */ feedback: PropTypes.string, /** * Specify the time it takes for the feedback message to timeout */ feedbackTimeout: PropTypes.number, /** * Specify an optional `onAnimationEnd` handler that is called when the underlying * animation ends */ onAnimationEnd: PropTypes.func, /** * Specify an optional `onClick` handler that is called when the underlying * `<button>` is clicked */ onClick: PropTypes.func }; export { Copy as default };