Skip to content

Commit

Permalink
🐛 Textarea: Skru av autosize ved manuell resize (#2518)
Browse files Browse the repository at this point in the history
* 🐛 Textarea: Skru av autosize ved manuell resize

* typer stories

* ikke debounce resize
  • Loading branch information
HalvorHaugan authored Nov 28, 2023
1 parent 21d5ddd commit 8dda590
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/fast-monkeys-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@navikt/ds-react": patch
"@navikt/ds-css": patch
---

:bug: Textarea: Skru av autosize ved manuell resize
6 changes: 6 additions & 0 deletions @navikt/core/css/form/textarea.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
}

.navds-textarea__wrapper {
--__ac-textarea-height: initial;

position: relative;
min-width: 100%;
}

.navds-textarea__input:first-child {
height: var(--__ac-textarea-height);
}

.navds-textarea__input {
appearance: none;
padding: var(--a-spacing-2);
Expand Down
40 changes: 26 additions & 14 deletions @navikt/core/react/src/form/stories/textarea.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, StoryObj } from "@storybook/react";
import React from "react";
import { Meta, StoryFn, StoryObj } from "@storybook/react";
import React, { useState } from "react";
import { Button } from "../../button";
import { Textarea } from "../index";

Expand Down Expand Up @@ -38,11 +38,11 @@ export const Default: StoryObj<typeof Textarea> = {
},
};

export const Small = () => {
export const Small: StoryFn = () => {
return <Textarea size="small" label="Ipsum enim quis culpa" />;
};

export const Description = () => {
export const Description: StoryFn = () => {
return (
<div className="colgap">
<Textarea
Expand All @@ -58,7 +58,7 @@ export const Description = () => {
);
};

export const Error = () => {
export const Error: StoryFn = () => {
return (
<div className="colgap">
<Textarea
Expand Down Expand Up @@ -89,7 +89,7 @@ export const Error = () => {
);
};

export const Disabled = () => {
export const Disabled: StoryFn = () => {
return (
<div className="colgap">
<Textarea label="Ipsum enim quis culpa" disabled />
Expand All @@ -98,19 +98,19 @@ export const Disabled = () => {
);
};

export const HideLabel = () => {
export const HideLabel: StoryFn = () => {
return <Textarea label="Ipsum enim quis culpa" hideLabel />;
};

export const MaxLength = () => {
export const MaxLength: StoryFn = () => {
return <Textarea maxLength={50} label="Ipsum enim quis culpa" />;
};

export const MinRows = () => {
export const MinRows: StoryFn = () => {
return <Textarea minRows={5} label="Ipsum enim quis culpa" />;
};

export const MaxRows = () => {
export const MaxRows: StoryFn = () => {
return (
<Textarea
maxRows={3}
Expand All @@ -120,11 +120,23 @@ export const MaxRows = () => {
);
};

export const Resize = () => {
export const Resize: StoryFn = () => {
return <Textarea resize label="Ipsum enim quis culpa" />;
};

export const Readonly = () => {
export const Controlled: StoryFn = () => {
const [value, setValue] = useState("");
return (
<Textarea
resize
label="Ipsum enim quis culpa"
value={value}
onChange={(event) => setValue(event.currentTarget.value)}
/>
);
};

export const Readonly: StoryFn = () => {
return (
<div className="colgap">
<Textarea
Expand All @@ -143,7 +155,7 @@ export const Readonly = () => {
);
};

export const AutoScrollbar = (props) => (
export const AutoScrollbar: StoryFn<typeof Textarea> = (props) => (
<div
style={{
border: "1px solid lightGreen",
Expand All @@ -156,12 +168,12 @@ export const AutoScrollbar = (props) => (
<h1>Header</h1>
</div>
<Textarea
{...props}
resize
label="Textarea with autoScrollbar"
description="Description"
maxLength={30}
UNSAFE_autoScrollbar
{...props}
/>
<div style={{ border: "1px dashed gray" }}>
<Button>Send</Button>
Expand Down
4 changes: 2 additions & 2 deletions @navikt/core/react/src/react-css.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "react";

declare module "react" {
interface CSSProperties {
[key: `--ac-${string}`]: string | undefined;
[key: `--__ac-${string}`]: string | undefined;
[key: `--ac-${string}`]: string | number | undefined;
[key: `--__ac-${string}`]: string | number | undefined;
}
}
31 changes: 24 additions & 7 deletions @navikt/core/react/src/util/TextareaAutoSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(

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

if (inputRef.current?.style.height || inputRef.current?.style.width) {
// User has resized manually
if (inputRef.current?.style.overflow === "hidden") {
setState((oldState) => ({ ...oldState, overflow: false })); // The state update isn't important, we just need to trigger a rerender
}
return;
}

syncHeightWithFlushSync();
};

Expand Down Expand Up @@ -223,6 +232,20 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
}
};

const mainStyle: React.CSSProperties = {
["--__ac-textarea-height"]: state.outerHeightStyle + "px",
// Need a large enough difference to allow scrolling.
// This prevents infinite rendering loop.
overflow:
state.overflow &&
!autoScrollbar &&
!inputRef.current?.style.height &&
!inputRef.current?.style.width
? "hidden"
: undefined,
...style,
};

return (
<>
<textarea
Expand All @@ -231,13 +254,7 @@ const TextareaAutosize = forwardRef<HTMLTextAreaElement, TextareaAutosizeProps>(
ref={handleRef}
// Apply the rows prop to get a "correct" first SSR paint
rows={minRows}
style={{
height: state.outerHeightStyle,
// Need a large enough difference to allow scrolling.
// This prevents infinite rendering loop.
overflow: state.overflow && !autoScrollbar ? "hidden" : undefined,
...style,
}}
style={mainStyle}
{...other}
className={className}
/>
Expand Down

0 comments on commit 8dda590

Please sign in to comment.