Web components support #874
Replies: 3 comments 10 replies
-
I seem to have a very hacky, but working solution. You can use it at your own risk. /**
*
* @param shadowRootContainer - The HTML element that is the shadowRoot's parent
* @param portalRoot - The HTML element that you want Modals to be teleported to
* @returns
*/
export default function patch(
shadowRootContainer: HTMLElement,
portalRoot?: HTMLElement
) {
const elementById = Document.prototype.getElementById;
const element = portalRoot
? portalRoot
: shadowRootContainer.shadowRoot?.children[0];
if (!element) return;
Document.prototype.getElementById = function (elementId: string) {
if (elementId === "headlessui-portal-root") {
const d = document.createElement("div");
d.id = "headlessui-portal-root";
element.appendChild(d);
return d;
}
return elementById.call(this, elementId);
};
const activeElementDescriptorGetter = Object.getOwnPropertyDescriptor(
Document.prototype,
"activeElement"
)?.get;
Object.defineProperty(Document.prototype, "activeElement", {
get: function () {
const activeElement = activeElementDescriptorGetter?.call(this);
if (activeElement === shadowRootContainer) {
return shadowRootContainer.shadowRoot?.activeElement;
}
},
});
const targetGetter = Object.getOwnPropertyDescriptor(
Event.prototype,
"target"
)?.get;
Object.defineProperty(Event.prototype, "target", {
get: function () {
const target = targetGetter?.call(this);
if (target === shadowRootContainer) {
return this.path[0];
}
return target;
},
});
}
|
Beta Was this translation helpful? Give feedback.
-
I could really use a subset of this functionality in our large react application. We have tailwind embedded into a larger project with shadow root to isolate overlapping classes. I would like to use headlessui too but the portals it creates go up into the root document instead of the nearest shadow root. It would be so awesome to have an option to make portals go to the nearest shadow via this API if available: https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode could look into a PR unless someone has an idea already. |
Beta Was this translation helpful? Give feedback.
-
Hi, any help with the following? I'm trying to use your patch in the code found here:
|
Beta Was this translation helpful? Give feedback.
-
Currently there are multiple issues if you want to use Headless UI inside / with web components.
Instead of opening a few issues, I'm going to close all of them and link to this discussion.
This will be easier if we want to work on better web components support, that we have a single spot to refer to and have a discussion.
Here are a few related issues:
This is not a promise that we will have full proper web components support, but I wanted to do some cleanup in general.
Beta Was this translation helpful? Give feedback.
All reactions