Comments (15)
In real life, most of the applications use a library where you register the icons and use them in the component. The registry expects an object where the key is the icon name, and the value is the SVG content. Let's take my library as an example:
import { SvgIconsModule, fromSvgProps } from '@ngneat/icons';
import * as icons from '../assets/svg/my-icons.model';
@NgModule({
imports: [
SvgIconsModule.forRoot({
icons: fromSvgProps(icons)
})
],
bootstrap: [AppComponent]
})
export class AppModule {}
And now we can use the icons:
<svg-icon key="settings"></svg-icon>
<svg-icon key="home"></svg-icon>
In these cases, there is no benefit of using constants
because we need an object
. To support your library, I made the fromSvgProps
util:
export function fromSvgProps(propsMap: { [key: string]: { name: string; data: string } }) {
const map = {};
for (const { name, data } of Object.values(icons)) {
map[name] = data;
}
return map;
}
We can change the API to something like:
import { location, settings } from '...';
SvgIconsModule.forRoot({
icons: [location, settings]
})
But imagine a big application with hundreds of icons. You'll need to maintain it, and your file will be huge.
from svg-to-ts.
I will start to work on this soon - hope that I can find time and deliver it asap.
from svg-to-ts.
Great! Thanks. In the meantime, when I'll publish it next week, I will instruct to use the fromSvgProps
helper function.
from svg-to-ts.
I have implemented a first version that is now able to convert icon to object, constants and files. I just want to quickly check if my current implementation meets your requirement and ensure that I understood you correctly.
So if we would convert a folder with the following icons:
- expressionLess.svg
- full.svg
- laughing.svg
we would end up with the following file. ``Γ¬con.ts```
export const awesomeIcons = {
expressionLess:'<svg xmlns="ht....',
full: '<svg xmlns= ....',
laughing: '<svg xmlns="http://www....'
};
When converting to an object I thought to give the consumer the following options:
type | default | output the version number | |
---|---|---|---|
fileName | stirng | my-icons | file name of the generated file |
delimiter | CAMEL, KEBAP, SNAKE | SNAKE | delimiter which is used to generate the types and name properties |
svgoConfig | string or config object | check help command - to large to display | a path to your svgoConfiguration JSON file or an inline configuration object |
srcFiles | string | "/*.svg" | input files matching the given filename pattern |
outputDirectory | string | "./dist" | name of the output directory |
objectName | string | "icons" | name of the exported constant |
Does this meet your requirements?
from svg-to-ts.
Yes, that's great. I'd also add an option to export it as default
.
export default { ... }
Maybe check if objectName
is default
.
from svg-to-ts.
I also think the default delimiter should be camelCase. This is the standard.
from svg-to-ts.
I added in the README a recommended flow with your library.
from svg-to-ts.
Thx a lot. Glad to hear that you like the library and that you want to include it in an article. Yes, we could add this feature. But I am not sure if providing the icons as an object has benefits.
I think that delivering the icons as an object makes them not tree shakable. Currently, we deliver the icons either as singleconsts
so that they are tree shakable or even as single files, so that they are tree shakable and can additionally be split to lazy loaded feature modules.
But maybe there's a good reason to do so. Theoretically we could provide all three approaches, object, const and single file. What do you see as benefit of having them as object props instead of individual consts
?
from svg-to-ts.
If you find it unsuitable for your library, let me know, and I'll create a dedicated library for this :)
from svg-to-ts.
No, after your explanation I think it makes sense - with this, we could support all scenarios - object, const and multiple files.
from svg-to-ts.
Thanks for the quick collaboration! I'll update my library.
from svg-to-ts.
Awesome, let me know if everything worked
from svg-to-ts.
Works great.
from svg-to-ts.
Epic! Glad to hear - let me know once your blog post is out. If you want we can also link to it from our README.
from svg-to-ts.
Awesome thx.
from svg-to-ts.
Related Issues (20)
- `svg-to-ts-files` always generate a single file HOT 2
- Deliver correct ESM
- svg - same id of path generated for all icons HOT 1
- update to svgo version 3.x HOT 1
- Run within a module repository HOT 1
- How to customize size and apply primary color HOT 6
- Unexpected viewBox removal HOT 1
- Possibility to extract only the "d" attribute of the path HOT 1
- 'svg-to-ts' is not recognized as an internal or external command (Angular 15) HOT 5
- SVG not completely converted HOT 1
- Config option "tsx: true" error: Parameter 'props' implicitly has an 'any' type. HOT 3
- Threeshaking not working HOT 1
- Only converting new files
- Expression produces a union type that is too complex to represent. HOT 1
- Tutorial requires a paid Medium account HOT 2
- Option to use existing dir structure when using globs
- When generating an interface, Icon's do not implement the Interface
- Option to generate constant name in uppercase HOT 1
- Add support for verbatimModuleSyntax HOT 4
- Incorrect conversion to React component
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svg-to-ts.