Clarity Angular Documentation

repository·main·Indexed 19 days ago

https://github.com/vmware-clarity/ng-clarity

A design system providing reusable UI components and styles for Angular applications, including the @clr/angular package. Features include the Figma Token Publisher for synchronizing CSS custom properties to Figma variables, accessibility addons (@clr/addons/a11y) with zoom level detection and overflow tabs, and advanced datagrid filtering components (@clr/addons/datagrid-filters) supporting multiple property types and filter modes.

Tokens
181K
Snippets
550
Records
758
Agent score
62%

What's inside Clarity Angular

  1. Overview of AppFx Datagrid

    main
    AppFx Datagrid is a data-bound list control built on top of the Clarity clr-datagrid. It displays items from an array-based source in columns and rows, supporting selection, sorting, and filtering. It is designed to reduce markup complexity, provide a consistent UX, and centralize cross-cutting concerns like accessibility (A11y), localization (L10n), and zoom support (2x and 4x).
  2. Overview of Figma Token Publisher

    main

    The Figma Token Publisher is a tool that synchronizes Clarity design tokens from CSS custom properties (found in dist/clr-ui/clr-ui.css) to Figma as variables using the Figma Variables REST API.

    Key Principle: The CSS is the single source of truth. The tool is designed to mirror the CSS state into Figma, performing diffs to ensure only necessary create, update, or delete operations are executed.

  3. Understand Clarity UI versioning behavior

    main

    The version of @clr/ui is aligned with the version of @clr/angular and does not follow independent semantic versioning.

    Important Note: Because versioning is tied to @clr/angular, breaking changes may occasionally be introduced in minor versions to maintain alignment. It is highly recommended to pin your project to a specific version and treat every upgrade as a potential breaking change.

  4. Understand the Clarity Angular package structure

    main

    The Clarity Angular repository is organized into three main projects that define the public packages:

    • projects/ui: Contains the @clr/ui package, which is a standalone CSS library providing global Clarity styles.
    • projects/angular: Contains the @clr/angular package, which implements Angular components by depending on @clr/ui and other core packages.
    • projects/demo: An Angular application used to demonstrate Clarity components.
  5. Use ClrControl components for form feedback

    main

    Clarity provides several components to manage the lifecycle and visual feedback of form controls within a clr-control-container:

    • clr-control-container: The parent component that wraps form controls and manages shared state like labels, helpers, and errors.
    • clr-control-error: Displays error messages associated with a control.
    • clr-control-helper: Displays helper text or guidance for a control.
    • clr-control-success: Displays success feedback for a control.
    • clr-control-label: A directive (used via the label attribute) that manages the label text and accessibility linking for a control.
    <clr-control-container>
      <clr-control-label for="my-input">Username</clr-control-label>
      <input id="my-input" clrControl />
      <clr-control-error>This field is required</clr-control-error>
      <clr-control-helper>Enter your unique username</clr-control-helper>
      <clr-control-success>Username is available!</clr-control-success>
    </clr-control-container>
  6. How the Figma Token Publisher works

    main

    The publishing process follows these steps:

    1. Build: Generate the stylesheet using npm run _build:ui to produce dist/clr-ui/clr-ui.css.
    2. Parse: The tool parses CSS custom properties and resolves values into Figma-compatible types: COLOR, FLOAT, STRING, or VARIABLE_ALIAS.
    3. Apply Rules: It applies scope, exclusion, and code-syntax rules defined in figma-tokens.config.json.
    4. Diff & Push: It compares the parsed tokens against the existing variables in the target Figma file and pushes only the required changes.
  7. Build selection lists with ClrOptions and ClrOption

    main

    Clarity provides a pattern for building complex selection components (like comboboxes or multi-select lists) using a hierarchy of components:

    1. ClrOptions (<clr-options>): The main container for the selection UI. It manages the overall state, loading states, and search text.
    2. ClrOptionGroup (<clr-option-group>): Used to group related options under a label.
    3. ClrOption (<clr-option>): Represents an individual selectable item. It accepts [id] and [clrValue] inputs.
    4. ClrOptionSelected ([clrOptionSelected]): A directive used within a template to identify which option is currently selected.

    Example structure:

    <clr-options>
      <clr-option-group [clrOptionGroupLabel="Group Label">
        <clr-option *clrOptionItems="let item of items" [clrValue="item">
          {{ item.name }}
        </clr-option>
      </clr-option-group>
    </clr-options>
  8. Define a Step in a Workflow

    main

    A Step is the fundamental unit of a workflow. It defines a specific stage in a multi-step process. When defining a Step, you can specify its component class, title, description, and how it interacts with the overall workflow model through mappings and validation.

    Key properties of the Step interface:

    • title: The display name of the step.
    • componentClass: The Angular component used to render the step's UI.
    • description: An optional description of the step's purpose.
    • mappings: An instance of Mappings<S, W> used to synchronize data between the step's local model (S) and the global wizard model (W).
    • model: A StepModel or StepModelFactory defining the step's state (loading, validation, etc.).
    • isRelevant: A Var<boolean> used to determine if the step should be shown based on other workflow values.
    • recreateComponent: A function (stepModelChanges?: ModelChanges) => boolean that determines if the component should be destroyed and recreated when the model changes.
    export interface Step {
        componentClass: Type<any>;
        description?: string;
        instantiateLazy?: boolean;
        isRelevant?: Var<boolean>;
        mappings?: Mappings<any, any>;
        model?: StepModel | StepModelFactory;
        navTitle?: string;
        recreateComponent?: (stepModelChanges?: ModelChanges) => boolean;
        summary?: (builder: PropertyViewSectionBuilder, stepModel?: StepModel) => PropertyViewSectionModel;
        title: string;
    }
  9. Build a property view model using PropertyViewBuilder

    main

    The PropertyViewBuilder provides a fluent API to construct a PropertyViewModel. You use the builder to define categories, sections, properties, and messages. The process typically follows this hierarchy: PropertyViewBuilder -> PropertyViewCategoryBuilder -> PropertyViewSectionBuilder -> (PropertyViewPropertyBuilder or PropertyViewMessageBuilder).

    To finish the construction, call .build() on the top-level builder.

    import { PropertyViewService } from '@clr/addons/property-view';
    
    // Inside a component or service
    const builder = this.propertyViewService.createPropertyViewBuilder();
    const viewModel = builder
      .category('cat1', 0)
        .title('General Information')
        .section('sec1')
          .title('System Details')
          .property('Version', '1.0.0')
          .exit()
        .exit()
      .build();
  10. Use ClrTimelineStep to build timelines

    main

    The ClrTimelineStep component is used to represent individual steps within a timeline. It supports different visual states via the ClrTimelineStepState enum.

    Available States:

    • CURRENT: The active step.
    • ERROR: A step that failed.
    • NOT_STARTED: A step that has not yet begun.
    • PROCESSING: A step currently in progress.
    • SUCCESS: A completed step.

    Component Structure:

    • clr-timeline-step: The main container. It accepts a clrState input.
    • clr-timeline-step-header: Used for the header content.
    • clr-timeline-step-title: Used for the step title.
    • clr-timeline-step-description: Used for the step description.
    <clr-timeline-step [clrState="SUCCESS">
      <clr-timeline-step-header>
        <clr-timeline-step-title>Step Title</clr-timeline-step-title>
      </clr-timeline-step-header>
      <clr-timeline-step-description>Step Description</clr-timeline-step-description>
    </clr-timeline-step>
  11. Implement custom focusable items with FocusableItem

    main

    To create custom components that participate in Clarity's focus management system (e.g., for roving tabindex or keyboard navigation), implement the FocusableItem abstract class. This allows your component to be managed by the FocusService and linked with other items using Linkers.

    Key properties to implement/manage:

    • id: A unique identifier for the item.
    • focus(): Method to trigger focus on the element.
    • blur(): Method to remove focus.
    • activate(): Method to handle selection/activation.
    • disabled: Boolean indicating if the item is interactive.
    • up, down, left, right: References to adjacent FocusableItems or Observable<FocusableItem>s to define navigation paths.
    export abstract class FocusableItem {
        abstract activate?(): void;
        abstract blur(): void;
        disabled?: boolean;
        down?: FocusableItem | Observable<FocusableItem>;
        abstract focus(): void;
        id: string;
        left?: FocusableItem | Observable<FocusableItem>;
        right?: FocusableItem | Observable<FocusableItem>;
        up?: FocusableItem | Observable<FocusableItem>;
    }