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
- Sizes
- Slots
- States
- Selection
- Nesting and collapsing
- Drag handle
- Another element
- CSS API
- Properties
§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>
);
}
import * as React from 'react'; import {Envelope} from '@gravity-ui/icons'; import {Icon, Flex, ListItemView} from '@gravity-ui/uikit'; const Rows = ({children}: React.PropsWithChildren) => ( <Flex direction="column" width="280px"> {children} </Flex> ); export default function App() { return ( <Rows> <ListItemView startContent={<Icon data={Envelope} size={16} />}>Inbox</ListItemView> </Rows> ); }
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.
import * as React from 'react'; import {Envelope} from '@gravity-ui/icons'; import {Icon, Flex, ListItemView} from '@gravity-ui/uikit'; const Rows = ({children}: React.PropsWithChildren) => ( <Flex direction="column" width="280px"> {children} </Flex> ); function SizesExample() { return ( <Rows> {(['s', 'm', 'l', 'xl'] as const).map((size) => ( <ListItemView key={size} size={size} startContent={<Icon data={Envelope} />}> {`Size ${size}`} </ListItemView> ))} </Rows> ); } export default function App() { return <SizesExample />; }
§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.
import * as React from 'react'; import {Clock, Envelope, Star} from '@gravity-ui/icons'; import {Icon, Label, Flex, ListItemView} from '@gravity-ui/uikit'; const Rows = ({children}: React.PropsWithChildren) => ( <Flex direction="column" width="280px"> {children} </Flex> ); function SlotsExample() { return ( <Rows> <ListItemView startContent={<Icon data={Envelope} size={16} />}>Inbox</ListItemView> <ListItemView startContent={<Icon data={Star} size={16} />} description="Flagged by you"> Starred </ListItemView> <ListItemView startContent={<Icon data={Clock} size={16} />} endContent={<Label>1</Label>}> Snoozed </ListItemView> </Rows> ); } export default function App() { return <SlotsExample />; }
<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 ownonClickincluded;selected— see Selection.
import * as React from 'react'; import {Flex, ListItemView} from '@gravity-ui/uikit'; const Rows = ({children}: React.PropsWithChildren) => ( <Flex direction="column" width="280px"> {children} </Flex> ); function StatesExample() { return ( <Rows> <ListItemView>Plain</ListItemView> <ListItemView active>Active: the keyboard cursor</ListItemView> <ListItemView hovered>Hovered</ListItemView> <ListItemView disabled>Disabled</ListItemView> </Rows> ); } export default function App() { return <StatesExample />; }
§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.
import * as React from 'react'; import {Flex, ListItemView} from '@gravity-ui/uikit'; const Rows = ({children}: React.PropsWithChildren) => ( <Flex direction="column" width="280px"> {children} </Flex> ); function SelectionExample() { return ( <Rows> <ListItemView selected selectionStyle="highlight"> Selected: highlight </ListItemView> <ListItemView selected selectionStyle="check"> Selected: check </ListItemView> <ListItemView selectionStyle="check">Not selected: the check is kept</ListItemView> <ListItemView selected selectionStyle="none"> Selected: shown by nothing </ListItemView> </Rows> ); } export default function App() { return <SelectionExample />; }
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.
import * as React from 'react'; import {Flex, ListItemView} from '@gravity-ui/uikit'; const Rows = ({children}: React.PropsWithChildren) => ( <Flex direction="column" width="280px"> {children} </Flex> ); function NestingExample() { const [collapsed, setCollapsed] = React.useState(false); return ( <Rows> <ListItemView collapsible collapsed={collapsed} onCollapseChange={setCollapsed}> Mailboxes </ListItemView> {collapsed ? null : ( <React.Fragment> <ListItemView nestedLevel={1}>Inbox</ListItemView> <ListItemView nestedLevel={1}>Starred</ListItemView> </React.Fragment> )} </Rows> ); } export default function App() { return <NestingExample />; }
§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.
import * as React from 'react'; import {Grip} from '@gravity-ui/icons'; import {Icon, Flex, ListItemView} from '@gravity-ui/uikit'; const Rows = ({children}: React.PropsWithChildren) => ( <Flex direction="column" width="280px"> {children} </Flex> ); function DragHandleExample() { return ( <Rows> {['Intro', 'Verse', 'Chorus'].map((part) => ( <ListItemView key={part} selectionStyle="check" dragHandle={ <span style={{display: 'flex', cursor: 'grab'}} aria-hidden="true"> <Icon data={Grip} size={12} /> </span> } > {part} </ListItemView> ))} </Rows> ); } export default function App() { return <DragHandleExample />; }
<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.
import * as React from 'react'; import {Envelope, TrashBin} from '@gravity-ui/icons'; import {Button, Icon, Flex, ListItemView} from '@gravity-ui/uikit'; const Rows = ({children}: React.PropsWithChildren) => ( <Flex direction="column" width="280px"> {children} </Flex> ); function CustomElementExample() { return ( <Rows> <ListItemView component="a" componentProps={{ href: 'https://gravity-ui.com', target: '_blank', rel: 'noreferrer', }} startContent={<Icon data={Envelope} size={16} />} > A row that is a link </ListItemView> <ListItemView component="button" componentProps={{type: 'button', onClick: () => console.log('onClick')}} startContent={<Icon data={TrashBin} size={16} />} endContent={ <Button view="flat" size="s" onClick={() => console.log('onEndContentClick')}> Undo </Button> } > A row that is a button </ListItemView> </Rows> ); } export default function App() { return <CustomElementExample />; }
§CSS API
| Name | Description |
|---|---|
--g-list-item-view-min-height | The minimum height of a row |
--g-list-item-view-border-radius | The corner radius |
--g-list-item-view-padding-inline | The inline padding |
--g-list-item-view-padding-block | The block padding |
--g-list-item-view-line-height | The line height of the content |
--g-list-item-view-controls-gap | The gap between the slots |
--g-list-item-view-controls-size | The size of the controls of a row |
--g-list-item-view-controls-border-radius | The corner radius of the controls |
--g-list-item-view-controls-icon-size | The size of the icons of the control |
--g-list-item-view-spacer-size | The indent of one nesting level |
--g-list-item-view-background-color | The background of a row |
--g-list-item-view-background-color-hover | The background of a hovered row |
--g-list-item-view-text-color | The colour of the content |
--g-list-item-view-description-color | The 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
| Name | Description | Type | Default |
|---|---|---|---|
| children | The content of the row | React.ReactNode | |
| size | The density of the row | 's' 'm' 'l' 'xl' | |
| startContent | The leading content: an icon, an avatar | React.ReactNode | |
| description | The second line of the row | React.ReactNode | |
| endContent | The trailing content: a label, a shortcut, an action | React.ReactNode | |
| dragHandle | The content of the outermost slot: the drag handle of a reorderable row | React.ReactNode | |
| active | The cursor of the keyboard on the row | boolean | |
| hovered | The hover highlight, forced by the props (false suppresses the :hover of the row) | boolean | |
| disabled | Whether the row is disabled | boolean | |
| selected | Whether the row is selected | boolean | |
| selectionStyle | How a selected row is shown | 'highlight' 'check' 'none' | |
| collapsible | Whether the row has the toggle of its branch | boolean | |
| collapsed | Whether the branch of the row is collapsed | boolean | |
| onCollapseChange | The handler of the toggle (a click on the row without onClick toggles the branch as well) | (collapsed: boolean) => void | |
| nestedLevel | The depth of the row in a tree: the indent in front of it | number | 0 |
| onClick | The handler of a click on the row (a click on a focusable descendant is not one) | React.MouseEventHandler<HTMLElement> | |
| component | The element the row is rendered as | React.ElementType | 'div' |
| componentProps | The props of that element | React.ComponentProps<component> | |
| isContainer | Renders the children as they are, without the layout of the slots | boolean | false |
| className | The CSS class of the row | string | |
| style | The inline style of the row | React.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.