@easy-shadcn/react
Version:
Use shadcn/ui easy&enhance like component library
196 lines (193 loc) • 8.49 kB
JavaScript
import * as React from 'react';
import { Fragment } from 'react';
import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
import { ChevronRightIcon, CheckIcon, DotFilledIcon } from '@radix-ui/react-icons';
import { cn } from '@easy-shadcn/utils';
import { jsxs, jsx, Fragment as Fragment$1 } from 'react/jsx-runtime';
var DropdownMenu = DropdownMenuPrimitive.Root;
var DropdownMenuTrigger = DropdownMenuPrimitive.Trigger;
var DropdownMenuGroup = DropdownMenuPrimitive.Group;
var DropdownMenuPortal = DropdownMenuPrimitive.Portal;
var DropdownMenuSub = DropdownMenuPrimitive.Sub;
var DropdownMenuSubTrigger = React.forwardRef(({ className, inset, children, ...props }, ref) => /* @__PURE__ */ jsxs(
DropdownMenuPrimitive.SubTrigger,
{
ref,
className: cn(
"flex cursor-default gap-2 select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none focus:bg-accent data-[state=open]:bg-accent [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
inset && "pl-8",
className
),
...props,
children: [
children,
/* @__PURE__ */ jsx(ChevronRightIcon, { className: "ml-auto size-4" })
]
}
));
DropdownMenuSubTrigger.displayName = DropdownMenuPrimitive.SubTrigger.displayName;
var DropdownMenuSubContent = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(
DropdownMenuPrimitive.SubContent,
{
ref,
className: cn(
"z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
className
),
...props
}
));
DropdownMenuSubContent.displayName = DropdownMenuPrimitive.SubContent.displayName;
var DropdownMenuContent = React.forwardRef(({ className, sideOffset = 4, ...props }, ref) => /* @__PURE__ */ jsx(DropdownMenuPrimitive.Portal, { children: /* @__PURE__ */ jsx(
DropdownMenuPrimitive.Content,
{
ref,
sideOffset,
className: cn(
"z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md",
"data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
className
),
...props
}
) }));
DropdownMenuContent.displayName = DropdownMenuPrimitive.Content.displayName;
var DropdownMenuItem = React.forwardRef(({ className, inset, ...props }, ref) => /* @__PURE__ */ jsx(
DropdownMenuPrimitive.Item,
{
ref,
className: cn(
"relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
inset && "pl-8",
className
),
...props
}
));
DropdownMenuItem.displayName = DropdownMenuPrimitive.Item.displayName;
var DropdownMenuCheckboxItem = React.forwardRef(({ className, children, checked, ...props }, ref) => /* @__PURE__ */ jsxs(
DropdownMenuPrimitive.CheckboxItem,
{
ref,
className: cn(
"relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
className
),
checked,
...props,
children: [
/* @__PURE__ */ jsx("span", { className: "absolute left-2 flex size-3.5 items-center justify-center", children: /* @__PURE__ */ jsx(DropdownMenuPrimitive.ItemIndicator, { children: /* @__PURE__ */ jsx(CheckIcon, { className: "size-4" }) }) }),
children
]
}
));
DropdownMenuCheckboxItem.displayName = DropdownMenuPrimitive.CheckboxItem.displayName;
var DropdownMenuRadioItem = React.forwardRef(({ className, children, ...props }, ref) => /* @__PURE__ */ jsxs(
DropdownMenuPrimitive.RadioItem,
{
ref,
className: cn(
"relative flex cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none transition-colors focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
className
),
...props,
children: [
/* @__PURE__ */ jsx("span", { className: "absolute left-2 flex size-3.5 items-center justify-center", children: /* @__PURE__ */ jsx(DropdownMenuPrimitive.ItemIndicator, { children: /* @__PURE__ */ jsx(DotFilledIcon, { className: "size-4 fill-current" }) }) }),
children
]
}
));
DropdownMenuRadioItem.displayName = DropdownMenuPrimitive.RadioItem.displayName;
var DropdownMenuLabel = React.forwardRef(({ className, inset, ...props }, ref) => /* @__PURE__ */ jsx(
DropdownMenuPrimitive.Label,
{
ref,
className: cn(
"px-2 py-1.5 text-sm font-semibold",
inset && "pl-8",
className
),
...props
}
));
DropdownMenuLabel.displayName = DropdownMenuPrimitive.Label.displayName;
var DropdownMenuSeparator = React.forwardRef(({ className, ...props }, ref) => /* @__PURE__ */ jsx(
DropdownMenuPrimitive.Separator,
{
ref,
className: cn("-mx-1 my-1 h-px bg-muted", className),
...props
}
));
DropdownMenuSeparator.displayName = DropdownMenuPrimitive.Separator.displayName;
var DropdownMenuShortcut = ({
className,
...props
}) => {
return /* @__PURE__ */ jsx(
"span",
{
className: cn("ml-auto text-xs tracking-widest opacity-60", className),
...props
}
);
};
DropdownMenuShortcut.displayName = "DropdownMenuShortcut";
var DropdownMenu2 = ({
children,
menu,
contentProps,
...resetProps
}) => {
const renderNormalItem = (item, idx) => {
const { subItems, name, shortcut, prefix, key, ...resetProps2 } = item;
if (subItems) {
return renderSubItem(item, idx);
}
return /* @__PURE__ */ jsxs(DropdownMenuItem, { ...resetProps2, children: [
prefix && /* @__PURE__ */ jsx("span", { className: "mr-2", children: prefix }),
name,
shortcut && /* @__PURE__ */ jsx(DropdownMenuShortcut, { children: shortcut })
] }, key || idx);
};
const renderSubItem = (menu2, idx) => {
if (!menu2.subItems || menu2.subItems.length === 0) {
return null;
}
return /* @__PURE__ */ jsxs(DropdownMenuSub, { children: [
/* @__PURE__ */ jsx(DropdownMenuSubTrigger, { children: menu2.name }),
/* @__PURE__ */ jsx(DropdownMenuPortal, { children: /* @__PURE__ */ jsx(DropdownMenuSubContent, { children: renderMenuItem(menu2.subItems) }) })
] }, menu2.key || idx);
};
const renderGroupItem = (group, groupIdx, separatorConf) => {
const { groupName, key, groupNameProps, items, ...resetProps2 } = group;
return /* @__PURE__ */ jsxs(Fragment, { children: [
groupIdx !== 0 && /* @__PURE__ */ jsx(DropdownMenuSeparator, {}),
groupName && /* @__PURE__ */ jsxs(Fragment$1, { children: [
/* @__PURE__ */ jsx(DropdownMenuLabel, { ...groupNameProps, children: groupName }),
/* @__PURE__ */ jsx(DropdownMenuSeparator, {})
] }),
/* @__PURE__ */ jsx(DropdownMenuGroup, { ...resetProps2, children: items.map((item, idx) => renderNormalItem(item, idx)) }),
separatorConf.nextIsGroup == false && separatorConf.total !== groupIdx + 1 && /* @__PURE__ */ jsx(DropdownMenuSeparator, {})
] }, key || groupIdx);
};
const renderMenuItem = (menu2) => {
return menu2.map((item, idx) => {
if ("name" in item) {
return renderNormalItem(item, idx);
}
if ("items" in item) {
return renderGroupItem(item, idx, {
total: menu2.length,
nextIsGroup: menu2[idx + 1] ? "items" in menu2[idx + 1] : false
});
}
return null;
});
};
return /* @__PURE__ */ jsxs(DropdownMenu, { ...resetProps, children: [
/* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children }),
/* @__PURE__ */ jsx(DropdownMenuContent, { ...contentProps, children: renderMenuItem(menu) })
] });
};
export { DropdownMenu2 as DropdownMenu };