Skip to content
Explanation Concepts

Concepts

This page explains how Klyroo.UI is put together, so the behavior you see in a page is predictable. For tasks, read the guides.

On this page

The pieces

There is one Blazor package, Klyroo.UI, and three small ones that add to it.

Klyroo.UI                       components, themes, locales, stylesheets, JavaScript, build targets
  Klyroo.UI.Server              IKlyRequestCookies over HttpContext (ASP.NET Core, server project only)
  Klyroo.UI.Images              image provider URL builders (CDNs and the local endpoint)
    Klyroo.UI.Images.Local      ASP.NET Core endpoint that resizes images with libvips

Klyroo.UI is a Razor class library. Its static web assets are klyroo.css, klyroo.preflight.css, klyroo.theme.css and a JavaScript initializer that Blazor loads on its own. Its buildTransitive MSBuild targets run in the project that references the package: they copy the theme for Tailwind, bundle icons, self-host fonts and compile Tailwind. A task assembly (Klyroo.UI.Build.Tasks) ships inside the package and is never referenced by an app.

There is no separate headless package in this repository. The packages that build on Klyroo.UI (tables, forms, dashboards) live in their own repositories and depend on the exact version of this one.

Components, slots and themes

Every styled component is made of slots: named parts such as root, label, leadingIcon. A ComponentTheme declares the classes of each slot, the classes added by each variant value (variant, size, color, and so on), the classes added when several variant values match together (compound variants), and the default value of each variant. The themes live in KlyrooOptions, one property per component (o.Button, o.Card, o.Modal).

When a component renders, ResolveSlots computes the classes of every slot in a fixed order, and each step is merged into the previous one with tailwind-merge, so a conflicting utility from a later step wins:

theme defaults  ->  AddKlyrooUi overrides  ->  KlyTheme scopes  ->  Ui parameter  ->  Class parameter (root slot only)
 (library)           (your app)                (a subtree)          (one instance,     (one instance,
                                                                     any slot)          root only)

A *Ui record has one property per slot. ButtonUi.Label holds classes for the label slot of one KlyButton. KlyTheme applies a Ui record to every component of one type below it. Attributes that are not parameters, such as data-testid or aria-label, are forwarded to the main element of the component.

Resolved classes are cached per theme and variant selection, so a theme becomes read-only the first time a component renders with it. A later change throws InvalidOperationException. The same goes for the palettes and the defaults in o.Colors and o.Defaults, for the locale options and for the icons: configure everything in AddKlyrooUi, which may run several times (a library, then the app) against the same options.

Colors are semantic. A component takes a KlyColor such as primary or error, or the name of a color you added. It renders data-color="primary", and the stylesheet maps that to three variables (--kly-color, --kly-color-text, --kly-color-contrast) that every theme class uses. That is why a new color needs no new CSS beyond those three variables, and why no theme contains a palette color such as bg-blue-500.

Two ways to get the CSS

The classes the components use are generated by Tailwind CSS v4. You choose where that happens.

  • Precompiled. Load klyroo.preflight.css and klyroo.css. Nothing to install. Class and Ui only take classes that already exist in klyroo.css or in your own CSS.
  • Compiled by your app. Import klyroo.theme.css after Tailwind. It holds the tokens, the dark variant and the list of classes the components use, so Tailwind generates them together with yours. KlyrooTailwind runs the Tailwind CLI for you at build time.

The tokens are the same in both modes, and so are the components. The mode only decides whether your own utilities exist. Loading both stylesheets at once repeats base utilities and breaks the responsive and dark variants, so pick one.

What runs in the browser and what runs in .NET

Opening and closing overlays, focus management, keyboard navigation, positioning, tab and accordion selection, the options of a select, and the display of toasts run in the browser, in custom elements (<kly-popover>, <kly-modal>, <kly-tabs> and so on) that the JavaScript initializer defines. .NET renders the markup and owns what you bind and what you handle: @bind-Value, @bind-Open, OnClick, validation.

 user input                      browser (custom element)                  .NET (component)
 ----------                      ------------------------                  ----------------
 click, key, drag  ------------> open, close, focus, filter, position
                                      |
                                      |  only when the parameter is bound
                                      +--- kly-toggle / kly-change --------> OpenChanged / ValueChanged
                                           (with a sequence number)           re-render with the new state
                                                                              data-kly-seq=N  ---> element

The consequences are practical:

  • A component with no @bind-Open, no @bind-Value and no handler never calls .NET when it changes. In Server mode that means no round trip per click or per key.
  • When you do bind, the element reports with a custom event, kly-toggle for an open state and kly-change for a value. Menus whose options come from .NET report the text typed with kly-search, and the editor reports a length limit with kly-limit. Blazor receives them as @onklytoggle, @onklychange, @onklysearch and @onklylimit with KlyToggleEventArgs, KlyChangeEventArgs, KlySearchEventArgs and KlyLimitEventArgs.
  • Every report carries a sequence number that .NET writes back as data-kly-seq. The element ignores a render that was computed before .NET saw its latest report, so a fast typist is not overwritten by a slow round trip.
  • The element never replaces nodes that Blazor rendered, and it re-applies what it set after an enhanced navigation. That lets Blazor re-render freely.

The JavaScript initializer, Klyroo.UI.lib.module.js, is loaded automatically by blazor.web.js, blazor.server.js, blazor.webassembly.js and blazor.webview.js, so there is no script tag to add. It also defines a small global, Klyroo: Klyroo.setColorMode('light' | 'dark' | 'system'), Klyroo.getColorMode() and Klyroo.toast(...), which IToastService calls. All interop calls from .NET pass strings and numbers only, so they survive trimming.

Dialogs opened from code with IKlyOverlayService need no extra JavaScript: a KlyOverlayHost (rendered by KlyApp) turns each call into a KlyModal or KlySlideover in the Blazor tree, so the native dialog does the focus work. Unlike toasts, the dialog is ordinary Blazor markup, which is what lets the component you open receive parameters and hand a result back. See Open a dialog from code.

Render modes

Every component works in static server rendering, Interactive Server, WebAssembly and Auto, with the same markup.

  • Static rendering. Form controls render real form fields and post. Overlays, tabs, accordions, popovers and tooltips work because their behavior is in the browser. What needs a .NET handler cannot work: a KlyButton with OnClick renders disabled until the page is interactive, and so do the sort buttons of a KlyTable.
  • Interactive modes. Bound values and handlers run in .NET. Interactive Server pays a round trip for each handler; WebAssembly runs them in the browser.
  • Auto. The first render is on the server, later ones on WebAssembly. Both must see the same configuration, which is why AddKlyrooUi is called with the same options in both projects, and why culture, locale, fonts and image providers are configured so that the server and the client agree.

Overrides that travel as cascading values (KlyApp, KlyTheme) do not cross a render mode boundary. A component with its own @rendermode starts a new tree, so put a KlyApp or a KlyTheme inside it, or use AddKlyrooUi, which every render mode shares.

Server features that degrade

Some features need a server request: reading a cookie while the page is static or prerendered, and writing the page head. Klyroo.UI knows only the abstraction IKlyRequestCookies; Klyroo.UI.Server implements it over IHttpContextAccessor and marks a response that read a cookie Cache-Control: private with Vary: Cookie. In a Blazor Hybrid web view there is no request, so the components fall back to what a web view can do: localStorage instead of cookies, KlyHead and a HeadOutlet instead of an inline head. See Platforms.

Texts and cultures

No component contains a literal text. Everything a person reads or hears comes from KlyMessages, an immutable record with one property per text, carried by a KlyLocale with a code, a name and a writing direction. A component resolves its locale from the cascaded KlyApp locale, then o.Locale, then the culture of the user among o.Locales. Dates and numbers format with the regional culture of the user when it speaks the language of the locale, and an explicit Culture parameter wins. Browser code receives its texts through data-* attributes rendered by .NET.

Design decisions and their cost

  • State lives in the browser unless you bind it. The benefit is cheap Server mode and components that work in static pages. The cost is that .NET does not know whether a popover is open until you bind it, and that a unit test cannot exercise the behavior, because bUnit runs no JavaScript.
  • Icons and fonts are resolved at build time. The benefit is that the page never asks a third party for anything. The cost is that a Release build needs the network once, or a filled cache, and that an icon name built at runtime must be declared in the project file.
  • Themes are frozen after first use. The benefit is that class resolution is a cache lookup. The cost is that configuration must happen in AddKlyrooUi, not in a component.
  • Colors are variables, not palette classes. The benefit is that one component serves every color and every palette, with the contrast rules checked once. The cost is that you cannot paint a component with an arbitrary Tailwind color through its theme without defining a semantic color for it.
  • The library renders no Markdown and runs no network call of its own. Chat messages take rendered markup, and every remote service (icons, fonts, images) is reached by the build or by your own configuration.

Errors you can expect

  • InvalidOperationException when a theme, the colors, the defaults, the locale options or the icon options change after a component used them.
  • InvalidOperationException naming the color and the rule when a palette cannot meet the contrast rules and AllowLowContrast is off.
  • Build diagnostics for the features that run at build time: KLYTW001 to KLYTW004 for Tailwind, KLYFONT001 to KLYFONT004 and KLYFONT010 for fonts, KLYICON001 to KLYICON004 for icons.
  • A missing icon renders empty with data-kly-missing-icon, and a font or icon that cannot be resolved is a warning in Debug and an error in Release.
  • An image provider or a preset that is not registered throws when the image is resolved.