Skip to content

The Open Standard for Generative UI

Update Tag component styles and ThemeProvider color settings (#176)

subhama93f4df6c394
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index c0c69c93c..ae26f2800 100644
--- a/js/packages/react-ui/package.json
+++ b/js/packages/react-ui/package.json
@@ -2,7 +2,7 @@
"type": "module",
"name": "@crayonai/react-ui",
"license": "MIT",
- "version": "0.8.19",
+ "version": "0.8.20",
"description": "Component library for Generative UI SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
diff --git a/js/packages/react-ui/src/components/FormControl/FormControl.tsx b/js/packages/react-ui/src/components/FormControl/FormControl.tsx
index 9f9d42b44..4c5801980 100644
--- a/js/packages/react-ui/src/components/FormControl/FormControl.tsx
+++ b/js/packages/react-ui/src/components/FormControl/FormControl.tsx
@@ -1,16 +1,19 @@
import clsx from "clsx";
-import React, { forwardRef } from "react";
-
+import React, { forwardRef, useMemo } from "react";
+import { FormControlProvider } from "./context";
export interface FormControlProps {
children: React.ReactNode;
className?: string;
style?: React.CSSProperties;
+ hasError?: boolean;
}
const FormControl = forwardRef<HTMLDivElement, FormControlProps>((props, ref) => {
+ const { children, className, style, hasError = false } = props;
+ const formControlContextValue = useMemo(() => ({ hasError }), [hasError]);
return (
- <div ref={ref} className={clsx("crayon-form-control", props.className)} style={props.style}>
- {props.children}
+ <div ref={ref} className={clsx("crayon-form-control", className)} style={style}>
+ <FormControlProvider value={formControlContextValue}>{children}</FormControlProvider>
</div>
);
});
diff --git a/js/packages/react-ui/src/components/FormControl/Hint/Hint.tsx b/js/packages/react-ui/src/components/FormControl/Hint/Hint.tsx
index 2e4be465e..e40bc2508 100644
--- a/js/packages/react-ui/src/components/FormControl/Hint/Hint.tsx
+++ b/js/packages/react-ui/src/components/FormControl/Hint/Hint.tsx
@@ -1,16 +1,27 @@
import clsx from "clsx";
import React, { forwardRef } from "react";
+import { useFormControlContext } from "../context";
export interface HintProps extends React.ComponentPropsWithoutRef<"div"> {
children: React.ReactNode;
className?: string;
style?: React.CSSProperties;
+ hasError?: boolean;
}
const Hint = forwardRef<HTMLDivElement, HintProps>(
- ({ children, className, style, ...props }, ref) => {
+ ({ children, className, style, hasError, ...props }, ref) => {
+ const ctx = useFormControlContext();
+ const resolvedHasError = hasError ?? ctx?.hasError ?? false;
return (
- <div ref={ref} className={clsx("crayon-hint", className)} style={style} {...props}>
+ <div
+ ref={ref}
+ className={clsx("crayon-hint", className, {
+ "crayon-hint-error": resolvedHasError,
+ })}
+ style={style}
+ {...props}
+ >
{children}
</div>
);
diff --git a/js/packages/react-ui/src/components/FormControl/Hint/hint.scss b/js/packages/react-ui/src/components/FormControl/Hint/hint.scss
index b6c357cfd..7cd5b42b5 100644
--- a/js/packages/react-ui/src/components/FormControl/Hint/hint.scss
+++ b/js/packages/react-ui/src/components/FormControl/Hint/hint.scss
@@ -8,4 +8,8 @@
align-items: center;
gap: cssUtils.$spacing-2xs;
padding-left: cssUtils.$spacing-2xs;
+
+ &-error {
+ color: cssUtils.$danger-primary-text;
+ }
}
diff --git a/js/packages/react-ui/src/components/FormControl/context.tsx b/js/packages/react-ui/src/components/FormControl/context.tsx
new file mode 100644
index 000000000..477633001
--- /dev/null
+++ b/js/packages/react-ui/src/components/FormControl/context.tsx
@@ -0,0 +1,18 @@
+import React, { createContext, useContext } from "react";
+
+export interface FormControlContextValue {
+ hasError: boolean;
+}
+
+const FormControlContext = createContext<FormControlContextValue | null>(null);
+
+export function useFormControlContext(): FormControlContextValue | null {
+ return useContext(FormControlContext);
+}
+
+export function FormControlProvider(
+ props: React.PropsWithChildren<{ value: FormControlContextValue }>,
+) {
+ const { value, children } = props;
+ return <FormControlContext.Provider value={value}>{children}</FormControlContext.Provider>;
+}
diff --git a/js/packages/react-ui/src/components/FormControl/stories/FormControl.stories.tsx b/js/packages/react-ui/src/components/FormControl/stories/FormControl.stories.tsx
index e9cc3517f..f9521b438 100644
--- a/js/packages/react-ui/src/components/FormControl/stories/FormControl.stories.tsx
+++ b/js/packages/react-ui/src/components/FormControl/stories/FormControl.stories.tsx
@@ -1,5 +1,6 @@
import type { Meta, StoryObj } from "@storybook/react";
-import { Info } from "lucide-react";
+import { AlertCircle, Info } from "lucide-react";
+import { useState } from "react";
import { Input } from "../../Input";
import { Label } from "../../Label";
import { FormControl } from "../FormControl";
@@ -43,6 +44,15 @@ const meta: Meta<typeof FormControl> = {
type: { summary: "CSSProperties" },
},
},
+ hasError: {
+ control: "boolean",
+ description: "Toggles error styling for child inputs and hints",
+ table: {
+ category: "Behavior",
+ type: { summary: "boolean" },
+ defaultValue: { summary: "false" },
+ },
+ },
},
};
@@ -72,3 +82,31 @@ export const WithMultipleChildren: Story = {
</FormControl>
),
};
+
+export const LengthValidation: Story = {
+ render: () => {
+ const [value, setValue] = useState("");
+ const hasError = value.length > 0 && (value.length > 10 || value.length < 3);
+ const helperText = hasError
+ ? "Name must be between 3 and 10 characters"
+ : "Enter a name between 3 and 10 characters";
+
+ return (
+ <div style={{ width: 420 }}>
+ <FormControl hasError={hasError}>
+ <Label htmlFor="name-validated">Name</Label>
+ <Input
+ id="name-validated"
+ placeholder="Type here"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
+ />
+ <Hint>
+ {hasError ? <AlertCircle size={14} /> : <Info size={14} />}
+ <span>{helperText}</span>
+ </Hint>
+ </FormControl>
+ </div>
+ );
+ },
+};
diff --git a/js/packages/react-ui/src/components/Input/Input.tsx b/js/packages/react-ui/src/components/Input/Input.tsx
index 22c73e15d..4fc9de433 100644
--- a/js/packages/react-ui/src/components/Input/Input.tsx
+++ b/js/packages/react-ui/src/components/Input/Input.tsx
@@ -1,10 +1,12 @@
import clsx from "clsx";
import React from "react";
+import { useFormControlContext } from "../FormControl/context";
export interface InputProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "size"> {
styles?: React.CSSProperties;
className?: string;
size?: "small" | "medium" | "large";
+ hasError?: boolean;
}
const sizes = {
@@ -14,12 +16,16 @@ const sizes = {
} as const;
export const Input = React.forwardRef<HTMLInputElement, InputProps>(
- ({ className, styles, size = "medium", ...props }, ref) => {
+ ({ className, styles, size = "medium", hasError, ...props }, ref) => {
+ const ctx = useFormControlContext();
+ const resolvedHasError = hasError ?? ctx?.hasError ?? false;
return (
<input
autoComplete="off"
ref={ref}
- className={clsx("crayon-input", sizes[size], className)}
+ className={clsx("crayon-input", sizes[size], className, {
+ "crayon-input-error": resolvedHasError,
+ })}
style={styles}
{...props}
/>
diff --git a/js/packages/react-ui/src/components/Input/input.scss b/js/packages/react-ui/src/components/Input/input.scss
index 02cfa2565..9c553108c 100644
--- a/js/packages/react-ui/src/components/Input/input.scss
+++ b/js/packages/react-ui/src/components/Input/input.scss
@@ -26,6 +26,9 @@
}
&:focus {
outline: none;
+ }
+
+ &:not(.crayon-input-error):focus {
border-color: cssUtils.$stroke-interactive-el;
}
@@ -36,7 +39,11 @@
color: cssUtils.$disabled-text;
}
- &:not(:focus):not(:disabled):hover {
+ &:not(:focus):not(:disabled):not(.crayon-input-error):hover {
border-color: cssUtils.$stroke-interactive-el;
}
+
+ &-error {
+ border-color: cssUtils.$stroke-danger-emphasis;
+ }
}
diff --git a/js/packages/react-ui/src/components/Input/stories/Input.stories.tsx b/js/packages/react-ui/src/components/Input/stories/Input.stories.tsx
index c95916166..5fc26dd58 100644
--- a/js/packages/react-ui/src/components/Input/stories/Input.stories.tsx
+++ b/js/packages/react-ui/src/components/Input/stories/Input.stories.tsx
@@ -40,6 +40,15 @@ const meta: Meta<typeof Input> = {
defaultValue: { summary: "false" },
},
},
+ hasError: {
+ control: "boolean",
+ description: "Whether the input is in an error state",
+ table: {
+ category: "Behavior",
+ type: { summary: "boolean" },
+ defaultValue: { summary: "false" },
+ },
+ },
placeholder: {
control: "text",
description: "The placeholder text for the input",
@@ -98,6 +107,26 @@ export const Default: Story = {
disabled={args.disabled}
value={args.value}
onChange={args.onChange}
+ hasError={args.hasError}
+ />
+ ),
+};
+
+export const ErrorState: Story = {
+ args: {
+ size: "medium",
+ placeholder: "Enter text...",
+ disabled: false,
+ hasError: true,
+ },
+ render: (args) => (
+ <Input
+ size={args.size}
+ placeholder={args.placeholder}
+ disabled={args.disabled}
+ value={args.value}
+ onChange={args.onChange}
+ hasError={args.hasError}
/>
),
};
@@ -136,3 +165,46 @@ const ControlledInput = () => {
},
},
};
+
+const ValidatedInput = () => {
+ const [value, setValue] = useState<string>("");
+ const hasError = value.length > 0 && (value.length < 3 || value.length > 10);
+ return (
+ <Input
+ size="medium"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
+ placeholder="Enter 3-10 characters"
+ hasError={hasError}
+ />
+ );
+};
+
+export const LengthValidation: Story = {
+ render: () => <ValidatedInput />,
+ parameters: {
+ docs: {
+ description: {
+ story: "Shows error when the value length is 1-2 or greater than 10 characters.",
+ },
+ source: {
+ code: `
+import { useState } from "react";
+
+const ValidatedInput = () => {
+ const [value, setValue] = useState<string>("");
+ const hasError = value.length > 0 && (value.length < 3 || value.length > 10);
+ return (
+ <Input
+ size="medium"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
+ placeholder="Enter 3-10 characters"
+ isError={hasError}
+ />
+ );
+};`,
+ },
+ },
+ },
+};
diff --git a/js/packages/react-ui/src/components/Shell/thread.scss b/js/packages/react-ui/src/components/Shell/thread.scss
index fe190dc95..8d0a23ef4 100644
--- a/js/packages/react-ui/src/components/Shell/thread.scss
+++ b/js/packages/react-ui/src/components/Shell/thread.scss
@@ -27,7 +27,7 @@ $center-align-spacing: calc(32px + cssUtils.$spacing-s);
}
.crayon-shell-thread-messages {
- max-width: 776px;
+ max-width: 880px;
margin: 0 auto;
display: flex;
flex-direction: column;
@@ -96,7 +96,7 @@ $center-align-spacing: calc(32px + cssUtils.$spacing-s);
}
.crayon-shell-thread-composer {
- max-width: 776px;
+ max-width: 880px;
width: 100%;
margin: 0 auto cssUtils.$spacing-m;
padding: 0 $center-align-spacing;
diff --git a/js/packages/react-ui/src/components/Tag/tag.scss b/js/packages/react-ui/src/components/Tag/tag.scss
index a21135c7d..2edda414c 100644
--- a/js/packages/react-ui/src/components/Tag/tag.scss
+++ b/js/packages/react-ui/src/components/Tag/tag.scss
@@ -22,7 +22,7 @@
&-sm {
padding: cssUtils.$spacing-2xs cssUtils.$spacing-xs;
- border-radius: cssUtils.$rounded-xs;
+ border-radius: cssUtils.$rounded-s;
height: 24px;
.crayon-tag-icon {
diff --git a/js/packages/react-ui/src/components/TextArea/TextArea.tsx b/js/packages/react-ui/src/components/TextArea/TextArea.tsx
index db46c2cee..ca32332d9 100644
--- a/js/packages/react-ui/src/components/TextArea/TextArea.tsx
+++ b/js/packages/react-ui/src/components/TextArea/TextArea.tsx
@@ -1,18 +1,29 @@
import clsx from "clsx";
import React, { forwardRef } from "react";
+import { useFormControlContext } from "../FormControl/context";
export interface TextAreaProps
extends Omit<React.TextareaHTMLAttributes<HTMLTextAreaElement>, "size"> {
className?: string;
placeholder?: string;
rows?: number;
+ hasError?: boolean;
}
const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>((props, ref) => {
- const { className, rows = 3, ...rest } = props;
+ const { className, rows = 3, hasError, ...rest } = props;
+ const ctx = useFormControlContext();
+ const resolvedHasError = hasError ?? ctx?.hasError ?? false;
return (
- <textarea ref={ref} className={clsx("crayon-textarea", className)} {...rest} rows={rows} />
+ <textarea
+ ref={ref}
+ className={clsx("crayon-textarea", className, {
+ "crayon-textarea-error": resolvedHasError,
+ })}
+ {...rest}
+ rows={rows}
+ />
);
});
diff --git a/js/packages/react-ui/src/components/TextArea/stories/TextArea.stories.tsx b/js/packages/react-ui/src/components/TextArea/stories/TextArea.stories.tsx
index 2e6feb38b..9f6f968e7 100644
--- a/js/packages/react-ui/src/components/TextArea/stories/TextArea.stories.tsx
+++ b/js/packages/react-ui/src/components/TextArea/stories/TextArea.stories.tsx
@@ -46,6 +46,15 @@ const meta = {
type: { summary: "CSSProperties" },
},
},
+ hasError: {
+ control: "boolean",
+ description: "Whether the text area is in an error state",
+ table: {
+ category: "Behavior",
+ type: { summary: "boolean" },
+ defaultValue: { summary: "false" },
+ },
+ },
},
tags: ["!dev", "autodocs"],
} satisfies Meta<typeof TextArea>;
@@ -59,3 +68,11 @@ export const Default: Story = {
rows: 3,
},
};
+
+export const ErrorState: Story = {
+ args: {
+ placeholder: "Enter your text here",
+ rows: 3,
+ hasError: true,
+ },
+};
diff --git a/js/packages/react-ui/src/components/TextArea/textArea.scss b/js/packages/react-ui/src/components/TextArea/textArea.scss
index 54060eb60..6afc391ae 100644
--- a/js/packages/react-ui/src/components/TextArea/textArea.scss
+++ b/js/packages/react-ui/src/components/TextArea/textArea.scss
@@ -20,4 +20,7 @@
&:disabled {
cursor: not-allowed;
}
+ &-error {
+ border-color: cssUtils.$stroke-danger-emphasis;
+ }
}
diff --git a/js/packages/react-ui/src/components/ThemeProvider/ThemeProvider.tsx b/js/packages/react-ui/src/components/ThemeProvider/ThemeProvider.tsx
index 47c8e001c..c54cf88aa 100644
--- a/js/packages/react-ui/src/components/ThemeProvider/ThemeProvider.tsx
+++ b/js/packages/react-ui/src/components/ThemeProvider/ThemeProvider.tsx
@@ -47,7 +47,7 @@ const lightTheme: ColorTheme = {
invertedFills: "rgba(21, 21, 21, 1)",
elevatedFills: "rgba(255, 255, 255, 0.6)",
sunkBgFills: "rgba(0, 0, 0, 0.04)",
- highlightSubtle: "rgba(0, 0, 0, 0.03)",
+ highlightSubtle: "rgba(0, 0, 0, 0.02)",
// Border colors
strokeDefault: "rgba(0, 0, 0, 0.04)",