@webext-core/isolated-elementcreateIsolatedElementasync function createIsolatedElement(
options: CreateIsolatedElementOptions,
): Promise<{
parentElement: HTMLElement;
isolatedElement: HTMLElement;
shadow: ShadowRoot;
}> {
// ...
}
Create an HTML element that has isolated styles from the rest of the page.
options: CreateIsolatedElementOptionsparentElement that can be added to the DOMshadow rootisolatedElement that you should mount your UI to.const { isolatedElement, parentElement } = createIsolatedElement({
name: 'example-ui',
css: { textContent: "p { color: red }" },
isolateEvents: true // or ['keydown', 'keyup', 'keypress']
});
// Create and mount your app inside the isolation
const ui = document.createElement("p");
ui.textContent = "Example UI";
isolatedElement.appendChild(ui);
// Add the UI to the DOM
document.body.appendChild(parentElement);
CreateIsolatedElementOptionsinterface CreateIsolatedElementOptions {
name: string;
mode?: "open" | "closed";
css?: { url: string } | { textContent: string };
isolateEvents?: boolean | string[];
}
Options that can be passed into createIsolatedElement.
name: stringNote that you can't attach a shadow root to every type of element. There are some that can't have a shadow DOM for security reasons (for example
ShadowRoot.mode.css?: { url: string } | { textContent: string }isolateEvents?: boolean | string[]event.stopPropagation will be called on events trying to bubble out of the shadow root.true to stop the propagation of a default set of events, ["keyup", "keydown", "keypress"]API reference generated by docs/generate-api-references.ts