List Item View

ListItemView is the view of a list row: it lays out the content of a row — the icon, the description, the trailing content — and draws the states its owner passes in.

import {ListItemView} from '@gravity-ui/uikit';

The List draws its rows with this view and exposes it as List.ItemView, with the props of the layout only. Reach for ListItemView itself to build a row outside of a list — a row of a menu of your own, an item of a tree — or when a row needs the nesting and collapsing props that a list does not pass.

Basic Usage

The view is a block element as wide as its container: the width of a row is the width of the list around it.

import {Envelope} from '@gravity-ui/icons';
import {Icon, ListItemView} from '@gravity-ui/uikit';

function Mailbox() {
  return (
    <ListItemView startContent={<Icon data={Envelope} size={16} />} onClick={openMailbox}>
      Inbox
    </ListItemView>
  );
}

The view has no ARIA role of its own: a row gets its role from the list it belongs to (option in a listbox, row in a grid), and role together with the rest of the DOM props reaches the root element as it is passed. Inside a <List> the props come ready from getItemProps().

Sizes

The size prop sets the density of a row — s, m, l or xl. It drives the minimum height, the paddings, the corner radius and the size of the controls of the row; xl also switches the text to body-2. The heights are those of the controls — 24, 28, 36 and 44px — and, as in the controls, the block paddings come from the height: a single line fills the row exactly, every next line adds its line height. The inline paddings grow with the size — 8, 12, 16 and 16px. A row without size has the height, the paddings and the radius of m with the controls of the base view; inside a <List> every row gets the size of the list.

Slots

The content of a row is its children, and the rest is in the props: startContent for a leading icon or an avatar, description for the second line, endContent for what closes the row — a label, a shortcut, an action.

<ListItemView
  startContent={<Icon data={Star} size={16} />}
  description="Flagged by you"
  endContent={<Label>3</Label>}
>
  Starred
</ListItemView>

States

A row shows four states, and each of them is a prop — the view keeps no state of its own:

  • active — the cursor of the keyboard: a list moves it with the arrow keys;
  • hovered — the hover highlight, forced by the props. Without the prop the row highlights itself on :hover; hovered={false} suppresses that, which is what a list does while a drag is going on;
  • disabled — the row is dimmed and stops reacting to the pointer, its own onClick included;
  • selected — see Selection.

Selection

selectionStyle says how a selected row is shown: highlight tints the row, check keeps a slot for a check mark in front of the content, none shows the selection by nothing — for a row that says it in its own content. The prop has no default: a selected row without it looks like any other. Inside a <List> the list picks the style itself — check for a multiple selection, highlight for a single one.

The check slot is kept for every row of a list, selected or not, so the content of the rows does not jump as the selection changes.

Nesting and collapsing

nestedLevel indents a row by the depth of a tree, and collapsible gives a row the toggle of its branch: collapsed is its state and onCollapseChange its handler. A click on the row itself toggles the branch too, unless the row has an onClick of its own.

The toggle is decoration: it carries aria-hidden and is out of the tab order, so the row itself is the only way to the branch. The aria-expanded of the row is yours to set.

Drag handle

dragHandle fills the outermost slot of a row, in front of the indent, the collapse toggle and the check mark, so the handle of a reorderable list stays at the edge of the row whatever else the row shows. The view knows nothing about drag and drop: the props of the library live on the element you pass.

<ListItemView
  dragHandle={
    <span {...dragHandleProps} aria-label="Drag to reorder">
      <Icon data={Grip} size={12} />
    </span>
  }
>
  {track.title}
</ListItemView>

Another element

component renders the row as another element — a link, a button — and componentProps are the props of that element: href, type, the handlers. With isContainer the view lays out nothing and leaves the markup of the row to you, keeping the states and the element.

CSS API

NameDescription
--g-list-item-view-min-heightThe minimum height of a row
--g-list-item-view-border-radiusThe corner radius
--g-list-item-view-padding-inlineThe inline padding
--g-list-item-view-padding-blockThe block padding
--g-list-item-view-line-heightThe line height of the content
--g-list-item-view-controls-gapThe gap between the slots
--g-list-item-view-controls-sizeThe size of the controls of a row
--g-list-item-view-controls-border-radiusThe corner radius of the controls
--g-list-item-view-controls-icon-sizeThe size of the icons of the control
--g-list-item-view-spacer-sizeThe indent of one nesting level
--g-list-item-view-background-colorThe background of a row
--g-list-item-view-background-color-hoverThe background of a hovered row
--g-list-item-view-text-colorThe colour of the content
--g-list-item-view-description-colorThe colour of the description

The variables apply to a row of any size: size only supplies the default geometry — the minimum height, the radius, the paddings and the controls — for a variable that is not set. The default block padding follows the minimum height and the line height; --g-list-item-view-padding-block fixes it. The section header of a list (List.SectionHeader) follows the same minimum height and inline padding, the variables included, so the rows and the headers of a list keep one edge.

Properties

NameDescriptionTypeDefault
childrenThe content of the rowReact.ReactNode
sizeThe density of the row's' 'm' 'l' 'xl'
startContentThe leading content: an icon, an avatarReact.ReactNode
descriptionThe second line of the rowReact.ReactNode
endContentThe trailing content: a label, a shortcut, an actionReact.ReactNode
dragHandleThe content of the outermost slot: the drag handle of a reorderable rowReact.ReactNode
activeThe cursor of the keyboard on the rowboolean
hoveredThe hover highlight, forced by the props (false suppresses the :hover of the row)boolean
disabledWhether the row is disabledboolean
selectedWhether the row is selectedboolean
selectionStyleHow a selected row is shown'highlight' 'check' 'none'
collapsibleWhether the row has the toggle of its branchboolean
collapsedWhether the branch of the row is collapsedboolean
onCollapseChangeThe handler of the toggle (a click on the row without onClick toggles the branch as well)(collapsed: boolean) => void
nestedLevelThe depth of the row in a tree: the indent in front of itnumber0
onClickThe handler of a click on the row (a click on a focusable descendant is not one)React.MouseEventHandler<HTMLElement>
componentThe element the row is rendered asReact.ElementType'div'
componentPropsThe props of that elementReact.ComponentProps<component>
isContainerRenders the children as they are, without the layout of the slotsbooleanfalse
classNameThe CSS class of the rowstring
styleThe inline style of the rowReact.CSSProperties

The rest of the props reach the root element of the row: role, id, tabIndex, aria-*, data-*, the handlers of the pointer and the keyboard.