Adding Components
Registry & renderer
LabScene.tsx contains a single data structure that connects component type strings to their geometry builders:
const COMPONENT_REGISTRY: Record<string, BuildFn> = {
breadboard: () => buildBreadboard(COLS),
'long-breadboard': () => buildLongBreadboard(COLS),
'xor-gate': (inst) => buildDip14(inst.mountedAt.col, 'XOR'),
resistor: (inst) => {
const r = inst as Extract<ComponentInstance, { type: 'resistor' }>;
return buildResistor(hole(r.mountedAt.col, r.mountedAt.row), ..., r.ohms);
},
diode: (inst) => { ... },
'npn-bjt': (inst) => { ... },
'n-mosfet': (inst) => { ... },
'op-amp': (inst) => { ... },
// ...
};The renderer dispatches through it:
function buildInstance(inst: ComponentInstance, all: ComponentInstance[]) {
const builder = COMPONENT_REGISTRY[inst.type];
return builder ? builder(inst, all) : null;
}If a type has no entry, buildInstance returns null and the component is silently skipped. This is how any "pending" types work — they're valid in the schema but invisible until a builder is added.
The BuildFn signature
type BuildFn = ( inst: ComponentInstance, all: ComponentInstance[], ) => THREE.Group | null;
The all parameter is the full component list. Wires need it to look up where other components are mounted (to resolve IC pin positions, for example). Most non-wire builders ignore it.
Casting safely inside a registry entry
The registry value receives ComponentInstance (the union). To access type-specific fields, cast using Extract:
'led': (inst) => {
const l = inst as Extract<ComponentInstance, { type: 'led' }>;
// ↑ now l.color and l.mountedAt are typed
return buildLed(
hole(l.mountedAt.col, l.mountedAt.row),
hole(l.mountedAt.col + 1, l.mountedAt.row),
l.color,
);
},A switch in the renderer couples every component type to a single function. The registry decouples them: each type's builder can live in a separate file, be imported independently, and be added or removed without touching any existing case. It also makes the set of renderable types machine-readable — you can iterate Object.keys(COMPONENT_REGISTRY)to know what's supported.
Wires and the two-argument form
Wires are the only type that uses the second all argument, because resolving an IC pin or passive lead requires finding the referenced component's position:
wire: (inst, all) => {
const w = inst as Extract<ComponentInstance, { type: 'wire' }>;
const from = resolvePin(w.from, all); // ← needs all
const to = resolvePin(w.to, all);
if (!from || !to) return null; // graceful: bad ref = no wire
return buildWire(from, to, w.color);
},Adding a new entry — checklist
- Type variant added to
ComponentInstanceinsrc/labs/types.ts - Geometry builder written in
src/components/<name>/and exported fromsrc/components/index.ts - Builder imported at the top of
LabScene.tsx - One new key added to
COMPONENT_REGISTRY - Optional:
Standalonevariant for the showcase card (buildXxxStandalone()) andEceComponentViewer