@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
1 lines • 2.21 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","names":["timeoutId: NodeJS.Timeout | null"],"sources":["../../../../src/hooks/use-clipboard/index.ts"],"sourcesContent":["\"use client\"\n\nimport copy from \"copy-to-clipboard\"\nimport { useCallback, useEffect, useState } from \"react\"\nimport { isNumber, isString } from \"../../utils\"\n\nexport interface UseClipboardOptions {\n format?: string\n timeout?: number\n}\n\n/**\n * `useClipboard` is a custom hook that performs the operation of copying a value to the clipboard.\n *\n * @see https://yamada-ui.com/docs/hooks/use-clipboard\n */\nexport const useClipboard = (\n defaultValue = \"\",\n timeoutOrOptions: number | UseClipboardOptions = {},\n) => {\n const [copied, setCopied] = useState(false)\n\n const [value, setValue] = useState(defaultValue)\n\n useEffect(() => setValue(defaultValue), [defaultValue])\n\n const { timeout = 1500, ...copyOptions } = isNumber(timeoutOrOptions)\n ? { timeout: timeoutOrOptions }\n : timeoutOrOptions\n\n const onCopy = useCallback(\n (newValue?: any) => {\n if (!isString(newValue)) {\n newValue = value\n } else {\n setValue(newValue)\n }\n\n const copied = copy(newValue, copyOptions)\n\n setCopied(copied)\n },\n [value, copyOptions],\n )\n\n useEffect(() => {\n let timeoutId: NodeJS.Timeout | null = null\n\n if (copied)\n timeoutId = setTimeout(() => {\n setCopied(false)\n }, timeout)\n\n return () => {\n if (timeoutId) clearTimeout(timeoutId)\n }\n }, [timeout, copied])\n\n return {\n copied,\n setValue,\n value,\n onCopy,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,MAAa,gBACX,eAAe,IACf,mBAAiD,EAAE,KAChD;CACH,MAAM,CAAC,QAAQ,iCAAsB,MAAM;CAE3C,MAAM,CAAC,OAAO,gCAAqB,aAAa;AAEhD,4BAAgB,SAAS,aAAa,EAAE,CAAC,aAAa,CAAC;CAEvD,MAAM,EAAE,UAAU,KAAM,GAAG,gEAAyB,iBAAiB,GACjE,EAAE,SAAS,kBAAkB,GAC7B;CAEJ,MAAM,iCACH,aAAmB;AAClB,MAAI,iDAAU,SAAS,CACrB,YAAW;MAEX,UAAS,SAAS;AAKpB,2CAFoB,UAAU,YAAY,CAEzB;IAEnB,CAAC,OAAO,YAAY,CACrB;AAED,4BAAgB;EACd,IAAIA,YAAmC;AAEvC,MAAI,OACF,aAAY,iBAAiB;AAC3B,aAAU,MAAM;KACf,QAAQ;AAEb,eAAa;AACX,OAAI,UAAW,cAAa,UAAU;;IAEvC,CAAC,SAAS,OAAO,CAAC;AAErB,QAAO;EACL;EACA;EACA;EACA;EACD"}