Gravity UI UIKit

repository·main·Indexed 21 days ago

https://github.com/gravity-ui/uikit

A foundational React component library for the Gravity UI design system, providing over 70 components for production-grade web applications. It includes standard UI elements such as buttons, form controls, modals, menus, tabs, typography, and layout primitives, as well as the ThemeProvider and design tokens.

Tokens
225.4K
Snippets
608
Records
820
Agent score
74%

What's inside @gravity-ui/uikit

  1. Overview of Gravity UI UIKit

    main

    UIKit is a collection of 70+ React components that form the core of the Gravity UI design system. It is designed for high-density web applications and handles complex requirements out of the box.

    Key Features:

    • 70+ Components: Includes input fields, overlays, data displays, layout primitives, and feedback components.
    • Built-in Theming: Supports light, dark, and high-contrast themes. You can use the Themer tool to customize tokens.
    • RTL Support: Full support for right-to-left layouts.
    • Advanced Capabilities: Built-in support for accessibility, RTL, Server-Side Rendering (SSR), and Internationalization (I18N).
  2. Use the unstable_Menu component

    main

    The unstable_Menu component is a future replacement for Menu and DropdownMenu. It displays a list of choices in a Popup when interacting with a trigger.

    Warning: This component is marked as unstable, meaning breaking changes may occur in minor or patch releases.

    • unstable_Menu: The main container.
    • unstable_MenuItem: An individual option within the menu.
    • unstable_MenuDivider: A divider used to group options.
    • unstable_MenuTrigger: A built-in default trigger (a Button with an ellipsis icon).
    import {
      unstable_Menu as Menu,
      unstable_MenuItem as MenuItem,
      unstable_MenuDvivider as MenuDivider,
      unstable_MenuTrigger as MenuTrigger,
    } from '@gravity-ui/uikit/unstable';
    
    function BasicMenu() {
      return (
        <Menu trigger={<MenuTrigger />}>
          <MenuItem>Copy</MenuItem>
          <MenuItem>Move</MenuItem>
          <MenuDivider />
          <MenuItem theme="danger">Delete</MenuItem>
        </Menu>
      );
    }
  3. Use the Label component

    main

    The Label component is used to highlight specific information, typically single-line text. It supports various color codes to indicate importance and can include interactive elements like Close or Copy buttons for simple actions. You can also display information in a key: value format by passing the key as children and the value via the value prop.

    import {Label} from '@gravity-ui/uikit';
    
    // Basic usage
    <Label>Information</Label>
    
    // Key-value usage
    <Label value="Value">Key</Label>
  4. Use the Breadcrumbs component

    main

    Breadcrumbs is a navigation element that displays the user's current location within a website's hierarchy. It provides a trail of links that allow users to navigate back to higher-level pages, which is particularly useful for applications with deep or complex page structures.

    import {Breadcrumbs} from '@gravity-ui/uikit';
  5. Use the UserLabel component

    main

    UserLabel is a compact chip containing a user's avatar and their name or email. It is designed for inline mentions and lists.

    For larger profile blocks, use the User component instead. For a standalone picture, use the Avatar component.

    import {UserLabel} from '@gravity-ui/uikit';
    
    // Basic usage with different types
    <UserLabel type="person" text="Charles Darwin" />
    <UserLabel type="email" text="email@example.com" />
    <UserLabel type="empty" text="Alan Turing" />
  6. How `useList` hooks and components work together

    main

    The useList suite provides a set of hooks designed to create stateless List components. The core architectural pattern is that the hooks manage all complex logic (state, filtering, keyboard navigation, tree structures), while you are responsible for implementing the "dumb" view components.

    Core Abstractions

    Hooks (Logic):

    • useList: The primary hook for managing list state.
    • useListKeydown: Handles keyboard navigation for the list.
    • useListFilter: Manages filtering logic for list items.

    Components (View only):

    • ListItemView: A stateless component for rendering an individual item.
    • ListContainerView: A container component for the list.
    • ListItemExpandIcon: An icon used to indicate expandable tree items.
    • ListRecursiveRenderer: A component specifically for rendering nested/tree structures.

    Utilities:

    • getListItemClickHandler: Generates click handlers for list items.
    • getItemRenderState: Computes the props needed for a ListItemView based on the current list state.
    • scrollToListItem: Utility for scrolling to a specific item.
    • computeItemSize: Calculates item dimensions.
    • getListParsedState and getListItemQa: State and QA utilities.
    import {
        type unstable_ListItemId as ListItemId,
        type unstable_ListItemType as ListItemType,
        unstable_ListContainerView as ListContainerView,
        unstable_ListItemView as ListItemView,
        unstable_getItemRenderState as getItemRenderState,
        unstable_useList as useList,
        unstable_useListKeydown as useListKeydown,
        unstable_useListState as useListState,
    } from '@gravity-ui/uikit/unstable';
    
    const items: ListItemType<string>[] = ['one', 'two', 'free', 'four', 'five'];
    
    function List() {
        const containerRef = React.useRef<HTMLDivElement>(null);
    
        const list = useList({items});
        const onItemClick = getListItemClickHandler({list, multiple: true});
        useListKeydown({onItemClick, containerRef, list});
    
        return (
            <ListContainerView ref={containerRef}>
                {list.structure.items.map((_, i) => {
                    const {props} = getItemRenderState({
                        id: String(i),
                        mapItemDataToContentProps: (title) => ({title}),
                        onItemClick,
                        list,
                    });
    
                    return <ListItemView key={i} {...props} />;
                })}
            </ListContainerView>
        );
    }
  7. When to use UIKit vs other Gravity UI packages

    main

    UIKit is intended for standard application UI elements. Use the following guidance to choose the right package for your needs:

    Use UIKit for...Use these specialized packages for...
    Standard UI: Buttons, form controls, modals, menus, tabs, typography, and layout primitives.Feature-rich Data Grids: Use @gravity-ui/table for virtualization, resizing, and grouping.
    Theming Foundation: ThemeProvider, design tokens, and CSS variables.Charts & Visualization: Use @gravity-ui/charts.
    Simple Tables: Basic selection, sorting, and row actions.Navigation Shells: Use @gravity-ui/navigation for headers, footers, and sidebars.
    Icon Rendering: The Icon component.SVG Icons: Use @gravity-ui/icons to get the actual icon assets.
    Date/Time Controls: Use @gravity-ui/date-components for pickers and calendars.
  8. Format Hotkey values

    main

    Keyboard shortcuts are defined using a specific string format:

    • Key combinations: Use the <key>+<key> format (e.g., shift+tab).
    • Multiple shortcuts: Separate different shortcut sequences with a space (e.g., ctrl+a ctrl+c).
    • Platform shorthand: Use mod as a shorthand for cmd on Mac and ctrl on other platforms (e.g., mod+v renders as ⌘+V on Mac and Ctrl+V on PC).
    • Literal symbols: To render a literal plus or minus sign, use the keywords plus or minus (e.g., mod+plus or mod+minus).
    // Examples of value formatting
    <Hotkey value="shift+tab" />
    <Hotkey value="ctrl+a ctrl+c" />
    <Hotkey value="mod+v" />
    <Hotkey value="mod+plus" />
  9. Understand the UIKit spacing scale

    main

    UIKit uses a scale-based spacing system rather than raw pixels to maintain design consistency. Every step is a multiple of a base unit (--g-spacing-base), which defaults to 4px.

    Common steps include:

    • 0: 0px
    • 0.5: 2px
    • 1: 4px
    • 2: 8px
    • 5: 20px
    • 10: 40px

    Changing the --g-spacing-base variable rescales the entire system proportionally.

    /* Example of CSS variable usage */
    .example-class {
      margin-right: var(--g-spacing-5); /* 20px */
    }
  10. Modal component overview

    main
    The Modal component is used to create pop-up windows that overlay the main page content. When a modal is opened, page scrolling is disabled, and focus is automatically moved to its content. The children of the Modal are rendered inside a Portal. You can use Modal to build dialogs, alerts, confirmations, and other overlay elements.
  11. Configure TextArea appearance with view and pin

    main

    The appearance of the TextArea is managed via the view and pin properties.

    View

    • normal: The default view.
    • clear: Used when the TextArea is wrapped in a custom container.

    Pin

    Controls the border styling of the left and right edges:

    • round-brick: Rounded on the left, sharp (brick) on the right.
    • brick-brick: Sharp edges on both sides.
    • brick-round: Sharp on the left, rounded on the right.
    <TextArea view="normal" pin="round-brick" />
    <TextArea view="clear" pin="brick-brick" />
    <TextArea view="clear" pin="brick-round" />