Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix attaching styles, check shadowroot #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/hooks/useStyleSheet.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
/* eslint-disable no-extra-boolean-cast */
import { RefObject } from "react";

import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
import { getOwnerDocument } from "../utils/ownerDocument";
import { getNonce } from "../utils/nonce";

// Bundler is configured to load this as a processed minified CSS-string
import styles from "../css/styles.css";

const styleElementMap: Map<Document, HTMLStyleElement> = new Map();
const styleElementMap: Map<Document | ShadowRoot, HTMLStyleElement> = new Map();

/**
* Injects CSS code into the document's <head>
*/
export const useStyleSheet = (nodeRef: RefObject<HTMLDivElement>): void => {
useIsomorphicLayoutEffect(() => {
const parentDocument = nodeRef.current ? nodeRef.current.ownerDocument : document;

if (typeof parentDocument !== "undefined" && !styleElementMap.has(parentDocument)) {
const styleElement = parentDocument.createElement("style");
const ownerDocument = getOwnerDocument(nodeRef.current);
const parentDocument = ownerDocument || document;
if (
parentDocument &&
typeof parentDocument !== "undefined" &&
!styleElementMap.has(parentDocument)
) {
const styleElement = document.createElement("style");
definite2 marked this conversation as resolved.
Show resolved Hide resolved
styleElement.innerHTML = styles;
styleElementMap.set(parentDocument, styleElement);

// Conform to CSP rules by setting `nonce` attribute to the inline styles
const nonce = getNonce();
if (nonce) styleElement.setAttribute("nonce", nonce);

parentDocument.head.appendChild(styleElement);
if (parentDocument instanceof ShadowRoot) {
parentDocument.appendChild(styleElement);
} else {
parentDocument.head.appendChild(styleElement);
}
}
}, []);
};
11 changes: 11 additions & 0 deletions src/utils/ownerDocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const getOwnerDocument = (node: HTMLElement | null): ShadowRoot | Document | null => {
let parent = node && node.parentNode;
const ownerDocument = node && node.ownerDocument;
while (parent) {
if (parent instanceof ShadowRoot) {
return parent;
}
parent = parent.parentNode;
}
return ownerDocument;
};