basecamp / basecamp/trix

Issue with rails hotwire trix textarea resize

Open
#1,167 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
20k
Forks
1.1k
Avg merge
3d 12h
Merged PRs (30d)
13

Description

Hello, im trying to create a textarea resize grippie more or less like the SO for a trix textarea with rais stimulus so i set this controller below

the issue is when i pull down the textarea the textarea blow all the page, and i have any idea why, someone that know hotwire better can point my mistake please?

<%= form.rich_text_area :body, required: true, data: { target: 'textarea-resizer.textarea' } %>




// app/javascript/controllers/textarea_resizer_controller.js

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
static targets = ["textarea", "grippie"];

connect() {
this.textareaTarget.classList.add("processed");
this.setupGrippie();
}

setupGrippie() {
this.grippieTarget.addEventListener("mousedown", this.startDrag.bind(this));
}

startDrag(event) {
event.preventDefault();
this.lastMousePos = this.mousePosition(event).y;
this.staticOffset = this.textareaTarget.offsetHeight - this.lastMousePos;
this.textareaTarget.style.opacity = 0.25;

document.addEventListener("mousemove", this.performDrag.bind(this));
document.addEventListener("mouseup", this.endDrag.bind(this));
}

performDrag(event) {
const currentMousePos = this.mousePosition(event).y;
let newHeight = this.staticOffset + currentMousePos;

if (this.lastMousePos >= currentMousePos) {
newHeight -= 5;
}
this.lastMousePos = currentMousePos;
newHeight = Math.max(32, newHeight);

this.textareaTarget.style.height = `${newHeight}px`;

if (newHeight < 32) {
this.endDrag(event);
}
}

endDrag() {
document.removeEventListener("mousemove", this.performDrag.bind(this));
document.removeEventListener("mouseup", this.endDrag.bind(this));

this.textareaTarget.style.opacity = 1;
this.lastMousePos = 0;
this.staticOffset = null;
}

mousePosition(event) {
return {
x: event.clientX + document.documentElement.scrollLeft,
y: event.clientY + document.documentElement.scrollTop,
};
}
}

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the supplied markup and app/javascript/controllers/textarea_resizer_controller.js, then reproduce the drag behavior with the Trix rich text area. Determine why dragging causes the textarea to expand across the page; done means the resize grip changes the editor height without uncontrolled expansion.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, rails
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.