-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/MN-80/modify-svg-dynamically (#496)
Add custom styling to SVG overlays --------- Co-authored-by: souyahia-monk <[email protected]>
- Loading branch information
1 parent
12edd24
commit 1e71e67
Showing
9 changed files
with
169 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
54 changes: 54 additions & 0 deletions
54
packages/camera/src/components/Overlay/SVGElementMapper.js
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,54 @@ | ||
/* eslint-disable react/no-array-index-key */ | ||
import PropTypes from 'prop-types'; | ||
import React, { useMemo } from 'react'; | ||
|
||
import { useInnerHTML, useJSXTransformAttributes, useCustomSVGAttributes } from './hooks'; | ||
|
||
export default function SVGElementMapper({ | ||
element, | ||
rootStyles, | ||
pathStyles, | ||
}) { | ||
const Tag = useMemo(() => element.tagName, [element]); | ||
const innerHTML = useInnerHTML({ element }); | ||
const transformedAttributes = useJSXTransformAttributes(element); | ||
const customAttributes = useCustomSVGAttributes({ | ||
element, | ||
rootStyles, | ||
pathStyles, | ||
}); | ||
const attributes = useMemo(() => ({ | ||
...transformedAttributes, | ||
...customAttributes, | ||
style: { | ||
...transformedAttributes?.style ?? {}, | ||
...customAttributes?.style ?? {}, | ||
}, | ||
}), []); | ||
const children = useMemo(() => [...element.children], [element]); | ||
|
||
return ( | ||
<Tag {...attributes}> | ||
{innerHTML} | ||
{children.map((child, id) => ( | ||
<SVGElementMapper | ||
key={id.toString()} | ||
element={child} | ||
rootStyles={rootStyles} | ||
pathStyles={pathStyles} | ||
/> | ||
))} | ||
</Tag> | ||
); | ||
} | ||
|
||
SVGElementMapper.propTypes = { | ||
element: PropTypes.any.isRequired, | ||
pathStyles: PropTypes.object, | ||
rootStyles: PropTypes.object, | ||
}; | ||
|
||
SVGElementMapper.defaultProps = { | ||
pathStyles: {}, | ||
rootStyles: {}, | ||
}; |
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,4 @@ | ||
export { default as useInnerHTML } from './useInnerHTML'; | ||
export { default as useJSXTransformAttributes } from './useJSXTransformAttributes'; | ||
export { default as useCustomSVGAttributes } from './useCustomSVGAttributes'; | ||
export { default as useXMLParser } from './useXMLParser'; |
21 changes: 21 additions & 0 deletions
21
packages/camera/src/components/Overlay/hooks/useCustomSVGAttributes.js
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,21 @@ | ||
import { useMemo } from 'react'; | ||
|
||
export default function useCustomSVGAttributes({ | ||
element, | ||
rootStyles, | ||
pathStyles, | ||
}) { | ||
return useMemo(() => { | ||
const elementTag = element.tagName; | ||
|
||
if (elementTag === 'svg') { | ||
return { style: rootStyles ?? {} }; | ||
} | ||
|
||
return { style: pathStyles ?? {} }; | ||
}, [ | ||
element, | ||
rootStyles, | ||
pathStyles, | ||
]); | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/camera/src/components/Overlay/hooks/useInnerHTML.js
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,11 @@ | ||
import { useMemo } from 'react'; | ||
|
||
export default function useInnerHTML({ element }) { | ||
return useMemo(() => { | ||
if (element.tagName === 'style' && !!element.innerHTML) { | ||
return element.innerHTML; | ||
} | ||
|
||
return null; | ||
}, [element]); | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/camera/src/components/Overlay/hooks/useJSXTransformAttributes.js
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,35 @@ | ||
import { useMemo } from 'react'; | ||
|
||
function tranformJsxAttribute(key, value) { | ||
switch (key) { | ||
case 'class': | ||
return { key: 'className', value }; | ||
case 'xml:space': | ||
return { key: 'xmlSpace', value }; | ||
case 'style': | ||
return value.split(';') | ||
.map((style) => style.split(':')) | ||
.reduce((prev, curr) => { | ||
const [styleKey, styleValue] = curr; | ||
const transformedKey = styleKey.replace(/-./g, (css) => css.toUpperCase()[1]); | ||
return { | ||
...prev, | ||
[transformedKey]: styleValue, | ||
}; | ||
}, {}); | ||
default: | ||
return { key, value }; | ||
} | ||
} | ||
|
||
export default function useJSXTransformAttributes(element) { | ||
return useMemo(() => element | ||
.getAttributeNames() | ||
.reduce((prev, attr) => { | ||
const { key, value } = tranformJsxAttribute(attr, element.getAttribute(attr)); | ||
return { | ||
...prev, | ||
[key]: value, | ||
}; | ||
}, {}), [element]); | ||
} |
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,5 @@ | ||
import { useMemo } from 'react'; | ||
|
||
export default function useXMLParser(xml) { | ||
return useMemo(() => new DOMParser().parseFromString(xml, 'text/xml'), [xml]); | ||
} |
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 |
---|---|---|
@@ -1,31 +1,42 @@ | ||
import React, { useEffect, useMemo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Image } from 'react-native'; | ||
import React, { useEffect, useMemo } from 'react'; | ||
import log from '../../utils/log'; | ||
|
||
export default function Overlay({ label, svg, ...passThoughProps }) { | ||
const base64 = useMemo(() => btoa(unescape(encodeURIComponent(svg))), [svg]); | ||
import { useXMLParser } from './hooks'; | ||
import SVGElementMapper from './SVGElementMapper'; | ||
|
||
export default function Overlay({ label, svg, rootStyles, pathStyles }) { | ||
const doc = useXMLParser(svg); | ||
const svgElement = useMemo(() => { | ||
const svgElm = doc.children[0]; | ||
if (svgElm.tagName !== 'svg') { | ||
throw new Error('Invalid Overlay SVG: expected <svg> tag as the first children of XML document.'); | ||
} | ||
return svgElm; | ||
}, [doc]); | ||
|
||
useEffect(() => { | ||
log(['[Event] Loading sight', label]); | ||
}, [label]); | ||
|
||
return ( | ||
<Image | ||
accessibilityLabel={`Overlay ${label}`} | ||
source={{ uri: `data:image/svg+xml;base64,${base64}` }} | ||
width="100%" | ||
height="100%" | ||
{...passThoughProps} | ||
<SVGElementMapper | ||
element={svgElement} | ||
rootStyles={rootStyles} | ||
pathStyles={pathStyles} | ||
/> | ||
); | ||
} | ||
|
||
Overlay.propTypes = { | ||
label: PropTypes.string, | ||
pathStyles: PropTypes.object, | ||
rootStyles: PropTypes.object, | ||
svg: PropTypes.string.isRequired, | ||
}; | ||
|
||
Overlay.defaultProps = { | ||
label: '', | ||
pathStyles: {}, | ||
rootStyles: {}, | ||
}; |