Skip to content

Commit

Permalink
Textarea: debounce ResizeObserver for å unngå feilmelding (#2685)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalvorHaugan authored Jan 31, 2024
1 parent 34acbb8 commit 56abe56
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-hounds-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Textarea: debounce ResizeObserver for å unngå feilmelding
11 changes: 5 additions & 6 deletions @navikt/core/react/src/util/TextareaAutoSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
});
};

const handleResize = () => {
const handleResize = debounce(() => {
renders.current = 0;

if (inputRef.current?.style.height || inputRef.current?.style.width) {
Expand All @@ -179,13 +179,12 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
}

syncHeightWithFlushSync();
};
});

const debounceHandleResize = debounce(handleResize);
const input = inputRef.current!;
const containerWindow = ownerWindow(input);

containerWindow.addEventListener("resize", debounceHandleResize);
containerWindow.addEventListener("resize", handleResize);

let resizeObserver: ResizeObserver;
if (typeof ResizeObserver !== "undefined") {
Expand All @@ -194,8 +193,8 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
}

return () => {
debounceHandleResize.clear();
containerWindow.removeEventListener("resize", debounceHandleResize);
handleResize.clear();
containerWindow.removeEventListener("resize", handleResize);
if (resizeObserver) {
resizeObserver.disconnect();
}
Expand Down

0 comments on commit 56abe56

Please sign in to comment.