From 36cd4728fdfaa662e25c819cde1783311154a8f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Necati=20=C3=96zmen?= Date: Tue, 7 Jan 2025 10:03:22 +0300 Subject: [PATCH 1/2] docs(blog): update TypeScript Record image and edit article on using SVGs in React --- .../blog/2023-06-16-typescript-record.md | 2 +- ...7-react-svg.md => 2025-01-07-react-svg.md} | 91 ++++++++++++++++--- 2 files changed, 81 insertions(+), 12 deletions(-) rename documentation/blog/{2023-11-17-react-svg.md => 2025-01-07-react-svg.md} (85%) diff --git a/documentation/blog/2023-06-16-typescript-record.md b/documentation/blog/2023-06-16-typescript-record.md index d8da049fc4c0..03c17a26759b 100644 --- a/documentation/blog/2023-06-16-typescript-record.md +++ b/documentation/blog/2023-06-16-typescript-record.md @@ -4,7 +4,7 @@ description: We'll explore TypeScript Record type with examples. slug: typescript-record-type authors: abdullah_numan tags: [typescript] -image: https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-06-16-typescript-record/social.png +image: https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-06-16-typescript-record/social-2.png hide_table_of_contents: false --- diff --git a/documentation/blog/2023-11-17-react-svg.md b/documentation/blog/2025-01-07-react-svg.md similarity index 85% rename from documentation/blog/2023-11-17-react-svg.md rename to documentation/blog/2025-01-07-react-svg.md index fd8a98f6e230..963282b557f5 100644 --- a/documentation/blog/2023-11-17-react-svg.md +++ b/documentation/blog/2025-01-07-react-svg.md @@ -4,10 +4,12 @@ description: Let's talk about React SVG and how it makes the process of adding a slug: react-svg authors: chidume_nnamdi tags: [react] -image: https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-11-17-react-svg/social.png +image: https://refine.ams3.cdn.digitaloceanspaces.com/blog/2023-11-17-react-svg/social-2.png hide_table_of_contents: false --- +**This article was last updated on January 7, 2025, to include sections on Accessibility Best Practices for SVGs and Performance Optimization Tips for Using SVGs in React.** + ## Introduction In the world of making websites look good, pictures, especially images, are super important. They make up a big part (60%!) of what you see online, making websites more interesting and helping to share information. Among the many tools that developers use, React SVG stands out. It's like a superhero for putting cool and scalable graphics, especially Scalable Vector Graphics (SVG), into websites. @@ -21,19 +23,13 @@ Steps we'll cover: - [What is SVG?](#what-is-svg) - [Importing SVGs](#importing-svgs) - [Importing as a React Component:](#importing-as-a-react-component) +- [Using an `` Tag](#using-an-img-tag) - [Inline SVG](#inline-svg) - [Using `react-svg`](#using-react-svg) +- [Troubleshooting SVG Issues](#troubleshooting-svg-issues) +- [Performance optimization with SVGs](#performance-optimization-with-svgs) - [Styling SVGs in React](#styling-svgs-in-react) - - [Inline SVG as JSX](#inline-svg-as-jsx) - - [Manipulating SVG Props](#manipulating-svg-props) - - [Dynamic SVGs](#dynamic-svgs) - - [Initializing State and Props](#initializing-state-and-props) - - [Creating SVG Components](#creating-svg-components) - - [Animating SVG Elements](#animating-svg-elements) - - [Composing SVG Components](#composing-svg-components) - - [Using Third-Party Libraries](#using-third-party-libraries) - - [Animation with SVGs in React](#animation-with-svgs-in-react) - - [Create or Import an SVG](#create-or-import-an-svg) +- [SVG vs Other Image Formats](#svg-vs-other-image-formats) ## What is SVG? @@ -196,6 +192,66 @@ Ensure you have these loaders installed (`npm install --save-dev react-svg-loade Choose the method that best suits your project requirements and workflow. Each approach has its strengths, and the choice often depends on factors such as ease of use, scalability, and the specific needs of your application. +## Troubleshooting SVG Issues + +Add a section regarding common issues people encounter and how one can resolve those while using SVGs in React. + +### SVG Not Rendering + +Ensure that the path to the SVG file is correct. + +- Check whether your bundler is set up to handle SVG files correctly. + +### SVG Styling Not Applying + +Inline styles in React can sometimes clash. Be sure you're using React-compatible syntax: + +```tsx +style={{ fill: "red" }} +``` + +### Poor performance with complex SVGs + +- Minify the SVG or simplify the paths using tools like SVGO. + +- Replace complex animations with pure CSS animations or lightweight alternatives. + +### Accessibility Issues + +- Make sure you have added ARIA roles and labels for screen readers. + +### Dynamic SVGs Not Updating + +- Make sure props are being passed correctly and updating the state tied to your SVG. + +## Performance optimization with SVGs + +Cover how to optimize SVGs for better performance, especially for large or complex graphics. + +Although SVGs are small and scaleable, bad ones have performance hurts. Here's how to optimize SVGs: + +- **Minify SVG Files:** +- Use tools like [SVGO](https://github.com/svg/svgo) to remove unnecessary metadata and reduce file size. + +- **Inline SVGs in moderation:** + +For larger SVGs, consider using `` tags over using inline SVGs for the purpose of avoiding large DOMs. + +- **Simplify Complex Graphics:** + +Minimize the number of paths and nodes in your SVGs by employing design tools like Adobe Illustrator and Sketch. + +- **Lazy Load SVGs:** Lazy-load non-critical SVGs to improve page load time. + +- **Icon Sprite Sheets:** Combine multiple SVG icons into one sprite sheet and use them in your views with `` tags: + +```tsx + + {" "} + {" "} + +``` + ## Styling SVGs in React We have seen ways to import and use SVGs in React. Now, let's look at ways we can style SVGs in React. @@ -539,3 +595,16 @@ const InteractiveSVG = () => { export default InteractiveSVG; ``` + +## SVG vs Other Image Formats + +What are some of the advantages of SVGs compared to raster formats like PNG, JPG, or GIF? A brief comparison: + +- **Scalability**: Since SVGs are resolution-independent, they upscale perfectly, whereas with raster images, pixelation starts degrading visible quality beyond certain dimensions. +- **Interactivity**: SVGs can be controlled via CSS and JavaScript to provide animations and user interactions, unlike PNG or JPG. + +- **File Size**: SVGs are often smaller for simple graphics, making them ideal for logos and icons, but they can become larger for complex designs. + +- **Accessibility**: Since SVGs are text-based, this intrinsically means they are far more accessible and searchable. Provided that proper configuration has been applied, screen readers can read out SVGs. + +- **Browser Support**: Most modern browsers handle SVG natively and therefore are quite predictable for web development. From 713d0ce7c45cf376acd0265eb250a4ede8ac6ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Necati=20=C3=96zmen?= Date: Thu, 9 Jan 2025 10:00:40 +0300 Subject: [PATCH 2/2] docs(blog): correct spelling of "scalable" in SVG optimization blog post --- documentation/blog/2025-01-07-react-svg.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/blog/2025-01-07-react-svg.md b/documentation/blog/2025-01-07-react-svg.md index 963282b557f5..ffca8eccea31 100644 --- a/documentation/blog/2025-01-07-react-svg.md +++ b/documentation/blog/2025-01-07-react-svg.md @@ -228,7 +228,7 @@ style={{ fill: "red" }} Cover how to optimize SVGs for better performance, especially for large or complex graphics. -Although SVGs are small and scaleable, bad ones have performance hurts. Here's how to optimize SVGs: +Although SVGs are small and scalable, bad ones have performance hurts. Here's how to optimize SVGs: - **Minify SVG Files:** - Use tools like [SVGO](https://github.com/svg/svgo) to remove unnecessary metadata and reduce file size.