# Scrollable

> Scrolls content that doesn't fit. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/scrollable/

:::tip[In plain words]
Lets you scroll content that doesn't fit, with a scroll bar.
:::

Makes its content scrollable vertically, horizontally or `"both"`. Give it an `id` to scroll from code (`pz.Command.scroll_to`), `anchor_bottom=True` to stick to the end (chats, logs), and style the rail and scroller with the theme or your own colors.

`pz.scrollable` · Category: [Layout](https://albakore.github.io/pyzed/library/#layout) · [Full reference →](https://albakore.github.io/pyzed/reference/widgets/#scrollable)

**Code:**

```python
pz.container(pz.scrollable(
    pz.column([ui.list_item(f"Item {i}", on_open, icon="file", inset=True) for i in range(1, 30)]),
    height=220,
), width=300)
```

## When to use

- For long lists, documents and any content taller than its space.

## Usage

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

pz.container(pz.scrollable(
    pz.column([ui.list_item(f"Item {i}", on_open, icon="file", inset=True) for i in range(1, 30)]),
    height=220,
), width=300)
```

## 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.
:::

### Direction

```python
pz.container(pz.scrollable(pz.column([ui.list_item(f"Item {i}", icon="file", inset=True)
                                       for i in range(1, 30)]), height=130), width=240)
```

```python
pz.container(pz.scrollable(pz.container(pz.row([ui.chip(f"tag {i}") for i in range(1, 25)], spacing=6),
                                        padding=(0, 0, 14, 0)),
                           direction="horizontal"), width=300)
```

## Events

Scrollable has **1 event 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_scroll` | `(info)` | Callable that receives a dict with `x`, `y`, `relative_x`, `relative_y`, `width`, `height`, `content_width` and `content_height`. |

**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 Feed(pz.App):
    def __init__(self):
        self.items = [f"Post {i}" for i in range(1, 30)]
        self.at_end = False

    def scrolled(self, info):            # on_scroll: x, y, relative_x, relative_y, sizes
        self.at_end = info["relative_y"] > 0.95

    def view(self):
        return pz.column([
            pz.scrollable(pz.column([ui.list_item(p, icon="file") for p in self.items]),
                          height=200, on_scroll=self.scrolled),
            ui.muted("You reached the end" if self.at_end else "Scroll down"),
        ], spacing=6, width=300)
```

**Declarative:**

```python
from dataclasses import dataclass

@dataclass
class Scrolled:
    info: dict

class Feed(pz.App):
    items = [f"Post {i}" for i in range(1, 30)]

    def update(self, message):
        match message:
            case Scrolled(info) if info["relative_y"] > 0.95:
                self.items = self.items + [f"Post {len(self.items) + 1}"]   # load more

    def view(self):
        return pz.scrollable(pz.column([ui.list_item(p) for p in self.items]), height=200,
                             on_scroll=Scrolled)
```

**Functional:**

```python
from dataclasses import dataclass

@dataclass(frozen=True)
class State:
    progress: float = 0.0

def view(state, set_state):
    return pz.column([
        ui.progress(state.progress),
        pz.scrollable(pz.column([ui.list_item(f"Post {i}") for i in range(30)]), height=200,
                      on_scroll=lambda info: set_state(progress=info["relative_y"])),
    ], spacing=6, width=300)
```

## Properties and API

### `pz.scrollable`

```python
scrollable(
    content,
    *,
    direction='vertical',
    width=None,
    height=None,
    padding=None,
    id=None,
    anchor_bottom=False,
    rail=None,
    scroller=None,
    scroller_hover=None,
    scrollbar_width=8.0,
    scroller_width=None,
    margin=0.0,
    on_scroll=None,
)
```

Makes its content scrollable.

| Property | Default | Description |
| --- | --- | --- |
| `content` | *required* | Content: a `Widget`, a string or a number (converted to text). |
| `direction` | `"vertical"` | `"vertical"`, `"horizontal"` or `"both"`. |
| `width` | `None` | Width: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `height` | `None` | Height: pixels, `"fill"`, `"shrink"` or `"fill:N"` (proportion). |
| `padding` | `None` | Inner padding: `8`, `(vertical, horizontal)` or `(top, right, bottom, left)`. |
| `id` | `None` | Stable identifier used to focus or scroll it with a `Command`. |
| `anchor_bottom` | `False` | Keeps the scroll pinned to the end (logs, terminals, chats). |
| `rail` | `None` | Scrollbar rail color. |
| `scroller` | `None` | Scroller color. |
| `scroller_hover` | `None` | Scroller color on hover. |
| `scrollbar_width` | `8.0` | Scrollbar width. |
| `scroller_width` | `None` | Scroller width (defaults to the scrollbar's). |
| `margin` | `0.0` | Margin between the scrollbar and the content. |

Events: `on_scroll` ([events](#events)).

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

## Related

- [Column](https://albakore.github.io/pyzed/library/column/): Stacks children vertically.
- [List item](https://albakore.github.io/pyzed/library/list-item/): A list row with icon, text and an end slot.
