OpenSwiftUI Project
Preview OpenSwiftUI views in VS Code
OpenSwiftUI Preview 0.1.0 brings #Preview and PreviewProvider discovery to VS Code, with native macOS and iOS Simulator previews for Xcode apps and Swift library packages.
OpenSwiftUI Preview 0.1.0 is now available in the Visual Studio Marketplace. Open a Swift file, choose a preview, and run it in a native macOS window or an iOS Simulator app while you edit in VS Code.
The extension finds both #Preview and PreviewProvider declarations. It uses the project's OpenSwiftUI dependency, including binaries supplied through OpenSwiftUI-spm, and prepares the preview host outside the source project. Supported projects need no preview JSON or extra application target.
Install the extension
Install OpenSwiftUI Preview by OpenSwiftUI Project from the Marketplace. If the VS Code command-line tool is on your path, you can also run:
code --install-extension OpenSwiftUIProject.openswiftui-preview
Version 0.1.0 requires a local Mac, Xcode 26.6 / Swift 6.3, and VS Code 1.88 or later. The extension depends on the Swift extension. Install an iOS Simulator runtime if you want to preview on iOS, and open a trusted project folder so the extension can build and run its code.
Choose a preview from the current file
Start with a view in a target that depends on OpenSwiftUI:
import OpenSwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
#Preview {
ContentView()
}
#Preview("Large preview") {
ContentView()
.frame(width: 320, height: 240)
}
Open Preview in the left Activity Bar. Under Current File, these declarations appear as ContentView and Large preview. Explicit names take priority; an unnamed preview uses the root View type when it can be determined.
Select a row, or click Run Preview above a declaration. If the file belongs to more than one target, choose its owning target, then select This Mac or an iOS Simulator. The extension saves the selected file and builds the native host.
Existing PreviewProvider declarations work too:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDisplayName("Hello from PreviewProvider")
}
}
Both forms can live in the same file. The sidebar first uses names found in source. After a build, it reads the provider's compiled preview metadata, including names computed by Swift code and separate entries for multiple provider children.
Use the project's dependencies and resources
Automatic integration supports two project structures:
- Xcode applications. The extension derives a preview build from the owning app target and retains its source files, dependencies, build settings, and resource phases. Generated projects, such as Tuist projects, must already exist.
- Swift library packages. The extension creates an application host that references the original local package and its dependency graph. The target must be available through a library product.
The repository includes a DemoKit package and a DemoApp Xcode project. Both use OpenSwiftUI-spm 0.24.3 and the same ContentView. Open either example folder in VS Code to try the workflow.
For #Preview, the extension first builds with the project's original macro and reads the compiler expansion. It then adapts that expansion into a source overlay for the native host. This keeps preview access in the original module, including access to internal and private view types, without editing source files or the project definition.
Assets and package resources come from the target's build graph. The preview bypasses the normal app entry point, so provide any required models, services, or environment values inside the preview declaration.
Run, rebuild, and stop
The preview is a running native view in a separate window. Input and state updates are handled by the linked OpenSwiftUI runtime. Gesture, keyboard, and focus behavior depend on that runtime's support.
After editing code, run OpenSwiftUI: Rebuild Preview from the Command Palette. Rebuild starts a new process and resets state. Version 0.1.0 runs one preview session at a time. Use OpenSwiftUI: Stop Preview to end it, or OpenSwiftUI: Show Preview Log to inspect build output and host errors.
This release supports OpenSwiftUI on macOS and iOS Simulator. Apple SwiftUI, visionOS, Linux native previews, and remote hosts are outside its current scope. Preview traits are unsupported; provider device, layout, and orientation settings are not applied. See the extension guide for project restrictions and troubleshooting, and report issues with the selected destination and relevant Preview log output.
