Skip to content

Commit

Permalink
added closeOnClick option
Browse files Browse the repository at this point in the history
  • Loading branch information
coderwelsch committed Aug 6, 2024
1 parent ee041f9 commit c842ffb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
40 changes: 26 additions & 14 deletions src/components/popover-menu/popover-menu-panel-item.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { PopoverButton as HeadlessUiPopoverButton } from "@headlessui/react";
import { classNames } from "../../util/class-names";

const itemIntents = {
Expand All @@ -17,6 +18,7 @@ export interface PopoverMenuPanelItemProps {
Icon?: React.ComponentType<{ className: string }>;
variant?: keyof typeof itemIntents;
active?: boolean;
closeOnClick?: boolean;
}

export const PopoverMenuPanelItem = ({
Expand All @@ -25,25 +27,35 @@ export const PopoverMenuPanelItem = ({
Icon,
variant = "neutral",
active,
closeOnClick,
}: PopoverMenuPanelItemProps) => {
const intentStyles = itemIntents[variant];
const classes = classNames(
"relative flex w-full cursor-pointer flex-row items-center gap-3 overflow-hidden px-4 py-2 text-sm font-normal focus:ring-2 focus:ring-primary-200",
intentStyles,
active && activeItemIntents[variant],
active &&
"before:absolute before:left-0 before:top-0 before:h-full before:w-0.5 before:rounded-r-md"
);

return (
<div
className={classNames(
"relative flex w-full cursor-pointer flex-row items-center gap-3 overflow-hidden px-4 py-2 text-sm font-normal focus:ring-2 focus:ring-primary-200",
intentStyles,
active && activeItemIntents[variant],
active &&
"before:absolute before:left-0 before:top-0 before:h-full before:w-0.5 before:rounded-r-md"
)}
role="menuitem"
tabIndex={0}
onClick={onClick}
onKeyDown={onClick}
>
const content = (
<>
{Icon && <Icon className={classNames("h-3.5 w-3.5")} />}
{children}
</>
);

if (closeOnClick) {
return (
<HeadlessUiPopoverButton className={classes} onClick={onClick}>
{content}
</HeadlessUiPopoverButton>
);
}

return (
<div className={classes} onClick={onClick} role="menuitem" onKeyDown={onClick} tabIndex={0}>
{content}
</div>
);
};
18 changes: 13 additions & 5 deletions src/components/popover-menu/popover-menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ export const Default: Story = {
<PopoverMenu.Panel>
<PopoverMenu.Panel.Title>You</PopoverMenu.Panel.Title>

<PopoverMenu.Panel.Item Icon={EditIcon}>Edit profile</PopoverMenu.Panel.Item>
<PopoverMenu.Panel.Item Icon={ChatIcon}>Support</PopoverMenu.Panel.Item>
<PopoverMenu.Panel.Item Icon={AddIcon}>Invite member</PopoverMenu.Panel.Item>
<PopoverMenu.Panel.Item closeOnClick Icon={EditIcon}>
Edit profile
</PopoverMenu.Panel.Item>

<PopoverMenu.Panel.Item closeOnClick Icon={ChatIcon}>
Support
</PopoverMenu.Panel.Item>

<PopoverMenu.Panel.Item closeOnClick Icon={AddIcon}>
Invite member
</PopoverMenu.Panel.Item>

<PopoverMenu.Panel.Divider />

<PopoverMenu.Panel.Group>
<PopoverMenu.Panel.Title>Danger Zone</PopoverMenu.Panel.Title>

<PopoverMenu.Panel.Item Icon={DeleteIcon} variant="danger">
Item 1
<PopoverMenu.Panel.Item closeOnClick Icon={DeleteIcon} variant="danger">
Delete Account
</PopoverMenu.Panel.Item>
</PopoverMenu.Panel.Group>
</PopoverMenu.Panel>
Expand Down

0 comments on commit c842ffb

Please sign in to comment.