How to resolve Material Icons with a cool Resolver
Adding required 100+ material icons in a react component is always bit messy. By considering Resolver Pattern you can resolve them with resolverπ
Before dig into writing a resolver, let's first walkthrough about Strategy/Resolver Pattern.
π Resolver Pattern
This is a simpler design pattern - it allows you to:
- Encapsulate a family of related algorithms
- Allow them to vary
- Allow a class to maintain a single purpose and implementation
- Separate the calculation from the delivery of its results
- By doing this we can maintain standard principles, leaving the code open to extension but closed to modification.
π Problem
While developing SPA Icons play a very vital role, So as per User Experience we are using icons on a very high demand, Material UI is a most commonly used design system with ReactJS application development, Having 100+ imports in your components for Icon specific only is a messy thing i ever seen.
π Solution
Considering Resolver Pattern, Let's develop a resolver with Javascript/React/Angular/Vue to resolve the icon which is required to use inside a component, You only have to import a resolver file inside your components and pass a key related to your icon, resolver will compare that key in imports itself and return you an specific icon, cool π right? Let's develop this small resolver to solve our major problem rocket π
π Steps for developing Icon Resolver π§°
- Create a directory named
resolvers
- Create a
.js
file nameiconResolver.js
- Write a parametrised function for resolving an icon
π Development - Step by Step
Import all Material Icons inside your
iconResolver.js
fileimport { Dashboard, Home, List, Menu, ShoppingCart } from "@mui/icons-material";
inside your function create an object of icons with key/value pairs by consuming imported icons
// list of all available icons const icons = { default: Dashboard, dashboard: Dashboard, menu: Menu, cart: ShoppingCart, home: Home, list: List }
Checking if required icon from a component file is available in resolver imports or not
const _icon = icons[name] ? icons[name] : icons['default'];
Let's return a resolved icon to a desired file π
return(<_icon key={`resolved_icon_${name}`}/>);
Consume resolver in your component by importing it and passing a key of icon you need:
// Component
import IconResolver from '../../core/resolvers/iconResolver';
<div>
{IconResolver('home)}
</div>
π Full Resolver Source Code
Here's a full source of resolver from a separate file iconResolver.js
import { Dashboard, Home, List, Menu, ShoppingCart } from "@mui/icons-material";
function IconResolver(name) {
// list of all available icons
const icons = {
default: Dashboard,
dashboard: Dashboard,
menu: Menu,
cart: ShoppingCart,
home: Home,
list: List
}
// checking if icon which is invoked from a file is available in resolver or not
const _icon = icons[name] ? icons[name] : icons['default'];
// returning a resolved icon to a desired file
return(<_icon key={`resolved_icon_${name}`}/>);
}
export default IconResolver;
Here's a source of resolver consumption from a component
// Component
import IconResolver from '../../core/resolvers/iconResolver';
<div>
{IconResolver('home)}
</div>
Enjoy resolving 100 and 1000+ icons from a single file π€©