Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/primitive-render-prop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@diceui/shared": minor
"@diceui/checkbox-group": minor
"@diceui/combobox": minor
"@diceui/mention": minor
"@diceui/tags-input": minor
---

Accept a `render` element on every part, like Base UI's `render`, as well as `asChild`. The shadcn CLI rewrites `asChild` into `render` when it installs a component into a Base UI project, so those installs now keep working.
2 changes: 1 addition & 1 deletion packages/combobox/src/combobox-arrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const ComboboxArrow = React.forwardRef<SVGSVGElement, ComboboxArrowProps>(
display: "block",
}}
>
{props.asChild ? (
{props.asChild || props.render ? (
props.children
) : (
<path d="M0 10 L15 0 L30 10" fill="currentColor" />
Expand Down
30 changes: 30 additions & 0 deletions packages/combobox/test/combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,34 @@ describe("Combobox", () => {
expect(badges[1]).toHaveAttribute("aria-posinset", "2");
expect(badges[1]).toHaveAttribute("aria-setsize", "2");
});
test.each([
[
"asChild",
{ asChild: true },
<svg key="svg" data-testid="arrow" viewBox="0 0 20 10">
<path d="M0 0 L20 0" />
</svg>,
],
[
"render",
{ render: <svg data-testid="arrow" viewBox="0 0 20 10" /> },
<path key="path" d="M0 0 L20 0" />,
],
])("renders custom arrow content with %s", (_, arrowProps, children) => {
render(
<Combobox.Root defaultOpen>
<Combobox.Anchor>
<Combobox.Input />
</Combobox.Anchor>
<Combobox.Content>
<Combobox.Arrow {...arrowProps}>{children}</Combobox.Arrow>
<Combobox.Item value="kickflip">Kickflip</Combobox.Item>
</Combobox.Content>
</Combobox.Root>,
);

const paths = screen.getByTestId("arrow").querySelectorAll("path");
expect(paths).toHaveLength(1);
expect(paths[0]).toHaveAttribute("d", "M0 0 L20 0");
});
});
18 changes: 17 additions & 1 deletion packages/shared/src/components/primitive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type PrimitivePropsWithRef<E extends IntrinsicElementsKeys> = Omit<
> & {
/** Whether to render the wrapped component and merge their props. */
asChild?: boolean;
/**
* An element to render in place of the default one, with the props merged
* into it, like Base UI's `render` in its element form.
*/
render?: React.ReactElement;
ref?: React.Ref<React.ElementRef<E>>;
};

Expand All @@ -28,7 +33,18 @@ function createPrimitive<E extends IntrinsicElementsKeys>(
React.ElementRef<E>,
PrimitivePropsWithRef<E>
>((props, forwardedRef) => {
const { asChild, ...primitiveProps } = props;
const { asChild, render, ...primitiveProps } = props;

if (React.isValidElement(render)) {
const { children } = primitiveProps as { children?: React.ReactNode };
return React.createElement(
Slot,
{ ...primitiveProps, ref: forwardedRef as React.Ref<HTMLElement> },
children === undefined
? render
: React.cloneElement(render, undefined, children),
);
}

if (asChild) {
return React.createElement(Slot, {
Expand Down
58 changes: 58 additions & 0 deletions packages/shared/test/primitive.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,62 @@ describe("Primitive", () => {
expect(element).toHaveAttribute("data-custom", "value");
expect(element).toHaveAttribute("aria-label", "test label");
});
it("renders the render element in place of the default one", () => {
render(
<Primitive.div data-testid="primitive" render={<button type="button" />}>
Click me
</Primitive.div>,
);

const button = screen.getByRole("button", { name: "Click me" });
expect(button).toHaveAttribute("data-testid", "primitive");
expect(screen.queryByText("Click me")?.tagName).toBe("BUTTON");
});

it("merges props, handlers and refs into the render element", () => {
const ref = React.createRef<HTMLDivElement>();
const onPrimitiveClick = vi.fn();
const onElementClick = vi.fn();
render(
<Primitive.div
ref={ref}
className="primitive-class"
onClick={onPrimitiveClick}
render={
<button
type="button"
className="element-class"
onClick={onElementClick}
/>
}
>
Click me
</Primitive.div>,
);

const button = screen.getByRole("button", { name: "Click me" });
button.click();
expect(button).toHaveClass("primitive-class", "element-class");
expect(onPrimitiveClick).toHaveBeenCalledOnce();
expect(onElementClick).toHaveBeenCalledOnce();
expect(ref.current).toBe(button);
});

it("renders the render element alone without children", () => {
render(<Primitive.span render={<svg data-testid="icon" />} />);

expect(screen.getByTestId("icon").tagName.toLowerCase()).toBe("svg");
});

it("prefers render over asChild", () => {
render(
<Primitive.div asChild render={<button type="button" />}>
<span>Click me</span>
</Primitive.div>,
);

expect(screen.getByRole("button")).toContainElement(
screen.getByText("Click me"),
);
});
});
Loading