Class ComponentPreview<T>.PreviewBuilder
Builder for configuring a preview's parameters and services.
Inheritance
ComponentPreview<T>.PreviewBuilder
Assembly: BlazorToolkit.Previews.dll
public class ComponentPreview<T>.PreviewBuilder
Methods
Registers additional services into the render container — the escape hatch for component
libraries (e.g. .ConfigureServices(s => s.AddMudServices())).
Declaration
public ComponentPreview<T>.PreviewBuilder ConfigureServices(Action<IServiceCollection> configure)
Parameters
Returns
Injects a service implementation for this specific preview.
This overrides any host-level service registration.
Declaration
public ComponentPreview<T>.PreviewBuilder Inject<TService>(TService implementation) where TService : class
Parameters
| Type |
Name |
Description |
| TService |
implementation |
The service implementation to inject.
|
Returns
Type Parameters
| Name |
Description |
| TService |
The service type.
|
Declaration
public ComponentPreview<T>.PreviewBuilder Mock<TService>(Action<IStub<TService>> configure) where TService : class
Parameters
| Type |
Name |
Description |
| Action<IStub<TService>> |
configure |
|
Returns
Type Parameters
| Name |
Description |
| TService |
|
.Mock<IWeatherService>(s => s.SetupAsync(x => x.GetTempAsync(), 78))
Renders this preview in a live Blazor Server circuit (prerender off) — @onclick, real JS
interop (including from OnInitializedAsync), and OnAfterRender all run. This is the
default; call it to override a IPreviewHost that defaulted the project to static.
Declaration
public ComponentPreview<T>.PreviewBuilder RenderInteractive()
Returns
Renders this preview as a single static SSR pass (no circuit): no interaction and JS-painted content
is absent, but it shows the true server-rendered snapshot and [StreamRendering]. Use it to
demonstrate SSR-only behavior, or for a cheaper flicker-free render of a pure-markup component.
Declaration
public ComponentPreview<T>.PreviewBuilder RenderStatic()
Returns
Adds one or more base folders for resolving CSS asset references for this preview only, layered on
top of the project's IPreviewHost defaults. Folders may be project-relative, escape
the project (..</code>), or be absolute.
Declaration
public ComponentPreview<T>.PreviewBuilder WithAssetRoots(params string[] folders)
Parameters
| Type |
Name |
Description |
| string[] |
folders |
|
Returns
Renders the preview as an authenticated user, so AuthorizeView/[Authorize] show
their authorized content. Without this the preview is anonymous.
Declaration
public ComponentPreview<T>.PreviewBuilder WithAuthenticatedUser(string name, params string[] roles)
Parameters
| Type |
Name |
Description |
| string |
name |
The user's display/identity name.
|
| string[] |
roles |
Optional roles for role-based authorization.
|
Returns
Supplies a cascading value to the component (matches [CascadingParameter]).
Declaration
public ComponentPreview<T>.PreviewBuilder WithCascadingValue<TValue>(TValue value, string? name = null, bool isFixed = false)
Parameters
| Type |
Name |
Description |
| TValue |
value |
The value to cascade.
|
| string |
name |
Optional cascade name for [CascadingParameter(Name = ...)].
|
| bool |
isFixed |
Whether the cascade never changes (renderer optimization).
|
Returns
Type Parameters
WithChildContent(RenderFragment, string)
Sets child content from a render fragment.
Declaration
public ComponentPreview<T>.PreviewBuilder WithChildContent(RenderFragment fragment, string parameterName = "ChildContent")
Parameters
Returns
WithChildContent(string, string)
Sets child content from a raw HTML/markup string.
Declaration
public ComponentPreview<T>.PreviewBuilder WithChildContent(string markup, string parameterName = "ChildContent")
Parameters
Returns
WithChildContent<TChild>(string)
Sets child content to a single nested child component.
Declaration
public ComponentPreview<T>.PreviewBuilder WithChildContent<TChild>(string parameterName = "ChildContent") where TChild : IComponent
Parameters
| Type |
Name |
Description |
| string |
parameterName |
|
Returns
Type Parameters
| Name |
Description |
| TChild |
The child component type to render.
|
Configures routed stub HTTP responses for components that call more than one endpoint — match by
method and path, with a fallback Default. Requests that match no rule and have no default get a
benign 200. Inject a real HttpClient instead if you want real network behavior.
Declaration
public ComponentPreview<T>.PreviewBuilder WithHttpResponse(Action<IHttpMockBuilder> configure)
Parameters
Returns
.WithHttpResponse(r => r
.OnGet("/api/weather", res => res.WithJson(forecastJson))
.OnPost("/api/orders", res => res.WithJson("""{"id": 42}"""))
.Default(res => res.WithStatus(404)))
Sets the canned response the preview host's stub HttpClient returns for every request
(for components that inject HttpClient directly).
Declaration
public ComponentPreview<T>.PreviewBuilder WithHttpResponse(string content, string mediaType = "application/json", int statusCode = 200)
Parameters
Returns
Wraps the previewed component in a layout, rendering it as the layout's Body — the idiomatic
way to place the component inside app chrome, a state/context provider, or several nested providers
authored as a normal layout. Overrides any UseLayout default the IPreviewHost
declares, for this preview only.
Declaration
public ComponentPreview<T>.PreviewBuilder WithLayout<TLayout>() where TLayout : LayoutComponentBase
Returns
Type Parameters
| Name |
Description |
| TLayout |
The layout component (a LayoutComponentBase) to wrap with.
|
Sets the current location so NavigationManager.Uri and query-string reads reflect it,
e.g. .WithLocation("/orders?tab=open").
Declaration
public ComponentPreview<T>.PreviewBuilder WithLocation(string relativeUri)
Parameters
| Type |
Name |
Description |
| string |
relativeUri |
|
Returns
Sets a parameter value for the preview.
Declaration
public ComponentPreview<T>.PreviewBuilder WithParameter<TValue>(Expression<Func<T, TValue>> propertySelector, TValue value)
Parameters
| Type |
Name |
Description |
| Expression<Func<T, TValue>> |
propertySelector |
Expression selecting the parameter property.
|
| TValue |
value |
The value to set.
|
Returns
Type Parameters
| Name |
Description |
| TValue |
The parameter type.
|
Adds one or more scripts for this preview only, layered on top of the project's
IPreviewHost defaults. Paths may be project-relative, escape the project
(..</code>), or be absolute.
Declaration
public ComponentPreview<T>.PreviewBuilder WithScripts(params string[] paths)
Parameters
| Type |
Name |
Description |
| string[] |
paths |
|
Returns
Adds one or more stylesheets for this preview only, layered on top of the project's
IPreviewHost defaults. Paths may be project-relative, escape the project
(..</code>), or be absolute.
Declaration
public ComponentPreview<T>.PreviewBuilder WithStylesheets(params string[] paths)
Parameters
| Type |
Name |
Description |
| string[] |
paths |
|
Returns
Renders the preview as a specific ClaimsPrincipal for full control over identity,
roles, and claims.
Declaration
public ComponentPreview<T>.PreviewBuilder WithUser(ClaimsPrincipal user)
Parameters
Returns