Skip to content

Commit

Permalink
Adds autoscroll param to gr.Textbox() (#5488)
Browse files Browse the repository at this point in the history
* autoscroll param

* add changeset

* fix

* test fix

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
dawoodkhan82 and gradio-pr-bot committed Sep 12, 2023
1 parent afcf3c4 commit 8909e42
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/curly-files-know.md
@@ -0,0 +1,6 @@
---
"@gradio/textbox": minor
"gradio": minor
---

feat:Adds `autoscroll` param to `gr.Textbox()`
6 changes: 6 additions & 0 deletions gradio/components/textbox.py
Expand Up @@ -67,6 +67,7 @@ def __init__(
visible: bool = True,
elem_id: str | None = None,
autofocus: bool = False,
autoscroll: bool = True,
elem_classes: list[str] | str | None = None,
type: Literal["text", "password", "email"] = "text",
text_align: Literal["left", "right"] | None = None,
Expand Down Expand Up @@ -96,6 +97,7 @@ def __init__(
text_align: How to align the text in the textbox, can be: "left", "right", or None (default). If None, the alignment is left if `rtl` is False, or right if `rtl` is True. Can only be changed if `type` is "text".
rtl: If True and `type` is "text", sets the direction of the text to right-to-left (cursor appears on the left of the text). Default is False, which renders cursor on the right.
show_copy_button: If True, includes a copy button to copy the text in the textbox. Only applies if show_label is True.
autoscroll: If True, will automatically scroll to the bottom of the textbox when the value changes.
"""
if type not in ["text", "password", "email"]:
raise ValueError('`type` must be one of "text", "password", or "email".')
Expand All @@ -109,6 +111,7 @@ def __init__(
self.show_copy_button = show_copy_button
self.autofocus = autofocus
self.select: EventListenerMethod
self.autoscroll = autoscroll
"""
Event listener for when the user selects text in the Textbox.
Uses event data gradio.SelectData to carry `value` referring to selected substring, and `index` tuple referring to selected range endpoints.
Expand Down Expand Up @@ -147,6 +150,7 @@ def get_config(self):
"container": self.container,
"text_align": self.text_align,
"rtl": self.rtl,
"autoscroll": self.autoscroll,
**IOComponent.get_config(self),
}

Expand All @@ -169,6 +173,7 @@ def update(
rtl: bool | None = None,
show_copy_button: bool | None = None,
autofocus: bool | None = None,
autoscroll: bool | None = None,
):
return {
"lines": lines,
Expand All @@ -188,6 +193,7 @@ def update(
"autofocus": autofocus,
"text_align": text_align,
"rtl": rtl,
"autoscroll": autoscroll,
"__type__": "update",
}

Expand Down
2 changes: 2 additions & 0 deletions js/textbox/interactive/InteractiveTextbox.svelte
Expand Up @@ -49,6 +49,7 @@
export let rtl = false;
export let text_align: "left" | "right" | undefined = undefined;
export let autofocus = false;
export let autoscroll = true;
</script>

<Block
Expand Down Expand Up @@ -79,6 +80,7 @@
{show_copy_button}
{autofocus}
{container}
{autoscroll}
on:change={() => gradio.dispatch("change", value)}
on:input={() => gradio.dispatch("input")}
on:submit={() => gradio.dispatch("submit")}
Expand Down
9 changes: 5 additions & 4 deletions js/textbox/shared/Textbox.svelte
Expand Up @@ -26,11 +26,12 @@
export let rtl = false;
export let autofocus = false;
export let text_align: "left" | "right" | undefined = undefined;
export let autoscroll = true;
let el: HTMLTextAreaElement | HTMLInputElement;
let copied = false;
let timer: NodeJS.Timeout;
let autoscroll: boolean;
let can_scroll: boolean;
$: value, el && lines !== max_lines && resize({ target: el });
Expand All @@ -46,11 +47,11 @@
}>();
beforeUpdate(() => {
autoscroll = el && el.offsetHeight + el.scrollTop > el.scrollHeight - 100;
can_scroll = el && el.offsetHeight + el.scrollTop > el.scrollHeight - 100;
});
const scroll = (): void => {
if (autoscroll) {
if (can_scroll && autoscroll) {
el.scrollTo(0, el.scrollHeight);
}
};
Expand All @@ -62,7 +63,7 @@
}
}
afterUpdate(() => {
if (autoscroll) {
if (can_scroll && autoscroll) {
scroll();
}
value_is_output = false;
Expand Down
2 changes: 2 additions & 0 deletions js/textbox/static/StaticTextbox.svelte
Expand Up @@ -35,6 +35,7 @@
export let rtl = false;
export let text_align: "left" | "right" | undefined = undefined;
export let autofocus = false;
export let autoscroll = true;
</script>

<Block
Expand Down Expand Up @@ -65,6 +66,7 @@
{show_copy_button}
{autofocus}
{container}
{autoscroll}
on:change={() => gradio.dispatch("change", value)}
on:input={() => gradio.dispatch("input")}
on:submit={() => gradio.dispatch("submit")}
Expand Down
1 change: 1 addition & 0 deletions test/test_components.py
Expand Up @@ -106,6 +106,7 @@ def test_component_functions(self):
"rtl": False,
"text_align": None,
"autofocus": False,
'autoscroll': True,
}

@pytest.mark.asyncio
Expand Down

0 comments on commit 8909e42

Please sign in to comment.