Skip to content

The Open Standard for Generative UI

fix(react-ui): avoid nested button in foldable section trigger (#668)

Rabi Shanker Guhae680d0f29c25
diff --git a/packages/react-ui/package.json b/packages/react-ui/package.json
index 3dec6c192..796c39044 100644
--- a/packages/react-ui/package.json
+++ b/packages/react-ui/package.json
@@ -112,6 +112,7 @@
"@radix-ui/react-select": "^2.1.5",
"@radix-ui/react-separator": "^1.1.7",
"@radix-ui/react-slider": "^1.2.2",
+ "@radix-ui/react-slot": "^1.2.4",
"@radix-ui/react-switch": "^1.1.2",
"@radix-ui/react-tabs": "^1.1.2",
"@radix-ui/react-toggle-group": "^1.1.2",
diff --git a/packages/react-ui/src/components/IconButton/IconButton.tsx b/packages/react-ui/src/components/IconButton/IconButton.tsx
index 5a375f279..b1a07d2ce 100644
--- a/packages/react-ui/src/components/IconButton/IconButton.tsx
+++ b/packages/react-ui/src/components/IconButton/IconButton.tsx
@@ -1,3 +1,4 @@
+import { Slot, Slottable } from "@radix-ui/react-slot";
import clsx from "clsx";
import { ButtonHTMLAttributes, forwardRef, ReactNode } from "react";
@@ -19,6 +20,12 @@ export interface IconButtonProps extends ButtonHTMLAttributes<HTMLButtonElement>
shape?: IconButtonShape;
className?: string;
appearance?: IconButtonAppearance;
+ /**
+ * Render as the provided child element instead of a `<button>`. Useful when the
+ * icon button is nested inside another interactive element (e.g. an accordion
+ * trigger) where a nested `<button>` would be invalid HTML.
+ */
+ asChild?: boolean;
}
const normalIconButtonVariants = {
@@ -55,14 +62,18 @@ export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>((props,
size = "medium",
shape = "square",
appearance = "normal",
+ asChild = false,
+ children,
...rest
} = props;
const iconButtonVariants =
appearance === "normal" ? normalIconButtonVariants : destructiveIconButtonVariants;
+ const Comp = asChild ? Slot : "button";
+
return (
- <button
+ <Comp
ref={ref}
className={clsx(
"openui-icon-button",
@@ -73,8 +84,9 @@ export const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>((props,
)}
{...rest}
>
+ <Slottable>{children}</Slottable>
{icon && <span className="openui-icon-button-icon">{icon}</span>}
- </button>
+ </Comp>
);
});
diff --git a/packages/react-ui/src/components/SectionBlock/FoldableSection.tsx b/packages/react-ui/src/components/SectionBlock/FoldableSection.tsx
index 03fb049c9..218faadfa 100644
--- a/packages/react-ui/src/components/SectionBlock/FoldableSection.tsx
+++ b/packages/react-ui/src/components/SectionBlock/FoldableSection.tsx
@@ -63,13 +63,16 @@ export const FoldableSectionTrigger = forwardRef<
<Separator className="openui-foldable-section-trigger-content-separator" />
<div className="openui-foldable-section-trigger-content-icon-button-wrapper">
<IconButton
+ asChild
icon={
<ChevronRight className="openui-foldable-section-trigger-content-icon-button-icon" />
}
size="3-extra-small"
variant="secondary"
className="openui-foldable-section-trigger-content-icon-button"
- />
+ >
+ <span aria-hidden="true" />
+ </IconButton>
<div className="openui-foldable-section-trigger-content-text">{text}</div>
</div>
</div>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index a3f2e860f..552908354 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1550,6 +1550,9 @@ importers:
'@radix-ui/react-slider':
specifier: ^1.2.2
+ '@radix-ui/react-slot':
+ specifier: ^1.2.4
+ version: 1.2.4(@types/[email protected])([email protected])
'@radix-ui/react-switch':
specifier: ^1.1.2