# List item

> A list row with icon, text and an end slot. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/list-item/

:::tip[In plain words]
One row of a list: an icon, a name and maybe something on the right.
:::

A list row: an icon, the text and `start`/`end` slots for any widget. It supports `selected`, `focused` (focus border), `disabled`, nesting with `indent`, and `on_right_press` for a context menu.

`ui.list_item` · Category: [Lists and menus](https://albakore.github.io/pyzed/library/#lists-and-menus) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#list_item)

**Code:**

```python
pz.column([
    ui.list_item("main.py", on_open, icon="file-code", icon_color="#3572a5", selected=True, inset=True),
    ui.list_item("README.md", on_open, icon="file-text", inset=True, end=ui.badge("M", tint="warning")),
    ui.list_item("Cargo.toml", on_open, icon="file-cog", inset=True, end=ui.kbd("ctrl-1", size="xs")),
    ui.list_item("old.txt", on_open, icon="file", inset=True, disabled=True),
], width=320)
```

## When to use

- For any vertical list: recent files, search results, settings categories, people.
- Use `inset=True` inside panels so the highlight has a margin.

## Usage

```python
import pyzed as pz
from pyzed import ui

class Recent(pz.App):
    def __init__(self):
        self.files = ["main.py", "app.py", "README.md"]
        self.current = "main.py"

    def view(self):
        return pz.column([
            ui.list_item(f, lambda f=f: setattr(self, "current", f), icon="file",
                         selected=f == self.current, inset=True)
            for f in self.files
        ])
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Content

```python
pz.container(ui.list_item("README.md", on_open), width=240)
```

```python
pz.container(ui.list_item("README.md", on_open, icon="file-text"), width=240)
```

```python
pz.container(ui.list_item("main.py", on_open, icon="file-code", icon_color="#3572a5"), width=240)
```

```python
pz.container(ui.list_item("app.py", on_open, icon="file-code", end=ui.badge("M", tint="warning")), width=240)
```

```python
pz.container(ui.list_item("Buy milk", on_open, start=ui.checkbox(True, on_toggle)), width=240)
```

```python
pz.container(ui.list_item("Delete project", on_open, icon="trash-2", color="error"), width=240)
```

### State

```python
pz.container(ui.list_item("main.py", on_open, icon="file-code", selected=True), width=240)
```

```python
pz.container(ui.list_item("main.py", on_open, icon="file-code", focused=True), width=240)
```

```python
pz.container(ui.list_item("old.txt", on_open, icon="file", disabled=True), width=240)
```

```python
pz.container(ui.list_item("main.py", on_open, icon="file-code", selected=True, inset=True), width=240)
```

```python
pz.container(ui.list_item("button.rs", on_open, icon="file-code", indent=24), width=240)
```

### States and events

`on_right_press` opens a context menu and `tooltip=` shows on hover.

```python
pz.container(ui.list_item("main.py", on_press, icon="file", description="src/app"), width=300)
```

```python
pz.container(ui.list_item("Rename me", on_press, icon="pencil", on_double_press=on_rename), width=300)
```

## Events

List item has **3 events of its own**, listed below. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

| Event | Handler gets | When |
| --- | --- | --- |
| `on_press` | `()` | Message or callable sent on press. `None` disables the control. |
| `on_right_press` | `()` | Right click. |
| `on_double_press` | `()` | Sent on a double click (open, rename…). |

**Handler gets** is what Pyzed passes to your callable: `()` means nothing. Any handler can also be a message (a value that is not callable), which goes to `update()`.

### Handling the events

The same events written three ways. **Imperative**: methods of the app change `self`. **Declarative**: the handlers produce messages and one `update()` decides. **Functional**: a pure function draws an immutable state and asks for changes with `set_state(...)`. See [Imperative, declarative and functional](https://albakore.github.io/pyzed/guides/code-styles/).

**Imperative:**

```python
class Recent(pz.App):
    def __init__(self):
        self.files = ["main.py", "app.py", "README.md"]
        self.current = "main.py"
        self.opened = None

    def view(self):
        return pz.column([
            ui.list_item(f, lambda f=f: setattr(self, "current", f), icon="file",   # on_press
                         selected=f == self.current, inset=True,
                         on_double_press=lambda f=f: setattr(self, "opened", f),
                         on_right_press=lambda f=f: print("menu for", f))
            for f in self.files
        ], width=280)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Select:
    path: str

@dataclass
class Open:
    path: str

class Recent(pz.App):
    files = ["main.py", "app.py", "README.md"]
    current, opened = "main.py", None

    def update(self, message):
        match message:
            case Select(path):
                self.current = path
            case Open(path):
                self.opened = path

    def view(self):
        return pz.column([ui.list_item(f, Select(f), icon="file", selected=f == self.current,
                                       on_double_press=Open(f)) for f in self.files], width=280)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    files: tuple = ("main.py", "app.py", "README.md")
    current: str = "main.py"

def file_row(name, selected, select):
    return ui.list_item(name, select, icon="file", selected=selected, inset=True)

def view(state, set_state):
    return pz.column([file_row(f, f == state.current, lambda f=f: set_state(current=f))
                      for f in state.files], width=280)
```

## Properties and API

### `ui.list_item`

```python
list_item(
    content,
    on_press=None,
    *,
    icon=None,
    icon_color=None,
    start=None,
    end=None,
    selected=False,
    focused=False,
    indent=0,
    height=26,
    color=None,
    inset=False,
    disabled=False,
    on_right_press=None,
    on_double_press=None,
    description=None,
    tooltip=None,
    theme=None,
)
```

A list row: icon, text and a slot at the end.

| Property | Default | Description |
| --- | --- | --- |
| `content` | *required* | Text or widget. |
| `icon` | `None` | Name of a Lucide icon (`pyzed icons` lists them). |
| `icon_color` | `None` | Icon color: a theme token or a hex color. |
| `start` | `None` | Widgets at the start (left). |
| `end` | `None` | Widgets at the end (right). |
| `selected` | `False` | Marks it as selected. |
| `focused` | `False` | Draws a focus border. |
| `indent` | `0` | Left indentation, in pixels. |
| `height` | `26` | Row height. |
| `color` | `None` | Text color. |
| `inset` | `False` | With margin and rounded corners (menus, palettes). |
| `disabled` | `False` | Disables it (ignores clicks). |
| `description` | `None` | A muted second line (the row grows to 40 px unless `height` is set). |
| `tooltip` | `None` | Text shown on hover. |
| `theme` | `None` | Theme to use instead of the current one (`pyzed.theme.current()`). |

Events: `on_press`, `on_right_press`, `on_double_press` ([events](#events)).

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#list_item)

## Related

- [List header](https://albakore.github.io/pyzed/library/list-header/): A small section title for lists, with an end slot.
- [Tree](https://albakore.github.io/pyzed/library/tree/): A collapsible file tree with icons and git colors.
- [Menu](https://albakore.github.io/pyzed/library/menu/): A context menu with icons, shortcuts and checks.
