UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

46 lines (42 loc) 1.62 kB
"use client"; const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs'); const require_utils_index = require('../../utils/index.cjs'); let react = require("react"); react = require_rolldown_runtime.__toESM(react); let copy_to_clipboard = require("copy-to-clipboard"); copy_to_clipboard = require_rolldown_runtime.__toESM(copy_to_clipboard); //#region src/hooks/use-clipboard/index.ts /** * `useClipboard` is a custom hook that performs the operation of copying a value to the clipboard. * * @see https://yamada-ui.com/docs/hooks/use-clipboard */ const useClipboard = (defaultValue = "", timeoutOrOptions = {}) => { const [copied, setCopied] = (0, react.useState)(false); const [value, setValue] = (0, react.useState)(defaultValue); (0, react.useEffect)(() => setValue(defaultValue), [defaultValue]); const { timeout = 1500,...copyOptions } = (0, require_utils_index.utils_exports.isNumber)(timeoutOrOptions) ? { timeout: timeoutOrOptions } : timeoutOrOptions; const onCopy = (0, react.useCallback)((newValue) => { if (!(0, require_utils_index.utils_exports.isString)(newValue)) newValue = value; else setValue(newValue); setCopied((0, copy_to_clipboard.default)(newValue, copyOptions)); }, [value, copyOptions]); (0, react.useEffect)(() => { let timeoutId = null; if (copied) timeoutId = setTimeout(() => { setCopied(false); }, timeout); return () => { if (timeoutId) clearTimeout(timeoutId); }; }, [timeout, copied]); return { copied, setValue, value, onCopy }; }; //#endregion exports.useClipboard = useClipboard; //# sourceMappingURL=index.cjs.map