Skip to content

Commit

Permalink
Fix #6079: useStyle better handling of undefined ID
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Mar 5, 2024
1 parent 2e950d4 commit cb0ef73
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions components/lib/hooks/useStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ export const useStyle = (css, options = {}) => {
const defaultDocument = DomHandler.isClient() ? window.document : undefined;
const { document = defaultDocument, manual = false, name = `style_${++_id}`, id = undefined, media = undefined } = options;

const getCurrentStyleRef = (styleContainer) => {
const existingStyle = styleContainer.querySelector(`style[data-primereact-style-id="${name}"]`);

if (existingStyle) {
return existingStyle;
}

if (id !== undefined) {
const existingElement = document.getElementById(id);

if (existingElement) {
return existingElement;
}
}

// finally if not found create the new style
return document.createElement('style');
};

const update = (newCSS) => {
isLoaded && css !== newCSS && (styleRef.current.textContent = newCSS);
};
Expand All @@ -21,16 +40,16 @@ export const useStyle = (css, options = {}) => {

const styleContainer = context?.styleContainer || document.head;

styleRef.current = styleContainer.querySelector(`style[data-primereact-style-id="${name}"]`) || document.getElementById(id) || document.createElement('style');
styleRef.current = getCurrentStyleRef(styleContainer);

if (!styleRef.current.isConnected) {
styleRef.current.type = 'text/css';
id && (styleRef.current.id = id);
media && (styleRef.current.media = media);
if (id) styleRef.current.id = id;
if (media) styleRef.current.media = media;

DomHandler.addNonce(styleRef.current, (context && context.nonce) || PrimeReact.nonce);
styleContainer.appendChild(styleRef.current);
name && styleRef.current.setAttribute('data-primereact-style-id', name);
if (name) styleRef.current.setAttribute('data-primereact-style-id', name);
}

styleRef.current.textContent = css;
Expand Down

0 comments on commit cb0ef73

Please sign in to comment.