-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added color to the images based on the learning path #391
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { React, useState, useEffect } from '@common' | ||
import { ParticipantStudy } from '@api' | ||
import { getImageUrl, getAltText } from './card-images' | ||
import { colors } from '@theme'; | ||
|
||
const SVGManipulator: React.FC<{src: string, fillColor: string, altText: string}> = ({ src, fillColor, altText }) => { | ||
const [imgSrc, setImgSrc] = useState(''); | ||
|
||
useEffect(() => { | ||
if(src.includes('.png')){ | ||
setImgSrc(src) | ||
return; | ||
} | ||
fetch(src) | ||
.then((response) => response.text()) | ||
.then((data) => { | ||
const parser = new DOMParser(); | ||
const doc = parser.parseFromString(data, 'image/svg+xml'); | ||
|
||
const paths = doc.querySelectorAll('path'); | ||
paths.forEach((path) => { | ||
const currentFill = path.getAttribute('fill'); | ||
const newFill = currentFill && currentFill != colors.lavender ? fillColor : currentFill; | ||
path.setAttribute('fill', newFill || 'none'); | ||
}); | ||
const updatedSVG = new XMLSerializer().serializeToString(doc); | ||
|
||
const svgBlob = new Blob([updatedSVG], { type: 'image/svg+xml' }); | ||
const url = URL.createObjectURL(svgBlob); | ||
|
||
setImgSrc(url); | ||
|
||
return () => URL.revokeObjectURL(url); | ||
}); | ||
}, [src, fillColor]); | ||
|
||
return <img src={imgSrc} alt={altText} className='study-card-image' />; | ||
}; | ||
|
||
|
||
export const StudyCardImage: React.FC<{study: ParticipantStudy}> = ({ study }) => { | ||
|
||
return ( | ||
<SVGManipulator | ||
src={getImageUrl(study.imageId)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More a question of where getImageUrl is defined - do we still want to reference the kinetic s3 bucket or are all the images officially local now with your placeholder addition here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getImageUrl is defined in the card-images.tsx file which is inside the study-cards-images component folder. The kinetic s3 bucket is no longer needed. |
||
fillColor={study.learningPath?.color || colors.lavender} | ||
altText={getAltText(study.imageId)} | ||
/> | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not an expert with SVGs so im curious about this approach, it looks like we are modifying the
fill
property in the SVG and then replacing the image?would this be possible/simpler with CSS? something like this? not sure if it work, but would simplify this a bit: https://stackoverflow.com/questions/9529300/can-i-change-the-fill-color-of-an-svg-path-with-css
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that approach initially and found that since we are using an img tag and using the svg as a src, it would not work. For the above approach to work, the svg file should be rendered directly instead of using an img tag, since the images are in the public folder, I could not import them, to use them directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another problem I noticed with that approach is that on setting the fill values on the svg container, it automatically applies to all the paths, unless a fill property is already present. So we have to manually edit all the paths which don't have a fill value and set it to none, and remove the fill value for the paths which have the secondary color. This way by setting the fill value once, it is applied to all the paths which don't have a fill value (secondary color). The only problem is, the svg files have to be edited manually and should be placed inside the src folder