UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

62 lines 2.36 kB
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime"; import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import * as React from 'react'; import copyToClipboard from 'clipboard-copy'; export function useCopier(contents, opts) { var _ref = opts || {}, onCopied = _ref.onCopied, onError = _ref.onError, onClick = _ref.onClick, _ref$timeout = _ref.timeout, timeout = _ref$timeout === void 0 ? 2000 : _ref$timeout; var copyTimeoutRef = React.useRef(undefined); var _React$useState = React.useState(false), _React$useState2 = _slicedToArray(_React$useState, 2), recentlySuccessful = _React$useState2[0], setRecentlySuccessful = _React$useState2[1]; var copy = React.useCallback(/*#__PURE__*/function () { var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(event) { var content; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: clearTimeout(copyTimeoutRef.current); setRecentlySuccessful(false); _context.prev = 2; content = typeof contents === 'function' ? contents() : contents; if (!content) { _context.next = 7; break; } _context.next = 7; return copyToClipboard(content); case 7: onCopied == null || onCopied(); _context.next = 13; break; case 10: _context.prev = 10; _context.t0 = _context["catch"](2); onError == null || onError(_context.t0); case 13: onClick == null || onClick(event); copyTimeoutRef.current = setTimeout(function () { clearTimeout(copyTimeoutRef.current); setRecentlySuccessful(false); }, timeout); case 15: case "end": return _context.stop(); } }, _callee, null, [[2, 10]]); })); return function (_x) { return _ref2.apply(this, arguments); }; }(), [contents, timeout, onCopied, onError, onClick]); return { copy: copy, recentlySuccessful: recentlySuccessful }; }