-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Menu): allow for positioning menu based on a coordinate within v…
…iewport
- Loading branch information
1 parent
085f195
commit 4a5f958
Showing
10 changed files
with
375 additions
and
138 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
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,62 @@ | ||
import React, { ForwardedRef } from "react"; | ||
import { | ||
MenuOverlay, | ||
MenuOverlayProps, | ||
} from "@intellij-platform/core/Menu/MenuOverlay"; | ||
import { useOverlayPositionFromOrigin } from "@intellij-platform/core/Menu/useOverlayPositionFromOrigin"; | ||
import { useObjectRef } from "@react-aria/utils"; | ||
|
||
interface MenuOverlayFromOriginProps | ||
extends Pick<MenuOverlayProps, "onClose" | "defaultAutoFocus"> { | ||
/** | ||
* Origin point within the viewport, based on which the menu overlay should be positioned. | ||
* Any pointer/mouse event, or a plain object with clientX and clientY can be passed. | ||
*/ | ||
origin: | ||
| { | ||
/** | ||
* Horizontal coordinate of the origin point within the viewport. | ||
* See {@link MouseEvent.clientX} | ||
*/ | ||
clientX: number; | ||
/** | ||
* Vertical coordinate of the origin point within the viewport. | ||
* See {@link MouseEvent.clientX} | ||
*/ | ||
clientY: number; | ||
} | ||
| undefined; | ||
children: React.ReactNode; | ||
} | ||
|
||
/** | ||
* Menu overlay position based on an origin point on the screen. | ||
* Useful when the menu is opened by a pointer event. | ||
*/ | ||
export const MenuOverlayFromOrigin = React.forwardRef( | ||
function MenuOverlayFromOrigin( | ||
{ children, origin, ...otherProps }: MenuOverlayFromOriginProps, | ||
forwardedRef: ForwardedRef<HTMLDivElement> | ||
) { | ||
const overlayRef = useObjectRef(forwardedRef); | ||
const { positionProps } = useOverlayPositionFromOrigin({ | ||
overlayRef, | ||
origin, | ||
containerPadding: { x: 0, y: 4 }, | ||
}); | ||
return ( | ||
<> | ||
{Boolean(origin) && ( | ||
<MenuOverlay | ||
overlayProps={positionProps} | ||
overlayRef={overlayRef} | ||
restoreFocus | ||
{...otherProps} | ||
> | ||
{children} | ||
</MenuOverlay> | ||
)} | ||
</> | ||
); | ||
} | ||
); |
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.