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

19 feature create popovertsx component #76

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
4 changes: 3 additions & 1 deletion components/atoms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export * from './vertical-line';
export * from './link-btn';
export * from './card';
export * from './tag';
export * from './file-input';
export * from './popover';
export * from './btn';
export * from './file-input';
63 changes: 63 additions & 0 deletions components/atoms/popover/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use client';
import { cn } from '@/lib/cn';
import { Btn } from '@/components/atoms/btn';
import { useState, useEffect, useRef } from 'react';

interface Props extends React.HTMLProps<HTMLDivElement> {
btn: React.ReactElement;
children?: React.ReactNode;
}

export const Popover: React.FC<Props> = ({
btn,
children,
className,
...props
}) => {
const [isVisible, setIsVisible] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
const contentRef = useRef<HTMLDivElement>(null);

const toggleVisibility = () => {
setIsVisible(!isVisible);
};

useEffect(() => {
const handleOutsideClick = (event: MouseEvent) => {
if (
contentRef.current &&
!contentRef.current.contains(event.target as Node) &&
buttonRef.current &&
!buttonRef.current.contains(event.target as Node)
) {
setIsVisible(false);
}
};

document.addEventListener('mousedown', handleOutsideClick);
return () => {
document.removeEventListener('mousedown', handleOutsideClick);
};
}, []);

return (
<div className={cn('flex flex-col items-center gap-sm')}>
<section ref={buttonRef} onClick={toggleVisibility}>
{btn}
</section>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use Btn component instead of section tag. I don't think it's necessary to pass in a btn. You can add a 'text' prop for the btn text


{isVisible && (
<div
{...props}
className={cn(
'flex items-center rounded-md !border-line-width border-dashed border-light-neutral-gray bg-light-background p-lg text-light-primary',
className,
)}
ref={contentRef}
>
{children}
</div>
)}
</div>
);
};
2 changes: 2 additions & 0 deletions components/atoms/popover/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Popover as default } from './component';
export * from './component';