Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

feat: Autogrowing Textarea (#2424)

Ivan Starkove930c2b34af2
diff --git a/packages/design-system/src/components/ai-input/autogrow-text-area.stories.tsx b/packages/design-system/src/components/ai-input/autogrow-text-area.stories.tsx
new file mode 100644
index 0000000000..b2354272fb
--- /dev/null
+++ b/packages/design-system/src/components/ai-input/autogrow-text-area.stories.tsx
@@ -0,0 +1,52 @@
+import { AutogrowTextArea } from "./autogrow-text-area";
+import { Box } from "../box";
+import { Grid } from "../grid";
+import { ScrollArea } from "../scroll-area";
+import { StorySection, StoryGrid } from "../storybook";
+
+export default {
+ title: "Library/Autogrow Text Area",
+};
+
+export const Demo = () => {
+ return (
+ <>
+ <StorySection title="Autogrow textarea">
+ <StoryGrid css={{ width: 200 }}>
+ <Box css={{ backgroundColor: "black", color: "white", padding: 8 }}>
+ <AutogrowTextArea
+ defaultValue={`1\n2\n3\n4\n5\n6\n7\n8`}
+ placeholder="Enter value..."
+ />
+ </Box>
+ </StoryGrid>
+ <Box css={{ height: 16 }} />
+ <StoryGrid css={{ width: 200 }}>
+ <Box css={{ backgroundColor: "black", color: "white", padding: 8 }}>
+ <AutogrowTextArea
+ state="invalid"
+ value="invalid value"
+ placeholder="Enter value..."
+ />
+ </Box>
+ </StoryGrid>
+ </StorySection>
+
+ <StorySection title="Autogrow textarea with scroll and max-height">
+ <StoryGrid css={{ width: 200 }}>
+ <Box css={{ backgroundColor: "black", color: "white", padding: 8 }}>
+ <Grid>
+ <ScrollArea css={{ maxHeight: 100 }}>
+ <AutogrowTextArea
+ defaultValue={`1\n2\n3\n4\n5\n6\n7\n8`}
+ placeholder="Enter value..."
+ />
+ </ScrollArea>
+ </Grid>
+ </Box>
+ </StoryGrid>
+ </StorySection>
+ </>
+ );
+};
+Demo.storyName = "Autogrow Text Area";
diff --git a/packages/design-system/src/components/ai-input/autogrow-text-area.tsx b/packages/design-system/src/components/ai-input/autogrow-text-area.tsx
new file mode 100644
index 0000000000..baee1a5e36
--- /dev/null
+++ b/packages/design-system/src/components/ai-input/autogrow-text-area.tsx
@@ -0,0 +1,97 @@
+/**
+ * Implementation of the autogrowing text input for the AI
+ * https://www.figma.com/file/xCBegXEWxROLqA1Y31z2Xo/%F0%9F%93%96-Webstudio-Design-Docs?node-id=7586%3A48784&mode=dev
+ */
+
+import { type ComponentProps, type Ref, forwardRef } from "react";
+import { type CSS, css, theme } from "../../stitches.config";
+import { textVariants } from "../text";
+import { Grid } from "../grid";
+import { useControllableState } from "@radix-ui/react-use-controllable-state";
+
+const commonStyle = css(textVariants.regular, {
+ border: "none",
+ paddingRight: 0,
+ paddingLeft: 0,
+ paddingTop: 0,
+ paddingBottom: 0,
+ boxSizing: "border-box",
+ gridArea: "1 / 1 / 2 / 2",
+ background: "transparent",
+ color: "inherit",
+ textWrap: "wrap",
+ overflowWrap: "break-word",
+ whiteSpace: "pre-wrap",
+ overflow: "hidden",
+});
+
+const style = css(commonStyle, {
+ outline: "none",
+ resize: "none",
+ "&::placeholder": {
+ color: theme.colors.foregroundContrastSubtle,
+ },
+ "&:disabled": {
+ color: theme.colors.foregroundDisabled,
+ background: theme.colors.backgroundInputDisabled,
+ },
+ variants: {
+ state: {
+ invalid: {
+ color: theme.colors.foregroundDestructive,
+ },
+ },
+ },
+});
+
+type Props = Omit<
+ ComponentProps<"textarea">,
+ "value" | "defaultValue" | "onChange"
+> & {
+ css?: CSS;
+ state?: "invalid";
+ value?: string;
+ defaultValue?: string;
+ onChange?: (value: string) => void;
+};
+
+export const AutogrowTextArea = forwardRef(
+ (
+ { css, className, state, value, defaultValue, onChange, ...props }: Props,
+ ref: Ref<HTMLTextAreaElement>
+ ) => {
+ // use
+ const [textValue, setTextValue] = useControllableState({
+ prop: value,
+ defaultProp: defaultValue,
+ onChange,
+ });
+
+ return (
+ <Grid>
+ <div
+ className={style({
+ css: { visibility: "hidden", ...css },
+ state,
+ className,
+ })}
+ >
+ {textValue}{" "}
+ </div>
+ <textarea
+ rows={1}
+ value={textValue}
+ onChange={(event) => setTextValue(event.target.value)}
+ spellCheck={false}
+ className={style({
+ state,
+ className,
+ })}
+ ref={ref}
+ {...props}
+ />
+ </Grid>
+ );
+ }
+);
+AutogrowTextArea.displayName = "AutogrowTextArea";