diff --git a/src/components/Clipboard.tsx b/src/components/Clipboard.tsx
new file mode 100644
index 0000000..691124a
--- /dev/null
+++ b/src/components/Clipboard.tsx
@@ -0,0 +1,49 @@
+import { useCopyToClipboard } from '@wordpress/compose';
+import { useState, useEffect } from '@wordpress/element';
+import { Button } from '@wordpress/components';
+
+type CSSStyle = Record;
+
+const Clipboard = ({
+ text,
+ style,
+}: {
+ text: string;
+ style: CSSStyle;
+}): JSX.Element => {
+ const [hasCopied, setHasCopied] = useState(false);
+
+ useEffect(() => {
+ let clipTimeout: NodeJS.Timeout | null = null;
+
+ if (hasCopied) {
+ clipTimeout = setTimeout(() => {
+ setHasCopied(false);
+ }, 4000);
+ }
+
+ return () => {
+ if (clipTimeout) {
+ window.clearTimeout(clipTimeout);
+ }
+ };
+ }, [hasCopied]);
+
+ const onCopy = () => setHasCopied(true);
+
+ const clipRef = useCopyToClipboard(text, onCopy);
+
+ return (
+ <>
+
+ >
+ );
+};
+
+export default Clipboard;
diff --git a/src/components/SdkModal.tsx b/src/components/SdkModal.tsx
index b8f4145..254b20e 100644
--- a/src/components/SdkModal.tsx
+++ b/src/components/SdkModal.tsx
@@ -1,10 +1,10 @@
-import { Modal, Button } from '@wordpress/components';
+import { Modal } from '@wordpress/components';
import Snippet from './Snippet';
-import { useMemo, useState, useEffect } from '@wordpress/element';
-import { useCopyToClipboard } from '@wordpress/compose';
+import { useMemo } from '@wordpress/element';
import { Flag } from '../../types';
import { __ } from '@wordpress/i18n';
import TsSupport from './TsSupport';
+import Clipboard from './Clipboard';
interface SdkModalProps {
item: Flag;
@@ -12,34 +12,6 @@ interface SdkModalProps {
}
const SdkModal = ({ item, closeSdkModal }: SdkModalProps): JSX.Element => {
- const [hasJsCopied, setHasJsCopied] = useState(false);
- const [hasPhpCopied, setHasPhpCopied] = useState(false);
-
- useEffect(() => {
- let jsTimeout: NodeJS.Timeout | null = null;
- let phpTimeout: NodeJS.Timeout | null = null;
-
- if (hasJsCopied) {
- jsTimeout = setTimeout(() => {
- setHasJsCopied(false);
- }, 4000);
- }
-
- if (hasPhpCopied) {
- phpTimeout = setTimeout(() => {
- setHasPhpCopied(false);
- }, 4000);
- }
- return () => {
- if (jsTimeout) {
- window.clearTimeout(jsTimeout);
- }
- if (phpTimeout) {
- window.clearTimeout(phpTimeout);
- }
- };
- }, [hasJsCopied, hasPhpCopied]);
-
const jsSnippet = useMemo(() => {
return `import domReady from '@wordpress/dom-ready';
domReady(function () {
@@ -58,49 +30,35 @@ domReady(function () {
}`;
}, [item.name]);
- const jsRef = useCopyToClipboard(jsSnippet, onJsCopy);
-
- function onJsCopy() {
- setHasJsCopied(true);
- }
-
- const phpRef = useCopyToClipboard(phpSnippet, onPhpCopy);
-
- function onPhpCopy() {
- setHasPhpCopied(true);
- }
-
return (
-
+
{__('PHP Snippet', 'mr-feature-flags')}
-
{__('JavaScript Snippet', 'mr-feature-flags')}
-
diff --git a/src/components/TsSupport.tsx b/src/components/TsSupport.tsx
index cd412be..220e149 100644
--- a/src/components/TsSupport.tsx
+++ b/src/components/TsSupport.tsx
@@ -1,5 +1,6 @@
import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
+import Clipboard from './Clipboard';
import Snippet from './Snippet';
@@ -26,6 +27,15 @@ export {};`;
flags.d.ts at the
root of the plugin / theme and add below declaration.
+
);