SwiftUI previews
Use Clerk.preview() in #Preview blocks to render Clerk-powered SwiftUI screens without launching a full app session.
Use Clerk.preview()
Inject Clerk.preview() into your preview environment:
import ClerkKit
import SwiftUI
#Preview("Signed Out") {
ContentView()
.environment(Clerk.preview { preview in
preview.isSignedIn = false
})
}
#Preview("Signed In") {
ContentView()
.environment(Clerk.preview())
}- Name
isSignedIn- Type
Bool- Description
Controls whether the preview uses signed-in or signed-out mock state. Defaults to
true.
Use ClerkEnvironment.json for realistic previews
For more realistic preview behavior, add your instance's /v1/environment response to a ClerkEnvironment.json file in your app bundle. Clerk.preview() will load it automatically.
Copy your environment response
Open the following URL in a new browser tab. Replace YOUR_FRONTEND_API_URL with your .
https://YOUR_FRONTEND_API_URL/v1/environmentThen, copy the raw JSON response body.
Create ClerkEnvironment.json
In Xcode:
- Add a new file named
ClerkEnvironment.jsonto your app project. - Paste the
/v1/environmentJSON response into the file. - Ensure your app target is selected in Target Membership so the file is included in the main bundle.
Refresh previews
Rebuild the preview canvas. Clerk.preview() will:
- Look for
ClerkEnvironment.jsoninBundle.main. - Use that environment if found and decodable.
- Fall back to
Clerk.Environment.mockif the file is missing or invalid.
Troubleshooting
- If your preview still looks generic, confirm the file name is exactly
ClerkEnvironment.jsonand that it is part of your app target. Clerk.preview()is preview-only. Outside SwiftUI previews, it returnsClerk.shared.
Feedback
Last updated on