Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

1009 B
Raw Blame
import { type RefObject, useLayoutEffect } from "react";

function resize(input: HTMLTextAreaElement) {
  // Reset before measuring so deleting text can shrink the field. CSS caps the visible height.
  input.style.height = "0px";
  input.style.height = `${input.scrollHeight}px`;
}

export function useTextareaHeight(ref: RefObject<HTMLTextAreaElement | null>, text: string) {
  // biome-ignore lint/correctness/useExhaustiveDependencies: measure after the controlled value commits
  useLayoutEffect(() => {
    if (ref.current) resize(ref.current);
  }, [ref, text]);

  useLayoutEffect(() => {
    const input = ref.current;
    if (!input) return;
    let width = input.clientWidth;
    const observer = new ResizeObserver(() => {
      // Height changes from our own measurement must not trigger another resize.
      if (input.clientWidth === width) return;
      width = input.clientWidth;
      resize(input);
    });
    observer.observe(input);
    return () => observer.disconnect();
  }, [ref]);
}