= ({ code }) => {
+ const [copySuccess, setCopySuccess] = useState(false);
+
+ const handleCopy = async () => {
+ try {
+ await navigator.clipboard.writeText(code);
+ setCopySuccess(true);
+ setTimeout(() => setCopySuccess(false), 2000);
+ } catch (err) {
+ console.error("Failed to copy code: ", err);
+ }
+ };
+
+ return (
+
+
+ {code}
+
+
+ {copySuccess && (
+
+ Copied!
+
+ )}
+
+ );
+};
+
+export default CodeViewer;
diff --git a/src/Example.tsx b/src/Example.tsx
new file mode 100644
index 0000000..6879892
--- /dev/null
+++ b/src/Example.tsx
@@ -0,0 +1,36 @@
+import React, { useState } from "react";
+import jsxToString from "react-element-to-jsx-string";
+import CodeViewer from "./CodeViewer";
+import "./main.css";
+
+interface ExampleProps {
+ children: React.ReactNode;
+}
+
+const Example: React.FC = ({ children }) => {
+ const [showCode, setShowCode] = useState(false);
+
+ const handleToggleCode = () => setShowCode(!showCode);
+
+ // Convert children to JSX string
+ const designCode = jsxToString(children, {
+ useFunctionCode: true,
+ functionNameOnly: true,
+ });
+
+ return (
+
+ {children}
+
+ {showCode && (
+
+
+
+ )}
+
+ );
+};
+
+export default Example;
diff --git a/src/main.css b/src/main.css
index 4dc4eeb..aea7347 100644
--- a/src/main.css
+++ b/src/main.css
@@ -3,3 +3,39 @@ body {
margin: 0;
padding: 0;
}
+
+.example-container {
+ position: relative; /* This allows absolute positioning of the button and code viewer */
+}
+
+.toggle-button {
+ position: absolute;
+ top: 10px; /* Position from the top */
+ left: 10px; /* Position from the left */
+ background-color: #3b82f6; /* Blue color */
+ color: white;
+ padding: 8px 16px;
+ border-radius: 8px;
+ border: none;
+ cursor: pointer;
+ transition: background-color 0.3s ease;
+ z-index: 10; /* Ensure the button stays above other content */
+}
+
+.toggle-button:hover {
+ background-color: #2563eb; /* Darker blue on hover */
+}
+
+.code-viewer {
+ position: absolute;
+ top: 20px;
+ left: 10px;
+ right: 10px;
+ bottom: 10px;
+ background-color: black;
+ padding: 16px;
+ border-radius: 8px;
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
+ overflow-y: auto;
+ height: 40vh;
+}