Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions src/components/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PopoverProps {
className?: string;
classButton?: string;
align?: 'left' | 'right';
direction?: 'up' | 'down';
}

/**
Expand All @@ -25,12 +26,19 @@ export interface PopoverProps {
*
* @property {string} [classButton]
* - Custom classes for the trigger button.
*
*
* @property {'left' | 'right'} [align='right']
* - The alignment of the popover panel relative to the trigger button.
*
* @property {'up' | 'down'} [direction='down']
* - The direction of the popover panel relative to the trigger button.
*
* @returns {JSX.Element}
* - The rendered Popover component.
*
*/

const Popover = ({ childrenButton, panel, className, classButton, align = 'right' }: PopoverProps): JSX.Element => {
const Popover = ({ childrenButton, panel, className, classButton, align = 'right', direction = 'down' }: PopoverProps): JSX.Element => {
const [isOpen, setIsOpen] = useState(false);
const panelRef = useRef<HTMLDivElement | null>(null);
const [showContent, setShowContent] = useState(isOpen);
Expand Down Expand Up @@ -88,8 +96,10 @@ const Popover = ({ childrenButton, panel, className, classButton, align = 'right
<div
ref={panelRef}
className={
'absolute z-50 mt-1 transform rounded-md border border-gray-10 ' +
`${align === 'left' ? 'left-0 origin-top-left' : 'right-0 origin-top-right'} ` +
'absolute z-50 transform rounded-md border border-gray-10 ' +
`${direction === 'up' ? 'bottom-full mb-1' : 'mt-1'} ` +
`${align === 'left' ? 'right-0' : 'left-0'} ` +
`${direction === 'up' ? (align === 'left' ? 'origin-bottom-right' : 'origin-bottom-left') : (align === 'left' ? 'origin-top-right' : 'origin-top-left')} ` +
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, maybe we could add an object map so its more readable, wdyt?

im thinking in something like this:

const originMap = {
  up:   { left: 'origin-bottom-right', right: 'origin-bottom-left' },
  down: { left: 'origin-top-right',    right: 'origin-top-left'    },
};

// usage
`${originMap[direction][align]}`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

`bg-surface py-1.5 shadow-subtle duration-100 ease-out dark:bg-gray-5 ${transitionOpacity} ${transitionScale}`
}
>
Expand Down
16 changes: 16 additions & 0 deletions src/components/popover/__test__/Popover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ describe('Popover', () => {
await waitFor(() => expect(getByText('Popover Content')).toBeInTheDocument());
});

it('applies the correct classes for left alignment', () => {
const { getByText, queryByText } = renderPopover({ align: 'left' });
fireEvent.click(getByText('Open Popover'));
const leftPanel = queryByText('Popover Content')?.parentElement?.parentElement;
expect(leftPanel).toHaveClass('right-0');
expect(leftPanel).toHaveClass('origin-top-right');
});

it('applies the correct classes for right alignment', () => {
const { getByText, queryByText } = renderPopover({ align: 'right' });
fireEvent.click(getByText('Open Popover'));
const rightPanel = queryByText('Popover Content')?.parentElement?.parentElement;
expect(rightPanel).toHaveClass('left-0');
expect(rightPanel).toHaveClass('origin-top-left');
});

it('should call onMouseDown stopPropagation when the button is clicked', () => {
const stopPropagationSpy = vi.fn();
const { getByText } = renderPopover();
Expand Down
Loading