Skip to content

fix(react-textarea): Fixed useless div in Body - #1794

Closed
kachurun wants to merge 2 commits into
CopilotKit:mainfrom
kachurun:fix/HoveringToolbarFix
Closed

kachurun wants to merge 2 commits into
CopilotKit:mainfrom
kachurun:fix/HoveringToolbarFix

Conversation

@kachurun

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR primarily fixes the issue where the react-textarea component was injecting an unnecessary div into the body.

It also addresses several bugs related to window positioning — the position was being calculated before the content inside the window was rendered, which in some cases caused the window to overlap the content.

This PR can be merged, but I don’t consider the work complete — in my opinion, the component needs a full rewrite.

Here are the main issues still in the code and/or logic:

HoveringToolbar is bundled into react-textarea by default
It shouldn’t be. I expect to control this behavior myself and insert the HoveringToolbar wherever I need it. For example, I might want to render a button inside the input that triggers the window, or even trigger the UI with a hotkey from anywhere in the app — not just when a textarea is focused.

No proper positioning logic
The window lacks flexibility in how it’s positioned. I generally avoid extra dependencies, but in this case, Floating UI would be very helpful. For example, Radix UI uses it under the hood.

Magic numbers in the code
Take verticalOffsetFromCorner, for example — it’s not configurable and doesn’t really make sense, so I just zeroed it out. You can achieve the same effect using padding on the root element (which already has 0.5em on all sides).

Breaks in complex layouts
The window cannot position itself properly if it’s rendered inside an iframe or if the target element lives inside a custom scroll container.

And that’s not even the full list. Fixing all of this will require a serious rewrite, which would be a breaking change.
I’d be happy to take that on if I get the green light.

@vercel

vercel Bot commented May 11, 2025

Copy link
Copy Markdown
Contributor

@kachurun is attempting to deploy a commit to the CopilotKit Team on Vercel.

A member of the Team first needs to authorize it.

@kachurun kachurun changed the title react-textarea: Fixed useless div in Body fix(react-textarea): Fixed useless div in Body May 11, 2025
const minGapFromEdge = 60;
const verticalOffsetFromCorner = 35;
const horizontalOffsetFromCorner = 15;
let top = rect.top + window.scrollY - el.offsetHeight + verticalOffsetFromCorner;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

el.offsetHeight here doesn't make any sense because it renders as an empty div, so its height will be equal to 1em by default (just block paddings). Also, it's weird to position it 35px from the top side of the selection. It must use the bottom if we're trying to position the modal below the selection.

Comment on lines -67 to -68
if (top < minGapFromEdge) {
top = rect.bottom + window.scrollY + minGapFromEdge;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is possible only in a crazy scenario when the textarea is positioned above the start of the document. I don't think it's worth caring about this case. Fix me if I'm wrong and I rewrite it like let top = Math.max(verticalOffsetFromCorner, rect.bottom + window.scrollY + verticalOffsetFromCorner);

Comment on lines -62 to -64
const minGapFromEdge = 60;
const verticalOffsetFromCorner = 35;
const horizontalOffsetFromCorner = 15;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic numbers are uncontrolled by the user. And what is "edge" and what is "corner" is pretty unclear. I reset it but left the variables to make future improvements easier.


el.style.top = `${top}px`;
el.style.left = `${left}px`;
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason to recalculate it on every selection change (e.g., input in the textarea).

editorState: EditingEditorState;
apiConfig: InsertionEditorApiConfig;
performInsertion: (insertedText: string) => void;
closeWindow: () => void;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This thing isn't used anywhere.

Comment on lines +110 to 112
if (!isShown) {
return null;
}

@kachurun kachurun May 11, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to portal an empty <Menu> until we need to show the modal.

@kachurun
kachurun force-pushed the fix/HoveringToolbarFix branch from ea3626a to fc83c68 Compare May 11, 2025 06:42
@changesets-bot-copilotkit

Copy link
Copy Markdown

⏭️ Changeset Not Required

Latest commit: fc83c68

No changes in this PR affected the @copilitkit/* packages. Merging this PR will not cause a version bump for any packages.

Changeset is not required for this PR.

@suhasdeshpande

Copy link
Copy Markdown
Contributor

@kachurun Thanks for opening this. If you are, let's do all the changes, if you prefer multiple PRs, we can do that as well. We have not seen many instances of textarea component being used and hece it was not touched in long time. We would be more than happy to take this contribution. :)

@kachurun

Copy link
Copy Markdown
Contributor Author

@suhasdeshpande Cool, I’ll make the next changes, but maybe not within the textarea component.
I think it would be better to create a simpler "contenteditable" basic component and separate the HoveringToolbar from the textarea, untangling it from some dependencies (like its dependency on Slate contexts). This is a pretty big task and should be done in separate PRs and discussed with the team first. After that, the textarea is basically just a styled contenteditable component, maybe with the HoveringToolbar inside for compatibility (though I’d prefer not to do that).

So, for now, I think we should merge this PR to fix current minor issue and not expect all these changes in the upcoming commits to this one.

@tylerslaton

Copy link
Copy Markdown
Contributor

This has been merged as apart of this PR #1868, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants