-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: close dropdown on item click (#111)
* add closeDropdownOnItemClick util function * use of closeDropdownOnItemClick in every dropdown * created custom Dropdown component * created custom ExportDropdownButton component * add ThemeSelector component in components index * use of ExportDropdownButton * use of custom Dropdown * fix imports to be consistent * removed unused component * removed unused global util function * fix header * Revert "fix imports to be consistent" This reverts commit dd2c0b1. * fix test error * style fix * minor fixes * fix type in Dropdown component --------- Co-authored-by: sa.cux <sabrina.cuccureddu@green-share.it>
- Loading branch information
1 parent
f6d3fc4
commit d8f3b16
Showing
10 changed files
with
202 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { FC, HTMLProps, ReactElement, ReactNode } from "react"; | ||
|
||
export type DropdownProps = { | ||
position?: | ||
| "dropdown-top" | ||
| "dropdown-bottom" | ||
| "dropdown-left" | ||
| "dropdown-right"; | ||
align?: "dropdown-end"; | ||
renderButton: ReactElement; | ||
items: (HTMLProps<HTMLLIElement> & { | ||
"data-testid"?: string; | ||
onClick?: () => void; | ||
renderItem: ReactNode; | ||
})[]; | ||
}; | ||
|
||
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(({ onClick, renderItem, ...liProps }, index) => { | ||
return ( | ||
<li | ||
{...liProps} | ||
key={index} | ||
onClick={() => { | ||
onClick?.(); | ||
closeDropdownOnItemClick(); | ||
}} | ||
> | ||
<p>{renderItem}</p> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</div> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}, | ||
]} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.