diff --git a/examples/array-based-subtraction.fixture.tsx b/examples/array-based-subtraction.fixture.tsx index 2fe1d9b..88f0cf5 100644 --- a/examples/array-based-subtraction.fixture.tsx +++ b/examples/array-based-subtraction.fixture.tsx @@ -1,13 +1,16 @@ -import { JsCadFixture } from "../lib/components/jscad-fixture" -import { Colorize, Cuboid, Sphere, Subtract } from "../lib/jscad-fns" +import Example from "src/Example"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; +import { Cuboid, Subtract } from "../lib/jscad-fns"; export default () => ( - - - - {[-4, -2, 0, 2, 4].map((value, index) => ( - - ))} - - -) + + + + + {[-4, -2, 0, 2, 4].map((value, index) => ( + + ))} + + + +); diff --git a/examples/circle.fixture.tsx b/examples/circle.fixture.tsx index 67afa3a..f13b336 100644 --- a/examples/circle.fixture.tsx +++ b/examples/circle.fixture.tsx @@ -1,8 +1,11 @@ -import { Circle } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Circle } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/colorize.fixture.tsx b/examples/colorize.fixture.tsx index ad8897a..1787634 100644 --- a/examples/colorize.fixture.tsx +++ b/examples/colorize.fixture.tsx @@ -1,10 +1,13 @@ -import { JsCadFixture } from "../lib/components/jscad-fixture" -import { Colorize, Cube } from "../lib/jscad-fns" +import Example from "src/Example"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; +import { Colorize, Cube } from "../lib/jscad-fns"; export default () => ( - - - - - -) + + + + + + + +); diff --git a/examples/cone.fixture.tsx b/examples/cone.fixture.tsx index 7fe82a5..098bb14 100644 --- a/examples/cone.fixture.tsx +++ b/examples/cone.fixture.tsx @@ -1,17 +1,20 @@ -import { CylinderElliptic } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { CylinderElliptic } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/cube.fixture.tsx b/examples/cube.fixture.tsx index 65ee63f..969cc45 100644 --- a/examples/cube.fixture.tsx +++ b/examples/cube.fixture.tsx @@ -1,8 +1,11 @@ -import { Cube } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Cube } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/cuboid.fixture.tsx b/examples/cuboid.fixture.tsx index f075400..10b7df9 100644 --- a/examples/cuboid.fixture.tsx +++ b/examples/cuboid.fixture.tsx @@ -1,8 +1,11 @@ -import { Cuboid } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Cuboid } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/custom-sphere.fixture.tsx b/examples/custom-sphere.fixture.tsx index e8f8a9a..69492cb 100644 --- a/examples/custom-sphere.fixture.tsx +++ b/examples/custom-sphere.fixture.tsx @@ -1,8 +1,11 @@ -import { Sphere } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Sphere } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/custom-torus.fixture.tsx b/examples/custom-torus.fixture.tsx index a402c39..8c2ad58 100644 --- a/examples/custom-torus.fixture.tsx +++ b/examples/custom-torus.fixture.tsx @@ -1,16 +1,19 @@ -import { Torus } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Torus } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/custom.fixture.tsx b/examples/custom.fixture.tsx index 25a00e1..adcbf4b 100644 --- a/examples/custom.fixture.tsx +++ b/examples/custom.fixture.tsx @@ -1,14 +1,17 @@ -import { booleans, primitives } from "@jscad/modeling" -import { Custom } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import { booleans, primitives } from "@jscad/modeling"; +import Example from "src/Example"; +import { Custom } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; -const cube = primitives.cube({ size: 10 }) -const sphere = primitives.sphere({ radius: 6, segments: 32 }) +const cube = primitives.cube({ size: 10 }); +const sphere = primitives.sphere({ radius: 6, segments: 32 }); -const intersected = booleans.subtract(cube, sphere) +const intersected = booleans.subtract(cube, sphere); export default () => ( - - - -) + + + + + +); diff --git a/examples/cylinder.fixture.tsx b/examples/cylinder.fixture.tsx index 973fe2a..793f3a6 100644 --- a/examples/cylinder.fixture.tsx +++ b/examples/cylinder.fixture.tsx @@ -1,15 +1,18 @@ -import { Cylinder } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Cylinder } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - - -) + + + + + + +); diff --git a/examples/ellipsoid.fixture.tsx b/examples/ellipsoid.fixture.tsx index 3c4aab3..25050b7 100644 --- a/examples/ellipsoid.fixture.tsx +++ b/examples/ellipsoid.fixture.tsx @@ -1,8 +1,11 @@ -import { Ellipsoid } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Ellipsoid } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/extrude-from-slices.fixture.tsx b/examples/extrude-from-slices.fixture.tsx index 64b30b7..c9f510e 100644 --- a/examples/extrude-from-slices.fixture.tsx +++ b/examples/extrude-from-slices.fixture.tsx @@ -1,56 +1,59 @@ -import { ExtrudeFromSlices } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" -import jscad from "@jscad/modeling" +import jscad from "@jscad/modeling"; +import Example from "src/Example"; +import { ExtrudeFromSlices } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; -const { bezier } = jscad.curves +const { bezier } = jscad.curves; const { circle, line, polygon, rectangle, roundedRectangle, star } = - jscad.primitives + jscad.primitives; const { extrudeLinear, extrudeRotate, extrudeFromSlices, slice } = - jscad.extrusions -const { mat4 } = jscad.maths + jscad.extrusions; +const { mat4 } = jscad.maths; const baseSlice = slice.fromPoints([ [2, 2], [-2, 2], [-2, -2], [2, -2], -]) +]); -const xCurve = bezier.create([1, 1.8, 0.4, 1]) -const yCurve = bezier.create([1, 1.8, 0.5]) +const xCurve = bezier.create([1, 1.8, 0.4, 1]); +const yCurve = bezier.create([1, 1.8, 0.5]); export default () => ( - - + + - -) + return newslice; + }} + /> + + +); diff --git a/examples/extrude-linear.fixture.tsx b/examples/extrude-linear.fixture.tsx index be9fcc5..1d8744a 100644 --- a/examples/extrude-linear.fixture.tsx +++ b/examples/extrude-linear.fixture.tsx @@ -1,21 +1,24 @@ -import { JsCadFixture } from "../lib/components/jscad-fixture" -import { ExtrudeLinear } from "../lib/jscad-fns" -import { Polygon } from "../lib/jscad-fns/polygon" +import Example from "src/Example"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; +import { ExtrudeLinear } from "../lib/jscad-fns"; +import { Polygon } from "../lib/jscad-fns/polygon"; export default () => ( - - - - - -) + + + + + + + +); diff --git a/examples/extrude-rectangular.fixture.tsx b/examples/extrude-rectangular.fixture.tsx index 03876ca..cb9153c 100644 --- a/examples/extrude-rectangular.fixture.tsx +++ b/examples/extrude-rectangular.fixture.tsx @@ -1,26 +1,29 @@ -import { JsCadFixture } from "../lib/components/jscad-fixture" -import { ExtrudeRectangular } from "../lib/jscad-fns" -import { Polygon } from "../lib/jscad-fns/polygon" +import Example from "src/Example"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; +import { ExtrudeRectangular } from "../lib/jscad-fns"; +import { Polygon } from "../lib/jscad-fns/polygon"; export default () => ( - - - - - -) + + + + + + + +); diff --git a/examples/high-geodesic-sphere.fixture.tsx b/examples/high-geodesic-sphere.fixture.tsx index 902b203..25ca143 100644 --- a/examples/high-geodesic-sphere.fixture.tsx +++ b/examples/high-geodesic-sphere.fixture.tsx @@ -1,13 +1,16 @@ -import { GeodesicSphere } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { GeodesicSphere } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/hull-chain.fixture.tsx b/examples/hull-chain.fixture.tsx index 6e6c048..e82f54c 100644 --- a/examples/hull-chain.fixture.tsx +++ b/examples/hull-chain.fixture.tsx @@ -1,12 +1,15 @@ -import { Cuboid, Ellipsoid, HullChain } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Cuboid, Ellipsoid, HullChain } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - - - - - -) + + + + + + + + + +); diff --git a/examples/hull.fixture.tsx b/examples/hull.fixture.tsx index 488cb2d..2313261 100644 --- a/examples/hull.fixture.tsx +++ b/examples/hull.fixture.tsx @@ -1,11 +1,14 @@ -import { Cuboid, Ellipsoid, Hull, Translate } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Cuboid, Ellipsoid, Hull } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - - - - -) + + + + + + + + +); diff --git a/examples/low-geodesic-sphere.fixture.tsx b/examples/low-geodesic-sphere.fixture.tsx index 647d09e..a94ea1d 100644 --- a/examples/low-geodesic-sphere.fixture.tsx +++ b/examples/low-geodesic-sphere.fixture.tsx @@ -1,8 +1,11 @@ -import { GeodesicSphere } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { GeodesicSphere } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/partial-cylindrical-elliptic.fixture.tsx b/examples/partial-cylindrical-elliptic.fixture.tsx index 84bb8f6..0614301 100644 --- a/examples/partial-cylindrical-elliptic.fixture.tsx +++ b/examples/partial-cylindrical-elliptic.fixture.tsx @@ -1,15 +1,18 @@ -import { CylinderElliptic } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { CylinderElliptic } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/polygon.fixture.tsx b/examples/polygon.fixture.tsx index d49d870..7441711 100644 --- a/examples/polygon.fixture.tsx +++ b/examples/polygon.fixture.tsx @@ -1,18 +1,21 @@ -import { JsCadFixture } from "../lib/components/jscad-fixture" -import { Polygon } from "../lib/jscad-fns/polygon" +import Example from "src/Example"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; +import { Polygon } from "../lib/jscad-fns/polygon"; export default () => ( - - - -) + + + + + +); diff --git a/examples/project.fixture.tsx b/examples/project.fixture.tsx index 22d7b1d..e5a5288 100644 --- a/examples/project.fixture.tsx +++ b/examples/project.fixture.tsx @@ -1,21 +1,24 @@ -import { JsCadFixture } from "../lib/components/jscad-fixture" -import { Project } from "../lib/jscad-fns" -import { Polygon } from "../lib/jscad-fns/polygon" +import Example from "src/Example"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; +import { Project } from "../lib/jscad-fns"; +import { Polygon } from "../lib/jscad-fns/polygon"; export default () => ( - - - - - -) + + + + + + + +); diff --git a/examples/rectangle.fixture.tsx b/examples/rectangle.fixture.tsx index f8643ce..47b6de0 100644 --- a/examples/rectangle.fixture.tsx +++ b/examples/rectangle.fixture.tsx @@ -1,8 +1,11 @@ -import { Rectangle } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Rectangle } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/rotate.fixture.tsx b/examples/rotate.fixture.tsx index 27fe756..3f5a3ea 100644 --- a/examples/rotate.fixture.tsx +++ b/examples/rotate.fixture.tsx @@ -1,21 +1,24 @@ -import { Polygon } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" -import { Rotate } from "../lib/jscad-fns" +import Example from "src/Example"; +import { Polygon } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; +import { Rotate } from "../lib/jscad-fns"; export default () => ( - - - - - -) + + + + + + + +); diff --git a/examples/rounded-cuboid.fixture.tsx b/examples/rounded-cuboid.fixture.tsx index 2666fa5..3ca5e67 100644 --- a/examples/rounded-cuboid.fixture.tsx +++ b/examples/rounded-cuboid.fixture.tsx @@ -1,13 +1,16 @@ -import { RoundedCuboid } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { RoundedCuboid } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/rounded-cylinder.fixture.tsx b/examples/rounded-cylinder.fixture.tsx index 5b5948a..59cebfd 100644 --- a/examples/rounded-cylinder.fixture.tsx +++ b/examples/rounded-cylinder.fixture.tsx @@ -1,14 +1,17 @@ -import { RoundedCylinder } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { RoundedCylinder } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/sphere.fixture.tsx b/examples/sphere.fixture.tsx index 35fa24b..2b0b56d 100644 --- a/examples/sphere.fixture.tsx +++ b/examples/sphere.fixture.tsx @@ -1,8 +1,11 @@ -import { Sphere } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Sphere } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/subtract.fixture.tsx b/examples/subtract.fixture.tsx index 3d27252..8d8afc5 100644 --- a/examples/subtract.fixture.tsx +++ b/examples/subtract.fixture.tsx @@ -1,11 +1,14 @@ -import { Cube, Sphere, Subtract } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Cube, Sphere, Subtract } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - - - - -) + + + + + + + + +); diff --git a/examples/torus.fixture.tsx b/examples/torus.fixture.tsx index a26d1b8..b4a8f0d 100644 --- a/examples/torus.fixture.tsx +++ b/examples/torus.fixture.tsx @@ -1,8 +1,16 @@ -import { Torus } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { Torus } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/examples/translate.fixture.tsx b/examples/translate.fixture.tsx index 1556fec..761eaf1 100644 --- a/examples/translate.fixture.tsx +++ b/examples/translate.fixture.tsx @@ -1,26 +1,29 @@ -import { Cuboid } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" -import { Translate } from "../lib/jscad-fns" +import Example from "src/Example"; +import { Cuboid } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; +import { Translate } from "../lib/jscad-fns"; export default () => ( - - - - - - - - + + + + - - - - - + + + + + + + + + + + - - -) + + +); diff --git a/examples/twisted-cylindrical-elliptic.fixture.tsx b/examples/twisted-cylindrical-elliptic.fixture.tsx index dbd78b4..1e9705f 100644 --- a/examples/twisted-cylindrical-elliptic.fixture.tsx +++ b/examples/twisted-cylindrical-elliptic.fixture.tsx @@ -1,15 +1,18 @@ -import { CylinderElliptic } from "../lib" -import { JsCadFixture } from "../lib/components/jscad-fixture" +import Example from "src/Example"; +import { CylinderElliptic } from "../lib"; +import { JsCadFixture } from "../lib/components/jscad-fixture"; export default () => ( - - - -) + + + + + +); diff --git a/package.json b/package.json index 1147e38..7c4720b 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,10 @@ }, "dependencies": { "color": "^4.2.3", - "react-reconciler": "^0.29.2" + "react-element-to-jsx-string": "^15.0.0", + "react-icons": "^5.3.0", + "react-reconciler": "^0.29.2", + "react-syntax-highlighter": "^15.6.1" }, "devDependencies": { "@biomejs/biome": "^1.9.3", diff --git a/src/CodeViewer.tsx b/src/CodeViewer.tsx new file mode 100644 index 0000000..681c323 --- /dev/null +++ b/src/CodeViewer.tsx @@ -0,0 +1,72 @@ +import React, { useState } from "react"; +import { FiCopy } from "react-icons/fi"; +import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; +import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism"; + +interface CodeViewerProps { + code: string; +} + +const CodeViewer: React.FC = ({ 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; +}