feature detect CSSStylesheet (#7628)

* feature detect CSSStylesheet

* add changeset

* logs

* polyfill instead

* polyfill instead

* remove polyfill

* fix lockfile

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
pngwn 2024-03-06 20:27:39 +00:00 committed by GitHub
parent 1a4b089e78
commit ba8cc48b13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -0,0 +1,6 @@
---
"@gradio/app": patch
"gradio": patch
---
fix:feature detect CSSStylesheet

View File

@ -1,3 +1,16 @@
let supports_adopted_stylesheets = false;
if (
"attachShadow" in Element.prototype &&
"adoptedStyleSheets" in Document.prototype
) {
// Both Shadow DOM and adoptedStyleSheets are supported
const shadow_root_test = document
.createElement("div")
.attachShadow({ mode: "open" });
supports_adopted_stylesheets = "adoptedStyleSheets" in shadow_root_test;
}
export function mount_css(url: string, target: HTMLElement): Promise<void> {
const base = new URL(import.meta.url).origin;
const _url = new URL(url, base).href;
@ -23,7 +36,8 @@ export function prefix_css(
string: string,
version: string,
style_element = document.createElement("style")
): HTMLStyleElement {
): HTMLStyleElement | null {
if (!supports_adopted_stylesheets) return null;
style_element.remove();
const stylesheet = new CSSStyleSheet();