-
-
Save Shelob9/2dd838530ac973a03eba04ee417fdbd7 to your computer and use it in GitHub Desktop.
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
/** | |
* External dependencies | |
*/ | |
import PropTypes from 'prop-types'; | |
/** | |
* WordPress dependencies | |
*/ | |
// eslint-disable-next-line import/no-extraneous-dependencies | |
import { useEffect } from '@wordpress/element'; | |
/** | |
* Add an inline script to body | |
*/ | |
export const useWithInlineScript = ({ inlineScript }) => { | |
useEffect(() => { | |
if (!window) { | |
return; | |
} | |
const script = document.createElement('script'); | |
script.appendChild(document.createTextNode(inlineScript)); | |
document.body.appendChild(script); | |
}, []); | |
} | |
useWithInlineScript.propTypes = { | |
inlineScript: PropTypes.string.isRequired, | |
} | |
/** | |
* Add a script tag to body | |
*/ | |
export const useWithScript = ({ scriptSource }) => { | |
useEffect(() => { | |
if (!window) { | |
return; | |
} | |
const script = document.createElement('script'); | |
script.src = scriptSource; | |
script.async = true; | |
document.body.appendChild(script); | |
}, []); | |
} | |
useWithScript.propTypes = { | |
scriptSource: PropTypes.string.isRequired, | |
} |
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
/** | |
* External dependencies | |
*/ | |
import PropTypes from 'prop-types'; | |
/** | |
* WordPress dependencies | |
*/ | |
// eslint-disable-next-line import/no-extraneous-dependencies | |
import { useEffect,useRef } from '@wordpress/element'; | |
/** | |
* Add an inline script to body | |
*/ | |
export const useWithInlineScript = ({ inlineScript }) => { | |
const script = useRef(); | |
useEffect(() => { | |
if (!window) { | |
return; | |
} | |
script.current = document.createElement('script'); | |
script.current.appendChild(document.createTextNode(inlineScript)); | |
document.body.appendChild(script.current); | |
return () => { | |
script.current.remove(); | |
} | |
}, []); | |
} | |
useWithInlineScript.propTypes = { | |
inlineScript: PropTypes.string.isRequired, | |
} | |
/** | |
* Add a script tag to body | |
*/ | |
export const useWithScript = ({ scriptSource }) => { | |
const script = useRef(); | |
useEffect(() => { | |
if (!window) { | |
return; | |
} | |
script.current = document.createElement('script'); | |
script.current.src = scriptSource; | |
script.current.async = true; | |
document.body.appendChild(script.current); | |
return () => { | |
script.current.remove(); | |
} | |
}, []); | |
} | |
useWithScript.propTypes = { | |
scriptSource: PropTypes.string.isRequired, | |
} |
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
/** | |
* External dependencies | |
*/ | |
import PropTypes from 'prop-types'; | |
/** | |
* WordPress dependencies | |
*/ | |
// eslint-disable-next-line import/no-extraneous-dependencies | |
import { Fragment, useEffect } from '@wordpress/element'; | |
/** | |
* Add an inline script to body and render component | |
*/ | |
export const WithInlineScript = ({ inlineScript, children }) => { | |
useEffect(() => { | |
if (!window) { | |
return; | |
} | |
const script = document.createElement('script'); | |
script.appendChild(document.createTextNode(inlineScript)); | |
document.body.appendChild(script); | |
}, []); | |
return <Fragment>{children}</Fragment>; | |
} | |
WithInlineScript.propTypes = { | |
inlineScript: PropTypes.string.isRequired, | |
children: PropTypes.node.isRequired | |
} | |
/** | |
* Add a script tag to body and render component. | |
*/ | |
export const WithScript = ({ scriptSource, children }) => { | |
useEffect(() => { | |
if (!window) { | |
return; | |
} | |
const script = document.createElement('script'); | |
script.src = scriptSource; | |
script.async = true; | |
document.body.appendChild(script); | |
}, []); | |
return <Fragment>{children}</Fragment>; | |
} | |
WithScript.propTypes = { | |
scriptSource: PropTypes.string.isRequired, | |
children: PropTypes.node.isRequired | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment