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 616c0eb
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions components/lib/hooks/useStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,33 @@ 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 existingStyleContainerChild = styleContainer.querySelector(`style[data-primereact-style-id="${name}"]`);

if (existingStyleContainerChild) {
return existingStyleContainerChild;
}

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

if (existingElementWithId) {
return existingElementWithId;
}
}

return document.createElement('style');
};

const update = (newCSS) => {
isLoaded && css !== newCSS && (styleRef.current.textContent = newCSS);
};

const load = () => {
if (!document || isLoaded) return;

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';
Expand Down

0 comments on commit 616c0eb

Please sign in to comment.