-
Notifications
You must be signed in to change notification settings - Fork 14
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
fix(PPDSC-2605): safari svg filters fix #487
fix(PPDSC-2605): safari svg filters fix #487
Conversation
You can preview these changes on: |
*/ | ||
const loadSVGFiltersForSafari = (path: string) => { | ||
if (!isSafari) return null; | ||
const {sanitize} = dompurify; |
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.
wondering if we can load the dompurify with dynamic import so that our bundle size does not increase
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.
In this case we can, but we are already using the dompurify in another Component: site/components/tools/svg-previewer.tsx
. So I think the bundle size is already including that.
I tried to import it dynamically there as well but I wasn't able to figure out how to dynamically import an npm package in a react component body. If you have any ideas, happy to discuss.
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.
there is a difference I believe, there is a bundle per page, so the previewer will include it only there, and since we start using it for SVG's and they are on all pages it will start to be included everywhere.
I think you might need to do that inside useEffect and maybe create a custom hook in order to achieve it. I am not sure if its with the time these extra 10kb but have a try
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.
Updated the code, the sanitizer should be load dynamically only for Safari now.
@evgenitsn fyi it looks like this is being picked up by the Percy tests, so you will have some to resolve. |
Thanks Mike, yes I noticed this in a few more cases. I will push an update. Marking it as WIP now as the implementation would change a bit. |
@mutebg @mstuartf I've updated the implementation, it turned out that appending only the section is not reliable solution because not all filters are contained inside it. So currently for Safari we are just inlining the SVG into the code (same as before the svg/tsx change) and for the rest of the browsers we are using external SVGs inside the i.e. how it is supposed to work. |
@mstuartf @mutebg @LukeFinch Thank for looking into this. I think it's in a good shape now, ready for reviews. |
PPDSC-2605
What
This fixes the issue with Safari where some of the SVG are not rendered correctly.
The issue is that Safari does not support additional filters like shadows, blurs and more if the svg is external i.e.imported using
<use href={...} />
.The workaround for this is to create a new empty
<svg>
and add the<defs>
section of the svgs (the part where the filters are located) and add this svg anywhere in the DOM.What I did is to check if the browser is Safari, and if it is to read the svg as raw string, sanitize it, get substring of that
<defs> .. </defs>
and append it as empty, hidden svg next to the original svg.If the browser is not Safari, this logic is skipped and nothing changes.
Looks like this approach fixes the Safari issues with the illustrations.
Example of the DOM for Safari vs Chrome
Safari:
![image](https://user-images.githubusercontent.com/7677924/203534477-91af07c5-58ac-4f74-b0fe-9a32bf9fb535.png)
Chrome:
![image](https://user-images.githubusercontent.com/7677924/203534803-70457be6-a6eb-4a12-bb43-c7764c620658.png)
I have done:
I have tested manually:
Before:
After:
![image](https://user-images.githubusercontent.com/7677924/203537221-58bcb158-2530-45d1-8f43-9762a5b3a572.png)
Who should review this PR:
How to test: