Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asChild API in Components by Adding Radix Slottable #6105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

valentinpolitov
Copy link

Description

This PR fixes the broken asChild API in multiple components by wrapping children with Slottable from @radix-ui/react-slot. This ensures proper props forwarding and compatibility with custom components passed as children.

Affected Components

  • Accordion
  • ContextMenu
  • Dialog
  • Drawer
  • DropdownMenu
  • Menubar
  • NavigationMenu
  • Select
  • Sheet

Changes

Before:

The asChild API was broken because children were directly rendered, preventing proper props forwarding.

{children}

After:

children is wrapped with Slottable to restore asChild functionality.

<Slottable>{children}</Slottable>

Why this change?

  • Fixing asChild Support: Resolves a known issue where props were not forwarded to custom components passed via asChild.
  • Alignment with Radix UI: Implements the recommended approach for components with multiple children using Slottable.
  • Consistency: Ensures the asChild API works uniformly across all affected components.

Screenshots

Screenshot 2024-12-17 at 13 33 41

Before

Screenshot 2024-12-17 at 13 34 45

After

Screenshot 2024-12-17 at 13 38 53

Reference

From Radix UI Documentation:
When working with components that have multiple children, Slottable ensures props are forwarded correctly.

// your-button.jsx
import React from "react";
import { Slot, Slottable } from "@radix-ui/react-slot";

function Button({ asChild, children, leftElement, rightElement, ...props }) {
  const Comp = asChild ? Slot : "button";
  return (
    <Comp {...props}>
      {leftElement}
      <Slottable>{children}</Slottable>
      {rightElement}
    </Comp>
  );
}

Checklist

  • Fixed asChild support in all listed components.
  • Verified props forwarding with Slottable.
  • Backward compatibility ensured.

Wrap `children` with `Slottable` from `@radix-ui/react-slot` to fix broken `asChild` API.
This ensures proper props forwarding when custom components are passed as children.

Affected components:
- Accordion
- ContextMenu
- Dialog
- Drawer
- DropdownMenu
- Menubar
- NavigationMenu
- Select
- Sheet

Resolves issues with `asChild` compatibility and aligns with Radix UI best practices.
Copy link

vercel bot commented Dec 17, 2024

@valentinpolitov is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant