-
Notifications
You must be signed in to change notification settings - Fork 3
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 #391 from openstax/image-colors
Added color to the images based on the learning path
- Loading branch information
Showing
7 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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)} | ||
fillColor={study.learningPath?.color || colors.lavender} | ||
altText={getAltText(study.imageId)} | ||
/> | ||
) | ||
} |
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