mui / mui/base-ui

[popover] Is there a way to render the `Popover` oven an existing element in the DOM?

Open
#2,157 4 comments 0 reactions 0 assignees View on GitHub
component: popover support: question
Dominant language
TypeScript
Stars
10.9k
Forks
543
Avg merge
1d 20h
Merged PRs (30d)
101

Description

# Get help

### Context:
- On the page, I have the special element with the `id="special-trigger"`.
- I have an existing `SpecialPopover` component that contains all the styles and content formatting logic.

I want to render the `SpecialPopover` component over the `div#special-trigger`, queried from the DOM using the `querySelector()`.

### Issue:

Unfortunately, the `Popover` doesn't allow me to use anything beyond its own React tree as a trigger. Also, I can't pass the `HTMLElement` as a trigger. _As it'll create a duplicate in the DOM._

### Example

Codesandbox - https://codesandbox.io/p/sandbox/external-popover-trigger-forked-g463pp

### Questions
Is there a recommended way to deal with rendering the `Popover` component over an arbitrary element in the DOM, using it as the `Trigger`? That includes forwarding the `Popover.Trigger` props onto that element, including the event listeners.
![Image](https://github.com/user-attachments/assets/400175d8-b1d7-43d4-a299-089d556ab5f0)

---

Previously, we used the `tippy.js` imperative constructor - https://atomiks.github.io/tippyjs/v6/constructor/. It allows assigning popups to an existing HTML element pretty trivially:
```tsx
tippy(specialElement, {
content: "Here's the default content",
trigger: 'click'
});
```
Unfortunately, the technology is deprecated, has its a11y challenges and rough edges around focus/interactions handling.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.