VKUI Documentation

repository·master·Indexed 22 days ago

https://github.com/vkcom/vkui

A library of adaptive React components based on the VK design system for building web applications across various platforms. Includes tools such as @vkontakte/vkui-codemods for automatic version migrations and @vkontakte/storybook-addons for enhanced Storybook development, featuring a Live Code Editor and color scheme toggles.

Tokens
183.6K
Snippets
629
Records
877
Agent score
77%

What's inside VKUI

  1. What is @vkontakte/vkui-mcp?

    master
    The @vkontakte/vkui-mcp package is a Model Context Protocol (MCP) server designed for VKUI documentation. It provides AI assistants with direct access to VKUI components, hooks, code examples, and migration recommendations, enabling more accurate AI-driven development with the VKUI library.
  2. Overview of VKUI features and design

    master

    VKUI is a library of adaptive React components designed for web application development. It is based on the official VK design system.

    Key Features

    • Adaptivity: Components automatically adjust to screen sizes (smartphones, tablets, and computers).
    • Multiplatform Support: VKUI mimics both iOS and Android styles to maintain a consistent look with vk.com and m.vk.com across different environments.
    • Color Schemes: Built-in support for both light and dark themes to ensure accessibility.

    Design Resources

    You can use the official open design libraries available in the VK Figma Community:

    • VKUI Common Library [Beta]: The primary component library for Android and desktop.
    • VKUI iOS Library: Components specifically for iOS, including ready-to-use screen templates.
  3. Use DateRangeInput for date range selection

    master

    The DateRangeInput component allows users to select a start and end date via manual text input or a calendar picker.

    Note: This component is designed for tablets and desktops. Its behavior on mobile devices is not guaranteed.

    If you need a standalone calendar for range selection, use CalendarRange. If you need a single date and time input with a popup calendar, use DateInput.

    <DateRangeInput />
  4. Use ChipsSelect for multi-value selection

    master

    The ChipsSelect component allows users to select multiple values from a dropdown list. Each selected item is rendered as an individual Chip component. You can provide a predefined list of options via the options prop.

    <ChipsSelect
      options={[
        { value: 'red', label: 'Красный' },
        { value: 'blue', label: 'Синий' },
        { value: 'green', label: 'Зеленый' },
      ]}
      placeholder="Выберите значение"
    />
  5. Use DateInput for date and time entry

    master

    The DateInput component allows users to enter a date manually or select one via a calendar popup.

    Important Usage Notes:

    • Platform Support: This component is designed for tablets and desktops. Its behavior is not guaranteed on mobile devices.
    • Alternatives:
      • For date range selection (with a popup calendar), use DateRangeInput.
      • For a standalone calendar component (without an input field), use Calendar.
    <DateInput />
  6. Control the Search component state

    master

    The Search component supports standard React controlled and uncontrolled patterns:

    • Uncontrolled mode: Do not pass the value prop. Use defaultValue to set an initial value.
    • Controlled mode: Use the value and onChange props to manage the state manually.
    // Uncontrolled state
    <Search defaultValue="Поиск" />;
    
    // Controlled state
    const [value, setValue] = React.useState('Поиск');
    
    <Search value={value} onChange={(event) => setValue(event.target.value)} />;
  7. Compose complex headers with FormItem subcomponents

    master

    When a field header is composite (contains multiple elements), use FormItem subcomponents to manage layout and accessibility:

    • <FormItem.Top>: A wrapper for the composite header that handles alignment and spacing.
    • <FormItem.TopLabel>: Renders the field label. It defaults to a <label> tag if htmlFor is provided. You can override the tag via the Component prop.
    • <FormItem.TopAside>: Renders additional content (like character counters) to the right of the label.
    const id = React.useId()
    
    return (
      <FormItem
        top={
          <FormItem.Top>
            <FormItem.TopLabel htmlFor={id}>Дополнительная информация</FormItem.TopLabel>
            <FormItem.TopAside>0/100</FormItem.TopAside>
          </FormItem.Top>
        }
      >
        <Textarea id={id} name="about" />
      </FormItem>
    );
  8. Understand ModalPage behavior and modes

    master

    The ModalPage component implements a modal window that adapts its appearance based on screen resolution.

    Desktop Mode (Resolution >= 768px)

    Acts as a dialog window.

    • The size prop limits the maximum width.
    • Focus is trapped within the ModalPage content using Tab and Shift + Tab.
    • Important: If opening a second modal on top of another, set disableFocusTrap on the first (bottom) modal to prevent focus management conflicts.
    • Closing triggers onClose with:
      • click-close-button (clicking the close button)
      • click-overlay (clicking the overlay)
      • escape-key (pressing Esc)

    Mobile Mode (Resolution <= 767px)

    Acts as a bottom sheet (panel sliding up from the bottom).

    • Focus is trapped within the content.
    • Closing triggers onClose with:
      • click-overlay (clicking the overlay)
      • escape-key (pressing Esc)
      • swipe-down (swiping down via touch/mouse)
    • Note: If platform="vkcom" is set in ConfigProvider, mobile behavior is ignored and it defaults to desktop mode.
    • To prevent the close button from being covered by floating elements (like webview controls), use hasCustomPanelHeaderAfter in ConfigProvider to offset the top of the modal.
  9. Improve CustomSelect accessibility (a11y)

    master

    Async Loading Indicators

    To notify screen reader users during data fetching, use the fetching prop. You can customize the announcement text using:

    • fetchingInProgressLabel: Default is "Список опций загружается...".
    • fetchingCompletedLabel: Default is "Опций загружено: ${options.length}".

    Fixing Selection Visibility

    In older versions, some screen readers (like NVDA) struggled to identify the selected option because the input value was reset on blur.

    Solution: Use the accessible flag.

    • In v8+, accessible={true} is the default and is highly recommended.
    • Do not disable this flag, as it will be removed in v9.
  10. How to use the usePagination hook

    master

    The usePagination hook provides full control over the pagination logic and item array. Use this hook when you need:

    • A non-standard pagination UI.
    • Integration with custom logic.
    • Complex transition animations.

    The hook returns an array of items representing the pages and ellipsis markers (e.g., [1, 'start-ellipsis', 4, 5, 6, 'end-ellipsis', 10]).

    const items = usePagination({
      totalPages: 10,
      currentPage: 5,
    });
    
    // items → [1, 'start-ellipsis', 4, 5, 6, 'end-ellipsis', 10]
    
    return (
      <nav>
        {items.map((item) =>
          item === 'start-ellipsis' || item === 'end-ellipsis' ? (
            <span key={item}>...</span>
          ) : (
            <button key={item}>{item}</button>
          ),
        )}
      </nav>
    );