-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
feat: close dropdown on item click #111
Changes from 15 commits
2b5cccd
74845d8
2276fc7
72dd670
82538bf
b5dac60
8253222
dd2c0b1
73d7de2
44bad90
5455927
78edef7
0af843a
06ae31e
2a9eb0d
047025f
4b66c74
68b8d3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||||||
import { FC, ReactElement } from "react"; | ||||||||
|
||||||||
export type DropdownProps = React.PropsWithChildren<{ | ||||||||
position?: | ||||||||
| "dropdown-top" | ||||||||
| "dropdown-bottom" | ||||||||
| "dropdown-left" | ||||||||
| "dropdown-right"; | ||||||||
align?: "dropdown-end"; | ||||||||
renderButton: ReactElement; | ||||||||
items: (React.HTMLProps<HTMLLIElement> & { | ||||||||
["data-testid"]?: string; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need the square brackets here (and the other places where data-testid is used in ThemeSelector.jsx) |
||||||||
onClick?: () => void; | ||||||||
renderItem: string | ReactElement; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With ReactElement you can remove string
Suggested change
|
||||||||
})[]; | ||||||||
}>; | ||||||||
|
||||||||
export const closeDropdownOnItemClick = (): void => { | ||||||||
const activeElement = document.activeElement as HTMLElement | null; | ||||||||
if (activeElement && activeElement instanceof HTMLElement) { | ||||||||
activeElement.blur(); | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
export const Dropdown: FC<DropdownProps> = ({ | ||||||||
renderButton, | ||||||||
items, | ||||||||
position = "dropdown-bottom", | ||||||||
align, | ||||||||
}) => { | ||||||||
return ( | ||||||||
<div className={`dropdown ${position} ${align ?? ""}`}> | ||||||||
<div | ||||||||
tabIndex={0} | ||||||||
role="button" | ||||||||
className="btn border-0 p-0 bg-transparent hover:bg-transparent" | ||||||||
> | ||||||||
{renderButton} | ||||||||
</div> | ||||||||
<ul | ||||||||
tabIndex={0} | ||||||||
className="menu menu-sm dropdown-content mt-3 z-[1] p-2 shadow bg-base-100 rounded-box w-52" | ||||||||
> | ||||||||
{items.map((item, index) => { | ||||||||
const { onClick, renderItem, ...liProps } = item; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't need item, you can already destructure it in the loop
Suggested change
|
||||||||
return ( | ||||||||
<li | ||||||||
{...liProps} | ||||||||
key={index} | ||||||||
onClick={() => { | ||||||||
onClick?.(); | ||||||||
closeDropdownOnItemClick(); | ||||||||
}} | ||||||||
> | ||||||||
<p>{renderItem}</p> | ||||||||
</li> | ||||||||
); | ||||||||
})} | ||||||||
</ul> | ||||||||
</div> | ||||||||
); | ||||||||
}; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { FC } from "react"; | ||
import { Dropdown } from "@/components"; | ||
import { exportAsImage } from "@/utils"; | ||
|
||
type ExportDropdownButtonProps = { | ||
selector: string; | ||
filename?: string; | ||
}; | ||
|
||
export const ExportDropdownButton: FC<ExportDropdownButtonProps> = ({ | ||
selector, | ||
filename, | ||
}) => { | ||
return ( | ||
<Dropdown | ||
renderButton={ | ||
<button className="btn btn-primary rounded cursor-pointer"> | ||
Export as image | ||
</button> | ||
} | ||
items={[ | ||
{ | ||
renderItem: "Download as PNG", | ||
onClick: () => { | ||
exportAsImage(selector, "download", filename); | ||
}, | ||
}, | ||
{ | ||
renderItem: "Copy to Clipboard", | ||
onClick: () => { | ||
exportAsImage(selector, "clipboard", filename); | ||
}, | ||
}, | ||
]} | ||
/> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you really need React.PropsWithChildren?