File tree Expand file tree Collapse file tree 3 files changed +46
-36
lines changed
packages/component-library Expand file tree Collapse file tree 3 files changed +46
-36
lines changed Original file line number Diff line number Diff line change 1515 "types" : " ./dist/esm/useData/index.d.ts" ,
1616 "default" : " ./dist/esm/useData/index.js"
1717 },
18+ "./useCopy" : {
19+ "types" : " ./dist/esm/useCopy/index.d.ts" ,
20+ "default" : " ./dist/esm/useCopy/index.js"
21+ },
1822 "./useQueryParamsPagination" : {
1923 "types" : " ./dist/esm/useQueryParamsPagination/index.d.ts" ,
2024 "default" : " ./dist/esm/useQueryParamsPagination/index.js"
Original file line number Diff line number Diff line change @@ -4,13 +4,10 @@ import { Check } from "@phosphor-icons/react/dist/ssr/Check";
44import { Copy } from "@phosphor-icons/react/dist/ssr/Copy" ;
55import clsx from "clsx" ;
66import type { ComponentProps } from "react" ;
7- import { useCallback , useEffect , useState } from "react" ;
87
9- import styles from "./index.module.scss" ;
108import { Button } from "../unstyled/Button/index.jsx" ;
11- import { useLogger } from "../useLogger/index.jsx" ;
12-
13- const COPY_INDICATOR_TIME = 1000 ;
9+ import { useCopy } from "../useCopy" ;
10+ import styles from "./index.module.scss" ;
1411
1512type OwnProps = {
1613 text : string ;
@@ -30,37 +27,7 @@ export const CopyButton = ({
3027 className,
3128 ...props
3229} : Props ) => {
33- const [ isCopied , setIsCopied ] = useState ( false ) ;
34- const logger = useLogger ( ) ;
35- const copy = useCallback ( ( ) => {
36- navigator . clipboard
37- . writeText ( text )
38- . then ( ( ) => {
39- setIsCopied ( true ) ;
40- } )
41- . catch ( ( error : unknown ) => {
42- /* TODO do something here? */
43- logger . error ( error ) ;
44- } ) ;
45- } , [ text , logger ] ) ;
46-
47- useEffect ( ( ) => {
48- setIsCopied ( false ) ;
49- } , [ text ] ) ;
50-
51- useEffect ( ( ) => {
52- if ( isCopied ) {
53- const timeout = setTimeout ( ( ) => {
54- setIsCopied ( false ) ;
55- } , COPY_INDICATOR_TIME ) ;
56- return ( ) => {
57- clearTimeout ( timeout ) ;
58- } ;
59- } else {
60- return ;
61- }
62- } , [ isCopied ] ) ;
63-
30+ const { isCopied, copy } = useCopy ( text ) ;
6431 return (
6532 < Button
6633 onPress = { copy }
Original file line number Diff line number Diff line change 1+ "use client" ;
2+
3+ import { useCallback , useEffect , useState } from "react" ;
4+
5+ import { useLogger } from "../useLogger" ;
6+
7+ export const useCopy = ( text : string , copyIndicatorTime = 1000 ) => {
8+ const [ isCopied , setIsCopied ] = useState ( false ) ;
9+ const logger = useLogger ( ) ;
10+ const copy = useCallback ( ( ) => {
11+ navigator . clipboard
12+ . writeText ( text )
13+ . then ( ( ) => {
14+ setIsCopied ( true ) ;
15+ } )
16+ . catch ( ( error : unknown ) => {
17+ logger . error ( error ) ;
18+ } ) ;
19+ } , [ text , logger ] ) ;
20+
21+ useEffect ( ( ) => {
22+ setIsCopied ( false ) ;
23+ } , [ text ] ) ;
24+
25+ useEffect ( ( ) => {
26+ if ( isCopied ) {
27+ const timeout = setTimeout ( ( ) => {
28+ setIsCopied ( false ) ;
29+ } , copyIndicatorTime ) ;
30+ return ( ) => {
31+ clearTimeout ( timeout ) ;
32+ } ;
33+ } else {
34+ return ;
35+ }
36+ } , [ isCopied , copyIndicatorTime ] ) ;
37+
38+ return { isCopied, copy } ;
39+ } ;
You can’t perform that action at this time.
0 commit comments