-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89c2806
commit 9dcbcf5
Showing
10 changed files
with
461 additions
and
321 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,63 @@ | ||
import { | ||
ContextMenuProps as BluePrintContextMenuProps, | ||
Menu, | ||
MenuItem, | ||
MenuItemProps, | ||
showContextMenu, | ||
} from '@blueprintjs/core'; | ||
import React, { ComponentProps, ElementType, ReactNode } from 'react'; | ||
|
||
export interface ContextMenuItem extends MenuItemProps { | ||
data?: object; | ||
} | ||
|
||
type ElementProps<E = 'div'> = E extends ElementType | ||
? ComponentProps<E> | ||
: never; | ||
|
||
export interface BaseContextMenuProps { | ||
options: ContextMenuItem[]; | ||
onSelect: (data?: object) => void; | ||
} | ||
|
||
export interface ContextMenuProps<E> | ||
extends BaseContextMenuProps, | ||
Omit<BluePrintContextMenuProps, 'onSelect' | 'content' | 'children'> { | ||
as?: E; | ||
children: ReactNode; | ||
} | ||
|
||
export function ContextMenu<E extends ElementType = 'div'>( | ||
props: ContextMenuProps<E> & ElementProps<E>, | ||
) { | ||
const { options, onSelect, children, as: Wrapper = 'div', ...other } = props; | ||
|
||
function handleContextMenu(event: React.MouseEvent<HTMLElement>) { | ||
const content = ( | ||
<Menu> | ||
{options.map((option) => ( | ||
<MenuItem | ||
key={JSON.stringify(option)} | ||
{...option} | ||
onClick={() => onSelect(option?.data)} | ||
/> | ||
))} | ||
</Menu> | ||
); | ||
|
||
event.preventDefault(); | ||
showContextMenu({ | ||
content, | ||
targetOffset: { | ||
left: event.clientX, | ||
top: event.clientY, | ||
}, | ||
}); | ||
} | ||
|
||
return ( | ||
<Wrapper {...other} onContextMenu={handleContextMenu}> | ||
{children} | ||
</Wrapper> | ||
); | ||
} |
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
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.