Skip to content

Commit

Permalink
feature detect CSSStylesheet (#7628)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
pngwn and gradio-pr-bot committed Mar 6, 2024
1 parent 1a4b089 commit ba8cc48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/evil-emus-relax.md
@@ -0,0 +1,6 @@
---
"@gradio/app": patch
"gradio": patch
---

fix:feature detect CSSStylesheet
16 changes: 15 additions & 1 deletion js/app/src/css.ts
@@ -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;
Expand All @@ -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();
Expand Down

0 comments on commit ba8cc48

Please sign in to comment.