Gathering detailed insights and metrics for spritelab
Gathering detailed insights and metrics for spritelab
npm install spritelab
Typescript
Module System
Node Version
NPM Version
TypeScript (88.59%)
JavaScript (11.41%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
13
Last Day
1
Last Week
13
Last Month
13
Last Year
13
3 Stars
11 Commits
1 Watching
1 Branches
2 Contributors
Latest Version
0.2.5
Package Id
spritelab@0.2.5
Unpacked Size
76.97 kB
Size
15.39 kB
File Count
31
NPM Version
10.1.0
Node Version
20.9.0
Publised On
06 Jan 2025
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
13
Compared to previous week
Last month
0%
13
Compared to previous month
Last year
0%
13
Compared to previous year
The Ultimate Custom Icon Library for React
There are many approaches to using icons in React projects, each with its own pros and cons. Let's explore some of the common methods:
Libraries like React Icons, Material Icons, and Ant Icons are popular and easy to use. They offer thousands of icons readily available as React components. However, a downside of this approach is that these libraries can be quite heavy, increasing the bundle size—a common problem with React Icons.
You can also use SVG images directly with the <img>
tag. This approach is straightforward and eliminates the need for a heavy library. However, it lacks the flexibility and customizability of a dedicated icon library.
Since SVGs are markup just like HTML, you can use them directly in JSX. This makes the icons highly customizable. However, this approach can be challenging to maintain. Imagine having hundreds of React components in your project just for icons. Adding or removing icons would require significant refactoring and manual work.
An optimized approach is to use an SVG sprite, which is a single SVG file containing multiple icons. Libraries like Feather Icons and Lucide Icons provide this option. All the icons are compiled into one file, making it convenient. However, there are two main issues with static sprites:
SpriteLab takes the SVG sprite approach a step further. It allows you to create and maintain custom sprites from the terminal without installing any packages. With a few commands, you can have a custom icon library ready. You can add icons to your library using just the URL to the SVG file or the path to an SVG file on your device. SpriteLab automatically creates sprites and a React component that allows you to use any icon from your library with extreme flexibility.
You don't have to install the package. Just run:
1npx spritelab init
This will prompt you with a series of questions about how you’d like to set up your library. Once done, the following files will be created in your repo:
All the above files are generated automatically and do not require manual editing.
Once the initialization is complete, proceed to add icons to the library.
1npx spritelab add --name bell-fill --icon 'D:\\Downloads\\icons\\bell-fill.svg'
This will add the bell-fill
icon to the library.
1// The component name is Icon by default. 2// Use the icon name that you chose. 3<Icon icon="default/bell-fill" />
The component accepts all props that an SVG element accepts, making it highly customizable:
1<Icon icon="default/bell-fill" strokeWidth={2} width={32} height={32} />
To use any icon, make use of the icon
prop in the component. It uses the sprite-name/icon-name
naming convention. If you are using TypeScript, you get autocomplete for this prop, making your life easier.
init
Initialize the icon library by answering a few questions. This will create a sprite file, a React component.
Options
-y, --yes
: Skip the questions and use the default values.Example
1npx spritelab init
create
Create a new sprite.
Options
-n, --name <name>
: Name of the sprite to be created.Example
1npx spritelab create --name notifications
add
Add an icon to a sprite.
Options
-n, --name <name>
: Name of the icon to be added.-i, --icon <icon>
: URL or file path of the SVG icon to be added. Enclose the URL or file path in single or double quotes.-s, --sprite [sprite]
: Name of the sprite to add the icon to. If not provided, the icon will be added to the default sprite.Examples
1npx spritelab add --name bell-fill --icon 'D:\\Downloads\\icons\\bell-fill.svg'
1npx spritelab add --name bell-fill --icon 'https://api.iconify.design/bi/bell-fill.svg' --sprite notifications
remove
Remove an icon from a sprite.
Options
-n, --name <name>
: Name of the icon to be removed.-s, --sprite [sprite]
: Name of the sprite to remove the icon from. If not provided, the icon will be removed from the default sprite if it exists.Example
1npx spritelab remove --name bell-fill --sprite notifications
delete
Delete a sprite.
Options
-n, --name <name>
: Name of the sprite to be deleted. If not provided, the default sprite will be deleted.Example
1npx spritelab delete --name notifications
For small projects, the default configuration should be sufficient. You can add icons to the default sprite, and it should work fine.
For larger projects, where there might be hundreds or thousands of icons, adding all of them to a single SVG sprite can be inefficient. The sprite itself would be huge, impacting the initial page load.
To optimize, split icons into separate sprites based on their usage—similar to code-splitting in JavaScript. For example, you can load a sprite only when it’s needed, such as when a specific user action occurs.
Let’s say the bell-fill
icon is used only for notifications. First, create a new sprite:
1npx spritelab create --name notifications
Next, add the bell-fill
icon to the notifications
sprite:
1npx spritelab add --name bell-fill --icon 'D:\\Downloads\\icons\\bell-fill.svg' --sprite notifications
Remove the bell-fill
icon from the default sprite:
1npx spritelab remove --name bell-fill --sprite default
Use the bell-fill
icon from the notifications
sprite:
1<Icon icon="notifications/bell-fill" />
Internally, every sprite is a separate SVG file. A sprite is only loaded when it is used in the HTML document.
Browsers often implement aggressive caching, which can cause issues when icons are added or removed from a sprite. To address this, every time a change is made to the library, the icon component is updated with a version identifier. This ensures that the browser fetches the latest version of the sprite.
Feel free to suggest changes or new ideas. Raise issues if you find any bugs. If you wish to contribute by writing code, make the changes and raise a PR to the master
branch. PR guidelines are not yet set up, so please cooperate accordingly.
No vulnerabilities found.
No security vulnerabilities found.