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

Add support for popper.js Virtual elements in Popover2 #4642

Open
andyboscor opened this issue Apr 15, 2021 · 6 comments
Open

Add support for popper.js Virtual elements in Popover2 #4642

andyboscor opened this issue Apr 15, 2021 · 6 comments

Comments

@andyboscor
Copy link

Feature request

https://popper.js.org/docs/v2/virtual-elements/

@adidahiya adidahiya added this to the 4.x milestone Apr 15, 2021
@adidahiya adidahiya added this to Needs triage in 2021 planning via automation Apr 15, 2021
@adidahiya adidahiya moved this from Needs triage to Medium priority in 2021 planning Apr 15, 2021
@ro-savage
Copy link

This would be an awesome feature to have - mouse following would be really useful when you have a large target.

@switz

This comment was marked as spam.

@adidahiya adidahiya removed this from the 4.x milestone Aug 22, 2022
@adidahiya adidahiya added P1 and removed P2 labels Aug 22, 2022
@adidahiya
Copy link
Contributor

I agree that this would be a nice feature to add. In the meantime, I think you can implement such a feature yourself with Popover2/Tooltip2. Check out my quick version of this in this sandbox:

2022-08-22 11 25 14

@tillprochaska
Copy link

@adidahiya Thanks for posting the example showing how to implement this with the current Popover component!

One thing that would be really helpful is to retain the default behavior for keyboard navigation/focus behavior, i.e. when focusing the actual target (e.g. a button), the popover would be displayed and the position would be computed relative to the button’s position, but when hovering the button, it would position the popover relative to the cursor position.

@adidahiya adidahiya removed this from Medium priority in 2021 planning Jun 5, 2023
@adidahiya
Copy link
Contributor

I believe my approach demonstrated in the code sandbox above will still work with Blueprint v5, but the public instance method to reposition the popover is now called reposition() instead of forceUpdate(). See source:

public reposition = () => this.popperScheduleUpdate?.();

@adidahiya adidahiya added this to Needs triage in DX paper cuts via automation Sep 13, 2023
@ab-pm
Copy link

ab-pm commented May 15, 2024

I don't know about repositioning and everything, but I got this to work with virtual elements using the <Popover>s renderTarget in a controlled context menu popover by setting the ref to such a virtual element:

<Popover
	{}
	renderTarget={props => {
		const { left, top } = ; // wherever you want to render the overlay (coming from state in my case)
		const rect: DOMRect = {
			width: 0,
			height: 0,
			x: left,
			y: top,
			left,
			top,
			right: left,
			bottom: top,
			toJSON() { return this; },
		};
		// The `renderTarget` in <ContextMenuPopover> uses a
		// `<Portal><div className={Classes.CONTEXT_MENU_VIRTUAL_TARGET} style={contextMenu.targetOffset} ref={props.ref} /></Portal>`
		// but this is too slow to mount (causing jumping context menus) and also is an unnecessary <div> anyway.
		// We use a https://popper.js.org/docs/v2/virtual-elements/ instead, but augmented with some methods that Blueprint's <Popover> expects on its `targetRef` element
		(props.ref as RefCallback<VirtualElement & Pick<HTMLElement, 'closest' | 'contains'>>)({
			getBoundingClientRect() {
				return rect;
			},
			closest() { return null; },
			contains() { return false; },
			contextElement: document.body,
		});
		return ; // whatever else you're rendering as the target, using the `props` except for the `ref`
	}}
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
DX paper cuts
Needs triage
Development

No branches or pull requests

6 participants