refactor(ui): take refs as a prop instead of forwardRef
React 19 passes `ref` to function components as an ordinary prop, and the React team has said forwardRef will be deprecated in a future release. Button, IconButton, Input, Select and Textarea become plain function components whose props extend ComponentProps<"button" | "input" | …>, which already carries `ref`; each passes it on (Button to both Slot.Root and <button>; IconButton through its props to Button). Their displayName lines go too: the function names show in DevTools. IconButton and RigEditor import the ReactNode type instead of reaching for the global `React` namespace. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e3d86ea6cd
commit
52fc677ce5
6 files changed
+144
-121
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { lazy, Suspense, useId, useState } from "react";
|
||||
import { lazy, Suspense, useId, useState, type ReactNode } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { AlertTriangle, ChevronsUpDown } from "lucide-react";
|
||||
import clsx from "clsx";
|
||||
@@ -230,7 +230,7 @@ export function RigEditor() {
|
||||
// before it, so cascading option lists, dynamic labels and hidden-when
|
||||
// predicates line up.
|
||||
const previousValues: string[] = [];
|
||||
const renderedParts: React.ReactNode[] = [];
|
||||
const renderedParts: ReactNode[] = [];
|
||||
composite.parts.forEach((part, index) => {
|
||||
const stored = partValues[index] ?? "";
|
||||
if (!part.hiddenWhen?.(previousValues)) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { forwardRef, type ButtonHTMLAttributes } from "react";
|
||||
import type { ComponentProps } from "react";
|
||||
import { Slot } from "radix-ui";
|
||||
import clsx from "clsx";
|
||||
import { Spinner } from "@components/icons/animated";
|
||||
@@ -6,7 +6,8 @@ import { Spinner } from "@components/icons/animated";
|
||||
export type ButtonVariant = "primary" | "brand" | "secondary" | "outline" | "ghost" | "danger";
|
||||
export type ButtonSize = "sm" | "md" | "lg" | "icon" | "icon-sm";
|
||||
|
||||
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
||||
/** `ref` is an ordinary prop in React 19 — it comes with `ComponentProps<"button">`. */
|
||||
export interface ButtonProps extends ComponentProps<"button"> {
|
||||
variant?: ButtonVariant;
|
||||
size?: ButtonSize;
|
||||
/** Shows a spinner, disables the button and keeps its width. */
|
||||
@@ -42,43 +43,37 @@ export const buttonClasses = (variant: ButtonVariant = "primary", size: ButtonSi
|
||||
SIZES[size],
|
||||
);
|
||||
|
||||
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
(
|
||||
{
|
||||
variant = "primary",
|
||||
size = "md",
|
||||
loading = false,
|
||||
asChild = false,
|
||||
className,
|
||||
children,
|
||||
disabled,
|
||||
type,
|
||||
...props
|
||||
},
|
||||
ref,
|
||||
) => {
|
||||
if (asChild) {
|
||||
return (
|
||||
<Slot.Root ref={ref} className={clsx(buttonClasses(variant, size), className)} {...props}>
|
||||
{children}
|
||||
</Slot.Root>
|
||||
);
|
||||
}
|
||||
export function Button({
|
||||
variant = "primary",
|
||||
size = "md",
|
||||
loading = false,
|
||||
asChild = false,
|
||||
className,
|
||||
children,
|
||||
disabled,
|
||||
type,
|
||||
ref,
|
||||
...props
|
||||
}: ButtonProps) {
|
||||
if (asChild) {
|
||||
return (
|
||||
<button
|
||||
ref={ref}
|
||||
// A button inside a <form> defaults to submit — make every other one explicit.
|
||||
type={type ?? "button"}
|
||||
disabled={disabled || loading}
|
||||
aria-busy={loading || undefined}
|
||||
className={clsx(buttonClasses(variant, size), className)}
|
||||
{...props}
|
||||
>
|
||||
{loading && <Spinner />}
|
||||
<Slot.Root ref={ref} className={clsx(buttonClasses(variant, size), className)} {...props}>
|
||||
{children}
|
||||
</button>
|
||||
</Slot.Root>
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Button.displayName = "Button";
|
||||
}
|
||||
return (
|
||||
<button
|
||||
ref={ref}
|
||||
// A button inside a <form> defaults to submit — make every other one explicit.
|
||||
type={type ?? "button"}
|
||||
disabled={disabled || loading}
|
||||
aria-busy={loading || undefined}
|
||||
className={clsx(buttonClasses(variant, size), className)}
|
||||
{...props}
|
||||
>
|
||||
{loading && <Spinner />}
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -1,29 +1,35 @@
|
||||
import { forwardRef } from "react";
|
||||
import type { ReactNode } from "react";
|
||||
import { Button, type ButtonProps } from "./Button";
|
||||
import { Tooltip } from "./Tooltip";
|
||||
|
||||
interface IconButtonProps extends Omit<ButtonProps, "size" | "children" | "aria-label"> {
|
||||
/** Accessible name AND tooltip text — an icon-only control needs both. */
|
||||
label: string;
|
||||
children: React.ReactNode;
|
||||
children: ReactNode;
|
||||
size?: "icon" | "icon-sm";
|
||||
/** Hide the hover tooltip (the name stays for screen readers). */
|
||||
noTooltip?: boolean;
|
||||
tooltipSide?: "top" | "right" | "bottom" | "left";
|
||||
}
|
||||
|
||||
/** A square, icon-only button that cannot be rendered without a name. */
|
||||
export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(
|
||||
(
|
||||
{ label, children, variant = "ghost", size = "icon", noTooltip, tooltipSide, ...props },
|
||||
ref,
|
||||
) => (
|
||||
/**
|
||||
* A square, icon-only button that cannot be rendered without a name. A `ref`
|
||||
* reaches the button through `props`.
|
||||
*/
|
||||
export function IconButton({
|
||||
label,
|
||||
children,
|
||||
variant = "ghost",
|
||||
size = "icon",
|
||||
noTooltip,
|
||||
tooltipSide,
|
||||
...props
|
||||
}: IconButtonProps) {
|
||||
return (
|
||||
<Tooltip content={label} disabled={noTooltip} side={tooltipSide}>
|
||||
<Button ref={ref} variant={variant} size={size} aria-label={label} {...props}>
|
||||
<Button variant={variant} size={size} aria-label={label} {...props}>
|
||||
{children}
|
||||
</Button>
|
||||
</Tooltip>
|
||||
),
|
||||
);
|
||||
|
||||
IconButton.displayName = "IconButton";
|
||||
);
|
||||
}
|
||||
+40
-33
@@ -1,8 +1,8 @@
|
||||
import { forwardRef, type InputHTMLAttributes, type ReactNode } from "react";
|
||||
import type { ComponentProps, ReactNode } from "react";
|
||||
import clsx from "clsx";
|
||||
import { Field, controlClasses } from "./Field";
|
||||
|
||||
interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
|
||||
interface InputProps extends ComponentProps<"input"> {
|
||||
label?: string;
|
||||
error?: boolean;
|
||||
/** Inline error message shown below the input (also forces the red border). */
|
||||
@@ -15,35 +15,42 @@ interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
|
||||
leading?: ReactNode;
|
||||
}
|
||||
|
||||
export const Input = forwardRef<HTMLInputElement, InputProps>(
|
||||
({ label, error, errorText, helpText, labelExtra, leading, className, id, ...props }, ref) => {
|
||||
const isError = Boolean(error) || Boolean(errorText);
|
||||
return (
|
||||
<Field label={label} error={errorText} help={helpText} labelExtra={labelExtra} id={id}>
|
||||
{(control) => (
|
||||
<div className="relative">
|
||||
{leading && (
|
||||
<span className="text-text-muted pointer-events-none absolute inset-y-0 left-3 flex items-center [&_svg]:size-4">
|
||||
{leading}
|
||||
</span>
|
||||
export function Input({
|
||||
label,
|
||||
error,
|
||||
errorText,
|
||||
helpText,
|
||||
labelExtra,
|
||||
leading,
|
||||
className,
|
||||
id,
|
||||
ref,
|
||||
...props
|
||||
}: InputProps) {
|
||||
const isError = Boolean(error) || Boolean(errorText);
|
||||
return (
|
||||
<Field label={label} error={errorText} help={helpText} labelExtra={labelExtra} id={id}>
|
||||
{(control) => (
|
||||
<div className="relative">
|
||||
{leading && (
|
||||
<span className="text-text-muted pointer-events-none absolute inset-y-0 left-3 flex items-center [&_svg]:size-4">
|
||||
{leading}
|
||||
</span>
|
||||
)}
|
||||
<input
|
||||
ref={ref}
|
||||
{...control}
|
||||
aria-invalid={isError || undefined}
|
||||
className={clsx(
|
||||
controlClasses(isError),
|
||||
"h-control text-base sm:text-sm",
|
||||
leading ? "pl-9" : undefined,
|
||||
className,
|
||||
)}
|
||||
<input
|
||||
ref={ref}
|
||||
{...control}
|
||||
aria-invalid={isError || undefined}
|
||||
className={clsx(
|
||||
controlClasses(isError),
|
||||
"h-control text-base sm:text-sm",
|
||||
leading ? "pl-9" : undefined,
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</Field>
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Input.displayName = "Input";
|
||||
{...props}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</Field>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { forwardRef, type SelectHTMLAttributes } from "react";
|
||||
import type { ComponentProps } from "react";
|
||||
import { ChevronsUpDown } from "lucide-react";
|
||||
import clsx from "clsx";
|
||||
import { Field, controlClasses } from "./Field";
|
||||
@@ -8,7 +8,7 @@ interface SelectOption {
|
||||
label: string;
|
||||
}
|
||||
|
||||
interface SelectProps extends Omit<SelectHTMLAttributes<HTMLSelectElement>, "onChange"> {
|
||||
interface SelectProps extends Omit<ComponentProps<"select">, "onChange"> {
|
||||
label?: string;
|
||||
options: readonly SelectOption[];
|
||||
onChange: (value: string) => void;
|
||||
@@ -21,8 +21,19 @@ interface SelectProps extends Omit<SelectHTMLAttributes<HTMLSelectElement>, "onC
|
||||
* Android and iOS, type-ahead, and full keyboard support for free — and it
|
||||
* now matches the other controls' height (it used to be ~6px shorter).
|
||||
*/
|
||||
export const Select = forwardRef<HTMLSelectElement, SelectProps>(
|
||||
({ label, options, value, onChange, helpText, errorText, className, id, ...props }, ref) => (
|
||||
export function Select({
|
||||
label,
|
||||
options,
|
||||
value,
|
||||
onChange,
|
||||
helpText,
|
||||
errorText,
|
||||
className,
|
||||
id,
|
||||
ref,
|
||||
...props
|
||||
}: SelectProps) {
|
||||
return (
|
||||
<Field label={label} help={helpText} error={errorText} id={id}>
|
||||
{(control) => (
|
||||
// `className` sizes the control box (e.g. max-w-40), so the chevron
|
||||
@@ -52,7 +63,5 @@ export const Select = forwardRef<HTMLSelectElement, SelectProps>(
|
||||
</div>
|
||||
)}
|
||||
</Field>
|
||||
),
|
||||
);
|
||||
|
||||
Select.displayName = "Select";
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { forwardRef, type ReactNode, type TextareaHTMLAttributes } from "react";
|
||||
import type { ComponentProps, ReactNode } from "react";
|
||||
import clsx from "clsx";
|
||||
import { Field, controlClasses } from "./Field";
|
||||
|
||||
interface TextareaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
|
||||
interface TextareaProps extends ComponentProps<"textarea"> {
|
||||
label?: string;
|
||||
error?: boolean;
|
||||
errorText?: string;
|
||||
@@ -10,27 +10,33 @@ interface TextareaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
|
||||
labelExtra?: ReactNode;
|
||||
}
|
||||
|
||||
export const Textarea = forwardRef<HTMLTextAreaElement, TextareaProps>(
|
||||
({ label, error, errorText, helpText, labelExtra, className, id, ...props }, ref) => {
|
||||
const isError = Boolean(error) || Boolean(errorText);
|
||||
return (
|
||||
<Field label={label} error={errorText} help={helpText} labelExtra={labelExtra} id={id}>
|
||||
{(control) => (
|
||||
<textarea
|
||||
ref={ref}
|
||||
{...control}
|
||||
aria-invalid={isError || undefined}
|
||||
className={clsx(
|
||||
controlClasses(isError),
|
||||
"min-h-24 resize-y py-2 text-base leading-relaxed sm:text-sm",
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Textarea.displayName = "Textarea";
|
||||
export function Textarea({
|
||||
label,
|
||||
error,
|
||||
errorText,
|
||||
helpText,
|
||||
labelExtra,
|
||||
className,
|
||||
id,
|
||||
ref,
|
||||
...props
|
||||
}: TextareaProps) {
|
||||
const isError = Boolean(error) || Boolean(errorText);
|
||||
return (
|
||||
<Field label={label} error={errorText} help={helpText} labelExtra={labelExtra} id={id}>
|
||||
{(control) => (
|
||||
<textarea
|
||||
ref={ref}
|
||||
{...control}
|
||||
aria-invalid={isError || undefined}
|
||||
className={clsx(
|
||||
controlClasses(isError),
|
||||
"min-h-24 resize-y py-2 text-base leading-relaxed sm:text-sm",
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user