Course contentsShow
← C#
Lesson 1426 of 1,90812. Modern Frontend with Razor and BlazorFree lesson

Adaptive UI Components

Create components that adjust their complexity based on the device or platform.

In this lesson, you will learn how to create "Adaptive UI Components" that intelligently scale their feature set and complexity based on the client's detected capabilities.

The Adaptive Design Philosophy

In modern Blazor development, a "one-size-fits-all" approach often results in a poor user experience. A component that works perfectly on a desktop—such as a data-heavy grid with multi-column sorting—may be overwhelming or unusable on a mobile device. Adaptive UI allows your components to adjust their rendering logic based on the environment.

Instead of writing separate components for every platform, we use Breakpoint Detection and Capability Injection to let a single component adapt its own UI tree.

Implementing Adaptive Complexity

To create an adaptive component, we leverage BrowserInterop to detect the viewport width or device type, then use that state to conditionally render different markup.

Consider a SmartTable component that simplifies its UI for mobile:

@inject IBrowserViewportService ViewportService

<div class="table-container">
    @if (ViewportService.IsMobile)
    {
        <MobileList Items="Data" />
    }
    else
    {
        <ComplexDataTable Items="Data" EnableColumnSorting="true" />
    }
</div>

@code {
    [Parameter] public List<Record> Data { get; set; }
}

By decoupling the logic (the data) from the presentation (the UI structure), the SmartTable remains the single source of truth. The component evaluates its own complexity at render time: if the device is a mobile phone, it strips away heavy features like interactive column resizing or deep filtering, replacing them with a streamlined list view.

Why This Matters

When building adaptive components, you aren't just hiding CSS; you are modifying the component tree. This improves performance by preventing the browser from processing DOM elements that the user cannot interact with on their specific device. It reduces the "interactive weight" of your page, ensuring that mobile users enjoy a snappy experience while desktop users retain full functionality.

Key Takeaway: Adaptive UI components use environmental metadata to intelligently swap their internal render trees, ensuring the complexity of the interface always matches the capabilities of the user's device.