{"version":3,"file":"index.cjs","names":["useRef","useCopyToClipboard","Button"],"sources":["../../src/clipboard-button/index.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport { useRef, useEffect } from '@wordpress/element';\nimport { useCopyToClipboard } from '@wordpress/compose';\nimport deprecated from '@wordpress/deprecated';\nimport Button from '../button';\nimport type { ClipboardButtonProps } from './types';\nimport type { WordPressComponentProps } from '../context';\n\nconst TIMEOUT = 4000;\n\nexport default function ClipboardButton( {\n\tclassName,\n\tchildren,\n\tonCopy,\n\tonFinishCopy,\n\ttext,\n\t...buttonProps\n}: WordPressComponentProps< ClipboardButtonProps, 'button', false > ) {\n\tdeprecated( 'wp.components.ClipboardButton', {\n\t\tsince: '5.8',\n\t\talternative: 'wp.compose.useCopyToClipboard',\n\t} );\n\n\tconst timeoutIdRef = useRef< ReturnType< typeof setTimeout > >( undefined );\n\tconst ref = useCopyToClipboard( text, () => {\n\t\tonCopy();\n\t\tif ( timeoutIdRef.current ) {\n\t\t\tclearTimeout( timeoutIdRef.current );\n\t\t}\n\n\t\tif ( onFinishCopy ) {\n\t\t\ttimeoutIdRef.current = setTimeout( () => onFinishCopy(), TIMEOUT );\n\t\t}\n\t} );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tif ( timeoutIdRef.current ) {\n\t\t\t\tclearTimeout( timeoutIdRef.current );\n\t\t\t}\n\t\t};\n\t}, [] );\n\n\tconst classes = clsx( 'components-clipboard-button', className );\n\n\t// Workaround for inconsistent behavior in Safari, where <textarea> is not\n\t// the document.activeElement at the moment when the copy event fires.\n\t// This causes documentHasSelection() in the copy-handler component to\n\t// mistakenly override the ClipboardButton, and copy a serialized string\n\t// of the current block instead.\n\tconst focusOnCopyEventTarget: React.ClipboardEventHandler = ( event ) => {\n\t\t// @ts-expect-error: Should be currentTarget, but not changing because this component is deprecated.\n\t\tevent.target.focus();\n\t};\n\n\treturn (\n\t\t// Disable reasons: the parent component takes care of the __next40pxDefaultSize prop.\n\t\t// eslint-disable-next-line @wordpress/components-no-missing-40px-size-prop\n\t\t<Button\n\t\t\t{ ...buttonProps }\n\t\t\tclassName={ classes }\n\t\t\tref={ ref }\n\t\t\tonCopy={ focusOnCopyEventTarget }\n\t\t>\n\t\t\t{ children }\n\t\t</Button>\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,MAAM,UAAU;AAEhB,SAAwB,gBAAiB,EACxC,WACA,UACA,QACA,cACA,MACA,GAAG,eACkE;CACrE,CAAA,GAAA,sBAAA,QAAA,CAAY,iCAAiC;EAC5C,OAAO;EACP,aAAa;CACd,CAAE;CAEF,MAAM,gBAAA,GAAeA,mBAAAA,OAAAA,CAA2C,KAAA,CAAU;CAC1E,MAAM,OAAA,GAAMC,mBAAAA,mBAAAA,CAAoB,YAAY;EAC3C,OAAO;EACP,IAAK,aAAa,SACjB,aAAc,aAAa,OAAQ;EAGpC,IAAK,cACJ,aAAa,UAAU,iBAAkB,aAAa,GAAG,OAAQ;CAEnE,CAAE;CAEF,CAAA,GAAA,mBAAA,UAAA,OAAiB;EAChB,aAAa;GACZ,IAAK,aAAa,SACjB,aAAc,aAAa,OAAQ;EAErC;CACD,GAAG,CAAC,CAAE;CAEN,MAAM,WAAA,GAAU,KAAA,QAAA,CAAM,+BAA+B,SAAU;CAO/D,MAAM,0BAAwD,UAAW;EAExE,MAAM,OAAO,MAAM;CACpB;CAEA,OAGC,iBAAA,GAAA,kBAAA,IAAA,CAACC,oBAAAA,SAAD;EACC,GAAK;EACL,WAAY;EACN;EACN,QAAS;EAEP;CACK,CAAA;AAEV"}