Skip to content

Commit

Permalink
working cosmos fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Dec 10, 2024
1 parent 63d9e77 commit 8b07a55
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 19 deletions.
52 changes: 52 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.3/schema.json",
"organizeImports": {
"enabled": true
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"files": {
"ignore": ["cosmos-export", "dist", "package.json"]
},
"javascript": {
"formatter": {
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded",
"trailingCommas": "all",
"semicolons": "asNeeded",
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "off"
},
"complexity": {
"noForEach": "info"
},
"correctness": {
"useExhaustiveDependencies": "off"
},
"style": {
"noUselessElse": "off",
"noNonNullAssertion": "off",
"useNumberNamespace": "off",
"useFilenamingConvention": {
"level": "error",
"options": {
"strictCase": true,
"requireAscii": true,
"filenameCases": ["kebab-case", "export"]
}
}
}
}
}
}
Binary file modified bun.lockb
Binary file not shown.
16 changes: 16 additions & 0 deletions examples/resistor-and-capacitor.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SchematicViewer } from "lib/components/SchematicViewer"
import { renderToCircuitJson } from "lib/dev/render-to-circuit-json"

export default () => (
<div style={{ width: "100vw", height: "90vh" }}>
<SchematicViewer
circuitJson={renderToCircuitJson(
<board width="10mm" height="10mm">
<resistor name="R1" resistance={1000} schX={-2} />
<capacitor name="C1" capacitance="1uF" schX={2} />
<trace from=".R1 .pin2" to=".C1 .pin1" />
</board>,
)}
/>
</div>
)
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="/vite/main.tsx"></script>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
78 changes: 78 additions & 0 deletions lib/components/SchematicViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { useMouseMatrixTransform } from "use-mouse-matrix-transform"
import { convertCircuitJsonToSchematicSvg } from "circuit-to-svg"
import { useEffect, useMemo, useRef, useState } from "react"
import { toString as transformToString } from "transformation-matrix"

interface Props {
circuitJson: Array<{ type: string }>
}

export const SchematicViewer = ({ circuitJson }: Props) => {
const svgDivRef = useRef<HTMLDivElement>(null)
const { ref: containerRef } = useMouseMatrixTransform({
onSetTransform(transform) {
if (!svgDivRef.current) return
svgDivRef.current.style.transform = transformToString(transform)
},
})
const [containerWidth, setContainerWidth] = useState(0)
const [containerHeight, setContainerHeight] = useState(0)

useEffect(() => {
if (!containerRef.current) return

const updateDimensions = () => {
const rect = containerRef.current?.getBoundingClientRect()

setContainerWidth(rect?.width || 0)
setContainerHeight(rect?.height || 0)
}

// Set initial dimensions
updateDimensions()

// Add resize listener
const resizeObserver = new ResizeObserver(updateDimensions)
resizeObserver.observe(containerRef.current)

// Fallback to window resize
window.addEventListener("resize", updateDimensions)

return () => {
resizeObserver.disconnect()
window.removeEventListener("resize", updateDimensions)
}
}, [])

const svg = useMemo(() => {
if (!containerWidth || !containerHeight) return ""

return convertCircuitJsonToSchematicSvg(circuitJson as any, {
width: containerWidth,
height: containerHeight || 720,
})
}, [circuitJson, containerWidth, containerHeight])

return (
<div
ref={containerRef}
style={{
backgroundColor: "#F5F1ED",
overflow: "hidden",
cursor: "grab",
minHeight: "300px",
height: "100%",
}}
>
<div
ref={svgDivRef}
style={{
pointerEvents: "auto",
transformOrigin: "0 0",
}}
// biome-ignore lint/security/noDangerouslySetInnerHtml: <explanation>
dangerouslySetInnerHTML={{ __html: svg }}
/>
</div>
)
}
8 changes: 8 additions & 0 deletions lib/dev/render-to-circuit-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Core from "@tscircuit/core"
console.log(Core)

export const renderToCircuitJson = (board: React.ReactElement) => {
const circuit = new Core.Circuit()
circuit.add(board)
return circuit.getCircuitJson()
}
1 change: 0 additions & 1 deletion lib/test.fixture.tsx

This file was deleted.

13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@
"build:site": "cosmos-export"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@tscircuit/core": "^0.0.222",
"@types/bun": "latest",
"@types/react": "^19.0.1",
"@types/react-dom": "^19.0.2",
"@vitejs/plugin-react": "^4.3.4",
"react": "^19.0.0",
"circuit-json": "^0.0.111",
"react": "18",
"react-cosmos": "^6.2.1",
"react-cosmos-plugin-vite": "^6.2.0",
"react-dom": "^19.0.0",
"react-dom": "18",
"tsup": "^8.3.5",
"vite": "^6.0.3"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"circuit-to-svg": "^0.0.90"
"circuit-to-svg": "^0.0.90",
"debug": "^4.4.0",
"performance-now": "^2.1.0",
"react-reconciler": "^0.31.0",
"use-mouse-matrix-transform": "^1.1.13"
}
}
20 changes: 20 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react"
import { createRoot } from "react-dom/client"
import { renderToCircuitJson } from "lib/dev/render-to-circuit-json"
import { SchematicViewer } from "lib/components/SchematicViewer"

const root = createRoot(document.getElementById("root")!)

root.render(
<React.StrictMode>
<div style={{ width: "100vw", height: "100vh" }}>
<SchematicViewer
circuitJson={renderToCircuitJson(
<board width="10mm" height="10mm">
<resistor name="R1" resistance={1000} />
</board>,
)}
/>
</div>
</React.StrictMode>,
)
Empty file added test.ts
Empty file.
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"target": "ES2020",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
Expand All @@ -12,6 +12,7 @@
"paths": {
"lib/*": ["lib/*"]
},
"esModuleInterop": true,

// Bundler mode
"moduleResolution": "bundler",
Expand Down
9 changes: 6 additions & 3 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { defineConfig } from "vite"
import react from "@vitejs/plugin-react"
import { resolve } from "path"
import { resolve } from "node:path"

export default defineConfig({
plugins: [react()],
resolve: {
alias: {
lib: resolve(__dirname, "./lib")
}
lib: resolve(__dirname, "./lib"),
},
},
define: {
global: {},
},
})
10 changes: 0 additions & 10 deletions vite/main.tsx

This file was deleted.

0 comments on commit 8b07a55

Please sign in to comment.