Skip to content

feat(pcb-viewer): add controlled viewState API, deprecate initialState, and ship full all-features controlled fixture#695

Open
rushabhcodes wants to merge 2 commits intotscircuit:mainfrom
rushabhcodes:feat/controlled-view-state-api
Open

feat(pcb-viewer): add controlled viewState API, deprecate initialState, and ship full all-features controlled fixture#695
rushabhcodes wants to merge 2 commits intotscircuit:mainfrom
rushabhcodes:feat/controlled-view-state-api

Conversation

@rushabhcodes
Copy link
Copy Markdown
Contributor

@rushabhcodes rushabhcodes commented Mar 12, 2026

This pull request introduces a new controlled view state API for the PCBViewer component, enabling external control and synchronization of viewer state such as layer selection and visibility toggles. The new approach deprecates the previous initialState prop in favor of a more robust viewState/onViewStateChange pattern, and includes utilities for mapping between internal and public state representations. Additionally, it provides a fixture example demonstrating the new controlled state usage and improves type safety and code clarity throughout the codebase.

https://pcb-viewer-e9onzgtj6-tscircuit.vercel.app/?fixture=%7B%22path%22%3A%22src%2Fexamples%2F2026%2Fcontrolled-view-state.fixture.tsx%22%7D

Controlled View State API:

  • Added a new viewState prop and onViewStateChange callback to PCBViewer, allowing parent components to control and react to viewer state changes (layer, visibility toggles, etc.). The previous initialState prop is now deprecated, with a warning in development mode. (src/PCBViewer.tsx, README.md) [1] [2] [3] [4] [5]
  • Implemented mapping utilities to convert between public-facing PCBViewerViewState and internal ControlledViewState representations, ensuring seamless synchronization between external and internal state. (src/PCBViewer.tsx, src/global-store.ts) [1] [2]

Context and Store Synchronization:

  • Updated ContextProviders to support controlled state, synchronizing internal store state with external controlledViewState and propagating changes back via onControlledViewStateChange. This ensures two-way binding between parent and viewer. (src/components/ContextProviders.tsx)

Examples and Documentation:

  • Added a new fixture example (ControlledViewStateFixture) to demonstrate usage of the controlled view state API, including UI controls for toggling view options and observing state synchronization. (src/examples/2026/controlled-view-state.fixture.tsx)
  • Refactored the "all features" fixture to expose a reusable function for generating circuit JSON, improving testability and reusability. (src/examples/all-features.fixture.tsx) [1] [2]

Type Safety and Code Quality:

  • Improved type safety by refining imports and type annotations, and added error handling to ensure hooks are used within the correct context. (src/PCBViewer.tsx, src/global-store.ts) [1] [2]

These changes collectively modernize the PCBViewer API, making it more flexible and easier to integrate with external state management in React applications.

…e, and ship full all-features controlled fixture
Copilot AI review requested due to automatic review settings March 12, 2026 11:10
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 12, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
pcb-viewer Ready Ready Preview, Comment Mar 12, 2026 5:05pm

Request Review

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a controlled viewState / onViewStateChange API to PCBViewer to support external synchronization of layer + visibility toggles, while deprecating the previous initialState prop. This is supported by new mapping utilities and store synchronization logic, plus an end-to-end fixture demonstrating a fully controlled setup.

Changes:

  • Introduces PCBViewerViewState, mapping utilities, and new viewState / onViewStateChange props (with a dev warning for deprecated initialState).
  • Updates ContextProviders + global store to support syncing a controlled subset of viewer state to/from the Zustand store.
  • Adds a new controlled fixture and refactors the all-features fixture to export reusable circuit-json generation.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/PCBViewer.tsx Adds the public controlled view state API and bridges it into internal store state.
src/components/ContextProviders.tsx Implements two-way sync between controlled props and the Zustand store.
src/global-store.ts Exposes a ControlledViewState subset type and helper, and tightens useGlobalStore runtime safety.
src/examples/all-features.fixture.tsx Extracts getAllFeaturesCircuitJson() for reuse by other fixtures.
src/examples/2026/controlled-view-state.fixture.tsx New fixture demonstrating a fully controlled PCBViewer view state.
README.md Documents the new controlled props and deprecates initialState.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@seveibar seveibar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're not deprecating initialState

didn't DOPE introduce a context control pattern?

also lots of nonsense in this PR like queueMicrotask etc.

…pdate ContextProviders to handle state setting more robustly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants