HAVIT Blazor Bootstrap Documentation

repository·main·Indexed 20 days ago

https://github.com/havit/havit.blazor

A free collection of Bootstrap 5.3-based components for ASP.NET Blazor. The library includes Havit.Blazor.ApplicationInsights for telemetry tracking, an MCP server for AI-assisted API documentation, and Havit.Bootstrap for managing and compiling customized Bootstrap CSS.

Tokens
5.7K
Snippets
13
Records
34
Agent score
70%

What's inside HAVIT Blazor Bootstrap

  1. Overview of HAVIT Blazor Bootstrap components

    main
    HAVIT Blazor Bootstrap is a free component bundle providing Bootstrap 5.3 components specifically designed for ASP.NET Blazor applications. It includes a wide range of UI elements categorized into Forms, Buttons & Indicators, Data & Grid, Layout & Typography, Navigation, Modals & Interactions, and Special components. For interactive documentation and live demos, visit havit.blazor.eu.
  2. Use the HAVIT Blazor Documentation MCP Server

    main
    The HAVIT Blazor Documentation MCP (Model Context Protocol) server allows AI assistants (like GitHub Copilot) to access API documentation for HAVIT Blazor components. It provides tools to look up component parameters, properties, events, methods, and supporting types (enums, settings classes, delegates).
  3. Important development notes for Havit.Bootstrap

    main

    When working with the Havit.Bootstrap repository, adhere to these constraints:

    • Customization: Do not modify files inside the bootstrap folder directly, except when performing a version update. All HAVIT-specific overrides must be implemented within the scss folder.
    • Linting: The project uses its own .editorconfig settings, which differ from standard HAVIT settings to maintain compatibility with Bootstrap's stylelint configurations.
  4. Display Toast notifications using HxMessenger

    main

    To display toast messages, do not use the HxToast component directly in your user code. Instead, use the HxMessenger service pattern:

    1. Use HxMessenger as a wrapper for HxToastContainer to manage the display area.
    2. Dispatch messages through the IHxMessengerService to trigger HxToast notifications.
  5. How HxMarkdown handles HTML security

    main

    By default, HxMarkdown is secure. The SanitizeHtml parameter (which defaults to true) ensures that any HTML tags present in the input Markdown string are escaped before rendering.

    If you are processing content from a trusted source and want to allow raw HTML to pass through to the output, set SanitizeHtml to false either on the component instance or globally via HxMarkdown.Defaults.SanitizeHtml.

  6. Use the HxMarkdown component

    main

    The HxMarkdown component converts Markdown text into HTML and renders it using Bootstrap typography. It is designed to be a standalone parser without external dependencies.

    To use the component, pass your Markdown string to the Content parameter. You can also apply a custom CSS class via CssClass to wrap the output in a <div>.

    @* Basic usage *@
    <HxMarkdown Content="@markdownString" />
    
    @* Usage with a custom CSS class *@
    <HxMarkdown Content="@markdownString" CssClass="my-custom-class" />
  7. Test the MCP server with Copilot Chat

    main
    Once the server is configured in your IDE, you can interact with it using natural language in Copilot Chat. For example, asking What parameters does HxButton have? will trigger the get_component_docs tool to retrieve the relevant API documentation. For direct endpoint testing, you can use the provided .http file located at Havit.Blazor.Documentation.Mcp.http.
  8. Use Modals and Dialogs in HAVIT Blazor

    main

    HAVIT Blazor provides several components for handling modal interactions and dialogs:

    • HxMessageBox: A component used to display simple message boxes.
    • HxModal: A component that renders a modal dialog following the Bootstrap Modal pattern.
    • HxDialogBase: A base class designed to simplify the implementation of your own custom modal dialogs.
    • HxOffcanvas: A component that renders a Bootstrap Offcanvas (also known as a Drawer).
  9. Setup Havit.Blazor.ApplicationInsights

    main

    To use Havit.Blazor.ApplicationInsights, follow these three steps:

    1. Register services

    Register the services in both your Server project and WebAssembly (Client) project using AddBlazorApplicationInsights.

    Note on Configuration:

    • If the <HxApplicationInsights> component is prerendered or used in SSR, configure the options in the server project.
    • If rendered in WebAssembly without prerendering, configure in the client project.

    2. Add the script component

    Place the <HxApplicationInsights /> component inside the <head> element of your root layout or App.razor. It must be placed before any other scripts.

    3. Inject and use

    Inject IBlazorApplicationInsights into your components to track events and other telemetry.

    // 1. Register services (Server or Client Program.cs)
    builder.Services.AddBlazorApplicationInsights(options =>
    {
        options.JsSdkOptions.ConnectionString = "your-connection-string";
    });
    // 2. Add script component (App.razor or Root Layout)
    @using Havit.Blazor.ApplicationInsights.Components
    
    <head>
        <HxApplicationInsights @rendermode="..." />
    </head>
    // 3. Inject and use
    @inject IBlazorApplicationInsights AppInsights
    
    @code {
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                await AppInsights.TrackEventAsync(new EventTelemetry { Name = "my-event" });
            }
        }
    }
  10. Build and compile Havit.Bootstrap CSS

    main

    Havit.Bootstrap requires NodeJS (NPM or YARN) to compile, vendor-prefix, and minify CSS. The compilation process incorporates HAVIT-specific overrides located in the scss folder.

    Prerequisites

    • NodeJS
    • NPM or YARN

    Build Commands

    1. Install dependencies: Run yarn to install necessary packages.
    2. One-time build: Use the css script to compile, prefix, and minify the CSS.
    3. Development mode: Use the watch script to act as a file-watcher; it will automatically re-run compilation scripts whenever changes are saved.
    4. Deployment: Use the publish-to-blazor script to move the compiled files from the dist folder to Havit.Blazor.Components.Web.Bootstrap/wwwroot.
    yarn
    # To compile once
    yarn css
    # To watch for changes
    yarn watch
    # To publish to Blazor
    yarn publish-to-blazor
  11. Use Telemetry Initializers to attach tags

    main

    Telemetry initializers allow you to attach static tags (key-value pairs) to every telemetry item. Because the JS SDK does not support dynamic callbacks from .NET per item, tags are set at registration time.

    Default telemetry initializer

    Set this during service registration in Program.cs to ensure tags are present even on the initial auto-tracked page view.

    Runtime telemetry initializer

    Use AddTelemetryInitializerAsync to attach tags dynamically during the application lifecycle.

    // Via options (Registration)
    builder.Services.AddBlazorApplicationInsights(options =>
    {
        options.DefaultTelemetryInitializer = new TelemetryInitializer
        {
            CloudRoleName = "MyBlazorApp",
            ApplicationVersion = "1.2.3"
        };
    });
    
    // At runtime
    await AppInsights.AddTelemetryInitializerAsync(new TelemetryInitializer
    {
        CloudRoleName = "MyBlazorApp",
        ApplicationVersion = "1.2.3"
    });
  12. Configure Logging for WebAssembly

    main

    The library includes an ASP.NET Core logging provider that forwards ILogger entries to Application Insights as traces or exceptions. This is primarily designed for Interactive WebAssembly.

    Register the provider

    In your WebAssembly (Client) project (Program.cs), you must explicitly call AddConfiguration because the WASM host does not read appsettings.json automatically:

    builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
    builder.Logging.AddBlazorApplicationInsights();

    Configure log levels

    In wwwroot/appsettings.json, use the provider alias BlazorApplicationInsights to set log levels:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        },
        "BlazorApplicationInsights": {
          "LogLevel": {
            "Default": "Error",
            "MyApp.Pages": "Warning"
          }
        }
      }
    }