Class ComponentPreview<T>.PreviewBuilder

Builder for configuring a preview's parameters and services.

Inheritance
ComponentPreview<T>.PreviewBuilder
Namespace: BlazorToolkit.Previews
Assembly: BlazorToolkit.Previews.dll
Syntax
public class ComponentPreview<T>.PreviewBuilder

Methods

ConfigureServices(Action<IServiceCollection>)

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
Type Name Description
Action<IServiceCollection> configure
Returns
Type Description
ComponentPreview<T>.PreviewBuilder

Inject<TService>(TService)

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 Description
ComponentPreview<T>.PreviewBuilder

This builder for chaining.

Type Parameters
Name Description
TService

The service type.

Mock<TService>(Action<IStub<TService>>)

Provides a lightweight stub for a service interface. Configure return values via Setup<TResult>(Expression<Func<TService, TResult>>, TResult) / SetupAsync; unconfigured members return safe defaults. Interfaces only — use Inject<TService>(TService) with a real subclass for abstract classes.

Declaration
public ComponentPreview<T>.PreviewBuilder Mock<TService>(Action<IStub<TService>> configure) where TService : class
Parameters
Type Name Description
Action<IStub<TService>> configure
Returns
Type Description
ComponentPreview<T>.PreviewBuilder
Type Parameters
Name Description
TService
Examples
.Mock<IWeatherService>(s => s.SetupAsync(x => x.GetTempAsync(), 78))

RenderInteractive()

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
Type Description
ComponentPreview<T>.PreviewBuilder

RenderStatic()

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
Type Description
ComponentPreview<T>.PreviewBuilder

WithAssetRoots(params string[])

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
Type Description
ComponentPreview<T>.PreviewBuilder

WithAuthenticatedUser(string, params string[])

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
Type Description
ComponentPreview<T>.PreviewBuilder

WithCascadingValue<TValue>(TValue, string?, bool)

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 Description
ComponentPreview<T>.PreviewBuilder
Type Parameters
Name Description
TValue

WithChildContent(RenderFragment, string)

Sets child content from a render fragment.

Declaration
public ComponentPreview<T>.PreviewBuilder WithChildContent(RenderFragment fragment, string parameterName = "ChildContent")
Parameters
Type Name Description
RenderFragment fragment
string parameterName
Returns
Type Description
ComponentPreview<T>.PreviewBuilder

WithChildContent(string, string)

Sets child content from a raw HTML/markup string.

Declaration
public ComponentPreview<T>.PreviewBuilder WithChildContent(string markup, string parameterName = "ChildContent")
Parameters
Type Name Description
string markup
string parameterName
Returns
Type Description
ComponentPreview<T>.PreviewBuilder

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 Description
ComponentPreview<T>.PreviewBuilder
Type Parameters
Name Description
TChild

The child component type to render.

WithHttpResponse(Action<IHttpMockBuilder>)

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
Type Name Description
Action<IHttpMockBuilder> configure
Returns
Type Description
ComponentPreview<T>.PreviewBuilder
Examples
.WithHttpResponse(r => r
    .OnGet("/api/weather", res => res.WithJson(forecastJson))
    .OnPost("/api/orders", res => res.WithJson("""{"id": 42}"""))
    .Default(res => res.WithStatus(404)))

WithHttpResponse(string, string, int)

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
Type Name Description
string content
string mediaType
int statusCode
Returns
Type Description
ComponentPreview<T>.PreviewBuilder

WithLayout<TLayout>()

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 Description
ComponentPreview<T>.PreviewBuilder
Type Parameters
Name Description
TLayout

The layout component (a LayoutComponentBase) to wrap with.

WithLocation(string)

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
Type Description
ComponentPreview<T>.PreviewBuilder

WithParameter<TValue>(Expression<Func<T, TValue>>, TValue)

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 Description
ComponentPreview<T>.PreviewBuilder

This builder for chaining.

Type Parameters
Name Description
TValue

The parameter type.

WithScripts(params string[])

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
Type Description
ComponentPreview<T>.PreviewBuilder

WithStylesheets(params string[])

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
Type Description
ComponentPreview<T>.PreviewBuilder

WithUser(ClaimsPrincipal)

Renders the preview as a specific ClaimsPrincipal for full control over identity, roles, and claims.

Declaration
public ComponentPreview<T>.PreviewBuilder WithUser(ClaimsPrincipal user)
Parameters
Type Name Description
ClaimsPrincipal user
Returns
Type Description
ComponentPreview<T>.PreviewBuilder