Skip to content

Commit

Permalink
[base-ui][TextareaAutosize] Fix resizing instability (#41638)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeshanTamboli committed Apr 26, 2024
1 parent 341a6d2 commit f9a5847
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/mui-base/src/TextareaAutosize/TextareaAutosize.tsx
Expand Up @@ -65,6 +65,7 @@ const TextareaAutosize = React.forwardRef(function TextareaAutosize(
const { current: isControlled } = React.useRef(value != null);
const inputRef = React.useRef<HTMLTextAreaElement>(null);
const handleRef = useForkRef(forwardedRef, inputRef);
const heightRef = React.useRef<number | null>(null);
const shadowRef = React.useRef<HTMLTextAreaElement>(null);

const calculateTextareaStyles = React.useCallback(() => {
Expand Down Expand Up @@ -130,8 +131,12 @@ const TextareaAutosize = React.forwardRef(function TextareaAutosize(
return;
}

const outerHeightStyle = textareaStyles.outerHeightStyle;
const input = inputRef.current!;
input.style.height = `${textareaStyles.outerHeightStyle}px`;
if (heightRef.current !== outerHeightStyle) {
heightRef.current = outerHeightStyle;
input.style.height = `${outerHeightStyle}px`;
}
input.style.overflow = textareaStyles.overflowing ? 'hidden' : '';
}, [calculateTextareaStyles]);

Expand Down
8 changes: 8 additions & 0 deletions test/e2e/fixtures/TextareaAutosize/BasicTextareaAutosize.tsx
@@ -0,0 +1,8 @@
import * as React from 'react';
import { TextareaAutosize } from '@mui/base/TextareaAutosize';

function BasicTextareaAutosize() {
return <TextareaAutosize data-testid="textarea" />;
}

export default BasicTextareaAutosize;
32 changes: 32 additions & 0 deletions test/e2e/index.test.ts
Expand Up @@ -276,6 +276,38 @@ describe('e2e', () => {

expect(pageErrors.length).to.equal(0);
});

it('should not glitch when resizing', async () => {
await renderFixture('TextareaAutosize/BasicTextareaAutosize');

const textarea = await screen.getByTestId('textarea')!;

// Get the element's dimensions
const { x, y, width, height } = (await textarea.boundingBox())!;

// Calculate coordinates of bottom-right corner
const bottomRightX = x + width;
const bottomRightY = y + height;

// Get the initial height of textarea as a number
const initialHeight = await textarea.evaluate((textareaElement) =>
parseFloat(textareaElement.style.height),
);

// Move the mouse to the bottom-right corner, adjusting slightly to grab the resize handle
await page.mouse.move(bottomRightX - 5, bottomRightY - 5);

// Hold the mouse down without releasing the mouse button (mouseup) to grab the resize handle
await page.mouse.down();

// Move the mouse to resize the textarea
await page.mouse.move(bottomRightX + 50, bottomRightY + 50);

// Assert that the textarea height has increased after resizing
expect(
await textarea.evaluate((textareaElement) => parseFloat(textareaElement.style.height)),
).to.be.greaterThan(initialHeight);
});
});

describe('<TextField />', () => {
Expand Down

0 comments on commit f9a5847

Please sign in to comment.