Umbraco UI Library (UUI)

repository·main·Indexed 40 days ago

https://github.com/umbraco/umbraco.ui

A collection of 80+ web components built with Lit and TypeScript for building Umbraco CMS interfaces. Version 2.0.2 provides a single @umbraco-ui/uui package featuring components like uui-action-bar, uui-avatar-group, uui-badge, and uui-breadcrumbs, with support for theming, dark mode, and usage without a bundler via import maps.

Tokens
49.2K
Snippets
294
Records
334
Agent score
100%

What's inside UUI

  1. Use the file: protocol as an alternative to npm link

    main

    If npm link causes issues with workspace hoisting, you can use the file: protocol in the consumer's package.json. This copies the package instead of symlinking it.

    Warning: Because it copies the files, you must run npm install every time you rebuild UUI to see changes.

    // In the consumer's package.json
    {
      "dependencies": {
        "@umbraco-ui/uui": "file:../../../Umbraco.UI"
      }
    }
  2. Use UUI Form Control Mixins in v2

    main

    The UUIFormControlMixin maintains its v1 surface (including name, required, error, submit(), etc.). However, v2 introduces a split to allow for more control:

    • UUIFormControlMixin: The full surface (base + basics). This is an alias for UUIFormControlWithBasicsMixin.
    • UUIFormControlBaseMixin: A lean core containing only form association, value, pristine, and validator machinery. Use this when you want full control over properties and validators.

    Types for the lean base are exported as UUIFormControlBaseMixinElement/UUIFormControlBaseMixinInterface.

  3. Use UUIFormControlBaseMixin for custom form control logic

    main

    Use UUIFormControlBaseMixin when you need a lightweight core for form association without the standard 'basics' provided by the full mixin. This is ideal when your component requires full manual control over its own properties and validation logic.

    This mixin provides:

    • Form association
    • value: The current value of the control.
    • pristine: State indicating if the control has been interacted with.
    • Validator machinery: The underlying infrastructure to handle validation without the pre-configured required or error properties.
  4. Understand the UUI component file structure

    main

    UUI components follow a registration file pattern to separate pure class definitions from side-effect-driven element registration. This allows for easier testing, subclassing, and type checking without accidentally triggering customElements.define().

    Each component folder is structured as follows:

    • [name].ts: The registration file. It contains the side effect that registers the element and handles global type mapping.
    • [name].element.ts: The pure class definition. This file exports the class without any side effects.
    • [name].test.ts: Component tests.
    • [name].story.ts: Storybook stories.
    • README.md: Component documentation.
    button/
    ├── button.ts              # Registration file (side effect: registers the element)
    ├── button.element.ts      # Pure class definition (no side effects)
    ├── button.test.ts         # Tests
    ├── button.story.ts        # Storybook story
    └── README.md
  5. Detect and debug multiple UUI instances

    main

    Umbraco UI tracks loaded versions on the global object. When UUI loads, it appends its version to globalThis.__uuiVersions.

    If multiple copies of UUI are present on the same page (such as duplicate bundles or mismatched versions), a warning will be emitted to the console: Multiple versions of Umbraco UI detected: [version1], [version2]. This is not supported and may cause issues.

    You can manually inspect the globalThis.__uuiVersions array to diagnose version conflicts.

    // Inspect the array of loaded versions for diagnostics
    console.log(globalThis.__uuiVersions); // e.g. ["2.0.0"]
  6. Follow UUI naming conventions

    main

    When creating or working with UUI components, follow these specific naming rules:

    EntityConventionExample
    FilenamesDrop the uui- prefixbutton.element.ts
    Class namesKeep the UUI prefixUUIButtonElement
    Tag namesKeep the uui- prefixuui-button
    Event filesUse PascalCaseUUICardEvent.ts
  7. Use UUIFormControlMixin to integrate components into forms

    main

    Use UUIFormControlMixin (also exported as UUIFormControlWithBasicsMixin) to make a web component fully compatible with forms. This is the complete version that matches the v1 surface. It provides standard form properties and built-in validation logic.

    It adds the following properties and methods to your component:

    • name: The name of the control.
    • required: Boolean indicating if the field is mandatory.
    • requiredMessage: Custom message for required validation.
    • error: Error state indicator.
    • errorMessage: The message to display when an error occurs.
    • submit(): Method to trigger form submission logic.
    • valueMissing and customError: Built-in validators that work on top of the base functionality.
  8. Create a custom icon registry by extending UUIIconRegistry

    main

    You can create a custom icon registry that loads icons on demand by extending the UUIIconRegistry class. You must implement the acceptIcon(iconName) method. Inside this method, you can use this.provideIcon(iconName) to get an icon object and then assign the SVG data to its svg property.

    import { UUIIconRegistry } from '@umbraco-ui/uui/components/icon-registry/UUIIconRegistry.js';
    
    class MyIconRegistry extends UUIIconRegistry {
      protected acceptIcon(iconName) {
        const icon = this.provideIcon(iconName);
        // Load and assign SVG data:
        icon.svg = '<svg>...</svg>';
        return true;
      }
    }