-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #446 from Giphy/bottle-data-tag-fix
execute the bottle data tag
- Loading branch information
Showing
6 changed files
with
53 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@giphy/react-components': minor | ||
'@giphy/js-util': minor | ||
--- | ||
|
||
fix bottle data injection and replace magic tags |
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,36 @@ | ||
import { noUUIDRandom } from '@giphy/js-util' | ||
import React, { useRef, useLayoutEffect } from 'react' | ||
|
||
function processTag(tag: string) { | ||
tag = tag.replace(`%%CACHEBUSTER%%`, noUUIDRandom()) | ||
tag = tag.replace('%%TIMESTAMP%%', `${Date.now()}`) | ||
tag = tag.replace('%%APPBUNDLE%%', `web`) | ||
if (typeof window !== 'undefined') { | ||
tag = tag.replace('%%APP_WINDOW_SIZE%%', `${window.innerWidth},${window.innerHeight}`) | ||
tag = tag.replace('%%DEVICE_LANGUAGE%%', `${navigator.language}`) | ||
} | ||
return tag | ||
} | ||
/** | ||
* Execute a script tag in a React component. | ||
* https://macarthur.me/posts/script-tags-in-react/ | ||
*/ | ||
function BottleData({ markup }: { markup: string }) { | ||
const elRef = useRef<HTMLDivElement>(null) | ||
|
||
useLayoutEffect(() => { | ||
if (!elRef.current) return | ||
const range = document.createRange() | ||
range.selectNode(elRef.current) | ||
const documentFragment = range.createContextualFragment(processTag(markup)) | ||
|
||
// Inject the markup, triggering a re-run! | ||
elRef.current.innerHTML = '' | ||
elRef.current.append(documentFragment) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
return <div ref={elRef} dangerouslySetInnerHTML={{ __html: markup }}></div> | ||
} | ||
|
||
export default BottleData |
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