Skip to content

Commit

Permalink
fix(c): dropdown asChild
Browse files Browse the repository at this point in the history
  • Loading branch information
brianorwhatever committed Jan 30, 2025
1 parent 064c4a0 commit bf5bcc5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
67 changes: 32 additions & 35 deletions src/components/dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,55 +82,52 @@ export const Dropdown: React.FC<DropdownProps> = ({
);
};

interface DropdownItemProps {
export interface DropdownItemProps {
children: React.ReactNode;
href?: string;
onClick?: () => void;
asChild?: boolean;
className?: string;
onClick?: () => void;
}

export const DropdownItem = React.forwardRef<HTMLDivElement, DropdownItemProps>(
({ children, href, onClick, className }, ref) => {
const content = (
<div
ref={ref}
className={cn(
"w-full px-4 py-2 text-sm",
"text-gray-700 dark:text-gray-200",
className
)}
>
{children}
</div>
);

({ children, asChild = false, className, onClick }, ref) => {
return (
<Menu.Item>
{({ active }) =>
href ? (
<a
href={href}
onClick={onClick}
className={cn(
"block w-full",
active && "bg-gray-100 dark:bg-gray-700"
)}
>
{content}
</a>
) : (
{({ active }) => {
if (asChild) {
const child = React.Children.only(children) as React.ReactElement;
return React.cloneElement(child, {
className: cn(
"block w-full px-4 py-2 text-sm",
"text-gray-700 dark:text-gray-200",
active && "bg-gray-100 dark:bg-gray-700",
child.props.className,
className
),
onClick: (e: React.MouseEvent) => {
child.props.onClick?.(e);
onClick?.();
},
ref
});
}

return (
<button
type="button"
ref={ref as any}
onClick={onClick}
className={cn(
"block w-full text-left",
active && "bg-gray-100 dark:bg-gray-700"
"block w-full px-4 py-2 text-sm text-left",
"text-gray-700 dark:text-gray-200",
active && "bg-gray-100 dark:bg-gray-700",
className
)}
>
{content}
{children}
</button>
)
}
);
}}
</Menu.Item>
);
}
Expand Down
24 changes: 16 additions & 8 deletions src/docs/pages/components/DropdownPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,31 @@ const MyComponent = () => (
title="With Links"
description="Dropdown items as links"
code={`<Dropdown trigger={<Button>Navigation</Button>}>
<DropdownItem href="https://google.com">
Google
<DropdownItem asChild>
<a href="https://google.com" target="_blank" rel="noopener noreferrer">
Google
</a>
</DropdownItem>
<DropdownItem href="/settings">
Settings
<DropdownItem asChild>
<a href="/settings">
Settings
</a>
</DropdownItem>
<DropdownItem onClick={() => alert('Clicked!')}>
Action Button
</DropdownItem>
</Dropdown>`}
>
<Dropdown trigger={<Button>Navigation</Button>}>
<DropdownItem href="https://google.com">
Google
<DropdownItem asChild>
<a href="https://google.com" target="_blank" rel="noopener noreferrer">
Google
</a>
</DropdownItem>
<DropdownItem href="/settings">
Settings
<DropdownItem asChild>
<a href="/settings">
Settings
</a>
</DropdownItem>
<DropdownItem onClick={() => alert('Clicked!')}>
Action Button
Expand Down

0 comments on commit bf5bcc5

Please sign in to comment.