mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-09 21:16:26 +08:00
Make textarea input autosized (#1552)
This commit is contained in:
parent
dd68f7ff1c
commit
97506535c5
3 changed files with 30 additions and 1 deletions
|
@ -12,6 +12,7 @@ import KeyboardControl from "./keyboard_control";
|
||||||
import MarkdownRenderer from "./markdown_renderer";
|
import MarkdownRenderer from "./markdown_renderer";
|
||||||
import ScrollOnUpdate from "./scroll_on_update";
|
import ScrollOnUpdate from "./scroll_on_update";
|
||||||
import Session from "./session";
|
import Session from "./session";
|
||||||
|
import TextareaAutosize from "./textarea_autosize";
|
||||||
import Timer from "./timer";
|
import Timer from "./timer";
|
||||||
import UserForm from "./user_form";
|
import UserForm from "./user_form";
|
||||||
import VirtualizedLines from "./virtualized_lines";
|
import VirtualizedLines from "./virtualized_lines";
|
||||||
|
@ -31,6 +32,7 @@ export default {
|
||||||
MarkdownRenderer,
|
MarkdownRenderer,
|
||||||
ScrollOnUpdate,
|
ScrollOnUpdate,
|
||||||
Session,
|
Session,
|
||||||
|
TextareaAutosize,
|
||||||
Timer,
|
Timer,
|
||||||
UserForm,
|
UserForm,
|
||||||
VirtualizedLines,
|
VirtualizedLines,
|
||||||
|
|
25
assets/js/hooks/textarea_autosize.js
Normal file
25
assets/js/hooks/textarea_autosize.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
const BORDER_HEIGHT = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A hook that automatically matches textarea height to its content.
|
||||||
|
*/
|
||||||
|
const TextareaAutosize = {
|
||||||
|
mounted() {
|
||||||
|
this.autosize();
|
||||||
|
|
||||||
|
this.el.addEventListener("input", (event) => {
|
||||||
|
this.autosize();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
updated() {
|
||||||
|
this.autosize();
|
||||||
|
},
|
||||||
|
|
||||||
|
autosize() {
|
||||||
|
this.el.style.height = "0px";
|
||||||
|
this.el.style.height = `${this.el.scrollHeight + 2 * BORDER_HEIGHT}px`;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default TextareaAutosize;
|
|
@ -98,9 +98,11 @@ defmodule LivebookWeb.Output.InputComponent do
|
||||||
defp input(%{attrs: %{type: :textarea}} = assigns) do
|
defp input(%{attrs: %{type: :textarea}} = assigns) do
|
||||||
~H"""
|
~H"""
|
||||||
<textarea
|
<textarea
|
||||||
|
id={@id}
|
||||||
data-el-input
|
data-el-input
|
||||||
class="input min-h-[200px] tiny-scrollbar"
|
class="input min-h-[38px] max-h-[300px] tiny-scrollbar"
|
||||||
name="value"
|
name="value"
|
||||||
|
phx-hook="TextareaAutosize"
|
||||||
phx-debounce="blur"
|
phx-debounce="blur"
|
||||||
phx-target={@myself}
|
phx-target={@myself}
|
||||||
spellcheck="false"
|
spellcheck="false"
|
||||||
|
|
Loading…
Add table
Reference in a new issue