UNPKG

circuit-bricks

Version:

A modular, Lego-style SVG circuit component system for React (ALPHA - Not for production use)

86 lines 3.92 kB
/** * useCircuit Hook * * A React hook for managing circuit state and operations. Provides a complete * state management solution for circuit components and wires, with support for * selection, wire drawing, and undo/redo functionality. * * @returns {[CircuitState, CircuitActions]} A tuple containing the current circuit state and actions to modify it * * @example * const [state, actions] = useCircuit(); * * // Add a new component * const resistorId = actions.addComponent({ * type: 'resistor', * position: { x: 100, y: 100 }, * props: { resistance: 1000 } * }); * * // Connect components with a wire * actions.addWire( * { componentId: resistorId, portId: 'left' }, * { componentId: 'battery-1', portId: 'positive' } * ); * * // Access current state * console.log(state.components, state.wires); */ import { ComponentInstance, Wire, CircuitState, Point } from '../schemas/componentSchema'; export interface CircuitActions { /** Adds a new component to the circuit and returns its generated ID */ addComponent: (component: Omit<ComponentInstance, 'id'>) => string; /** Updates properties of an existing component */ updateComponent: (id: string, updates: Partial<ComponentInstance>) => void; /** Removes a component from the circuit */ removeComponent: (id: string) => void; /** Adds a new wire between two ports and returns its generated ID */ addWire: (from: Wire['from'], to: Wire['to'], style?: Wire['style']) => string; /** Updates properties of an existing wire */ updateWire: (id: string, updates: Partial<Wire>) => void; /** Removes a wire from the circuit */ removeWire: (id: string) => void; /** Selects a component, optionally adding to the current selection */ selectComponent: (id: string, addToSelection?: boolean) => void; /** Selects a wire, optionally adding to the current selection */ selectWire: (id: string, addToSelection?: boolean) => void; /** Deselects all currently selected components and wires */ deselectAll: () => void; /** Returns an array of currently selected components */ getSelectedComponents: () => ComponentInstance[]; /** Returns an array of currently selected wires */ getSelectedWires: () => Wire[]; /** Moves a component to a new position */ moveComponent: (id: string, newPosition: Point) => void; /** Moves all currently selected components by a delta */ moveSelectedComponents: (dx: number, dy: number) => void; /** Starts the process of drawing a wire from a component port */ startWireDrawing: (componentId: string, portId: string) => void; /** Completes the wire drawing process, connecting to another port */ completeWireDrawing: (componentId: string, portId: string) => boolean; /** Cancels the wire drawing process */ cancelWireDrawing: () => void; /** Checks if a connection between two ports is valid */ isValidConnection: (fromId: string, fromPortId: string, toId: string, toPortId: string) => boolean; /** Rotates a component by the specified degrees */ rotateComponent: (id: string, degrees: number) => void; /** Rotates all currently selected components by the specified degrees */ rotateSelectedComponents: (degrees: number) => void; /** Undoes the last action */ undo: () => void; /** Redoes the last undone action */ redo: () => void; /** Checks if an undo action is possible */ canUndo: () => boolean; /** Checks if a redo action is possible */ canRedo: () => boolean; } /** * React hook for managing circuit state * * @param initialState - Optional initial state for the circuit * @returns A tuple containing the circuit state and action functions */ export declare function useCircuit(initialState?: Partial<CircuitState>): [CircuitState, CircuitActions]; export default useCircuit; //# sourceMappingURL=useCircuit.d.ts.map