Skip to content

Commit 0db2721

Browse files
Add documentation for rx.auto_scroll (#1315)
* Add documentation for rx.auto_scroll Co-Authored-By: Alek Petuskey <[email protected]> * Remove React reference from auto_scroll documentation Co-Authored-By: Alek Petuskey <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek Petuskey <[email protected]>
1 parent c2e7086 commit 0db2721

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

docs/components/rendering_iterables.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ from pcweb.pages import docs
88

99
Recall again from the [basics]({docs.getting_started.basics.path}) that we cannot use Python `for` loops when referencing state vars in Reflex. Instead, use the `rx.foreach` component to render components from a collection of data.
1010

11+
For dynamic content that should automatically scroll to show the newest items, consider using the [auto scroll]({docs.library.dynamic_rendering.auto_scroll.path}) component together with `rx.foreach`.
12+
1113
```python demo exec
1214
class IterState(rx.State):
1315
color: list[str] = [
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)