Comments (10)
We can do that 👍Would be string enum because I think if you use the icons with a registry it's better to have a string as runtime representation than a number.
Generally, you should already get a list of all available icons by using the type. Usually, your IDEA can display them and you would see a list of all available icons. So if for example, your component accepts an icon of myIcon
as @Input
, the IDEA should give you a list of available icons as a hint.
Just out of curiosity, what is your use case?
from svg-to-ts.
Yes, I use IntelliJ and VSCode. My VSCode does not autocomplete it but IntelliJ does, although is not quite the same experience as with an enum.
But my main use case is that I have a components library using Storybook, and I can easily provide the list of available icons through a dropdown to the end user (UI/UX) team by setting a select
knob with the type of the enumerator.
I'm half way with a PR to the project with the enum implementation. My idea was to implement a variable "enumType": true
.
from svg-to-ts.
I second that. Enum instead of type! It is also possible to iterate through an enum, since it doesn't get "lost" from TS -> JS compilation. Therefore creating a demo page showing off all icons is much easier.
from svg-to-ts.
Fixed with version 4.2.0. Thanks to everybody involved in the discussion and special thank to @felipeplets for the awesome PR.
from svg-to-ts.
Wow. Sounds amazing. Looking forward to the PR! 👍
Yes, a flag enumType
which defaults to false sounds good.
from svg-to-ts.
I think we should support both. Maybe some background why it is currently a type. Most often the icons are used with a component and a registry. The usage for an end consumers would be the following:
<my-icon name="foo"></my-icon>
In such a scenario using a type has the following advantage.
-
Usually ideas are able to give you a preview of all available icon types when you want to pass the icon name over an
@Input()
to your icon component. With an enum, it would only show you the enum name and not the values. -
With a type you can simply pass down the value as a string. Like illustrated above. With an enum on the other hand you have to first assign the enum value to a public variable to make it available in your template.
template: `<my-icon name="myCustomIcons.foo"></my-icon>`
// ...
export class MyComponent {
myCustomIcons = myCustomIconsEnum;
}
But I also see the point where it is easier to display all the available icons.
from svg-to-ts.
I understand @kreuzerk.
I'm currently finishing this implementation and thus I was thinking that we can add two options:
1 - -e --enumName
to name the enumerator
2 - -de --disableEnum
to disable it (in case the default behaviour is to generate it) or -de --enableEnum
(in case the default behaviour is to not generate it)
My only question is what do you believe is the best as the default option?
from svg-to-ts.
It's also possible to export both an quasi enum
and a type
, in this way:
export type DinosaurIcon = 'tyrannosaurusIcon' | 'stegosaurusIcon';
export const DinosaurIcon = {
tyrannosaurusIcon: 'tyrannosaurusIcon' as DinosaurIcon,
stegosaurusIcon: 'stegosaurusIcon' as DinosaurIcon
};
Then you can pass in either a string, as currently, or use the DinosaurIcon.tyrannosaurusIcon.
And easily map it to an array, like so:
export function enumToStringArray<T>(_enum: Object): T[] {
return Object.keys(_enum).map(k => _enum[k]);
}
const iconArray = [
...enumToStringArray<DinosaurIcon>(DinosaurIcon),
];
from svg-to-ts.
@akehir I had implemented it as an enum but for my use case the object works too and we can use the same name so would be a much simpler implementation because no option would be needed in this case. This is the PR #36 (it has more code because it depends on another PR I raised earlier today #35)
from svg-to-ts.
I personally would prefer to generate types by default. Then I would also offer the possibility to generate only the const or const and type like suggested by @akehir. I would not generate both by default.
I imagine some options like this
generateTypes: boolean (default: true);
generateTypeObject:boolean (default: false);
from svg-to-ts.
Related Issues (20)
- Icon name using folder as name HOT 4
- `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
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.