-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor #4602 - Add unstyled feature
- Loading branch information
1 parent
840d557
commit 7d7be62
Showing
8 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { DomHandler, ObjectUtils } from "../utils/Utils"; | ||
|
||
let _id = 0; | ||
|
||
export const useStyle = (css = {}, options = {}) => { | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
|
||
const cssRef = useRef(null); | ||
const defaultDocument = DomHandler.isClient() ? window.document : undefined; | ||
const { document = defaultDocument, immediate = true, manual = false, name = `primereact_style_${++_id}`, media } = options; | ||
|
||
useEffect(() => { | ||
cssRef.current = css; | ||
}, [css]); | ||
|
||
const load = () => { | ||
if (!document) return; | ||
|
||
const el = document.querySelector(`[data-pc-name="${name}"]`) || document.createElement('style'); | ||
|
||
if (ObjectUtils.isNotEmpty(el) || !el.isConnected) { | ||
el.type = 'text/css'; | ||
el.setAttribute('data-pc-name', name); | ||
if (media) el.media = media; | ||
document.head.appendChild(el); | ||
} | ||
|
||
if (isLoaded) return; | ||
|
||
el.textContent = cssRef.current; | ||
setIsLoaded(true); | ||
}; | ||
|
||
const unload = () => { | ||
if (!document || !isLoaded) return; | ||
const node = document.querySelector(`[data-pc-name="${name}"]`); | ||
|
||
if (node && node.isConnected) { | ||
document.head.removeChild(node); | ||
setIsLoaded(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (immediate && !manual) load(); | ||
|
||
return () => unload(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [immediate, manual]); | ||
|
||
return { | ||
name, | ||
css: cssRef, | ||
unload, | ||
load, | ||
isLoaded | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters