|
| 1 | +```python exec |
| 2 | +import reflex as rx |
| 3 | +``` |
| 4 | + |
| 5 | +# Auto Scroll |
| 6 | + |
| 7 | +The `rx.auto_scroll` component is a div that automatically scrolls to the bottom when new content is added. This is useful for chat interfaces, logs, or any container where new content is dynamically added and you want to ensure the most recent content is visible. |
| 8 | + |
| 9 | +## Basic Usage |
| 10 | + |
| 11 | +```python demo exec |
| 12 | +import reflex as rx |
| 13 | + |
| 14 | +class AutoScrollState(rx.State): |
| 15 | + messages: list[str] = ["Initial message"] |
| 16 | + |
| 17 | + def add_message(self): |
| 18 | + self.messages.append(f"New message #{len(self.messages) + 1}") |
| 19 | + |
| 20 | +def auto_scroll_example(): |
| 21 | + return rx.vstack( |
| 22 | + rx.auto_scroll( |
| 23 | + rx.foreach( |
| 24 | + AutoScrollState.messages, |
| 25 | + lambda message: rx.box( |
| 26 | + message, |
| 27 | + padding="0.5em", |
| 28 | + border_bottom="1px solid #eee", |
| 29 | + width="100%" |
| 30 | + ) |
| 31 | + ), |
| 32 | + height="200px", |
| 33 | + width="300px", |
| 34 | + border="1px solid #ddd", |
| 35 | + border_radius="md", |
| 36 | + ), |
| 37 | + rx.button("Add Message", on_click=AutoScrollState.add_message), |
| 38 | + width="300px", |
| 39 | + align_items="center", |
| 40 | + ) |
| 41 | +``` |
| 42 | + |
| 43 | +The `auto_scroll` component automatically scrolls to show the newest content when it's added. In this example, each time you click "Add Message", a new message is added to the list and the container automatically scrolls to display it. |
| 44 | + |
| 45 | +## When to Use Auto Scroll |
| 46 | + |
| 47 | +- **Chat applications**: Keep the chat window scrolled to the most recent messages. |
| 48 | +- **Log viewers**: Automatically follow new log entries as they appear. |
| 49 | +- **Feed interfaces**: Keep the newest content visible in dynamically updating feeds. |
| 50 | + |
| 51 | +## Props |
| 52 | + |
| 53 | +`rx.auto_scroll` is based on the `rx.div` component and inherits all of its props. By default, it sets `overflow="auto"` to enable scrolling. |
| 54 | + |
| 55 | +Some common props you might use with `auto_scroll`: |
| 56 | + |
| 57 | +- `height`: Set the height of the scrollable container. |
| 58 | +- `width`: Set the width of the scrollable container. |
| 59 | +- `padding`: Add padding inside the container. |
| 60 | +- `border`: Add a border around the container. |
| 61 | +- `border_radius`: Round the corners of the container. |
| 62 | + |
| 63 | +## How It Works |
| 64 | + |
| 65 | +The component tracks when new content is added and maintains the scroll position in two scenarios: |
| 66 | + |
| 67 | +1. When the user is already near the bottom of the content (within 50 pixels), it will scroll to the bottom when new content is added. |
| 68 | +2. When the container didn't have a scrollbar before but does now (due to new content), it will automatically scroll to the bottom. |
| 69 | + |
| 70 | +This behavior ensures that users can scroll up to view older content without being forced back to the bottom, while still automatically following new content in most cases. |
0 commit comments